forked from QuantStack/git2cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiff_subcommand.cpp
More file actions
345 lines (302 loc) · 11.7 KB
/
diff_subcommand.cpp
File metadata and controls
345 lines (302 loc) · 11.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
#include <git2.h>
#include <optional>
#include <termcolor/termcolor.hpp>
#include "../utils/common.hpp"
#include "../utils/git_exception.hpp"
#include "../subcommand/diff_subcommand.hpp"
#include "../wrapper/patch_wrapper.hpp"
#include "../wrapper/repository_wrapper.hpp"
diff_subcommand::diff_subcommand(const libgit2_object&, CLI::App& app)
{
auto* sub = app.add_subcommand("diff", "Show changes between commits, commit and working tree, etc");
sub->add_option("<files>", m_files, "tree-ish objects to compare");
sub->add_flag("--stat", m_stat_flag, "Generate a diffstat");
sub->add_flag("--shortstat", m_shortstat_flag, "Output only the last line of --stat");
sub->add_flag("--numstat", m_numstat_flag, "Machine-friendly --stat");
sub->add_flag("--summary", m_summary_flag, "Output a condensed summary");
sub->add_flag("--name-only", m_name_only_flag, "Show only names of changed files");
sub->add_flag("--name-status", m_name_status_flag, "Show names and status of changed files");
sub->add_flag("--raw", m_raw_flag, "Generate the diff in raw format");
sub->add_flag("--cached,--staged", m_cached_flag, "Compare staged changes to HEAD");
sub->add_flag("--no-index", m_no_index_flag, "Compare two files on filesystem");
sub->add_flag("-R", m_reverse_flag, "Swap two inputs");
sub->add_flag("-a,--text", m_text_flag, "Treat all files as text");
sub->add_flag("--ignore-space-at-eol", m_ignore_space_at_eol_flag, "Ignore changes in whitespace at EOL");
sub->add_flag("-b,--ignore-space-change", m_ignore_space_change_flag, "Ignore changes in amount of whitespace");
sub->add_flag("-w,--ignore-all-space", m_ignore_all_space_flag, "Ignore whitespace when comparing lines");
sub->add_flag("--patience", m_patience_flag, "Generate diff using patience algorithm");
sub->add_flag("--minimal", m_minimal_flag, "Spend extra time to find smallest diff");
// TODO: add the following flags after the "move" subcommand has been implemented (needed for the tests)
// sub->add_option("-M,--find-renames", m_rename_threshold, "Detect renames")
// ->expected(0,1)
// ->each([this](const std::string&) { m_find_renames_flag = true; });
// sub->add_option("-C,--find-copies", m_copy_threshold, "Detect copies")
// ->expected(0,1)
// ->each([this](const std::string&) { m_find_copies_flag = true; });
// sub->add_flag("--find-copies-harder", m_find_copies_harder_flag, "Detect copies from unmodified files");
// sub->add_flag("-B,--break-rewrites", m_break_rewrites_flag, "Detect file rewrites");
sub->add_option("-U,--unified", m_context_lines, "Lines of context");
sub->add_option("--inter-hunk-context", m_interhunk_lines, "Context between hunks");
sub->add_option("--abbrev", m_abbrev, "Abbreviation length for object names")
->expected(0,1);
sub->add_flag("--color", m_colour_flag, "Show colored diff");
sub->add_flag("--no-color", m_no_colour_flag, "Turn off colored diff");
sub->callback([this]() { this->run(); });
}
void print_stats(const diff_wrapper& diff, bool use_colour, bool stat_flag, bool shortstat_flag, bool numstat_flag, bool summary_flag)
{
git_diff_stats_format_t format;
if (stat_flag)
{
if (shortstat_flag || numstat_flag || summary_flag)
{
throw git_exception("Only one of --stat, --shortstat, --numstat and --summary should be provided.", git2cpp_error_code::BAD_ARGUMENT);
}
else
{
format = GIT_DIFF_STATS_FULL;
}
}
else if (shortstat_flag)
{
if (numstat_flag || summary_flag)
{
throw git_exception("Only one of --stat, --shortstat, --numstat and --summary should be provided.", git2cpp_error_code::BAD_ARGUMENT);
}
else
{
format = GIT_DIFF_STATS_SHORT;
}
}
else if (numstat_flag)
{
if (summary_flag)
{
throw git_exception("Only one of --stat, --shortstat, --numstat and --summary should be provided.", git2cpp_error_code::BAD_ARGUMENT);
}
else
{
format = GIT_DIFF_STATS_NUMBER;
}
}
else if (summary_flag)
{
format = GIT_DIFF_STATS_INCLUDE_SUMMARY;
}
auto stats = diff.get_stats();
auto buf = stats.to_buf(format, 80);
if (use_colour && stat_flag)
{
// Add colors to + and - characters
std::string output(buf.ptr);
bool in_parentheses = false;
for (char c : output)
{
if (c == '(')
{
in_parentheses = true;
std::cout << c;
}
else if (c == ')')
{
in_parentheses = false;
std::cout << c;
}
else if (c == '+' && !in_parentheses)
{
std::cout << termcolor::green << '+' << termcolor::reset;
}
else if (c == '-' && !in_parentheses)
{
std::cout << termcolor::red << '-' << termcolor::reset;
}
else
{
std::cout << c;
}
}
}
else
{
std::cout << buf.ptr;
}
git_buf_dispose(&buf);
}
static int colour_printer([[maybe_unused]] const git_diff_delta* delta, [[maybe_unused]] const git_diff_hunk* hunk, const git_diff_line* line, void* payload)
{
bool use_colour = *reinterpret_cast<bool*>(payload);
// Only print origin for context/addition/deletion lines
// For other line types, content already includes everything
bool print_origin = (line->origin == GIT_DIFF_LINE_CONTEXT ||
line->origin == GIT_DIFF_LINE_ADDITION ||
line->origin == GIT_DIFF_LINE_DELETION);
if (use_colour)
{
switch (line->origin) {
case GIT_DIFF_LINE_ADDITION: std::cout << termcolor::green; break;
case GIT_DIFF_LINE_DELETION: std::cout << termcolor::red; break;
case GIT_DIFF_LINE_ADD_EOFNL: std::cout << termcolor::green; break;
case GIT_DIFF_LINE_DEL_EOFNL: std::cout << termcolor::red; break;
case GIT_DIFF_LINE_FILE_HDR: std::cout << termcolor::bold; break;
case GIT_DIFF_LINE_HUNK_HDR: std::cout << termcolor::cyan; break;
default: break;
}
}
if (print_origin)
{
std::cout << line->origin;
}
std::cout << std::string_view(line->content, line->content_len);
if (use_colour)
{
std::cout << termcolor::reset;
}
return 0;
}
void diff_subcommand::print_diff(diff_wrapper& diff, bool use_colour)
{
if (m_stat_flag || m_shortstat_flag || m_numstat_flag || m_summary_flag)
{
print_stats(diff, use_colour, m_stat_flag, m_shortstat_flag, m_numstat_flag, m_summary_flag);
return;
}
// TODO: add the following flags after the "move" subcommand has been implemented (needed for the tests)
// if (m_find_renames_flag || m_find_copies_flag || m_find_copies_harder_flag || m_break_rewrites_flag)
// {
// git_diff_find_options find_opts;
// git_diff_find_options_init(&find_opts, GIT_DIFF_FIND_OPTIONS_VERSION);
// if (m_find_renames_flag)
// {
// find_opts.flags |= GIT_DIFF_FIND_RENAMES;
// find_opts.rename_threshold = m_rename_threshold;
// }
// if (m_find_copies_flag)
// {
// find_opts.flags |= GIT_DIFF_FIND_COPIES;
// find_opts.copy_threshold = m_copy_threshold;
// }
// if (m_find_copies_harder_flag)
// {
// find_opts.flags |= GIT_DIFF_FIND_COPIES_FROM_UNMODIFIED;
// }
// if (m_break_rewrites_flag)
// {
// find_opts.flags |= GIT_DIFF_FIND_REWRITES;
// }
// diff.find_similar(&find_opts);
// }
git_diff_format_t format = GIT_DIFF_FORMAT_PATCH;
if (m_name_only_flag)
{
format = GIT_DIFF_FORMAT_NAME_ONLY;
}
else if (m_name_status_flag)
{
format = GIT_DIFF_FORMAT_NAME_STATUS;
}
else if (m_raw_flag)
{
format = GIT_DIFF_FORMAT_RAW;
}
diff.print(format, colour_printer, &use_colour);
}
diff_wrapper compute_diff_no_index(std::vector<std::string> files, git_diff_options& diffopts) //std::pair<buf_wrapper, diff_wrapper>
{
if (files.size() != 2)
{
throw git_exception("usage: git diff --no-index [<options>] <path> <path> [<pathspec>...]", git2cpp_error_code::BAD_ARGUMENT);
}
git_diff_options_init(&diffopts, GIT_DIFF_OPTIONS_VERSION);
std::string file1_str = read_file(files[0]);
std::string file2_str = read_file(files[1]);
if (file1_str.empty())
{
throw git_exception("Cannot read file: " + files[0], git2cpp_error_code::GENERIC_ERROR); //TODO: check error code with git
}
if (file2_str.empty())
{
throw git_exception("Cannot read file: " + files[1], git2cpp_error_code::GENERIC_ERROR); //TODO: check error code with git
}
auto patch = patch_wrapper::patch_from_files(files[0], file1_str, files[1], file2_str, &diffopts);
auto buf = patch.to_buf();
auto diff = diff_wrapper::diff_from_buffer(buf);
git_buf_dispose(&buf);
return diff;
}
void diff_subcommand::run()
{
git_diff_options diffopts;
git_diff_options_init(&diffopts, GIT_DIFF_OPTIONS_VERSION);
bool use_colour = false;
if (m_no_colour_flag)
{
if (m_colour_flag)
{
throw git_exception("Only one of --color and --no-color should be provided.", git2cpp_error_code::BAD_ARGUMENT);
}
else
{
use_colour = false;
}
}
else if (m_colour_flag)
{
use_colour = true;
}
if (m_no_index_flag)
{
auto diff = compute_diff_no_index(m_files, diffopts);
diff_subcommand::print_diff(diff, use_colour);
}
else
{
auto directory = get_current_git_path();
auto repo = repository_wrapper::open(directory);
diffopts.context_lines = m_context_lines;
diffopts.interhunk_lines = m_interhunk_lines;
diffopts.id_abbrev = m_abbrev;
if (m_reverse_flag) { diffopts.flags |= GIT_DIFF_REVERSE; }
if (m_text_flag) { diffopts.flags |= GIT_DIFF_FORCE_TEXT; }
if (m_ignore_space_at_eol_flag) { diffopts.flags |= GIT_DIFF_IGNORE_WHITESPACE_EOL; }
if (m_ignore_space_change_flag) { diffopts.flags |= GIT_DIFF_IGNORE_WHITESPACE_CHANGE; }
if (m_ignore_all_space_flag) { diffopts.flags |= GIT_DIFF_IGNORE_WHITESPACE; }
if (m_untracked_flag) { diffopts.flags |= GIT_DIFF_INCLUDE_UNTRACKED; }
if (m_patience_flag) { diffopts.flags |= GIT_DIFF_PATIENCE; }
if (m_minimal_flag) { diffopts.flags |= GIT_DIFF_MINIMAL; }
std::optional<tree_wrapper> tree1;
std::optional<tree_wrapper> tree2;
// TODO: throw error if m_files.size() > 2
if (m_files.size() >= 1)
{
tree1 = repo.treeish_to_tree(m_files[0]);
}
if (m_files.size() ==2)
{
tree2 = repo.treeish_to_tree(m_files[1]);
}
auto diff = [&repo, &tree1, &tree2, &diffopts, this]()
{
if (tree1.has_value() && tree2.has_value())
{
return repo.diff_tree_to_tree(tree1.value(), tree2.value(), &diffopts);
}
else if (m_cached_flag)
{
if (m_cached_flag || !tree1)
{
tree1 = repo.treeish_to_tree("HEAD");
}
return repo.diff_tree_to_index(tree1.value(), std::nullopt, &diffopts);
}
else if (tree1)
{
return repo.diff_tree_to_workdir_with_index(tree1.value(), &diffopts);
}
else
{
return repo.diff_index_to_workdir(std::nullopt, &diffopts);
}
}();
diff_subcommand::print_diff(diff, use_colour);
}
}