git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 4/5] patch-id: rewrite code that detects the beginning of a patch

From
Junio C Hamano <gitster@pobox.com>
Date
Jun 21, 2024, 23:18 UTC
Message-ID
<20240621231826.3280338-5-gitster@pobox.com>
In-Reply-To
<20240621231826.3280338-1-gitster@pobox.com>

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 "git diff-tree --stdin" shows;
 (2) "commit <object name>", which is "git log" shows; or
 (3) "From <object name>",  which is "git log --format=email" gives.

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 2bb73ae8 (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 2485eab5 (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 <gitster@pobox.com>
---
 builtin/patch-id.c | 29 +++++++++++++++++++++--------
 1 file changed, 21 insertions(+), 8 deletions(-)
diff --git a/builtin/patch-id.c b/builtin/patch-id.c
index 128e0997d8..a649966f31 100644
--- a/builtin/patch-id.c
+++ b/builtin/patch-id.c
@@ -80,16 +80,19 @@ static int get_one_patchid(struct object_id *next_oid, struct object_id *result,
 		const char *p = line;
 		int len;
 
-		/* Possibly skip over the prefix added by "log" or "format-patch" */
-		if (!skip_prefix(line, "commit ", &p) &&
-		    !skip_prefix(line, "From ", &p) &&
-		    starts_with(line, "\\ ") && 12 < strlen(line)) {
+		/*
+		 * If we see a line that begins with "<object name>",
+		 * "commit <object name>" or "From <object name>", it is
+		 * the beginning of a patch.  Return to the caller, as
+		 * we are done with the one we have been processing.
+		 */
+		if (skip_prefix(line, "commit ", &p))
+			;
+		else if (skip_prefix(line, "From ", &p))
+			;
+		if (!get_oid_hex(p, next_oid)) {
 			if (verbatim)
 				the_hash_algo->update_fn(&ctx, line, strlen(line));
-			continue;
-		}
-
-		if (!get_oid_hex(p, next_oid)) {
 			found_next = 1;
 			break;
 		}
@@ -130,6 +133,16 @@ static int get_one_patchid(struct object_id *next_oid, struct object_id *result,
 				break;
 		}
 
+		/*
+		 * A hunk about an incomplete line may have this
+		 * marker at the end, which should just be ignored.
+		 */
+		if (starts_with(line, "\\ ") && 12 < strlen(line)) {
+			if (verbatim)
+				the_hash_algo->update_fn(&ctx, line, strlen(line));
+			continue;
+		}
+
 		if (diff_is_binary) {
 			if (starts_with(line, "diff ")) {
 				diff_is_binary = 0;
-- 
2.45.2-786-g49444cbe9a
Previous: Patrick SteinhardtNext: Patrick Steinhardt
Message 12 of 20 in “Tighten patch header parsing in patch-id”
  1. 0/5 Tighten patch header parsing in patch-idJunio C Hamano, Jun 21, 2024
  2. 1/5 t4204: patch-id supports various input formatJunio C Hamano, Jun 21, 2024
  3. 2/5 patch-id: call flush_current_id() only when neededJunio C Hamano, Jun 21, 2024
  4. 3/5 patch-id: make get_one_patchid() more extensibleJunio C Hamano, Jun 21, 2024
  5. Patrick SteinhardtJul 29, 2024
  6. Junio C HamanoJul 29, 2024
  7. 5/5 patch-id: tighten code to detect the patch headerJunio C Hamano, Jun 21, 2024
  8. Patrick SteinhardtJul 29, 2024
  9. Junio C HamanoJul 29, 2024
  10. Patrick SteinhardtJul 30, 2024
  11. Patrick SteinhardtJul 30, 2024
  12. 4/5 patch-id: rewrite code that detects the beginning of a patchJunio C Hamano, Jun 21, 2024
  13. Patrick SteinhardtJul 29, 2024
  14. 0/5 Tighten patch header parsing in patch-idJunio C Hamano, Jul 30, 2024
  15. 1/5 t4204: patch-id supports various input formatJunio C Hamano, Jul 30, 2024
  16. 2/5 patch-id: call flush_current_id() only when neededJunio C Hamano, Jul 30, 2024
  17. 3/5 patch-id: make get_one_patchid() more extensibleJunio C Hamano, Jul 30, 2024
  18. 4/5 patch-id: rewrite code that detects the beginning of a patchJunio C Hamano, Jul 30, 2024
  19. 5/5 patch-id: tighten code to detect the patch headerJunio C Hamano, Jul 30, 2024
  20. Patrick SteinhardtJul 30, 2024

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.