Skip to content

Commit 001d76e

Browse files
committed
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 parent ba9725a commit 001d76e

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

src/diff.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,7 @@ static int file_cb(
460460
return error;
461461
}
462462

463-
static int line_cb(
463+
static int patchid_line_cb(
464464
const git_diff_delta *delta,
465465
const git_diff_hunk *hunk,
466466
const git_diff_line *line,
@@ -482,6 +482,14 @@ static int line_cb(
482482
break;
483483
case GIT_DIFF_LINE_CONTEXT:
484484
break;
485+
case GIT_DIFF_LINE_CONTEXT_EOFNL:
486+
case GIT_DIFF_LINE_ADD_EOFNL:
487+
case GIT_DIFF_LINE_DEL_EOFNL:
488+
/*
489+
* Ignore EOF without newlines for patch IDs as whitespace is
490+
* not supposed to be significant.
491+
*/
492+
return 0;
485493
default:
486494
git_error_set(GIT_ERROR_PATCH, "invalid line origin for patch");
487495
return -1;
@@ -518,7 +526,7 @@ int git_diff_patchid(git_oid *out, git_diff *diff, git_diff_patchid_options *opt
518526
if ((error = git_hash_ctx_init(&args.ctx)) < 0)
519527
goto out;
520528

521-
if ((error = git_diff_foreach(diff, file_cb, NULL, NULL, line_cb, &args)) < 0)
529+
if ((error = git_diff_foreach(diff, file_cb, NULL, NULL, patchid_line_cb, &args)) < 0)
522530
goto out;
523531

524532
if ((error = (flush_hunk(&args.result, &args.ctx))) < 0)

0 commit comments

Comments
 (0)