Skip to content

Commit 334f8cb

Browse files
committed
apply -v: show offset count when patch did not apply exactly
When the line number the patch intended to touch does not match the line in the version being patched, GNU patch reports that it applied the hunk at a different line number, with how big an offset. Teach "git apply" to do the same under --verbose option. Signed-off-by: Junio C Hamano <[email protected]>
1 parent 964498e commit 334f8cb

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

builtin/apply.c

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2432,7 +2432,8 @@ static void update_image(struct image *img,
24322432
}
24332433

24342434
static int apply_one_fragment(struct image *img, struct fragment *frag,
2435-
int inaccurate_eof, unsigned ws_rule)
2435+
int inaccurate_eof, unsigned ws_rule,
2436+
int nth_fragment)
24362437
{
24372438
int match_beginning, match_end;
24382439
const char *patch = frag->patch;
@@ -2638,6 +2639,15 @@ static int apply_one_fragment(struct image *img, struct fragment *frag,
26382639
apply = 0;
26392640
}
26402641

2642+
if (apply_verbosely && applied_pos != pos) {
2643+
int offset = applied_pos - pos;
2644+
if (apply_in_reverse)
2645+
offset = 0 - offset;
2646+
fprintf(stderr,
2647+
"Hunk #%d succeeded at %d (offset %d lines).\n",
2648+
nth_fragment, applied_pos + 1, offset);
2649+
}
2650+
26412651
/*
26422652
* Warn if it was necessary to reduce the number
26432653
* of context lines.
@@ -2785,12 +2795,14 @@ static int apply_fragments(struct image *img, struct patch *patch)
27852795
const char *name = patch->old_name ? patch->old_name : patch->new_name;
27862796
unsigned ws_rule = patch->ws_rule;
27872797
unsigned inaccurate_eof = patch->inaccurate_eof;
2798+
int nth = 0;
27882799

27892800
if (patch->is_binary)
27902801
return apply_binary(img, patch);
27912802

27922803
while (frag) {
2793-
if (apply_one_fragment(img, frag, inaccurate_eof, ws_rule)) {
2804+
nth++;
2805+
if (apply_one_fragment(img, frag, inaccurate_eof, ws_rule, nth)) {
27942806
error("patch failed: %s:%ld", name, frag->oldpos);
27952807
if (!apply_with_reject)
27962808
return -1;

0 commit comments

Comments
 (0)