git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH] Fix 'No newline...' annotation in rewrite diffs.

From
ABAdam Butcher <dev.lists@jessamine.co.uk>
Date
Aug 4, 2012, 21:07 UTC
Message-ID
<loom.20120804T230218-811@post.gmane.org>
In-Reply-To
<551f7f77570c84017ae93988f9202854@imap.force9.net>

When operating in --break-rewrites (-B) mode on a file with no newline terminator (and assuming --break-rewrites determines that the diff _is_ a rewrite), git diff previously concatenated the indicator comment '\ No newline at end of file' directly to the terminating line rather than on a line of its own. The resulting diff is broken; claiming that the last line actually contains the indicator text. Without -B there is no problem with the same files.

This patch fixes the former case by inserting a newline into the output prior to emitting the indicator comment.

A couple of tests have been added to the rewrite suite to confirm that the indicator comment is generated on its own line in both plain diff and rewrite mode. The latter test fails if the functional part of this patch (i.e. diff.c) is reverted. ---

Updates: Test only:
  - removed redundant para from commit msg
  - use test_seq shell function instead of seq
  - pull prep statements into individual tests
  - test expected success of git commands in prep
  - confirm that rewrite is considered a rewrite by diff -B
  - remove superfluous comments in favor of test descriptions
  - use variable to spell 'no newline' annotation to support simpler
    maintenance whilst still allowing to check for unexpected leading
    or trailing characters.
 diff.c                  |  1 +
 t/t4022-diff-rewrite.sh | 42 ++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 43 insertions(+)
diff --git a/diff.c b/diff.c
index 1a594df..f333de8 100644
--- a/diff.c
+++ b/diff.c
@@ -574,6 +574,7 @@ static void emit_rewrite_lines(struct emit_callback *ecb,
 	if (!endp) {
 		const char *plain = diff_get_color(ecb->color_diff,
 						   DIFF_PLAIN);
+		putc('\n', ecb->opt->file);
 		emit_line_0(ecb->opt, plain, reset, '\\',
 			    nneof, strlen(nneof));
 	}
diff --git a/t/t4022-diff-rewrite.sh b/t/t4022-diff-rewrite.sh
index c00a94b..1b7ae9f 100755
--- a/t/t4022-diff-rewrite.sh
+++ b/t/t4022-diff-rewrite.sh
@@ -66,5 +66,47 @@ test_expect_success 'suppress deletion diff with -B -D' '
 	grep -v "Linus Torvalds" actual
 '
 
+test_expect_success 'generate initial "no newline at eof" sequence file and 
commit' '
+
+   test_seq 1 99 >seq &&
+   printf 100 >>seq &&
+   git add seq &&
+   git commit seq -m seq
+'
+
+test_expect_success 'rewrite the middle 90% of sequence file and terminate with 
newline' '
+
+   test_seq 1 5 >seq &&
+   test_seq 9331 9420 >>seq &&
+   test_seq 96 100 >>seq
+'
+
+test_expect_success 'confirm that sequence file is considered a rewrite' '
+
+   git diff -B seq >res &&
+   grep "dissimilarity index" res
+'
+
+# Full annotation string used to check for erroneous leading or
+# trailing characters.  Backslash is double escaped due to usage
+# within dq argument to grep expansion below.  
+no_newline_anno='\\\\ No newline at end of file'
+
+test_expect_success 'no newline at eof is on its own line without -B' '
+
+	git diff seq >res &&
+	grep "^'"$no_newline_anno"'$" res &&
+	grep -v "^.\\+'"$no_newline_anno"'" res &&
+	grep -v "'"$no_newline_anno"'.\\+$" res
+'
+
+test_expect_success 'no newline at eof is on its own line with -B' '
+
+	git diff -B seq >res &&
+	grep "^'"$no_newline_anno"'$" res &&
+	grep -v "^.\\+'"$no_newline_anno"'" res &&
+	grep -v "'"$no_newline_anno"'.\\+$" res
+'
+
 test_done
 
-- 
1.7.11.msysgit.1.1.gf0affa1
Previous: Adam ButcherNext: Junio C Hamano
Message 32 of 34 in “Fix 'No newline...' annotation in rewrite diffs.”
  1. Fix 'No newline...' annotation in rewrite diffs.Adam Butcher, Aug 2, 2012
  2. Jeff KingAug 2, 2012
  3. Junio C HamanoAug 2, 2012
  4. Jeff KingAug 2, 2012
  5. Michał KiedrowiczAug 3, 2012
  6. Jeff KingAug 3, 2012
  7. Junio C HamanoAug 3, 2012
  8. Jeff KingAug 3, 2012
  9. tests: Introduce test_seqMichał Kiedrowicz, Aug 3, 2012
  10. Jeff KingAug 3, 2012
  11. Junio C HamanoAug 3, 2012
  12. Jeff KingAug 3, 2012
  13. Michał KiedrowiczAug 3, 2012
  14. Johannes SixtAug 4, 2012
  15. Junio C HamanoAug 4, 2012
  16. Michał KiedrowiczAug 6, 2012
  17. Jeff KingAug 6, 2012
  18. tests: Introduce test_seqMichał Kiedrowicz, Aug 3, 2012
  19. Junio C HamanoAug 3, 2012
  20. Jeff KingAug 3, 2012
  21. Junio C HamanoAug 3, 2012
  22. Michał KiedrowiczAug 4, 2012
  23. Adam ButcherAug 4, 2012
  24. tests: Introduce test_seqMichał Kiedrowicz, Aug 3, 2012
  25. Jeff KingAug 3, 2012
  26. Michał KiedrowiczAug 3, 2012
  27. tests: Introduce test_seqMichał Kiedrowicz, Aug 3, 2012
  28. Jeff KingAug 3, 2012
  29. Adam ButcherAug 2, 2012
  30. Junio C HamanoAug 2, 2012
  31. Adam ButcherAug 2, 2012
  32. Adam ButcherAug 4, 2012
  33. Junio C HamanoAug 5, 2012
  34. Fix '\ No newline...' annotation in rewrite diffsAdam Butcher, Aug 5, 2012

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.