Skip to content

Commit 5c8f37a

Browse files
committed
Extend diff example
Add --raw output format and (some) options to invoke rename/copy detection on the diff.
1 parent d958e37 commit 5c8f37a

File tree

1 file changed

+44
-6
lines changed

1 file changed

+44
-6
lines changed

examples/diff.c

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@ static int check_uint16_param(const char *arg, const char *pattern, uint16_t *va
8484
char *endptr = NULL;
8585
if (strncmp(arg, pattern, len))
8686
return 0;
87+
if (arg[len] == '\0' && pattern[len - 1] != '=')
88+
return 1;
89+
if (arg[len] == '=')
90+
len++;
8791
strval = strtoul(arg + len, &endptr, 0);
8892
if (endptr == arg)
8993
return 0;
@@ -110,13 +114,20 @@ static void usage(const char *message, const char *arg)
110114
exit(1);
111115
}
112116

117+
enum {
118+
FORMAT_PATCH = 0,
119+
FORMAT_COMPACT = 1,
120+
FORMAT_RAW = 2
121+
};
122+
113123
int main(int argc, char *argv[])
114124
{
115125
git_repository *repo = NULL;
116126
git_tree *t1 = NULL, *t2 = NULL;
117127
git_diff_options opts = GIT_DIFF_OPTIONS_INIT;
128+
git_diff_find_options findopts = GIT_DIFF_FIND_OPTIONS_INIT;
118129
git_diff_list *diff;
119-
int i, color = -1, compact = 0, cached = 0;
130+
int i, color = -1, format = FORMAT_PATCH, cached = 0;
120131
char *a, *treeish1 = NULL, *treeish2 = NULL;
121132
const char *dir = ".";
122133

@@ -137,11 +148,13 @@ int main(int argc, char *argv[])
137148
}
138149
else if (!strcmp(a, "-p") || !strcmp(a, "-u") ||
139150
!strcmp(a, "--patch"))
140-
compact = 0;
151+
format = FORMAT_PATCH;
141152
else if (!strcmp(a, "--cached"))
142153
cached = 1;
143154
else if (!strcmp(a, "--name-status"))
144-
compact = 1;
155+
format = FORMAT_COMPACT;
156+
else if (!strcmp(a, "--raw"))
157+
format = FORMAT_RAW;
145158
else if (!strcmp(a, "--color"))
146159
color = 0;
147160
else if (!strcmp(a, "--no-color"))
@@ -160,6 +173,20 @@ int main(int argc, char *argv[])
160173
opts.flags |= GIT_DIFF_INCLUDE_IGNORED;
161174
else if (!strcmp(a, "--untracked"))
162175
opts.flags |= GIT_DIFF_INCLUDE_UNTRACKED;
176+
else if (check_uint16_param(a, "-M", &findopts.rename_threshold) ||
177+
check_uint16_param(a, "--find-renames",
178+
&findopts.rename_threshold))
179+
findopts.flags |= GIT_DIFF_FIND_RENAMES;
180+
else if (check_uint16_param(a, "-C", &findopts.copy_threshold) ||
181+
check_uint16_param(a, "--find-copies",
182+
&findopts.copy_threshold))
183+
findopts.flags |= GIT_DIFF_FIND_COPIES;
184+
else if (!strcmp(a, "--find-copies-harder"))
185+
findopts.flags |= GIT_DIFF_FIND_COPIES_FROM_UNMODIFIED;
186+
else if (!strncmp(a, "-B", 2) || !strncmp(a, "--break-rewrites", 16)) {
187+
/* TODO: parse thresholds */
188+
findopts.flags |= GIT_DIFF_FIND_REWRITES;
189+
}
163190
else if (!check_uint16_param(a, "-U", &opts.context_lines) &&
164191
!check_uint16_param(a, "--unified=", &opts.context_lines) &&
165192
!check_uint16_param(a, "--inter-hunk-context=",
@@ -204,13 +231,24 @@ int main(int argc, char *argv[])
204231
else
205232
check(git_diff_index_to_workdir(&diff, repo, NULL, &opts), "Diff");
206233

234+
if ((findopts.flags & GIT_DIFF_FIND_ALL) != 0)
235+
check(git_diff_find_similar(diff, &findopts),
236+
"finding renames and copies ");
237+
207238
if (color >= 0)
208239
fputs(colors[0], stdout);
209240

210-
if (compact)
211-
check(git_diff_print_compact(diff, printer, &color), "Displaying diff");
212-
else
241+
switch (format) {
242+
case FORMAT_PATCH:
213243
check(git_diff_print_patch(diff, printer, &color), "Displaying diff");
244+
break;
245+
case FORMAT_COMPACT:
246+
check(git_diff_print_compact(diff, printer, &color), "Displaying diff");
247+
break;
248+
case FORMAT_RAW:
249+
check(git_diff_print_raw(diff, printer, &color), "Displaying diff");
250+
break;
251+
}
214252

215253
if (color >= 0)
216254
fputs(colors[0], stdout);

0 commit comments

Comments
 (0)