From: Junio C Hamano <gitster@pobox.com>
To: git@vger.kernel.org
Cc: "Shawn O. Pearce" <spearce@spearce.org>,
Clemens Buchacher <drizzd@aon.at>,
Linus Torvalds <torvalds@linux-foundation.org>
Subject: [PATCH] git-apply: Loosen "match_beginning" logic
Date: Sat, 30 Aug 2008 12:40:32 -0700 [thread overview]
Message-ID: <7vk5dygukv.fsf_-_@gitster.siamese.dyndns.org> (raw)
In-Reply-To: <7vhc92ie44.fsf@gitster.siamese.dyndns.org> (Junio C. Hamano's message of "Sat, 30 Aug 2008 10:53:15 -0700")
Even after a handfle attempts, match_beginning logic still has corner
cases:
1bf1a85 (apply: treat EOF as proper context., 2006-05-23)
65aadb9 (apply: force matching at the beginning., 2006-05-24)
4be6096 (apply --unidiff-zero: loosen sanity checks ..., 2006-09-17)
ee5a317 (Fix "git apply" to correctly enforce "match ..., 2008-04-06)
This is a tricky piece of code.
We still incorrectly enforce "match_beginning" for -U0 matches.
I noticed this while trying out an example sequence from Clemens Buchacher:
$ echo a >victim
$ git add victim
$ echo b >>victim
$ git diff -U0 >patch
$ cat patch
diff --git i/victim w/victim
index 7898192..422c2b7 100644
--- i/victim
+++ w/victim
@@ -1,0 +2 @@ a
+b
$ git apply --cached --unidiff-zero <patch
$ git show :victim
b
a
The change inserts a new line before the second line, but we insist it to
be applied at the beginning. As the result, the code refuses to apply it
at the original offset, and we end up adding the line at the beginning.
Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
builtin-apply.c | 5 ++++-
1 files changed, 4 insertions(+), 1 deletions(-)
diff --git c/builtin-apply.c w/builtin-apply.c
index 2216a0b..47261e1 100644
--- c/builtin-apply.c
+++ w/builtin-apply.c
@@ -1996,6 +1996,8 @@ static int apply_one_fragment(struct image *img, struct fragment *frag,
/*
* A hunk to change lines at the beginning would begin with
* @@ -1,L +N,M @@
+ * but we need to be careful. -U0 that inserts before the second
+ * line also has this pattern.
*
* And a hunk to add to an empty file would begin with
* @@ -0,0 +N,M @@
@@ -2003,7 +2005,8 @@ static int apply_one_fragment(struct image *img, struct fragment *frag,
* In other words, a hunk that is (frag->oldpos <= 1) with or
* without leading context must match at the beginning.
*/
- match_beginning = frag->oldpos <= 1;
+ match_beginning = (!frag->oldpos ||
+ (frag->oldpos == 1 && !unidiff_zero));
/*
* A hunk without trailing lines must match at the end.
next prev parent reply other threads:[~2008-08-30 19:41 UTC|newest]
Thread overview: 17+ messages / expand[flat|nested] mbox.gz Atom feed top
2008-08-30 16:45 [PATCH] git gui: show diffs with a minimum of 1 context line Clemens Buchacher
2008-08-30 16:56 ` [PATCH] git gui: use apply --unidiff-zero when staging hunks without context Clemens Buchacher
2008-08-30 17:53 ` Junio C Hamano
2008-08-30 19:03 ` [PATCH] git apply: do not match beginning in special unidiff-zero case Clemens Buchacher
2008-08-30 19:54 ` Junio C Hamano
2008-08-30 19:40 ` Junio C Hamano [this message]
2008-08-30 19:43 ` [PATCH] git gui: use apply --unidiff-zero when staging hunks without context Johannes Sixt
2008-08-30 20:27 ` Clemens Buchacher
2008-08-30 20:52 ` Junio C Hamano
2008-08-30 21:00 ` Clemens Buchacher
2008-09-01 19:40 ` Shawn O. Pearce
2008-08-30 17:19 ` [PATCH] git gui: show diffs with a minimum of 1 context line Junio C Hamano
2008-08-30 17:30 ` Junio C Hamano
2008-08-30 20:46 ` Clemens Buchacher
2008-09-01 19:33 ` Shawn O. Pearce
2008-09-01 19:41 ` Clemens Buchacher
2008-09-01 19:47 ` Shawn O. Pearce
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=7vk5dygukv.fsf_-_@gitster.siamese.dyndns.org \
--to=gitster@pobox.com \
--cc=drizzd@aon.at \
--cc=git@vger.kernel.org \
--cc=spearce@spearce.org \
--cc=torvalds@linux-foundation.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).