@matheusbsbs / matheus-bagliano / commits / 82a62015a7
Fork 0

patch-id: ignore newline at end of file in diff_flush_patch_id()

Whitespace is ignored when calculating patch IDs. This is done by removing all whitespace from diff lines before hashing them, including a newline at the end of a file. If that newline is missing, however, diff reports that fact in a separate line containing "\ No newline at end of file\n", and this marker is hashed like a context line. This goes against our goal of making patch IDs independent of whitespace. Use the same heuristic that 2485eab55cc (git-patch-id: do not trip over "no newline" markers, 2011-02-17) added to git patch-id instead and skip diff lines that start with a backslash and a space and are longer than twelve characters. Reported-by: Tilman Vogel <tilman.vogel@web.de> Initial-test-by: Tilman Vogel <tilman.vogel@web.de> Signed-off-by: René Scharfe <l.s.r@web.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

René Scharfe committed Aug 19, 2020 at 00:08 UTC 82a62015a7b55a56f779b9ddfb98a3b0552d2bb4
2 files changed +25
diff.c
+2
@@ -6044,6 +6044,8 @@ static void patch_id_consume(void *priv, char *line, unsigned long len)
6044 struct patch_id_t *data = priv;
6045 int new_len;
6046
6047 + if (len > 12 && starts_with(line, "\\ "))
6048 + return;
6049 new_len = remove_space(line, len);
6050
6051 the_hash_algo->update_fn(data->ctx, line, new_len);
t/t3500-cherry.sh
+23
@@ -55,4 +55,27 @@ test_expect_success \
55 expr "$(echo $(git cherry master my-topic-branch) )" : "+ [^ ]* - .*"
56 '
57
58 +test_expect_success 'cherry ignores whitespace' '
59 + git switch --orphan=upstream-with-space &&
60 + test_commit initial file &&
61 + >expect &&
62 + git switch --create=feature-without-space &&
63 +
64 + # A spaceless file on the feature branch. Expect a match upstream.
65 + printf space >file &&
66 + git add file &&
67 + git commit -m"file without space" &&
68 + git log --format="- %H" -1 >>expect &&
69 +
70 + # A further change. Should not match upstream.
71 + test_commit change file &&
72 + git log --format="+ %H" -1 >>expect &&
73 +
74 + git switch upstream-with-space &&
75 + # Same as the spaceless file, just with spaces and on upstream.
76 + test_commit "file with space" file "s p a c e" file-with-space &&
77 + git cherry upstream-with-space feature-without-space >actual &&
78 + test_cmp expect actual
79 +'
80 +
81 test_done