@matheusbsbs / matheus-bagliano / commits / 334f8cb22a
Fork 0

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 <gitster@pobox.com>

Junio C Hamano committed Mar 4, 2011 at 14:43 UTC 334f8cb22a085964f1ad7d6063c66767920b3332
1 file changed +14 -2
builtin/apply.c
+14 -2
@@ -2432,7 +2432,8 @@ static void update_image(struct image *img,
2432 }
2433
2434 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)
2437 {
2438 int match_beginning, match_end;
2439 const char *patch = frag->patch;
@@ -2638,6 +2639,15 @@ static int apply_one_fragment(struct image *img, struct fragment *frag,
2639 apply = 0;
2640 }
2641
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 +
2651 /*
2652 * Warn if it was necessary to reduce the number
2653 * of context lines.
@@ -2785,12 +2795,14 @@ static int apply_fragments(struct image *img, struct patch *patch)
2795 const char *name = patch->old_name ? patch->old_name : patch->new_name;
2796 unsigned ws_rule = patch->ws_rule;
2797 unsigned inaccurate_eof = patch->inaccurate_eof;
2798 + int nth = 0;
2799
2800 if (patch->is_binary)
2801 return apply_binary(img, patch);
2802
2803 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)) {
2806 error("patch failed: %s:%ld", name, frag->oldpos);
2807 if (!apply_with_reject)
2808 return -1;