git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Björn Gustavsson" <bgustavsson@gmail.com>
To: git@vger.kernel.org
Cc: Junio C Hamano <gitster@pobox.com>
Subject: [PATCH] apply: Allow blank *trailing* context lines to match beyond EOF
Date: Thu, 08 Apr 2010 06:14:31 +0200	[thread overview]
Message-ID: <4BBD5827.7030003@gmail.com> (raw)

In 51667147be, "git apply --whitespace=fix" was extended to
allow a blank context line to match beyond the end of the file,
but only if the context line was in the leading part of the
hunk (i.e. the hunk inserted additional contents at the end
of the file).

Drop the restriction that the context line must be in the
leading part of the hunk, thus allowing a file to be changed
from:

 a
 (blank line)

to:

 b
 a
 (blank line)

Note that the blank line will be kept, because "--whitespace=fix"
only removes trailing blank lines that a hunk would add, never
trailing blank lines in the context.

Signed-off-by: Björn Gustavsson <bgustavsson@gmail.com>
---
This patch should fix the problem observed by Junio, but note that
there will be one or more blank lines left at the end of the
file.

I am not sure whether that should be fixed. In this particular
case, the blank lines are part of the context but not part of
the file being patched, so it could be argued that the blank
lines should not be added back.

But there are already other circumstances in which
"--whitespace=fix" does not guarantee that the file does
not end with blank lines, for instance if we have:

 a
 b
 (blank line)
 (blank line)
 (blank line)
 (blank line)
 c
 d

and then delete the "c" and "d" lines:

 a
 b
 (blank line)
 (blank line)
 (blank line)
 (blank line)

In this case, not all of the blank lines are even part of the context,
so even if we'll change the rules and remove blanks line in the context,
there would still be blank lines left at the end of the file.


 builtin/apply.c          |   12 ++++++------
 t/t4124-apply-ws-rule.sh |   12 ++++++++++++
 2 files changed, 18 insertions(+), 6 deletions(-)

diff --git a/builtin/apply.c b/builtin/apply.c
index 7ca9047..d17e046 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -1864,13 +1864,13 @@ static int match_fragment(struct image *img,
 		if (match_end && (preimage->nr + try_lno != img->nr))
 			return 0;
 	} else if (ws_error_action == correct_ws_error &&
-		   (ws_rule & WS_BLANK_AT_EOF) && match_end) {
+		   (ws_rule & WS_BLANK_AT_EOF)) {
 		/*
-		 * This hunk that matches at the end extends beyond
-		 * the end of img, and we are removing blank lines
-		 * at the end of the file.  This many lines from the
-		 * beginning of the preimage must match with img, and
-		 * the remainder of the preimage must be blank.
+		 * This hunk extends beyond the end of img, and we are
+		 * removing blank lines at the end of the file.  This
+		 * many lines from the beginning of the preimage must
+		 * match with img, and the remainder of the preimage
+		 * must be blank.
 		 */
 		preimage_limit = img->nr - try_lno;
 	} else {
diff --git a/t/t4124-apply-ws-rule.sh b/t/t4124-apply-ws-rule.sh
index fb9ad24..451d75e 100755
--- a/t/t4124-apply-ws-rule.sh
+++ b/t/t4124-apply-ws-rule.sh
@@ -325,6 +325,18 @@ test_expect_success 'two missing blank lines at end with --whitespace=fix' '
 	test_cmp one expect
 '
 
+test_expect_success 'missing blank line at end, insert before end, --whitespace=fix' '
+	{ echo a; echo; } >one &&
+	git add one &&
+	{ echo b; echo a; echo; } >one &&
+	cp one expect &&
+	git diff -- one >patch &&
+	echo a >one &&
+	test_must_fail git apply patch &&
+	git apply --whitespace=fix patch &&
+	test_cmp one expect
+'
+
 test_expect_success 'shrink file with tons of missing blanks at end of file' '
 	{ echo a; echo b; echo c; } >one &&
 	cp one no-blank-lines &&
-- 
1.7.0.2.157.gb7e7f

             reply	other threads:[~2010-04-08  4:14 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-04-08  4:14 Björn Gustavsson [this message]
2010-04-08  5:32 ` [PATCH] apply: Allow blank *trailing* context lines to match beyond EOF Junio C Hamano

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=4BBD5827.7030003@gmail.com \
    --to=bgustavsson@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    /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).