Commit 5c8f37a3 by Russell Belfer

Extend diff example

Add --raw output format and (some) options to invoke rename/copy
detection on the diff.
parent d958e37a
...@@ -84,6 +84,10 @@ static int check_uint16_param(const char *arg, const char *pattern, uint16_t *va ...@@ -84,6 +84,10 @@ static int check_uint16_param(const char *arg, const char *pattern, uint16_t *va
char *endptr = NULL; char *endptr = NULL;
if (strncmp(arg, pattern, len)) if (strncmp(arg, pattern, len))
return 0; return 0;
if (arg[len] == '\0' && pattern[len - 1] != '=')
return 1;
if (arg[len] == '=')
len++;
strval = strtoul(arg + len, &endptr, 0); strval = strtoul(arg + len, &endptr, 0);
if (endptr == arg) if (endptr == arg)
return 0; return 0;
...@@ -110,13 +114,20 @@ static void usage(const char *message, const char *arg) ...@@ -110,13 +114,20 @@ static void usage(const char *message, const char *arg)
exit(1); exit(1);
} }
enum {
FORMAT_PATCH = 0,
FORMAT_COMPACT = 1,
FORMAT_RAW = 2
};
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
git_repository *repo = NULL; git_repository *repo = NULL;
git_tree *t1 = NULL, *t2 = NULL; git_tree *t1 = NULL, *t2 = NULL;
git_diff_options opts = GIT_DIFF_OPTIONS_INIT; git_diff_options opts = GIT_DIFF_OPTIONS_INIT;
git_diff_find_options findopts = GIT_DIFF_FIND_OPTIONS_INIT;
git_diff_list *diff; git_diff_list *diff;
int i, color = -1, compact = 0, cached = 0; int i, color = -1, format = FORMAT_PATCH, cached = 0;
char *a, *treeish1 = NULL, *treeish2 = NULL; char *a, *treeish1 = NULL, *treeish2 = NULL;
const char *dir = "."; const char *dir = ".";
...@@ -137,11 +148,13 @@ int main(int argc, char *argv[]) ...@@ -137,11 +148,13 @@ int main(int argc, char *argv[])
} }
else if (!strcmp(a, "-p") || !strcmp(a, "-u") || else if (!strcmp(a, "-p") || !strcmp(a, "-u") ||
!strcmp(a, "--patch")) !strcmp(a, "--patch"))
compact = 0; format = FORMAT_PATCH;
else if (!strcmp(a, "--cached")) else if (!strcmp(a, "--cached"))
cached = 1; cached = 1;
else if (!strcmp(a, "--name-status")) else if (!strcmp(a, "--name-status"))
compact = 1; format = FORMAT_COMPACT;
else if (!strcmp(a, "--raw"))
format = FORMAT_RAW;
else if (!strcmp(a, "--color")) else if (!strcmp(a, "--color"))
color = 0; color = 0;
else if (!strcmp(a, "--no-color")) else if (!strcmp(a, "--no-color"))
...@@ -160,6 +173,20 @@ int main(int argc, char *argv[]) ...@@ -160,6 +173,20 @@ int main(int argc, char *argv[])
opts.flags |= GIT_DIFF_INCLUDE_IGNORED; opts.flags |= GIT_DIFF_INCLUDE_IGNORED;
else if (!strcmp(a, "--untracked")) else if (!strcmp(a, "--untracked"))
opts.flags |= GIT_DIFF_INCLUDE_UNTRACKED; opts.flags |= GIT_DIFF_INCLUDE_UNTRACKED;
else if (check_uint16_param(a, "-M", &findopts.rename_threshold) ||
check_uint16_param(a, "--find-renames",
&findopts.rename_threshold))
findopts.flags |= GIT_DIFF_FIND_RENAMES;
else if (check_uint16_param(a, "-C", &findopts.copy_threshold) ||
check_uint16_param(a, "--find-copies",
&findopts.copy_threshold))
findopts.flags |= GIT_DIFF_FIND_COPIES;
else if (!strcmp(a, "--find-copies-harder"))
findopts.flags |= GIT_DIFF_FIND_COPIES_FROM_UNMODIFIED;
else if (!strncmp(a, "-B", 2) || !strncmp(a, "--break-rewrites", 16)) {
/* TODO: parse thresholds */
findopts.flags |= GIT_DIFF_FIND_REWRITES;
}
else if (!check_uint16_param(a, "-U", &opts.context_lines) && else if (!check_uint16_param(a, "-U", &opts.context_lines) &&
!check_uint16_param(a, "--unified=", &opts.context_lines) && !check_uint16_param(a, "--unified=", &opts.context_lines) &&
!check_uint16_param(a, "--inter-hunk-context=", !check_uint16_param(a, "--inter-hunk-context=",
...@@ -204,13 +231,24 @@ int main(int argc, char *argv[]) ...@@ -204,13 +231,24 @@ int main(int argc, char *argv[])
else else
check(git_diff_index_to_workdir(&diff, repo, NULL, &opts), "Diff"); check(git_diff_index_to_workdir(&diff, repo, NULL, &opts), "Diff");
if ((findopts.flags & GIT_DIFF_FIND_ALL) != 0)
check(git_diff_find_similar(diff, &findopts),
"finding renames and copies ");
if (color >= 0) if (color >= 0)
fputs(colors[0], stdout); fputs(colors[0], stdout);
if (compact) switch (format) {
check(git_diff_print_compact(diff, printer, &color), "Displaying diff"); case FORMAT_PATCH:
else
check(git_diff_print_patch(diff, printer, &color), "Displaying diff"); check(git_diff_print_patch(diff, printer, &color), "Displaying diff");
break;
case FORMAT_COMPACT:
check(git_diff_print_compact(diff, printer, &color), "Displaying diff");
break;
case FORMAT_RAW:
check(git_diff_print_raw(diff, printer, &color), "Displaying diff");
break;
}
if (color >= 0) if (color >= 0)
fputs(colors[0], stdout); fputs(colors[0], stdout);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment