Skip to content

Commit 3f288b6

Browse files
committed
patch-id: rewrite code that detects the beginning of a patch
The get_one_patchid() function reads input lines until it finds a patch header (the line that begins a patch), whose beginning is one of: (1) an "<object name>", which is what "git diff-tree --stdin" shows; (2) "commit <object name>", which is what "git log" shows; or (3) "From <object name>", which is what "git log --format=email" shows. When it finds such a line, it returns to the caller, reporting the <object name> it found, and the size of the "patch" it processed. The caller then calls the function again, which then ignores the commit log message, and then processes the lines in the patch part until it hits another "beginning of a patch". The above logic was fairly easy to see until 2bb73ae (patch-id: use starts_with() and skip_prefix(), 2016-05-28) reorganized the code, which made another logic that has nothing to do with the "where does the next patch begin?" logic, which came from 2485eab (git-patch-id: do not trip over "no newline" markers, 2011-02-17) that ignores the "\ No newline at the end", rolled into the same single if() statement. Let's split it out. The "\ No newline at the end" marker is part of the patch, should not appear before we start reading the patch part, and does not belong to the detection of patch header. Signed-off-by: Junio C Hamano <[email protected]>
1 parent 2438294 commit 3f288b6

File tree

1 file changed

+21
-8
lines changed

1 file changed

+21
-8
lines changed

builtin/patch-id.c

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -85,16 +85,19 @@ static int get_one_patchid(struct object_id *next_oid, struct object_id *result,
8585
const char *p = line;
8686
int len;
8787

88-
/* Possibly skip over the prefix added by "log" or "format-patch" */
89-
if (!skip_prefix(line, "commit ", &p) &&
90-
!skip_prefix(line, "From ", &p) &&
91-
starts_with(line, "\\ ") && 12 < strlen(line)) {
88+
/*
89+
* If we see a line that begins with "<object name>",
90+
* "commit <object name>" or "From <object name>", it is
91+
* the beginning of a patch. Return to the caller, as
92+
* we are done with the one we have been processing.
93+
*/
94+
if (skip_prefix(line, "commit ", &p))
95+
;
96+
else if (skip_prefix(line, "From ", &p))
97+
;
98+
if (!get_oid_hex(p, next_oid)) {
9299
if (verbatim)
93100
the_hash_algo->update_fn(&ctx, line, strlen(line));
94-
continue;
95-
}
96-
97-
if (!get_oid_hex(p, next_oid)) {
98101
found_next = 1;
99102
break;
100103
}
@@ -135,6 +138,16 @@ static int get_one_patchid(struct object_id *next_oid, struct object_id *result,
135138
break;
136139
}
137140

141+
/*
142+
* A hunk about an incomplete line may have this
143+
* marker at the end, which should just be ignored.
144+
*/
145+
if (starts_with(line, "\\ ") && 12 < strlen(line)) {
146+
if (verbatim)
147+
the_hash_algo->update_fn(&ctx, line, strlen(line));
148+
continue;
149+
}
150+
138151
if (diff_is_binary) {
139152
if (starts_with(line, "diff ")) {
140153
diff_is_binary = 0;

0 commit comments

Comments
 (0)