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,
+               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 
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

To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

Reply via email to