diff: ignore EOFNL for computing patch IDs The patch ID is supposed to be mostly context-insignificant and thus only includes added or deleted lines. As such, we shouldn't honor end-of-file-without-newline markers in diffs. Ignore such lines to fix how we compute the patch ID for such diffs.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
diff --git a/src/diff.c b/src/diff.c
index 6c25921..3c6a0f2 100644
--- a/src/diff.c
+++ b/src/diff.c
@@ -460,7 +460,7 @@ out:
return error;
}
-static int line_cb(
+static int patchid_line_cb(
const git_diff_delta *delta,
const git_diff_hunk *hunk,
const git_diff_line *line,
@@ -482,6 +482,14 @@ static int line_cb(
break;
case GIT_DIFF_LINE_CONTEXT:
break;
+ case GIT_DIFF_LINE_CONTEXT_EOFNL:
+ case GIT_DIFF_LINE_ADD_EOFNL:
+ case GIT_DIFF_LINE_DEL_EOFNL:
+ /*
+ * Ignore EOF without newlines for patch IDs as whitespace is
+ * not supposed to be significant.
+ */
+ return 0;
default:
git_error_set(GIT_ERROR_PATCH, "invalid line origin for patch");
return -1;
@@ -518,7 +526,7 @@ int git_diff_patchid(git_oid *out, git_diff *diff, git_diff_patchid_options *opt
if ((error = git_hash_ctx_init(&args.ctx)) < 0)
goto out;
- if ((error = git_diff_foreach(diff, file_cb, NULL, NULL, line_cb, &args)) < 0)
+ if ((error = git_diff_foreach(diff, file_cb, NULL, NULL, patchid_line_cb, &args)) < 0)
goto out;
if ((error = (flush_hunk(&args.result, &args.ctx))) < 0)