2023-04-22 20:17:23 +00:00
|
|
|
#include "git-compat-util.h"
|
2023-03-21 06:26:03 +00:00
|
|
|
#include "environment.h"
|
2023-03-21 06:25:54 +00:00
|
|
|
#include "gettext.h"
|
2018-08-13 11:33:04 +00:00
|
|
|
#include "range-diff.h"
|
2023-04-11 07:41:49 +00:00
|
|
|
#include "object-name.h"
|
2018-08-13 11:33:04 +00:00
|
|
|
#include "string-list.h"
|
|
|
|
#include "run-command.h"
|
2020-07-28 20:23:39 +00:00
|
|
|
#include "strvec.h"
|
2018-08-13 11:33:04 +00:00
|
|
|
#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 11:33:07 +00:00
|
|
|
#include "diffcore.h"
|
2018-08-13 11:33:13 +00:00
|
|
|
#include "commit.h"
|
2023-04-11 07:41:59 +00:00
|
|
|
#include "pager.h"
|
2018-08-13 11:33:13 +00:00
|
|
|
#include "pretty.h"
|
2018-08-13 11:33:14 +00:00
|
|
|
#include "userdiff.h"
|
2019-07-11 16:08:49 +00:00
|
|
|
#include "apply.h"
|
2021-02-05 14:44:48 +00:00
|
|
|
#include "revision.h"
|
2018-08-13 11:33:04 +00:00
|
|
|
|
|
|
|
struct patch_util {
|
|
|
|
/* For the search for an exact match */
|
|
|
|
struct hashmap_entry e;
|
|
|
|
const char *diff, *patch;
|
|
|
|
|
2018-08-13 11:33:05 +00:00
|
|
|
int i, shown;
|
2018-08-13 11:33:04 +00: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).
|
|
|
|
*/
|
2019-11-20 21:18:45 +00:00
|
|
|
static int read_patches(const char *range, struct string_list *list,
|
2020-07-28 20:25:12 +00:00
|
|
|
const struct strvec *other_arg)
|
2018-08-13 11:33:04 +00:00
|
|
|
{
|
|
|
|
struct child_process cp = CHILD_PROCESS_INIT;
|
2019-07-11 16:08:46 +00:00
|
|
|
struct strbuf buf = STRBUF_INIT, contents = STRBUF_INIT;
|
2018-08-13 11:33:04 +00:00
|
|
|
struct patch_util *util = NULL;
|
|
|
|
int in_header = 1;
|
2019-07-11 16:08:50 +00:00
|
|
|
char *line, *current_filename = NULL;
|
range-diff: use ssize_t for parsed "len" in read_patches()
As we iterate through the buffer containing git-log output, parsing
lines, we use an "int" to store the size of an individual line. This
should be a size_t, as we have no guarantee that there is not a
malicious 2GB+ commit-message line in the output.
Overflowing this integer probably doesn't do anything _too_ terrible. We
are not using the value to size a buffer, so the worst case is probably
an out-of-bounds read from before the array. But it's easy enough to
fix.
Note that we have to use ssize_t here, since we also store the length
result from parse_git_diff_header(), which may return a negative value
for error. That function actually returns an int itself, which has a
similar overflow problem, but I'll leave that for another day. Much
of the apply.c code uses ints and should be converted as a whole; in the
meantime, a negative return from parse_git_diff_header() will be
interpreted as an error, and we'll bail (so we can't handle such a case,
but given that it's likely to be malicious anyway, the important thing
is we don't have any memory errors).
Signed-off-by: Jeff King <peff@peff.net>
Acked-by: Derrick Stolee <dstolee@microsoft.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2021-08-09 22:48:48 +00:00
|
|
|
ssize_t len;
|
2019-07-11 16:08:46 +00:00
|
|
|
size_t size;
|
2022-03-04 18:32:16 +00:00
|
|
|
int ret = -1;
|
2018-08-13 11:33:04 +00:00
|
|
|
|
2020-07-28 20:25:12 +00:00
|
|
|
strvec_pushl(&cp.args, "log", "--no-color", "-p", "--no-merges",
|
strvec: fix indentation in renamed calls
Code which split an argv_array call across multiple lines, like:
argv_array_pushl(&args, "one argument",
"another argument", "and more",
NULL);
was recently mechanically renamed to use strvec, which results in
mis-matched indentation like:
strvec_pushl(&args, "one argument",
"another argument", "and more",
NULL);
Let's fix these up to align the arguments with the opening paren. I did
this manually by sifting through the results of:
git jump grep 'strvec_.*,$'
and liberally applying my editor's auto-format. Most of the changes are
of the form shown above, though I also normalized a few that had
originally used a single-tab indentation (rather than our usual style of
aligning with the open paren). I also rewrapped a couple of obvious
cases (e.g., where previously too-long lines became short enough to fit
on one), but I wasn't aggressive about it. In cases broken to three or
more lines, the grouping of arguments is sometimes meaningful, and it
wasn't worth my time or reviewer time to ponder each case individually.
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2020-07-28 20:26:31 +00:00
|
|
|
"--reverse", "--date-order", "--decorate=no",
|
range-diff: show submodule changes irrespective of diff.submodule
After generating diffs for each range to be compared using a 'git log'
invocation, range-diff.c::read_patches looks for the "diff --git" header
in those diffs to recognize the beginning of a new change.
In a project with submodules, and with 'diff.submodule=log' set in the
config, this header is missing for the diff of a changed submodule, so
any submodule changes are quietly ignored in the range-diff.
When 'diff.submodule=diff' is set in the config, the "diff --git" header
is also missing for the submodule itself, but is shown for submodule
content changes, which can easily confuse 'git range-diff' and lead to
errors such as:
error: git apply: bad git-diff - inconsistent old filename on line 1
error: could not parse git header 'diff --git path/to/submodule/and/some/file/within
'
error: could not parse log for '@{u}..@{1}'
Force the submodule diff format to its default ("short") when invoking
'git log' to generate the patches for each range, such that submodule
changes are always detected.
Add a test, including an invocation with '--creation-factor=100' to
force the second commit in the range not to be considered a complete
rewrite, in order to verify we do indeed get the "short" format.
Signed-off-by: Philippe Blain <levraiphilippeblain@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2022-06-06 20:59:13 +00:00
|
|
|
"--no-prefix", "--submodule=short",
|
strvec: fix indentation in renamed calls
Code which split an argv_array call across multiple lines, like:
argv_array_pushl(&args, "one argument",
"another argument", "and more",
NULL);
was recently mechanically renamed to use strvec, which results in
mis-matched indentation like:
strvec_pushl(&args, "one argument",
"another argument", "and more",
NULL);
Let's fix these up to align the arguments with the opening paren. I did
this manually by sifting through the results of:
git jump grep 'strvec_.*,$'
and liberally applying my editor's auto-format. Most of the changes are
of the form shown above, though I also normalized a few that had
originally used a single-tab indentation (rather than our usual style of
aligning with the open paren). I also rewrapped a couple of obvious
cases (e.g., where previously too-long lines became short enough to fit
on one), but I wasn't aggressive about it. In cases broken to three or
more lines, the grouping of arguments is sometimes meaningful, and it
wasn't worth my time or reviewer time to ponder each case individually.
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2020-07-28 20:26:31 +00: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=#",
|
|
|
|
"--no-abbrev-commit",
|
|
|
|
"--pretty=medium",
|
|
|
|
"--notes",
|
|
|
|
NULL);
|
2022-08-26 09:39:30 +00:00
|
|
|
strvec_push(&cp.args, range);
|
2019-11-20 21:18:45 +00:00
|
|
|
if (other_arg)
|
2020-07-29 00:37:20 +00:00
|
|
|
strvec_pushv(&cp.args, other_arg->v);
|
2018-08-13 11:33:04 +00:00
|
|
|
cp.out = -1;
|
|
|
|
cp.no_stdin = 1;
|
|
|
|
cp.git_cmd = 1;
|
|
|
|
|
|
|
|
if (start_command(&cp))
|
|
|
|
return error_errno(_("could not start `log`"));
|
2019-07-11 16:08:46 +00:00
|
|
|
if (strbuf_read(&contents, cp.out, 0) < 0) {
|
2018-08-13 11:33:04 +00:00
|
|
|
error_errno(_("could not read `log` output"));
|
|
|
|
finish_command(&cp);
|
2022-03-04 18:32:16 +00:00
|
|
|
goto cleanup;
|
2018-08-13 11:33:04 +00:00
|
|
|
}
|
2021-02-04 20:07:51 +00:00
|
|
|
if (finish_command(&cp))
|
2022-03-04 18:32:16 +00:00
|
|
|
goto cleanup;
|
2018-08-13 11:33:04 +00:00
|
|
|
|
2019-07-11 16:08:46 +00:00
|
|
|
line = contents.buf;
|
|
|
|
size = contents.len;
|
2021-08-09 22:47:42 +00:00
|
|
|
for (; size > 0; size -= len, line += len) {
|
2018-08-13 11:33:04 +00:00
|
|
|
const char *p;
|
range-diff: handle unterminated lines in read_patches()
When parsing our buffer of output from git-log, we have a
find_end_of_line() helper that finds the next newline, and gives us the
number of bytes to move past it, or the size of the whole remaining
buffer if there is no newline.
But trying to handle both those cases leads to some oddities:
- we try to overwrite the newline with NUL in the caller, by writing
over line[len-1]. This is at best redundant, since the helper will
already have done so if it saw a newline. But if it didn't see a
newline, it's actively wrong; we'll overwrite the byte at the end of
the (unterminated) line.
We could solve this just dropping the extra NUL assignment in the
caller and just letting the helper do the right thing. But...
- if we see a "diff --git" line, we'll restore the newline on top of
the NUL byte, so we can pass the string to parse_git_diff_header().
But if there was no newline in the first place, we can't do this.
There's no place to put it (the current code writes a newline
over whatever byte we obliterated earlier). The best we can do is
feed the complete remainder of the buffer to the function (which is,
in fact, a string, by virtue of being a strbuf).
To solve this, the caller needs to know whether we actually found a
newline or not. We could modify find_end_of_line() to return that
information, but we can further observe that it has only one caller.
So let's just inline it in that caller.
Nobody seems to have noticed this case, probably because git-log would
never produce input that doesn't end with a newline. Arguably we could
just return an error as soon as we see that the output does not end in a
newline. But the code to do so actually ends up _longer_, mostly because
of the cleanup we have to do in handling the error.
Signed-off-by: Jeff King <peff@peff.net>
Acked-by: Derrick Stolee <dstolee@microsoft.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2021-08-09 22:48:39 +00:00
|
|
|
char *eol;
|
|
|
|
|
|
|
|
eol = memchr(line, '\n', size);
|
|
|
|
if (eol) {
|
|
|
|
*eol = '\0';
|
|
|
|
len = eol + 1 - line;
|
|
|
|
} else {
|
|
|
|
len = size;
|
|
|
|
}
|
2018-08-13 11:33:04 +00:00
|
|
|
|
2019-07-11 16:08:46 +00:00
|
|
|
if (skip_prefix(line, "commit ", &p)) {
|
2018-08-13 11:33:04 +00:00
|
|
|
if (util) {
|
|
|
|
string_list_append(list, buf.buf)->util = util;
|
|
|
|
strbuf_reset(&buf);
|
|
|
|
}
|
2021-03-13 16:17:22 +00:00
|
|
|
CALLOC_ARRAY(util, 1);
|
2023-03-28 13:58:46 +00:00
|
|
|
if (repo_get_oid(the_repository, p, &util->oid)) {
|
2018-08-13 11:33:04 +00:00
|
|
|
error(_("could not parse commit '%s'"), p);
|
2022-03-04 18:32:16 +00:00
|
|
|
FREE_AND_NULL(util);
|
2018-08-13 11:33:04 +00:00
|
|
|
string_list_clear(list, 1);
|
2022-03-04 18:32:16 +00:00
|
|
|
goto cleanup;
|
2018-08-13 11:33:04 +00:00
|
|
|
}
|
|
|
|
util->matching = -1;
|
|
|
|
in_header = 1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-04-15 20:32:24 +00:00
|
|
|
if (!util) {
|
|
|
|
error(_("could not parse first line of `log` output: "
|
|
|
|
"did not start with 'commit ': '%s'"),
|
|
|
|
line);
|
|
|
|
string_list_clear(list, 1);
|
2022-03-04 18:32:16 +00:00
|
|
|
goto cleanup;
|
2020-04-15 20:32:24 +00:00
|
|
|
}
|
|
|
|
|
2019-07-11 16:08:46 +00:00
|
|
|
if (starts_with(line, "diff --git")) {
|
2019-07-11 16:08:49 +00:00
|
|
|
struct patch patch = { 0 };
|
|
|
|
struct strbuf root = STRBUF_INIT;
|
|
|
|
int linenr = 0;
|
2020-04-15 20:32:25 +00:00
|
|
|
int orig_len;
|
2019-07-11 16:08:49 +00:00
|
|
|
|
2018-08-13 11:33:04 +00:00
|
|
|
in_header = 0;
|
|
|
|
strbuf_addch(&buf, '\n');
|
|
|
|
if (!util->diff_offset)
|
|
|
|
util->diff_offset = buf.len;
|
range-diff: handle unterminated lines in read_patches()
When parsing our buffer of output from git-log, we have a
find_end_of_line() helper that finds the next newline, and gives us the
number of bytes to move past it, or the size of the whole remaining
buffer if there is no newline.
But trying to handle both those cases leads to some oddities:
- we try to overwrite the newline with NUL in the caller, by writing
over line[len-1]. This is at best redundant, since the helper will
already have done so if it saw a newline. But if it didn't see a
newline, it's actively wrong; we'll overwrite the byte at the end of
the (unterminated) line.
We could solve this just dropping the extra NUL assignment in the
caller and just letting the helper do the right thing. But...
- if we see a "diff --git" line, we'll restore the newline on top of
the NUL byte, so we can pass the string to parse_git_diff_header().
But if there was no newline in the first place, we can't do this.
There's no place to put it (the current code writes a newline
over whatever byte we obliterated earlier). The best we can do is
feed the complete remainder of the buffer to the function (which is,
in fact, a string, by virtue of being a strbuf).
To solve this, the caller needs to know whether we actually found a
newline or not. We could modify find_end_of_line() to return that
information, but we can further observe that it has only one caller.
So let's just inline it in that caller.
Nobody seems to have noticed this case, probably because git-log would
never produce input that doesn't end with a newline. Arguably we could
just return an error as soon as we see that the output does not end in a
newline. But the code to do so actually ends up _longer_, mostly because
of the cleanup we have to do in handling the error.
Signed-off-by: Jeff King <peff@peff.net>
Acked-by: Derrick Stolee <dstolee@microsoft.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2021-08-09 22:48:39 +00:00
|
|
|
if (eol)
|
|
|
|
*eol = '\n';
|
2020-04-15 20:32:25 +00:00
|
|
|
orig_len = len;
|
2019-10-02 21:10:47 +00:00
|
|
|
len = parse_git_diff_header(&root, &linenr, 0, line,
|
2019-07-11 16:08:49 +00:00
|
|
|
len, size, &patch);
|
2021-02-04 20:07:50 +00:00
|
|
|
if (len < 0) {
|
|
|
|
error(_("could not parse git header '%.*s'"),
|
|
|
|
orig_len, line);
|
2022-03-04 18:32:16 +00:00
|
|
|
FREE_AND_NULL(util);
|
2021-02-04 20:07:50 +00:00
|
|
|
string_list_clear(list, 1);
|
2022-03-04 18:32:16 +00:00
|
|
|
goto cleanup;
|
2021-02-04 20:07:50 +00:00
|
|
|
}
|
2019-07-11 16:08:49 +00:00
|
|
|
strbuf_addstr(&buf, " ## ");
|
|
|
|
if (patch.is_new > 0)
|
|
|
|
strbuf_addf(&buf, "%s (new)", patch.new_name);
|
|
|
|
else if (patch.is_delete > 0)
|
|
|
|
strbuf_addf(&buf, "%s (deleted)", patch.old_name);
|
|
|
|
else if (patch.is_rename)
|
|
|
|
strbuf_addf(&buf, "%s => %s", patch.old_name, patch.new_name);
|
|
|
|
else
|
|
|
|
strbuf_addstr(&buf, patch.new_name);
|
|
|
|
|
2019-07-11 16:08:50 +00:00
|
|
|
free(current_filename);
|
|
|
|
if (patch.is_delete > 0)
|
|
|
|
current_filename = xstrdup(patch.old_name);
|
|
|
|
else
|
|
|
|
current_filename = xstrdup(patch.new_name);
|
|
|
|
|
2019-07-11 16:08:49 +00:00
|
|
|
if (patch.new_mode && patch.old_mode &&
|
|
|
|
patch.old_mode != patch.new_mode)
|
|
|
|
strbuf_addf(&buf, " (mode change %06o => %06o)",
|
|
|
|
patch.old_mode, patch.new_mode);
|
|
|
|
|
|
|
|
strbuf_addstr(&buf, " ##");
|
2022-03-04 18:32:15 +00:00
|
|
|
release_patch(&patch);
|
2018-08-13 11:33:04 +00:00
|
|
|
} else if (in_header) {
|
2019-07-11 16:08:46 +00:00
|
|
|
if (starts_with(line, "Author: ")) {
|
2019-07-11 16:08:51 +00:00
|
|
|
strbuf_addstr(&buf, " ## Metadata ##\n");
|
2019-07-11 16:08:46 +00:00
|
|
|
strbuf_addstr(&buf, line);
|
2018-08-13 11:33:04 +00:00
|
|
|
strbuf_addstr(&buf, "\n\n");
|
2019-07-11 16:08:51 +00:00
|
|
|
strbuf_addstr(&buf, " ## Commit message ##\n");
|
2019-11-20 21:18:43 +00:00
|
|
|
} else if (starts_with(line, "Notes") &&
|
|
|
|
line[strlen(line) - 1] == ':') {
|
|
|
|
strbuf_addstr(&buf, "\n\n");
|
|
|
|
/* strip the trailing colon */
|
|
|
|
strbuf_addf(&buf, " ## %.*s ##\n",
|
|
|
|
(int)(strlen(line) - 1), line);
|
2019-07-11 16:08:46 +00:00
|
|
|
} else if (starts_with(line, " ")) {
|
|
|
|
p = line + len - 2;
|
|
|
|
while (isspace(*p) && p >= line)
|
|
|
|
p--;
|
|
|
|
strbuf_add(&buf, line, p - line + 1);
|
2018-08-13 11:33:04 +00:00
|
|
|
strbuf_addch(&buf, '\n');
|
|
|
|
}
|
|
|
|
continue;
|
2019-07-11 16:08:47 +00:00
|
|
|
} else if (skip_prefix(line, "@@ ", &p)) {
|
|
|
|
p = strstr(p, "@@");
|
2019-07-11 16:08:50 +00:00
|
|
|
strbuf_addstr(&buf, "@@");
|
|
|
|
if (current_filename && p[2])
|
|
|
|
strbuf_addf(&buf, " %s:", current_filename);
|
|
|
|
if (p)
|
|
|
|
strbuf_addstr(&buf, p + 2);
|
2019-07-11 16:08:49 +00:00
|
|
|
} else if (!line[0])
|
2018-08-13 11:33:04 +00:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
continue;
|
2019-07-11 16:08:46 +00:00
|
|
|
else if (line[0] == '>') {
|
2018-08-17 20:43:53 +00:00
|
|
|
strbuf_addch(&buf, '+');
|
2019-07-11 16:08:46 +00:00
|
|
|
strbuf_addstr(&buf, line + 1);
|
|
|
|
} else if (line[0] == '<') {
|
2018-08-17 20:43:53 +00:00
|
|
|
strbuf_addch(&buf, '-');
|
2019-07-11 16:08:46 +00:00
|
|
|
strbuf_addstr(&buf, line + 1);
|
|
|
|
} else if (line[0] == '#') {
|
2018-08-17 20:43:53 +00:00
|
|
|
strbuf_addch(&buf, ' ');
|
2019-07-11 16:08:46 +00:00
|
|
|
strbuf_addstr(&buf, line + 1);
|
2018-08-17 20:43:53 +00:00
|
|
|
} else {
|
2018-08-17 20:43:54 +00:00
|
|
|
strbuf_addch(&buf, ' ');
|
2019-07-11 16:08:46 +00:00
|
|
|
strbuf_addstr(&buf, line);
|
2018-08-17 20:43:53 +00:00
|
|
|
}
|
2018-08-13 11:33:04 +00:00
|
|
|
|
|
|
|
strbuf_addch(&buf, '\n');
|
|
|
|
util->diffsize++;
|
|
|
|
}
|
2022-03-04 18:32:16 +00:00
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
cleanup:
|
2019-07-11 16:08:46 +00:00
|
|
|
strbuf_release(&contents);
|
2018-08-13 11:33:04 +00:00
|
|
|
|
|
|
|
if (util)
|
|
|
|
string_list_append(list, buf.buf)->util = util;
|
|
|
|
strbuf_release(&buf);
|
2019-07-11 16:08:50 +00:00
|
|
|
free(current_filename);
|
2018-08-13 11:33:04 +00:00
|
|
|
|
2022-03-04 18:32:16 +00:00
|
|
|
return ret;
|
2018-08-13 11:33:04 +00:00
|
|
|
}
|
|
|
|
|
2022-08-25 17:09:48 +00:00
|
|
|
static int patch_util_cmp(const void *cmp_data UNUSED,
|
2022-08-19 10:08:46 +00:00
|
|
|
const struct patch_util *a,
|
|
|
|
const struct patch_util *b,
|
|
|
|
const char *keydata)
|
2018-08-13 11:33:04 +00:00
|
|
|
{
|
|
|
|
return strcmp(a->diff, keydata ? keydata : b->diff);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void find_exact_matches(struct string_list *a, struct string_list *b)
|
|
|
|
{
|
2020-11-11 20:02:20 +00:00
|
|
|
struct hashmap map = HASHMAP_INIT((hashmap_cmp_fn)patch_util_cmp, NULL);
|
2018-08-13 11:33:04 +00:00
|
|
|
int i;
|
|
|
|
|
|
|
|
/* 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;
|
2019-10-06 23:30:27 +00:00
|
|
|
hashmap_entry_init(&util->e, strhash(util->diff));
|
2019-10-06 23:30:29 +00:00
|
|
|
hashmap_add(&map, &util->e);
|
2018-08-13 11:33:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 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;
|
2019-10-06 23:30:27 +00:00
|
|
|
hashmap_entry_init(&util->e, strhash(util->diff));
|
2019-10-06 23:30:42 +00:00
|
|
|
other = hashmap_remove_entry(&map, util, e, NULL);
|
2018-08-13 11:33:04 +00:00
|
|
|
if (other) {
|
|
|
|
if (other->matching >= 0)
|
|
|
|
BUG("already assigned!");
|
|
|
|
|
|
|
|
other->matching = i;
|
|
|
|
util->matching = other->i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-02 18:55:05 +00:00
|
|
|
hashmap_clear(&map);
|
2018-08-13 11:33:04 +00:00
|
|
|
}
|
|
|
|
|
2022-12-13 11:13:48 +00:00
|
|
|
static int diffsize_consume(void *data,
|
|
|
|
char *line UNUSED,
|
|
|
|
unsigned long len UNUSED)
|
2018-08-13 11:33:04 +00:00
|
|
|
{
|
|
|
|
(*(int *)data)++;
|
2021-04-12 17:15:24 +00:00
|
|
|
return 0;
|
2018-08-13 11:33:04 +00:00
|
|
|
}
|
|
|
|
|
2022-12-13 11:13:48 +00:00
|
|
|
static void diffsize_hunk(void *data,
|
|
|
|
long ob UNUSED, long on UNUSED,
|
|
|
|
long nb UNUSED, long nn UNUSED,
|
|
|
|
const char *func UNUSED, long funclen UNUSED)
|
2018-11-02 06:39:40 +00:00
|
|
|
{
|
|
|
|
diffsize_consume(data, NULL, 0);
|
|
|
|
}
|
|
|
|
|
2018-08-13 11:33:04 +00:00
|
|
|
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;
|
2018-11-02 06:39:40 +00:00
|
|
|
if (!xdi_diff_outf(&mf1, &mf2,
|
|
|
|
diffsize_hunk, diffsize_consume, &count,
|
|
|
|
&pp, &cfg))
|
2018-08-13 11:33:04 +00:00
|
|
|
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 11:33:18 +00:00
|
|
|
static void output_pair_header(struct diff_options *diffopt,
|
2018-08-13 11:33:28 +00:00
|
|
|
int patch_no_width,
|
2018-08-13 11:33:18 +00:00
|
|
|
struct strbuf *buf,
|
2018-08-13 11:33:13 +00:00
|
|
|
struct strbuf *dashes,
|
|
|
|
struct patch_util *a_util,
|
|
|
|
struct patch_util *b_util)
|
2018-08-13 11:33:04 +00:00
|
|
|
{
|
2018-08-13 11:33:13 +00:00
|
|
|
struct object_id *oid = a_util ? &a_util->oid : &b_util->oid;
|
|
|
|
struct commit *commit;
|
2018-08-13 11:33:18 +00: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;
|
2023-02-28 16:13:27 +00:00
|
|
|
int abbrev = diffopt->abbrev;
|
2023-02-20 14:24:44 +00:00
|
|
|
|
|
|
|
if (abbrev < 0)
|
|
|
|
abbrev = DEFAULT_ABBREV;
|
2018-08-13 11:33:13 +00:00
|
|
|
|
|
|
|
if (!dashes->len)
|
|
|
|
strbuf_addchars(dashes, '-',
|
2023-03-28 13:58:46 +00:00
|
|
|
strlen(repo_find_unique_abbrev(the_repository, oid, abbrev)));
|
2018-08-13 11:33:13 +00:00
|
|
|
|
2018-08-13 11:33:18 +00: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 11:33:13 +00:00
|
|
|
strbuf_reset(buf);
|
2018-08-13 11:33:18 +00:00
|
|
|
strbuf_addstr(buf, status == '!' ? color_old : color);
|
2018-08-13 11:33:13 +00:00
|
|
|
if (!a_util)
|
2018-08-13 11:33:28 +00:00
|
|
|
strbuf_addf(buf, "%*s: %s ", patch_no_width, "-", dashes->buf);
|
2018-08-13 11:33:13 +00:00
|
|
|
else
|
2018-08-13 11:33:28 +00:00
|
|
|
strbuf_addf(buf, "%*d: %s ", patch_no_width, a_util->i + 1,
|
2023-03-28 13:58:46 +00:00
|
|
|
repo_find_unique_abbrev(the_repository, &a_util->oid, abbrev));
|
2018-08-13 11:33:13 +00:00
|
|
|
|
2018-08-13 11:33:18 +00: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 11:33:13 +00:00
|
|
|
|
|
|
|
if (!b_util)
|
2018-08-13 11:33:28 +00:00
|
|
|
strbuf_addf(buf, " %*s: %s", patch_no_width, "-", dashes->buf);
|
2018-08-13 11:33:13 +00:00
|
|
|
else
|
2018-08-13 11:33:28 +00:00
|
|
|
strbuf_addf(buf, " %*d: %s", patch_no_width, b_util->i + 1,
|
2023-03-28 13:58:46 +00:00
|
|
|
repo_find_unique_abbrev(the_repository, &b_util->oid, abbrev));
|
2018-08-13 11:33:13 +00:00
|
|
|
|
|
|
|
commit = lookup_commit_reference(the_repository, oid);
|
|
|
|
if (commit) {
|
2018-08-13 11:33:18 +00:00
|
|
|
if (status == '!')
|
|
|
|
strbuf_addf(buf, "%s%s", color_reset, color);
|
|
|
|
|
2018-08-13 11:33:13 +00:00
|
|
|
strbuf_addch(buf, ' ');
|
|
|
|
pp_commit_easy(CMIT_FMT_ONELINE, commit, buf);
|
|
|
|
}
|
2018-08-13 11:33:18 +00:00
|
|
|
strbuf_addf(buf, "%s\n", color_reset);
|
2018-08-13 11:33:13 +00:00
|
|
|
|
2018-07-22 09:57:10 +00:00
|
|
|
fwrite(buf->buf, buf->len, 1, diffopt->file);
|
2018-08-13 11:33:04 +00:00
|
|
|
}
|
|
|
|
|
2019-07-11 16:08:51 +00:00
|
|
|
static struct userdiff_driver section_headers = {
|
|
|
|
.funcname = { "^ ## (.*) ##$\n"
|
|
|
|
"^.?@@ (.*)$", REG_EXTENDED }
|
2018-08-13 11:33:14 +00:00
|
|
|
};
|
|
|
|
|
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 11:33:07 +00:00
|
|
|
static struct diff_filespec *get_filespec(const char *name, const char *p)
|
|
|
|
{
|
|
|
|
struct diff_filespec *spec = alloc_filespec(name);
|
|
|
|
|
2021-04-26 01:02:56 +00: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 11:33:07 +00:00
|
|
|
spec->data = (char *)p;
|
|
|
|
spec->size = strlen(p);
|
|
|
|
spec->should_munmap = 0;
|
|
|
|
spec->is_stdin = 1;
|
2019-07-11 16:08:51 +00:00
|
|
|
spec->driver = §ion_headers;
|
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 11:33:07 +00:00
|
|
|
|
|
|
|
return spec;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void patch_diff(const char *a, const char *b,
|
2019-07-11 16:08:45 +00:00
|
|
|
struct diff_options *diffopt)
|
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 11:33:07 +00:00
|
|
|
{
|
|
|
|
diff_queue(&diff_queued_diff,
|
|
|
|
get_filespec("a", a), get_filespec("b", b));
|
|
|
|
|
|
|
|
diffcore_std(diffopt);
|
|
|
|
diff_flush(diffopt);
|
|
|
|
}
|
|
|
|
|
2022-12-13 11:13:48 +00:00
|
|
|
static struct strbuf *output_prefix_cb(struct diff_options *opt UNUSED, void *data)
|
2021-02-05 14:46:12 +00:00
|
|
|
{
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
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 11:33:07 +00:00
|
|
|
static void output(struct string_list *a, struct string_list *b,
|
2021-02-05 14:46:12 +00:00
|
|
|
struct range_diff_options *range_diff_opts)
|
2018-08-13 11:33:04 +00:00
|
|
|
{
|
2018-08-13 11:33:13 +00:00
|
|
|
struct strbuf buf = STRBUF_INIT, dashes = STRBUF_INIT;
|
2018-08-13 11:33:28 +00:00
|
|
|
int patch_no_width = decimal_width(1 + (a->nr > b->nr ? a->nr : b->nr));
|
2018-08-13 11:33:05 +00:00
|
|
|
int i = 0, j = 0;
|
2021-02-05 14:46:12 +00:00
|
|
|
struct diff_options opts;
|
|
|
|
struct strbuf indent = STRBUF_INIT;
|
|
|
|
|
|
|
|
if (range_diff_opts->diffopt)
|
|
|
|
memcpy(&opts, range_diff_opts->diffopt, sizeof(opts));
|
|
|
|
else
|
2023-03-28 13:58:49 +00:00
|
|
|
repo_diff_setup(the_repository, &opts);
|
2021-02-05 14:46:12 +00:00
|
|
|
|
2021-09-04 07:50:58 +00:00
|
|
|
opts.no_free = 1;
|
2021-02-05 14:46:12 +00:00
|
|
|
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);
|
2018-08-13 11:33:05 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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) {
|
2021-02-05 14:46:13 +00:00
|
|
|
if (!range_diff_opts->right_only)
|
|
|
|
output_pair_header(&opts, patch_no_width,
|
2018-08-13 11:33:18 +00:00
|
|
|
&buf, &dashes, a_util, NULL);
|
2018-08-13 11:33:05 +00:00
|
|
|
i++;
|
|
|
|
continue;
|
|
|
|
}
|
2018-08-13 11:33:04 +00:00
|
|
|
|
2018-08-13 11:33:05 +00:00
|
|
|
/* Show unmatched RHS commits. */
|
|
|
|
while (j < b->nr && b_util->matching < 0) {
|
2021-02-05 14:46:13 +00:00
|
|
|
if (!range_diff_opts->left_only)
|
|
|
|
output_pair_header(&opts, patch_no_width,
|
2018-08-13 11:33:18 +00:00
|
|
|
&buf, &dashes, NULL, b_util);
|
2018-08-13 11:33:05 +00:00
|
|
|
b_util = ++j < b->nr ? b->items[j].util : NULL;
|
2018-08-13 11:33:04 +00:00
|
|
|
}
|
|
|
|
|
2018-08-13 11:33:05 +00:00
|
|
|
/* Show matching LHS/RHS pair. */
|
|
|
|
if (j < b->nr) {
|
|
|
|
a_util = a->items[b_util->matching].util;
|
2021-02-05 14:46:12 +00:00
|
|
|
output_pair_header(&opts, patch_no_width,
|
2018-08-13 11:33:18 +00:00
|
|
|
&buf, &dashes, a_util, b_util);
|
2021-02-05 14:46:12 +00:00
|
|
|
if (!(opts.output_format & DIFF_FORMAT_NO_OUTPUT))
|
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 11:33:07 +00:00
|
|
|
patch_diff(a->items[b_util->matching].string,
|
2021-02-05 14:46:12 +00:00
|
|
|
b->items[j].string, &opts);
|
2018-08-13 11:33:05 +00:00
|
|
|
a_util->shown = 1;
|
|
|
|
j++;
|
|
|
|
}
|
2018-08-13 11:33:04 +00:00
|
|
|
}
|
2018-08-13 11:33:13 +00:00
|
|
|
strbuf_release(&buf);
|
|
|
|
strbuf_release(&dashes);
|
2021-02-05 14:46:12 +00:00
|
|
|
strbuf_release(&indent);
|
2021-09-04 07:50:58 +00:00
|
|
|
opts.no_free = 0;
|
|
|
|
diff_free(&opts);
|
2018-07-22 09:57:12 +00:00
|
|
|
}
|
|
|
|
|
2018-08-13 11:33:04 +00:00
|
|
|
int show_range_diff(const char *range1, const char *range2,
|
2021-02-05 14:46:11 +00:00
|
|
|
struct range_diff_options *range_diff_opts)
|
2018-08-13 11:33:04 +00:00
|
|
|
{
|
|
|
|
int res = 0;
|
|
|
|
|
|
|
|
struct string_list branch1 = STRING_LIST_INIT_DUP;
|
|
|
|
struct string_list branch2 = STRING_LIST_INIT_DUP;
|
|
|
|
|
2021-02-05 14:46:13 +00:00
|
|
|
if (range_diff_opts->left_only && range_diff_opts->right_only)
|
2022-01-05 20:02:14 +00:00
|
|
|
res = error(_("options '%s' and '%s' cannot be used together"), "--left-only", "--right-only");
|
2021-02-05 14:46:13 +00:00
|
|
|
|
|
|
|
if (!res && read_patches(range1, &branch1, range_diff_opts->other_arg))
|
2018-08-13 11:33:04 +00:00
|
|
|
res = error(_("could not parse log for '%s'"), range1);
|
2021-02-05 14:46:11 +00:00
|
|
|
if (!res && read_patches(range2, &branch2, range_diff_opts->other_arg))
|
2018-08-13 11:33:04 +00:00
|
|
|
res = error(_("could not parse log for '%s'"), range2);
|
|
|
|
|
|
|
|
if (!res) {
|
|
|
|
find_exact_matches(&branch1, &branch2);
|
2021-02-05 14:46:11 +00:00
|
|
|
get_correspondences(&branch1, &branch2,
|
|
|
|
range_diff_opts->creation_factor);
|
2021-02-05 14:46:12 +00:00
|
|
|
output(&branch1, &branch2, range_diff_opts);
|
2018-08-13 11:33:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
string_list_clear(&branch1, 1);
|
|
|
|
string_list_clear(&branch2, 1);
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
2021-01-27 16:37:22 +00:00
|
|
|
|
|
|
|
int is_range_diff_range(const char *arg)
|
|
|
|
{
|
2021-02-05 14:44:48 +00:00
|
|
|
char *copy = xstrdup(arg); /* setup_revisions() modifies it */
|
|
|
|
const char *argv[] = { "", copy, "--", NULL };
|
|
|
|
int i, positive = 0, negative = 0;
|
|
|
|
struct rev_info revs;
|
|
|
|
|
2023-03-28 13:58:56 +00:00
|
|
|
repo_init_revisions(the_repository, &revs, NULL);
|
2021-02-05 14:44:48 +00:00
|
|
|
if (setup_revisions(3, argv, &revs, NULL) == 1) {
|
|
|
|
for (i = 0; i < revs.pending.nr; i++)
|
|
|
|
if (revs.pending.objects[i].item->flags & UNINTERESTING)
|
|
|
|
negative++;
|
|
|
|
else
|
|
|
|
positive++;
|
|
|
|
for (i = 0; i < revs.pending.nr; i++) {
|
|
|
|
struct object *obj = revs.pending.objects[i].item;
|
|
|
|
|
|
|
|
if (obj->type == OBJ_COMMIT)
|
|
|
|
clear_commit_marks((struct commit *)obj,
|
|
|
|
ALL_REV_FLAGS);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
free(copy);
|
revision.[ch]: provide and start using a release_revisions()
The users of the revision.[ch] API's "struct rev_info" are a major
source of memory leaks in the test suite under SANITIZE=leak, which in
turn adds a lot of noise when trying to mark up tests with
"TEST_PASSES_SANITIZE_LEAK=true".
The users of that API are largely one-shot, e.g. "git rev-list" or
"git log", or the "git checkout" and "git stash" being modified here
For these callers freeing the memory is arguably a waste of time, but
in many cases they've actually been trying to free the memory, and
just doing that in a buggy manner.
Let's provide a release_revisions() function for these users, and
start migrating them over per the plan outlined in [1]. Right now this
only handles the "pending" member of the struct, but more will be
added in subsequent commits.
Even though we only clear the "pending" member now, let's not leave a
trap in code like the pre-image of index_differs_from(), where we'd
start doing the wrong thing as soon as the release_revisions() learned
to clear its "diffopt". I.e. we need to call release_revisions() after
we've inspected any state in "struct rev_info".
This leaves in place e.g. clear_pathspec(&rev.prune_data) in
stash_working_tree() in builtin/stash.c, subsequent commits will teach
release_revisions() to free "prune_data" and other members that in
some cases are individually cleared by users of "struct rev_info" by
reaching into its members. Those subsequent commits will remove the
relevant calls to e.g. clear_pathspec().
We avoid amending code in index_differs_from() in diff-lib.c as well
as wt_status_collect_changes_index(), has_unstaged_changes() and
has_uncommitted_changes() in wt-status.c in a way that assumes that we
are already clearing the "diffopt" member. That will be handled in a
subsequent commit.
1. https://lore.kernel.org/git/87a6k8daeu.fsf@evledraar.gmail.com/
Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2022-04-13 20:01:35 +00:00
|
|
|
release_revisions(&revs);
|
2021-02-05 14:44:48 +00:00
|
|
|
return negative > 0 && positive > 0;
|
2021-01-27 16:37:22 +00:00
|
|
|
}
|