aboutsummaryrefslogtreecommitdiff
path: root/builtin-apply.c
diff options
context:
space:
mode:
authorJunio C Hamano <gitster@pobox.com>2008-04-06 20:04:29 -0700
committerJunio C Hamano <gitster@pobox.com>2008-04-06 20:04:29 -0700
commita1c0dca43a3513574e5bebb38989671960cdaf35 (patch)
treef67bb600dd5da2c3038dff2948a70f267cf237b2 /builtin-apply.c
parentaba201c6e8d9934157b9ba39b8e6b54c2fa7b6e1 (diff)
parentee5a317e0130ab2db59db97c644576335530512d (diff)
downloadgit-a1c0dca43a3513574e5bebb38989671960cdaf35.tar.gz
git-a1c0dca43a3513574e5bebb38989671960cdaf35.tar.xz
Merge branch 'jc/maint-apply-match-beginning'
* jc/maint-apply-match-beginning: Fix "git apply" to correctly enforce "match at the beginning"
Diffstat (limited to 'builtin-apply.c')
-rw-r--r--builtin-apply.c29
1 files changed, 16 insertions, 13 deletions
diff --git a/builtin-apply.c b/builtin-apply.c
index b5f78ac3a..abe73a0f8 100644
--- a/builtin-apply.c
+++ b/builtin-apply.c
@@ -1937,21 +1937,24 @@ static int apply_one_fragment(struct image *img, struct fragment *frag,
trailing = frag->trailing;
/*
- * If we don't have any leading/trailing data in the patch,
- * we want it to match at the beginning/end of the file.
+ * A hunk to change lines at the beginning would begin with
+ * @@ -1,L +N,M @@
*
- * But that would break if the patch is generated with
- * --unified=0; sane people wouldn't do that to cause us
- * trouble, but we try to please not so sane ones as well.
+ * And a hunk to add to an empty file would begin with
+ * @@ -0,0 +N,M @@
+ *
+ * In other words, a hunk that is (frag->oldpos <= 1) with or
+ * without leading context must match at the beginning.
*/
- if (unidiff_zero) {
- match_beginning = (!leading && !frag->oldpos);
- match_end = 0;
- }
- else {
- match_beginning = !leading && (frag->oldpos == 1);
- match_end = !trailing;
- }
+ match_beginning = frag->oldpos <= 1;
+
+ /*
+ * A hunk without trailing lines must match at the end.
+ * However, we simply cannot tell if a hunk must match end
+ * from the lack of trailing lines if the patch was generated
+ * with unidiff without any context.
+ */
+ match_end = !unidiff_zero && !trailing;
pos = frag->newpos ? (frag->newpos - 1) : 0;
preimage.buf = oldlines;