commit b6adedb6f92219b3795f616b46f18e752c5644be from: Stefan Sperling date: Wed Oct 07 22:18:10 2020 UTC fix line numbers in unidiff hunk headers and omit ",1"; matches diff(1) commit - fde86f3da94e40ac11b0cd229348d7ae91456270 commit + b6adedb6f92219b3795f616b46f18e752c5644be blob - da3c98842c7e864025e83544cc9206ac232c8289 blob + 649271a8542372b95f89e82a2a59f8814c6a24f6 --- lib/diff_output_unidiff.c +++ lib/diff_output_unidiff.c @@ -146,7 +146,7 @@ output_unidiff_chunk(struct diff_output_info *outinfo, const struct diff_result *result, const struct diff_chunk_context *cc) { - int rc; + int rc, left_start, left_len, right_start, right_len; off_t outoff = 0, *offp; if (diff_range_empty(&cc->left) && diff_range_empty(&cc->right)) @@ -183,9 +183,28 @@ output_unidiff_chunk(struct diff_output_info *outinfo, state->header_printed = true; } - rc = fprintf(dest, "@@ -%d,%d +%d,%d @@\n", - cc->left.start + 1, cc->left.end - cc->left.start, - cc->right.start + 1, cc->right.end - cc->right.start); + left_len = cc->left.end - cc->left.start; + if (left_len == 0 && cc->left.start > 0) + left_start = cc->left.start; + else + left_start = cc->left.start + 1; + + right_len = cc->right.end - cc->right.start; + if (right_len == 0 && cc->right.start > 0) + right_start = cc->right.start; + else + right_start = cc->right.start + 1; + + if (left_len == 1 && right_len != 1) { + rc = fprintf(dest, "@@ -%d +%d,%d @@\n", + left_start, right_start, right_len); + } else if (left_len != 1 && right_len == 1) { + rc = fprintf(dest, "@@ -%d,%d +%d @@\n", + left_start, left_len, right_start); + } else { + rc = fprintf(dest, "@@ -%d,%d +%d,%d @@\n", + left_start, left_len, right_start, right_len); + } if (rc < 0) return errno; if (outinfo) { blob - 12d576d9e937a74fa6092eb46826729a1c0f16fe blob + c4dc6791528bf9a0cf786652ef20c3cc0aaa4659 --- test/expect008.diff +++ test/expect008.diff @@ -1,6 +1,6 @@ --- test008.left.txt +++ test008.right.txt -@@ -1,1 +1,6 @@ +@@ -1 +1,6 @@ x +a +b