Skip to content

Commit aaacd64

Browse files
committed
blame: consult diff process for no-hunk detection
When a diff process is configured via diff.<driver>.process, consult it during blame's per-commit diffing. If the process returns no hunks for a commit's changes to a file, treat the commit as having no changes, causing blame to attribute lines to earlier commits. The consultation happens at the pass_blame_to_parent() callsite using diff_process_fill_hunks(), matching how builtin_diff() in diff.c uses the same function. A new diff_hunks_xpp() variant accepts a pre-populated xpparam_t so callers can pass external hunks, while the existing diff_hunks() retains its original signature and behavior. The copy-detection callsite is unaffected since it does not use the diff process. The subprocess is long-running (one startup cost amortized across the blame traversal), but each commit in the file's history incurs a round-trip to the tool. Signed-off-by: Michael Montalbo <mmontalbo@gmail.com>
1 parent 9225ca2 commit aaacd64

2 files changed

Lines changed: 32 additions & 24 deletions

File tree

blame.c

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
#include "tag.h"
2020
#include "trace2.h"
2121
#include "blame.h"
22+
#include "diff-process.h"
23+
#include "xdiff-interface.h"
2224
#include "alloc.h"
2325
#include "commit-slab.h"
2426
#include "bloom.h"
@@ -314,17 +316,25 @@ static struct commit *fake_working_tree_commit(struct repository *r,
314316

315317

316318

317-
static int diff_hunks(mmfile_t *file_a, mmfile_t *file_b,
318-
xdl_emit_hunk_consume_func_t hunk_func, void *cb_data, int xdl_opts)
319+
static int diff_hunks_xpp(mmfile_t *file_a, mmfile_t *file_b,
320+
xdl_emit_hunk_consume_func_t hunk_func,
321+
void *cb_data, xpparam_t *xpp)
319322
{
320-
xpparam_t xpp = {0};
321323
xdemitconf_t xecfg = {0};
322324
xdemitcb_t ecb = {NULL};
323325

324-
xpp.flags = xdl_opts;
325326
xecfg.hunk_func = hunk_func;
326327
ecb.priv = cb_data;
327-
return xdi_diff(file_a, file_b, &xpp, &xecfg, &ecb);
328+
return xdi_diff(file_a, file_b, xpp, &xecfg, &ecb);
329+
}
330+
331+
static int diff_hunks(mmfile_t *file_a, mmfile_t *file_b,
332+
xdl_emit_hunk_consume_func_t hunk_func, void *cb_data, int xdl_opts)
333+
{
334+
xpparam_t xpp = {0};
335+
336+
xpp.flags = xdl_opts;
337+
return diff_hunks_xpp(file_a, file_b, hunk_func, cb_data, &xpp);
328338
}
329339

330340
static const char *get_next_line(const char *start, const char *end)
@@ -1943,6 +1953,7 @@ static void pass_blame_to_parent(struct blame_scoreboard *sb,
19431953
struct blame_origin *parent, int ignore_diffs)
19441954
{
19451955
mmfile_t file_p, file_o;
1956+
xpparam_t xpp = {0};
19461957
struct blame_chunk_cb_data d;
19471958
struct blame_entry *newdest = NULL;
19481959

@@ -1961,10 +1972,21 @@ static void pass_blame_to_parent(struct blame_scoreboard *sb,
19611972
&sb->num_read_blob, ignore_diffs);
19621973
sb->num_get_patch++;
19631974

1964-
if (diff_hunks(&file_p, &file_o, blame_chunk_cb, &d, sb->xdl_opts))
1965-
die("unable to generate diff (%s -> %s)",
1966-
oid_to_hex(&parent->commit->object.oid),
1967-
oid_to_hex(&target->commit->object.oid));
1975+
xpp.flags = sb->xdl_opts;
1976+
/*
1977+
* If the diff process considers the files equivalent,
1978+
* skip the diff so blame looks past this commit.
1979+
*/
1980+
if (diff_process_fill_hunks(&sb->revs->diffopt, target->path,
1981+
&file_p, &file_o, &xpp)
1982+
!= DIFF_PROCESS_EQUIVALENT) {
1983+
if (diff_hunks_xpp(&file_p, &file_o, blame_chunk_cb,
1984+
&d, &xpp))
1985+
die("unable to generate diff (%s -> %s)",
1986+
oid_to_hex(&parent->commit->object.oid),
1987+
oid_to_hex(&target->commit->object.oid));
1988+
}
1989+
free(xpp.external_hunks);
19681990
/* The rest are the same as the parent */
19691991
blame_chunk(&d.dstq, &d.srcq, INT_MAX, d.offset, INT_MAX, 0,
19701992
parent, target, 0);

t/t4080-diff-process.sh

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -447,21 +447,6 @@ test_expect_success 'diff process skipped when tool omits capability' '
447447
test_must_be_empty stderr
448448
'
449449

450-
test_expect_success 'diff process fallback on malformed hunk line' '
451-
git -c diff.cdiff.process="$BACKEND --mode=bad-parse" \
452-
diff boundary.c >actual 2>stderr &&
453-
test_grep "^-OLD5" actual &&
454-
test_grep "^+NEW5" actual
455-
'
456-
457-
test_expect_success 'diff process skipped when tool omits capability' '
458-
git -c diff.cdiff.process="$BACKEND --mode=no-cap" \
459-
diff boundary.c >actual 2>stderr &&
460-
test_grep "^-OLD5" actual &&
461-
test_grep "^+NEW5" actual &&
462-
test_must_be_empty stderr
463-
'
464-
465450
#
466451
# Blame integration.
467452
#
@@ -566,4 +551,5 @@ test_expect_success 'blame --no-ext-diff uses builtin hunks' '
566551
test_grep "$CHANGE" line9 &&
567552
test_path_is_missing backend.log
568553
'
554+
569555
test_done

0 commit comments

Comments
 (0)