ArKanjo 0.5.1
A tool for find code duplicated functions in codebases
text_diff.cpp
Go to the documentation of this file.
2
4
5#include <string>
6
8 std::ostream* output;
9 bool printed = false;
10};
11
12int GitTextDiff::print_diff_file(const git_diff_delta* delta, float, void* payload) {
13 auto* data = static_cast<DiffOutputPayload*>(payload);
14
15 if (!data->output)
16 return 0;
17
18 std::string old_file = "--- a/";
19 old_file += delta->old_file.path;
20 old_file += "\n";
21
22 std::string new_file = "+++ b/";
23 new_file += delta->new_file.path;
24 new_file += "\n";
25
26 data->output->write(old_file.data(), old_file.size());
27 data->output->write(new_file.data(), new_file.size());
28
29 return 0;
30}
31
32int GitTextDiff::print_diff_hunk(const git_diff_delta*,const git_diff_hunk* hunk,void* payload) {
33 auto* data = static_cast<DiffOutputPayload*>(payload);
34
35 if (!data->output)
36 return 0;
37
38 auto formatter = FormatterManager::get_formatter();
39
40 std::string header(hunk->header, hunk->header_len);
41 header = formatter->colorize(header, Utils::COLOR::MAGENTA);
42
43 data->output->write(header.data(), header.size());
44
45 return 0;
46}
47
48int GitTextDiff::print_diff_line(
49 const git_diff_delta* /*delta*/,
50 const git_diff_hunk* /*hunk*/,
51 const git_diff_line* line,
52 void* payload
53) {
54 auto* data = static_cast<DiffOutputPayload*>(payload);
55 data->printed = true;
56
57 if (!data->output || line->content_len <= 0) {
58 return 0;
59 }
60
61 auto formatter = FormatterManager::get_formatter();
62 std::string output_line;
63 std::string content(line->content, line->content_len);
64
65 switch (line->origin) {
66 case GIT_DIFF_LINE_ADDITION:
67 output_line = formatter->colorize("+"+ content , Utils::COLOR::GREEN);
68 break;
69 case GIT_DIFF_LINE_DELETION:
70 output_line = formatter->colorize("-" + content, Utils::COLOR::RED);
71 break;
72 case GIT_DIFF_LINE_CONTEXT:
73 output_line = std::string(" ") + content;
74 break;
75 default:
76 output_line = content;
77 break;
78 }
79
80 /*
81 TODO: Perhaps should be a good idea use << operator instead of write,
82 but for that we need to handle the string termination character,
83 since content is not guaranteed to be null-terminated.
84 */
85 data->output->write(output_line.data(), output_line.size());
86 return 0;
87}
88
90 std::string_view first_text,
91 std::string_view first_name,
92 std::string_view second_text,
93 std::string_view second_name,
94 std::ostream& output
95) {
96 git_libgit2_init();
97
98 git_diff_options diff_options = GIT_DIFF_OPTIONS_INIT;
99
100 DiffOutputPayload payload{&output, false};
101
102 int error = git_diff_buffers(
103 first_text.data(),
104 first_text.size(),
105 first_name.data(),
106 second_text.data(),
107 second_text.size(),
108 second_name.data(),
109 &diff_options,
110 &GitTextDiff::print_diff_file,
111 nullptr,
112 &GitTextDiff::print_diff_hunk,
113 &GitTextDiff::print_diff_line,
114 &payload
115 );
116
117 git_libgit2_shutdown();
118
119 return {
120 .error = error,
121 .has_difference = payload.printed,
122 };
123}
static std::shared_ptr< IFormatter > get_formatter()
static DiffResult compare(std::string_view first_text, std::string_view first_name, std::string_view second_text, std::string_view second_name, std::ostream &output)
Definition: text_diff.cpp:89
@ MAGENTA
Magenta color.
Definition: utils.hpp:117
@ RED
Red color.
Definition: utils.hpp:113
@ GREEN
Green color.
Definition: utils.hpp:114
std::ostream * output
Definition: text_diff.cpp:8