summaryrefslogtreecommitdiff
path: root/range-diff.c
diff options
context:
space:
mode:
authorJunio C Hamano <gitster@pobox.com>2021-02-18 01:21:41 (GMT)
committerJunio C Hamano <gitster@pobox.com>2021-02-18 01:21:41 (GMT)
commitdadc91ff0c15b655070ad334a27a734e91635bd5 (patch)
treec53f828a0941fae682ee087bd21e935de607e2e5 /range-diff.c
parent77348b0e6e350ec81b3880f99c6077d165df2276 (diff)
parent1e79f973266cfe0e3bab0e26e869b682078e457d (diff)
downloadgit-dadc91ff0c15b655070ad334a27a734e91635bd5.zip
git-dadc91ff0c15b655070ad334a27a734e91635bd5.tar.gz
git-dadc91ff0c15b655070ad334a27a734e91635bd5.tar.bz2
Merge branch 'js/range-diff-one-side-only'
The "git range-diff" command learned "--(left|right)-only" option to show only one side of the compared range. * js/range-diff-one-side-only: range-diff: offer --left-only/--right-only options range-diff: move the diffopt initialization down one layer range-diff: combine all options in a single data structure range-diff: simplify code spawning `git log` range-diff: libify the read_patches() function again range-diff: avoid leaking memory in two error code paths
Diffstat (limited to 'range-diff.c')
-rw-r--r--range-diff.c101
1 files changed, 55 insertions, 46 deletions
diff --git a/range-diff.c b/range-diff.c
index a88612c..a3cc7c9 100644
--- a/range-diff.c
+++ b/range-diff.c
@@ -81,6 +81,8 @@ static int read_patches(const char *range, struct string_list *list,
finish_command(&cp);
return -1;
}
+ if (finish_command(&cp))
+ return -1;
line = contents.buf;
size = contents.len;
@@ -98,10 +100,10 @@ static int read_patches(const char *range, struct string_list *list,
if (get_oid(p, &util->oid)) {
error(_("could not parse commit '%s'"), p);
free(util);
+ free(current_filename);
string_list_clear(list, 1);
strbuf_release(&buf);
strbuf_release(&contents);
- finish_command(&cp);
return -1;
}
util->matching = -1;
@@ -113,10 +115,10 @@ static int read_patches(const char *range, struct string_list *list,
error(_("could not parse first line of `log` output: "
"did not start with 'commit ': '%s'"),
line);
+ free(current_filename);
string_list_clear(list, 1);
strbuf_release(&buf);
strbuf_release(&contents);
- finish_command(&cp);
return -1;
}
@@ -134,9 +136,16 @@ static int read_patches(const char *range, struct string_list *list,
orig_len = len;
len = parse_git_diff_header(&root, &linenr, 0, line,
len, size, &patch);
- if (len < 0)
- die(_("could not parse git header '%.*s'"),
- orig_len, line);
+ if (len < 0) {
+ error(_("could not parse git header '%.*s'"),
+ orig_len, line);
+ free(util);
+ free(current_filename);
+ string_list_clear(list, 1);
+ strbuf_release(&buf);
+ strbuf_release(&contents);
+ return -1;
+ }
strbuf_addstr(&buf, " ## ");
if (patch.is_new > 0)
strbuf_addf(&buf, "%s (new)", patch.new_name);
@@ -219,9 +228,6 @@ static int read_patches(const char *range, struct string_list *list,
strbuf_release(&buf);
free(current_filename);
- if (finish_command(&cp))
- return -1;
-
return 0;
}
@@ -459,12 +465,35 @@ static void patch_diff(const char *a, const char *b,
diff_flush(diffopt);
}
+static struct strbuf *output_prefix_cb(struct diff_options *opt, void *data)
+{
+ return data;
+}
+
static void output(struct string_list *a, struct string_list *b,
- struct diff_options *diffopt)
+ struct range_diff_options *range_diff_opts)
{
struct strbuf buf = STRBUF_INIT, dashes = STRBUF_INIT;
int patch_no_width = decimal_width(1 + (a->nr > b->nr ? a->nr : b->nr));
int i = 0, j = 0;
+ struct diff_options opts;
+ struct strbuf indent = STRBUF_INIT;
+
+ if (range_diff_opts->diffopt)
+ memcpy(&opts, range_diff_opts->diffopt, sizeof(opts));
+ else
+ diff_setup(&opts);
+
+ if (!opts.output_format)
+ opts.output_format = DIFF_FORMAT_PATCH;
+ opts.flags.suppress_diff_headers = 1;
+ opts.flags.dual_color_diffed_diffs =
+ range_diff_opts->dual_color;
+ opts.flags.suppress_hunk_header_line_count = 1;
+ opts.output_prefix = output_prefix_cb;
+ strbuf_addstr(&indent, " ");
+ opts.output_prefix_data = &indent;
+ diff_setup_done(&opts);
/*
* We assume the user is really more interested in the second argument
@@ -485,7 +514,8 @@ static void output(struct string_list *a, struct string_list *b,
/* Show unmatched LHS commit whose predecessors were shown. */
if (i < a->nr && a_util->matching < 0) {
- output_pair_header(diffopt, patch_no_width,
+ if (!range_diff_opts->right_only)
+ output_pair_header(&opts, patch_no_width,
&buf, &dashes, a_util, NULL);
i++;
continue;
@@ -493,7 +523,8 @@ static void output(struct string_list *a, struct string_list *b,
/* Show unmatched RHS commits. */
while (j < b->nr && b_util->matching < 0) {
- output_pair_header(diffopt, patch_no_width,
+ if (!range_diff_opts->left_only)
+ output_pair_header(&opts, patch_no_width,
&buf, &dashes, NULL, b_util);
b_util = ++j < b->nr ? b->items[j].util : NULL;
}
@@ -501,63 +532,41 @@ static void output(struct string_list *a, struct string_list *b,
/* Show matching LHS/RHS pair. */
if (j < b->nr) {
a_util = a->items[b_util->matching].util;
- output_pair_header(diffopt, patch_no_width,
+ output_pair_header(&opts, patch_no_width,
&buf, &dashes, a_util, b_util);
- if (!(diffopt->output_format & DIFF_FORMAT_NO_OUTPUT))
+ if (!(opts.output_format & DIFF_FORMAT_NO_OUTPUT))
patch_diff(a->items[b_util->matching].string,
- b->items[j].string, diffopt);
+ b->items[j].string, &opts);
a_util->shown = 1;
j++;
}
}
strbuf_release(&buf);
strbuf_release(&dashes);
-}
-
-static struct strbuf *output_prefix_cb(struct diff_options *opt, void *data)
-{
- return data;
+ strbuf_release(&indent);
}
int show_range_diff(const char *range1, const char *range2,
- int creation_factor, int dual_color,
- const struct diff_options *diffopt,
- const struct strvec *other_arg)
+ struct range_diff_options *range_diff_opts)
{
int res = 0;
struct string_list branch1 = STRING_LIST_INIT_DUP;
struct string_list branch2 = STRING_LIST_INIT_DUP;
- if (read_patches(range1, &branch1, other_arg))
+ if (range_diff_opts->left_only && range_diff_opts->right_only)
+ res = error(_("--left-only and --right-only are mutually exclusive"));
+
+ if (!res && read_patches(range1, &branch1, range_diff_opts->other_arg))
res = error(_("could not parse log for '%s'"), range1);
- if (!res && read_patches(range2, &branch2, other_arg))
+ if (!res && read_patches(range2, &branch2, range_diff_opts->other_arg))
res = error(_("could not parse log for '%s'"), range2);
if (!res) {
- struct diff_options opts;
- struct strbuf indent = STRBUF_INIT;
-
- if (diffopt)
- memcpy(&opts, diffopt, sizeof(opts));
- else
- diff_setup(&opts);
-
- if (!opts.output_format)
- opts.output_format = DIFF_FORMAT_PATCH;
- opts.flags.suppress_diff_headers = 1;
- opts.flags.dual_color_diffed_diffs = dual_color;
- opts.flags.suppress_hunk_header_line_count = 1;
- opts.output_prefix = output_prefix_cb;
- strbuf_addstr(&indent, " ");
- opts.output_prefix_data = &indent;
- diff_setup_done(&opts);
-
find_exact_matches(&branch1, &branch2);
- get_correspondences(&branch1, &branch2, creation_factor);
- output(&branch1, &branch2, &opts);
-
- strbuf_release(&indent);
+ get_correspondences(&branch1, &branch2,
+ range_diff_opts->creation_factor);
+ output(&branch1, &branch2, range_diff_opts);
}
string_list_clear(&branch1, 1);