From: Tom Jones Subject: diff: handle missing new lines in trailing context To: gameoftrees@openbsd.org Date: Thu, 29 Sep 2022 10:00:37 +0100 Hi folks, If there is a missing new line in the context we currently miss it and fail to print the warning. This trips up patch and leads to failures. This patch uses the chunk at result->chunks.head[cc->chunk.end] for checking the final atom. I am not sure if can assume there is a chunk there, but I haven't seen another way to access this correctly. It might make more sense to modify the result so it accounts for trailing context, or modify the whitespace check to take the final atom. I'm not sure which is the best way to do this. I have included a test, be warned that if you open these files with vim it will helpfully add the trailing new line and break the test. - Tom diff --git a/lib/diff_output_unidiff.c b/lib/diff_output_unidiff.c index 74b22eb..470302a 100644 --- a/lib/diff_output_unidiff.c +++ b/lib/diff_output_unidiff.c @@ -391,6 +391,11 @@ output_unidiff_chunk(struct diff_output_info *outinfo, FILE *dest, cc->left.end - chunk_end_line); if (rc) return rc; + rc = diff_output_trailing_newline_msg(outinfo, dest, + &result->chunks.head[cc->chunk.end]); + if (rc != DIFF_RC_OK) + return rc; + } return DIFF_RC_OK; diff --git a/test/expect125.diff b/test/expect125.diff new file mode 100644 index 0000000..dc9ca80 --- /dev/null +++ b/test/expect125.diff @@ -0,0 +1,12 @@ +--- test125.left.txt ++++ test125.right.txt +@@ -1,7 +1,7 @@ + This is a test + of missing trailing new lines + in context +-this line has a change ++this line has the change + this is the same + this is too + and this one +\ No newline at end of file diff --git a/test/test125.left.txt b/test/test125.left.txt new file mode 100644 index 0000000..ecce2a6 --- /dev/null +++ b/test/test125.left.txt @@ -0,0 +1,7 @@ +This is a test +of missing trailing new lines +in context +this line has a change +this is the same +this is too +and this one \ No newline at end of file diff --git a/test/test125.right.txt b/test/test125.right.txt new file mode 100644 index 0000000..1242106 --- /dev/null +++ b/test/test125.right.txt @@ -0,0 +1,7 @@ +This is a test +of missing trailing new lines +in context +this line has the change +this is the same +this is too +and this one \ No newline at end of file