2018-08-13 19:33:04 +08:00
|
|
|
#include "cache.h"
|
|
|
|
#include "range-diff.h"
|
|
|
|
#include "string-list.h"
|
|
|
|
#include "run-command.h"
|
|
|
|
#include "argv-array.h"
|
|
|
|
#include "hashmap.h"
|
|
|
|
#include "xdiff-interface.h"
|
|
|
|
#include "linear-assignment.h"
|
range-diff: also show the diff between patches
Just like tbdiff, we now show the diff between matching patches. This is
a "diff of two diffs", so it can be a bit daunting to read for the
beginner.
An alternative would be to display an interdiff, i.e. the hypothetical
diff which is the result of first reverting the old diff and then
applying the new diff.
Especially when rebasing frequently, an interdiff is often not feasible,
though: if the old diff cannot be applied in reverse (due to a moving
upstream), an interdiff can simply not be inferred.
This commit brings `range-diff` closer to feature parity with regard
to tbdiff.
To make `git range-diff` respect e.g. color.diff.* settings, we have
to adjust git_branch_config() accordingly.
Note: while we now parse diff options such as --color, the effect is not
yet the same as in tbdiff, where also the commit pairs would be colored.
This is left for a later commit.
Note also: while tbdiff accepts the `--no-patches` option to suppress
these diffs between patches, we prefer the `-s` (or `--no-patch`) option
that is automatically supported via our use of diff_opt_parse().
And finally note: to support diff options, we have to call
`parse_options()` such that it keeps unknown options, and then loop over
those and let `diff_opt_parse()` handle them. After that loop, we have
to call `parse_options()` again, to make sure that no unknown options
are left.
Helped-by: Thomas Gummerer <t.gummerer@gmail.com>
Helped-by: Eric Sunshine <sunshine@sunshineco.com>
Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2018-08-13 19:33:07 +08:00
|
|
|
#include "diffcore.h"
|
2018-08-13 19:33:13 +08:00
|
|
|
#include "commit.h"
|
|
|
|
#include "pretty.h"
|
2018-08-13 19:33:14 +08:00
|
|
|
#include "userdiff.h"
|
2018-08-13 19:33:04 +08:00
|
|
|
|
|
|
|
struct patch_util {
|
|
|
|
/* For the search for an exact match */
|
|
|
|
struct hashmap_entry e;
|
|
|
|
const char *diff, *patch;
|
|
|
|
|
2018-08-13 19:33:05 +08:00
|
|
|
int i, shown;
|
2018-08-13 19:33:04 +08:00
|
|
|
int diffsize;
|
|
|
|
size_t diff_offset;
|
|
|
|
/* the index of the matching item in the other branch, or -1 */
|
|
|
|
int matching;
|
|
|
|
struct object_id oid;
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Reads the patches into a string list, with the `util` field being populated
|
|
|
|
* as struct object_id (will need to be free()d).
|
|
|
|
*/
|
|
|
|
static int read_patches(const char *range, struct string_list *list)
|
|
|
|
{
|
|
|
|
struct child_process cp = CHILD_PROCESS_INIT;
|
|
|
|
FILE *in;
|
|
|
|
struct strbuf buf = STRBUF_INIT, line = STRBUF_INIT;
|
|
|
|
struct patch_util *util = NULL;
|
|
|
|
int in_header = 1;
|
|
|
|
|
|
|
|
argv_array_pushl(&cp.args, "log", "--no-color", "-p", "--no-merges",
|
|
|
|
"--reverse", "--date-order", "--decorate=no",
|
2018-08-18 04:43:53 +08:00
|
|
|
/*
|
|
|
|
* Choose indicators that are not used anywhere
|
|
|
|
* else in diffs, but still look reasonable
|
|
|
|
* (e.g. will not be confusing when debugging)
|
|
|
|
*/
|
|
|
|
"--output-indicator-new=>",
|
|
|
|
"--output-indicator-old=<",
|
|
|
|
"--output-indicator-context=#",
|
2018-08-13 19:33:04 +08:00
|
|
|
"--no-abbrev-commit", range,
|
|
|
|
NULL);
|
|
|
|
cp.out = -1;
|
|
|
|
cp.no_stdin = 1;
|
|
|
|
cp.git_cmd = 1;
|
|
|
|
|
|
|
|
if (start_command(&cp))
|
|
|
|
return error_errno(_("could not start `log`"));
|
|
|
|
in = fdopen(cp.out, "r");
|
|
|
|
if (!in) {
|
|
|
|
error_errno(_("could not read `log` output"));
|
|
|
|
finish_command(&cp);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (strbuf_getline(&line, in) != EOF) {
|
|
|
|
const char *p;
|
|
|
|
|
|
|
|
if (skip_prefix(line.buf, "commit ", &p)) {
|
|
|
|
if (util) {
|
|
|
|
string_list_append(list, buf.buf)->util = util;
|
|
|
|
strbuf_reset(&buf);
|
|
|
|
}
|
|
|
|
util = xcalloc(sizeof(*util), 1);
|
|
|
|
if (get_oid(p, &util->oid)) {
|
|
|
|
error(_("could not parse commit '%s'"), p);
|
|
|
|
free(util);
|
|
|
|
string_list_clear(list, 1);
|
|
|
|
strbuf_release(&buf);
|
|
|
|
strbuf_release(&line);
|
|
|
|
fclose(in);
|
|
|
|
finish_command(&cp);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
util->matching = -1;
|
|
|
|
in_header = 1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (starts_with(line.buf, "diff --git")) {
|
|
|
|
in_header = 0;
|
|
|
|
strbuf_addch(&buf, '\n');
|
|
|
|
if (!util->diff_offset)
|
|
|
|
util->diff_offset = buf.len;
|
2018-08-18 04:43:54 +08:00
|
|
|
strbuf_addch(&buf, ' ');
|
2018-08-13 19:33:04 +08:00
|
|
|
strbuf_addbuf(&buf, &line);
|
|
|
|
} else if (in_header) {
|
|
|
|
if (starts_with(line.buf, "Author: ")) {
|
|
|
|
strbuf_addbuf(&buf, &line);
|
|
|
|
strbuf_addstr(&buf, "\n\n");
|
|
|
|
} else if (starts_with(line.buf, " ")) {
|
2018-08-13 19:33:08 +08:00
|
|
|
strbuf_rtrim(&line);
|
2018-08-13 19:33:04 +08:00
|
|
|
strbuf_addbuf(&buf, &line);
|
|
|
|
strbuf_addch(&buf, '\n');
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
} else if (starts_with(line.buf, "@@ "))
|
|
|
|
strbuf_addstr(&buf, "@@");
|
|
|
|
else if (!line.buf[0] || starts_with(line.buf, "index "))
|
|
|
|
/*
|
|
|
|
* A completely blank (not ' \n', which is context)
|
|
|
|
* line is not valid in a diff. We skip it
|
|
|
|
* silently, because this neatly handles the blank
|
|
|
|
* separator line between commits in git-log
|
|
|
|
* output.
|
|
|
|
*
|
|
|
|
* We also want to ignore the diff's `index` lines
|
|
|
|
* because they contain exact blob hashes in which
|
|
|
|
* we are not interested.
|
|
|
|
*/
|
|
|
|
continue;
|
2018-08-18 04:43:53 +08:00
|
|
|
else if (line.buf[0] == '>') {
|
|
|
|
strbuf_addch(&buf, '+');
|
|
|
|
strbuf_add(&buf, line.buf + 1, line.len - 1);
|
|
|
|
} else if (line.buf[0] == '<') {
|
|
|
|
strbuf_addch(&buf, '-');
|
|
|
|
strbuf_add(&buf, line.buf + 1, line.len - 1);
|
|
|
|
} else if (line.buf[0] == '#') {
|
|
|
|
strbuf_addch(&buf, ' ');
|
|
|
|
strbuf_add(&buf, line.buf + 1, line.len - 1);
|
|
|
|
} else {
|
2018-08-18 04:43:54 +08:00
|
|
|
strbuf_addch(&buf, ' ');
|
2018-08-13 19:33:04 +08:00
|
|
|
strbuf_addbuf(&buf, &line);
|
2018-08-18 04:43:53 +08:00
|
|
|
}
|
2018-08-13 19:33:04 +08:00
|
|
|
|
|
|
|
strbuf_addch(&buf, '\n');
|
|
|
|
util->diffsize++;
|
|
|
|
}
|
|
|
|
fclose(in);
|
|
|
|
strbuf_release(&line);
|
|
|
|
|
|
|
|
if (util)
|
|
|
|
string_list_append(list, buf.buf)->util = util;
|
|
|
|
strbuf_release(&buf);
|
|
|
|
|
|
|
|
if (finish_command(&cp))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int patch_util_cmp(const void *dummy, const struct patch_util *a,
|
|
|
|
const struct patch_util *b, const char *keydata)
|
|
|
|
{
|
|
|
|
return strcmp(a->diff, keydata ? keydata : b->diff);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void find_exact_matches(struct string_list *a, struct string_list *b)
|
|
|
|
{
|
|
|
|
struct hashmap map;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
hashmap_init(&map, (hashmap_cmp_fn)patch_util_cmp, NULL, 0);
|
|
|
|
|
|
|
|
/* First, add the patches of a to a hash map */
|
|
|
|
for (i = 0; i < a->nr; i++) {
|
|
|
|
struct patch_util *util = a->items[i].util;
|
|
|
|
|
|
|
|
util->i = i;
|
|
|
|
util->patch = a->items[i].string;
|
|
|
|
util->diff = util->patch + util->diff_offset;
|
|
|
|
hashmap_entry_init(util, strhash(util->diff));
|
|
|
|
hashmap_add(&map, util);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Now try to find exact matches in b */
|
|
|
|
for (i = 0; i < b->nr; i++) {
|
|
|
|
struct patch_util *util = b->items[i].util, *other;
|
|
|
|
|
|
|
|
util->i = i;
|
|
|
|
util->patch = b->items[i].string;
|
|
|
|
util->diff = util->patch + util->diff_offset;
|
|
|
|
hashmap_entry_init(util, strhash(util->diff));
|
|
|
|
other = hashmap_remove(&map, util, NULL);
|
|
|
|
if (other) {
|
|
|
|
if (other->matching >= 0)
|
|
|
|
BUG("already assigned!");
|
|
|
|
|
|
|
|
other->matching = i;
|
|
|
|
util->matching = other->i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
hashmap_free(&map, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void diffsize_consume(void *data, char *line, unsigned long len)
|
|
|
|
{
|
|
|
|
(*(int *)data)++;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int diffsize(const char *a, const char *b)
|
|
|
|
{
|
|
|
|
xpparam_t pp = { 0 };
|
|
|
|
xdemitconf_t cfg = { 0 };
|
|
|
|
mmfile_t mf1, mf2;
|
|
|
|
int count = 0;
|
|
|
|
|
|
|
|
mf1.ptr = (char *)a;
|
|
|
|
mf1.size = strlen(a);
|
|
|
|
mf2.ptr = (char *)b;
|
|
|
|
mf2.size = strlen(b);
|
|
|
|
|
|
|
|
cfg.ctxlen = 3;
|
|
|
|
if (!xdi_diff_outf(&mf1, &mf2, diffsize_consume, &count, &pp, &cfg))
|
|
|
|
return count;
|
|
|
|
|
|
|
|
error(_("failed to generate diff"));
|
|
|
|
return COST_MAX;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void get_correspondences(struct string_list *a, struct string_list *b,
|
|
|
|
int creation_factor)
|
|
|
|
{
|
|
|
|
int n = a->nr + b->nr;
|
|
|
|
int *cost, c, *a2b, *b2a;
|
|
|
|
int i, j;
|
|
|
|
|
|
|
|
ALLOC_ARRAY(cost, st_mult(n, n));
|
|
|
|
ALLOC_ARRAY(a2b, n);
|
|
|
|
ALLOC_ARRAY(b2a, n);
|
|
|
|
|
|
|
|
for (i = 0; i < a->nr; i++) {
|
|
|
|
struct patch_util *a_util = a->items[i].util;
|
|
|
|
|
|
|
|
for (j = 0; j < b->nr; j++) {
|
|
|
|
struct patch_util *b_util = b->items[j].util;
|
|
|
|
|
|
|
|
if (a_util->matching == j)
|
|
|
|
c = 0;
|
|
|
|
else if (a_util->matching < 0 && b_util->matching < 0)
|
|
|
|
c = diffsize(a_util->diff, b_util->diff);
|
|
|
|
else
|
|
|
|
c = COST_MAX;
|
|
|
|
cost[i + n * j] = c;
|
|
|
|
}
|
|
|
|
|
|
|
|
c = a_util->matching < 0 ?
|
|
|
|
a_util->diffsize * creation_factor / 100 : COST_MAX;
|
|
|
|
for (j = b->nr; j < n; j++)
|
|
|
|
cost[i + n * j] = c;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (j = 0; j < b->nr; j++) {
|
|
|
|
struct patch_util *util = b->items[j].util;
|
|
|
|
|
|
|
|
c = util->matching < 0 ?
|
|
|
|
util->diffsize * creation_factor / 100 : COST_MAX;
|
|
|
|
for (i = a->nr; i < n; i++)
|
|
|
|
cost[i + n * j] = c;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = a->nr; i < n; i++)
|
|
|
|
for (j = b->nr; j < n; j++)
|
|
|
|
cost[i + n * j] = 0;
|
|
|
|
|
|
|
|
compute_assignment(n, n, cost, a2b, b2a);
|
|
|
|
|
|
|
|
for (i = 0; i < a->nr; i++)
|
|
|
|
if (a2b[i] >= 0 && a2b[i] < b->nr) {
|
|
|
|
struct patch_util *a_util = a->items[i].util;
|
|
|
|
struct patch_util *b_util = b->items[a2b[i]].util;
|
|
|
|
|
|
|
|
a_util->matching = a2b[i];
|
|
|
|
b_util->matching = i;
|
|
|
|
}
|
|
|
|
|
|
|
|
free(cost);
|
|
|
|
free(a2b);
|
|
|
|
free(b2a);
|
|
|
|
}
|
|
|
|
|
2018-08-13 19:33:18 +08:00
|
|
|
static void output_pair_header(struct diff_options *diffopt,
|
2018-08-13 19:33:28 +08:00
|
|
|
int patch_no_width,
|
2018-08-13 19:33:18 +08:00
|
|
|
struct strbuf *buf,
|
2018-08-13 19:33:13 +08:00
|
|
|
struct strbuf *dashes,
|
|
|
|
struct patch_util *a_util,
|
|
|
|
struct patch_util *b_util)
|
2018-08-13 19:33:04 +08:00
|
|
|
{
|
2018-08-13 19:33:13 +08:00
|
|
|
struct object_id *oid = a_util ? &a_util->oid : &b_util->oid;
|
|
|
|
struct commit *commit;
|
2018-08-13 19:33:18 +08:00
|
|
|
char status;
|
|
|
|
const char *color_reset = diff_get_color_opt(diffopt, DIFF_RESET);
|
|
|
|
const char *color_old = diff_get_color_opt(diffopt, DIFF_FILE_OLD);
|
|
|
|
const char *color_new = diff_get_color_opt(diffopt, DIFF_FILE_NEW);
|
|
|
|
const char *color_commit = diff_get_color_opt(diffopt, DIFF_COMMIT);
|
|
|
|
const char *color;
|
2018-08-13 19:33:13 +08:00
|
|
|
|
|
|
|
if (!dashes->len)
|
|
|
|
strbuf_addchars(dashes, '-',
|
|
|
|
strlen(find_unique_abbrev(oid,
|
|
|
|
DEFAULT_ABBREV)));
|
|
|
|
|
2018-08-13 19:33:18 +08:00
|
|
|
if (!b_util) {
|
|
|
|
color = color_old;
|
|
|
|
status = '<';
|
|
|
|
} else if (!a_util) {
|
|
|
|
color = color_new;
|
|
|
|
status = '>';
|
|
|
|
} else if (strcmp(a_util->patch, b_util->patch)) {
|
|
|
|
color = color_commit;
|
|
|
|
status = '!';
|
|
|
|
} else {
|
|
|
|
color = color_commit;
|
|
|
|
status = '=';
|
|
|
|
}
|
|
|
|
|
2018-08-13 19:33:13 +08:00
|
|
|
strbuf_reset(buf);
|
2018-08-13 19:33:18 +08:00
|
|
|
strbuf_addstr(buf, status == '!' ? color_old : color);
|
2018-08-13 19:33:13 +08:00
|
|
|
if (!a_util)
|
2018-08-13 19:33:28 +08:00
|
|
|
strbuf_addf(buf, "%*s: %s ", patch_no_width, "-", dashes->buf);
|
2018-08-13 19:33:13 +08:00
|
|
|
else
|
2018-08-13 19:33:28 +08:00
|
|
|
strbuf_addf(buf, "%*d: %s ", patch_no_width, a_util->i + 1,
|
2018-08-13 19:33:13 +08:00
|
|
|
find_unique_abbrev(&a_util->oid, DEFAULT_ABBREV));
|
|
|
|
|
2018-08-13 19:33:18 +08:00
|
|
|
if (status == '!')
|
|
|
|
strbuf_addf(buf, "%s%s", color_reset, color);
|
|
|
|
strbuf_addch(buf, status);
|
|
|
|
if (status == '!')
|
|
|
|
strbuf_addf(buf, "%s%s", color_reset, color_new);
|
2018-08-13 19:33:13 +08:00
|
|
|
|
|
|
|
if (!b_util)
|
2018-08-13 19:33:28 +08:00
|
|
|
strbuf_addf(buf, " %*s: %s", patch_no_width, "-", dashes->buf);
|
2018-08-13 19:33:13 +08:00
|
|
|
else
|
2018-08-13 19:33:28 +08:00
|
|
|
strbuf_addf(buf, " %*d: %s", patch_no_width, b_util->i + 1,
|
2018-08-13 19:33:13 +08:00
|
|
|
find_unique_abbrev(&b_util->oid, DEFAULT_ABBREV));
|
|
|
|
|
|
|
|
commit = lookup_commit_reference(the_repository, oid);
|
|
|
|
if (commit) {
|
2018-08-13 19:33:18 +08:00
|
|
|
if (status == '!')
|
|
|
|
strbuf_addf(buf, "%s%s", color_reset, color);
|
|
|
|
|
2018-08-13 19:33:13 +08:00
|
|
|
strbuf_addch(buf, ' ');
|
|
|
|
pp_commit_easy(CMIT_FMT_ONELINE, commit, buf);
|
|
|
|
}
|
2018-08-13 19:33:18 +08:00
|
|
|
strbuf_addf(buf, "%s\n", color_reset);
|
2018-08-13 19:33:13 +08:00
|
|
|
|
2018-07-22 17:57:10 +08:00
|
|
|
fwrite(buf->buf, buf->len, 1, diffopt->file);
|
2018-08-13 19:33:04 +08:00
|
|
|
}
|
|
|
|
|
2018-08-13 19:33:14 +08:00
|
|
|
static struct userdiff_driver no_func_name = {
|
|
|
|
.funcname = { "$^", 0 }
|
|
|
|
};
|
|
|
|
|
range-diff: also show the diff between patches
Just like tbdiff, we now show the diff between matching patches. This is
a "diff of two diffs", so it can be a bit daunting to read for the
beginner.
An alternative would be to display an interdiff, i.e. the hypothetical
diff which is the result of first reverting the old diff and then
applying the new diff.
Especially when rebasing frequently, an interdiff is often not feasible,
though: if the old diff cannot be applied in reverse (due to a moving
upstream), an interdiff can simply not be inferred.
This commit brings `range-diff` closer to feature parity with regard
to tbdiff.
To make `git range-diff` respect e.g. color.diff.* settings, we have
to adjust git_branch_config() accordingly.
Note: while we now parse diff options such as --color, the effect is not
yet the same as in tbdiff, where also the commit pairs would be colored.
This is left for a later commit.
Note also: while tbdiff accepts the `--no-patches` option to suppress
these diffs between patches, we prefer the `-s` (or `--no-patch`) option
that is automatically supported via our use of diff_opt_parse().
And finally note: to support diff options, we have to call
`parse_options()` such that it keeps unknown options, and then loop over
those and let `diff_opt_parse()` handle them. After that loop, we have
to call `parse_options()` again, to make sure that no unknown options
are left.
Helped-by: Thomas Gummerer <t.gummerer@gmail.com>
Helped-by: Eric Sunshine <sunshine@sunshineco.com>
Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2018-08-13 19:33:07 +08:00
|
|
|
static struct diff_filespec *get_filespec(const char *name, const char *p)
|
|
|
|
{
|
|
|
|
struct diff_filespec *spec = alloc_filespec(name);
|
|
|
|
|
2018-10-25 03:46:17 +08:00
|
|
|
fill_filespec(spec, &null_oid, 0, 0100644);
|
range-diff: also show the diff between patches
Just like tbdiff, we now show the diff between matching patches. This is
a "diff of two diffs", so it can be a bit daunting to read for the
beginner.
An alternative would be to display an interdiff, i.e. the hypothetical
diff which is the result of first reverting the old diff and then
applying the new diff.
Especially when rebasing frequently, an interdiff is often not feasible,
though: if the old diff cannot be applied in reverse (due to a moving
upstream), an interdiff can simply not be inferred.
This commit brings `range-diff` closer to feature parity with regard
to tbdiff.
To make `git range-diff` respect e.g. color.diff.* settings, we have
to adjust git_branch_config() accordingly.
Note: while we now parse diff options such as --color, the effect is not
yet the same as in tbdiff, where also the commit pairs would be colored.
This is left for a later commit.
Note also: while tbdiff accepts the `--no-patches` option to suppress
these diffs between patches, we prefer the `-s` (or `--no-patch`) option
that is automatically supported via our use of diff_opt_parse().
And finally note: to support diff options, we have to call
`parse_options()` such that it keeps unknown options, and then loop over
those and let `diff_opt_parse()` handle them. After that loop, we have
to call `parse_options()` again, to make sure that no unknown options
are left.
Helped-by: Thomas Gummerer <t.gummerer@gmail.com>
Helped-by: Eric Sunshine <sunshine@sunshineco.com>
Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2018-08-13 19:33:07 +08:00
|
|
|
spec->data = (char *)p;
|
|
|
|
spec->size = strlen(p);
|
|
|
|
spec->should_munmap = 0;
|
|
|
|
spec->is_stdin = 1;
|
2018-08-13 19:33:14 +08:00
|
|
|
spec->driver = &no_func_name;
|
range-diff: also show the diff between patches
Just like tbdiff, we now show the diff between matching patches. This is
a "diff of two diffs", so it can be a bit daunting to read for the
beginner.
An alternative would be to display an interdiff, i.e. the hypothetical
diff which is the result of first reverting the old diff and then
applying the new diff.
Especially when rebasing frequently, an interdiff is often not feasible,
though: if the old diff cannot be applied in reverse (due to a moving
upstream), an interdiff can simply not be inferred.
This commit brings `range-diff` closer to feature parity with regard
to tbdiff.
To make `git range-diff` respect e.g. color.diff.* settings, we have
to adjust git_branch_config() accordingly.
Note: while we now parse diff options such as --color, the effect is not
yet the same as in tbdiff, where also the commit pairs would be colored.
This is left for a later commit.
Note also: while tbdiff accepts the `--no-patches` option to suppress
these diffs between patches, we prefer the `-s` (or `--no-patch`) option
that is automatically supported via our use of diff_opt_parse().
And finally note: to support diff options, we have to call
`parse_options()` such that it keeps unknown options, and then loop over
those and let `diff_opt_parse()` handle them. After that loop, we have
to call `parse_options()` again, to make sure that no unknown options
are left.
Helped-by: Thomas Gummerer <t.gummerer@gmail.com>
Helped-by: Eric Sunshine <sunshine@sunshineco.com>
Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2018-08-13 19:33:07 +08:00
|
|
|
|
|
|
|
return spec;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void patch_diff(const char *a, const char *b,
|
|
|
|
struct diff_options *diffopt)
|
|
|
|
{
|
|
|
|
diff_queue(&diff_queued_diff,
|
|
|
|
get_filespec("a", a), get_filespec("b", b));
|
|
|
|
|
|
|
|
diffcore_std(diffopt);
|
|
|
|
diff_flush(diffopt);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void output(struct string_list *a, struct string_list *b,
|
|
|
|
struct diff_options *diffopt)
|
2018-08-13 19:33:04 +08:00
|
|
|
{
|
2018-08-13 19:33:13 +08:00
|
|
|
struct strbuf buf = STRBUF_INIT, dashes = STRBUF_INIT;
|
2018-08-13 19:33:28 +08:00
|
|
|
int patch_no_width = decimal_width(1 + (a->nr > b->nr ? a->nr : b->nr));
|
2018-08-13 19:33:05 +08:00
|
|
|
int i = 0, j = 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We assume the user is really more interested in the second argument
|
|
|
|
* ("newer" version). To that end, we print the output in the order of
|
|
|
|
* the RHS (the `b` parameter). To put the LHS (the `a` parameter)
|
|
|
|
* commits that are no longer in the RHS into a good place, we place
|
|
|
|
* them once we have shown all of their predecessors in the LHS.
|
|
|
|
*/
|
|
|
|
|
|
|
|
while (i < a->nr || j < b->nr) {
|
|
|
|
struct patch_util *a_util, *b_util;
|
|
|
|
a_util = i < a->nr ? a->items[i].util : NULL;
|
|
|
|
b_util = j < b->nr ? b->items[j].util : NULL;
|
|
|
|
|
|
|
|
/* Skip all the already-shown commits from the LHS. */
|
|
|
|
while (i < a->nr && a_util->shown)
|
|
|
|
a_util = ++i < a->nr ? a->items[i].util : NULL;
|
|
|
|
|
|
|
|
/* Show unmatched LHS commit whose predecessors were shown. */
|
|
|
|
if (i < a->nr && a_util->matching < 0) {
|
2018-08-13 19:33:28 +08:00
|
|
|
output_pair_header(diffopt, patch_no_width,
|
2018-08-13 19:33:18 +08:00
|
|
|
&buf, &dashes, a_util, NULL);
|
2018-08-13 19:33:05 +08:00
|
|
|
i++;
|
|
|
|
continue;
|
|
|
|
}
|
2018-08-13 19:33:04 +08:00
|
|
|
|
2018-08-13 19:33:05 +08:00
|
|
|
/* Show unmatched RHS commits. */
|
|
|
|
while (j < b->nr && b_util->matching < 0) {
|
2018-08-13 19:33:28 +08:00
|
|
|
output_pair_header(diffopt, patch_no_width,
|
2018-08-13 19:33:18 +08:00
|
|
|
&buf, &dashes, NULL, b_util);
|
2018-08-13 19:33:05 +08:00
|
|
|
b_util = ++j < b->nr ? b->items[j].util : NULL;
|
2018-08-13 19:33:04 +08:00
|
|
|
}
|
|
|
|
|
2018-08-13 19:33:05 +08:00
|
|
|
/* Show matching LHS/RHS pair. */
|
|
|
|
if (j < b->nr) {
|
|
|
|
a_util = a->items[b_util->matching].util;
|
2018-08-13 19:33:28 +08:00
|
|
|
output_pair_header(diffopt, patch_no_width,
|
2018-08-13 19:33:18 +08:00
|
|
|
&buf, &dashes, a_util, b_util);
|
range-diff: also show the diff between patches
Just like tbdiff, we now show the diff between matching patches. This is
a "diff of two diffs", so it can be a bit daunting to read for the
beginner.
An alternative would be to display an interdiff, i.e. the hypothetical
diff which is the result of first reverting the old diff and then
applying the new diff.
Especially when rebasing frequently, an interdiff is often not feasible,
though: if the old diff cannot be applied in reverse (due to a moving
upstream), an interdiff can simply not be inferred.
This commit brings `range-diff` closer to feature parity with regard
to tbdiff.
To make `git range-diff` respect e.g. color.diff.* settings, we have
to adjust git_branch_config() accordingly.
Note: while we now parse diff options such as --color, the effect is not
yet the same as in tbdiff, where also the commit pairs would be colored.
This is left for a later commit.
Note also: while tbdiff accepts the `--no-patches` option to suppress
these diffs between patches, we prefer the `-s` (or `--no-patch`) option
that is automatically supported via our use of diff_opt_parse().
And finally note: to support diff options, we have to call
`parse_options()` such that it keeps unknown options, and then loop over
those and let `diff_opt_parse()` handle them. After that loop, we have
to call `parse_options()` again, to make sure that no unknown options
are left.
Helped-by: Thomas Gummerer <t.gummerer@gmail.com>
Helped-by: Eric Sunshine <sunshine@sunshineco.com>
Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2018-08-13 19:33:07 +08:00
|
|
|
if (!(diffopt->output_format & DIFF_FORMAT_NO_OUTPUT))
|
|
|
|
patch_diff(a->items[b_util->matching].string,
|
|
|
|
b->items[j].string, diffopt);
|
2018-08-13 19:33:05 +08:00
|
|
|
a_util->shown = 1;
|
|
|
|
j++;
|
|
|
|
}
|
2018-08-13 19:33:04 +08:00
|
|
|
}
|
2018-08-13 19:33:13 +08:00
|
|
|
strbuf_release(&buf);
|
|
|
|
strbuf_release(&dashes);
|
2018-08-13 19:33:04 +08:00
|
|
|
}
|
|
|
|
|
2018-07-22 17:57:12 +08:00
|
|
|
static struct strbuf *output_prefix_cb(struct diff_options *opt, void *data)
|
|
|
|
{
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2018-08-13 19:33:04 +08:00
|
|
|
int show_range_diff(const char *range1, const char *range2,
|
2018-07-22 17:57:12 +08:00
|
|
|
int creation_factor, int dual_color,
|
|
|
|
struct diff_options *diffopt)
|
2018-08-13 19:33:04 +08:00
|
|
|
{
|
|
|
|
int res = 0;
|
|
|
|
|
|
|
|
struct string_list branch1 = STRING_LIST_INIT_DUP;
|
|
|
|
struct string_list branch2 = STRING_LIST_INIT_DUP;
|
|
|
|
|
|
|
|
if (read_patches(range1, &branch1))
|
|
|
|
res = error(_("could not parse log for '%s'"), range1);
|
|
|
|
if (!res && read_patches(range2, &branch2))
|
|
|
|
res = error(_("could not parse log for '%s'"), range2);
|
|
|
|
|
|
|
|
if (!res) {
|
2018-07-22 17:57:12 +08:00
|
|
|
struct diff_options opts;
|
|
|
|
struct strbuf indent = STRBUF_INIT;
|
|
|
|
|
|
|
|
memcpy(&opts, diffopt, sizeof(opts));
|
|
|
|
opts.output_format = DIFF_FORMAT_PATCH;
|
|
|
|
opts.flags.suppress_diff_headers = 1;
|
|
|
|
opts.flags.dual_color_diffed_diffs = dual_color;
|
|
|
|
opts.output_prefix = output_prefix_cb;
|
|
|
|
strbuf_addstr(&indent, " ");
|
|
|
|
opts.output_prefix_data = &indent;
|
|
|
|
diff_setup_done(&opts);
|
|
|
|
|
2018-08-13 19:33:04 +08:00
|
|
|
find_exact_matches(&branch1, &branch2);
|
|
|
|
get_correspondences(&branch1, &branch2, creation_factor);
|
2018-07-22 17:57:12 +08:00
|
|
|
output(&branch1, &branch2, &opts);
|
|
|
|
|
|
|
|
strbuf_release(&indent);
|
2018-08-13 19:33:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
string_list_clear(&branch1, 1);
|
|
|
|
string_list_clear(&branch2, 1);
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|