Skip to content

Commit

Permalink
WIP range-diff: offer to diff also merge commits
Browse files Browse the repository at this point in the history
Signed-off-by: Johannes Schindelin <[email protected]>
  • Loading branch information
dscho committed May 23, 2024
1 parent fe86abd commit ac9ecc8
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
11 changes: 11 additions & 0 deletions builtin/range-diff.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ int cmd_range_diff(int argc, const char **argv, const char *prefix)
{
struct diff_options diffopt = { NULL };
struct strvec other_arg = STRVEC_INIT;
struct strvec diff_merges_arg = STRVEC_INIT;
struct range_diff_options range_diff_opts = {
.creation_factor = RANGE_DIFF_CREATION_FACTOR_DEFAULT,
.diffopt = &diffopt,
Expand All @@ -33,6 +34,9 @@ int cmd_range_diff(int argc, const char **argv, const char *prefix)
OPT_PASSTHRU_ARGV(0, "notes", &other_arg,
N_("notes"), N_("passed to 'git log'"),
PARSE_OPT_OPTARG),
OPT_PASSTHRU_ARGV(0, "diff-merges", &diff_merges_arg,
N_("style"), N_("passed to 'git log'"),
PARSE_OPT_OPTARG),
OPT_BOOL(0, "left-only", &left_only,
N_("only emit output related to the first range")),
OPT_BOOL(0, "right-only", &right_only,
Expand All @@ -59,6 +63,12 @@ int cmd_range_diff(int argc, const char **argv, const char *prefix)
if (!simple_color)
diffopt.use_color = 1;

/* If `--diff-merges` was specified, imply `--merges` */
if (diff_merges_arg.nr) {
range_diff_opts.include_merges = 1;
strvec_pushv(&other_arg, diff_merges_arg.v);
}

for (i = 0; i < argc; i++)
if (!strcmp(argv[i], "--")) {
dash_dash = i;
Expand Down Expand Up @@ -152,6 +162,7 @@ int cmd_range_diff(int argc, const char **argv, const char *prefix)
res = show_range_diff(range1.buf, range2.buf, &range_diff_opts);

strvec_clear(&other_arg);
strvec_clear(&diff_merges_arg);
strbuf_release(&range1);
strbuf_release(&range2);

Expand Down
15 changes: 11 additions & 4 deletions range-diff.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ struct patch_util {
* as struct object_id (will need to be free()d).
*/
static int read_patches(const char *range, struct string_list *list,
const struct strvec *other_arg)
const struct strvec *other_arg,
unsigned int include_merges)
{
struct child_process cp = CHILD_PROCESS_INIT;
struct strbuf buf = STRBUF_INIT, contents = STRBUF_INIT;
Expand All @@ -46,7 +47,7 @@ static int read_patches(const char *range, struct string_list *list,
size_t size;
int ret = -1;

strvec_pushl(&cp.args, "log", "--no-color", "-p", "--no-merges",
strvec_pushl(&cp.args, "log", "--no-color", "-p",
"--reverse", "--date-order", "--decorate=no",
"--no-prefix", "--submodule=short",
/*
Expand All @@ -61,6 +62,8 @@ static int read_patches(const char *range, struct string_list *list,
"--pretty=medium",
"--notes",
NULL);
if (!include_merges)
strvec_push(&cp.args, "--no-merges");
strvec_push(&cp.args, range);
if (other_arg)
strvec_pushv(&cp.args, other_arg->v);
Expand Down Expand Up @@ -93,11 +96,14 @@ static int read_patches(const char *range, struct string_list *list,
}

if (skip_prefix(line, "commit ", &p)) {
char *q;
if (util) {
string_list_append(list, buf.buf)->util = util;
strbuf_reset(&buf);
}
CALLOC_ARRAY(util, 1);
if (include_merges && (q = strstr(p, " (from ")))
*q = '\0';
if (repo_get_oid(the_repository, p, &util->oid)) {
error(_("could not parse commit '%s'"), p);
FREE_AND_NULL(util);
Expand Down Expand Up @@ -563,13 +569,14 @@ int show_range_diff(const char *range1, const char *range2,

struct string_list branch1 = STRING_LIST_INIT_DUP;
struct string_list branch2 = STRING_LIST_INIT_DUP;
unsigned int include_merges = range_diff_opts->include_merges;

if (range_diff_opts->left_only && range_diff_opts->right_only)
res = error(_("options '%s' and '%s' cannot be used together"), "--left-only", "--right-only");

if (!res && read_patches(range1, &branch1, range_diff_opts->other_arg))
if (!res && read_patches(range1, &branch1, range_diff_opts->other_arg, include_merges))
res = error(_("could not parse log for '%s'"), range1);
if (!res && read_patches(range2, &branch2, range_diff_opts->other_arg))
if (!res && read_patches(range2, &branch2, range_diff_opts->other_arg, include_merges))
res = error(_("could not parse log for '%s'"), range2);

if (!res) {
Expand Down
1 change: 1 addition & 0 deletions range-diff.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ struct range_diff_options {
int creation_factor;
unsigned dual_color:1;
unsigned left_only:1, right_only:1;
unsigned include_merges:1;
const struct diff_options *diffopt; /* may be NULL */
const struct strvec *other_arg; /* may be NULL */
};
Expand Down

0 comments on commit ac9ecc8

Please sign in to comment.