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

[PATCH v2 07/14] apply: make parse_git_header public

From
Thomas Gummerer <t.gummerer@gmail.com>
Date
Jul 5, 2019, 17:06 UTC
Message-ID
<20190705170630.27500-8-t.gummerer@gmail.com>
In-Reply-To
<20190705170630.27500-1-t.gummerer@gmail.com>

Make parse_git_header a "public" function in apply.h, so we can re-use it in range-diff in a subsequent commit.

Signed-off-by: Thomas Gummerer <t.gummerer@gmail.com>
---

I considered creating a separate struct for only the metadata here, and embedding that in 'struct patch'. As struct patch is mostly metadata fields though, I decided against that to avoid more code churn here.

 apply.c | 68 ++++++++++++++++-----------------------------------------
 apply.h | 48 ++++++++++++++++++++++++++++++++++++++++
 2 files changed, 67 insertions(+), 49 deletions(-)
diff --git a/apply.c b/apply.c
index 468f1d3fee..04319c233f 100644
--- a/apply.c
+++ b/apply.c
@@ -207,40 +207,6 @@ struct fragment {
 #define BINARY_DELTA_DEFLATED	1
 #define BINARY_LITERAL_DEFLATED 2
 
-/*
- * This represents a "patch" to a file, both metainfo changes
- * such as creation/deletion, filemode and content changes represented
- * as a series of fragments.
- */
-struct patch {
-	char *new_name, *old_name, *def_name;
-	unsigned int old_mode, new_mode;
-	int is_new, is_delete;	/* -1 = unknown, 0 = false, 1 = true */
-	int rejected;
-	unsigned ws_rule;
-	int lines_added, lines_deleted;
-	int score;
-	int extension_linenr; /* first line specifying delete/new/rename/copy */
-	unsigned int is_toplevel_relative:1;
-	unsigned int inaccurate_eof:1;
-	unsigned int is_binary:1;
-	unsigned int is_copy:1;
-	unsigned int is_rename:1;
-	unsigned int recount:1;
-	unsigned int conflicted_threeway:1;
-	unsigned int direct_to_threeway:1;
-	unsigned int crlf_in_old:1;
-	struct fragment *fragments;
-	char *result;
-	size_t resultsize;
-	char old_oid_prefix[GIT_MAX_HEXSZ + 1];
-	char new_oid_prefix[GIT_MAX_HEXSZ + 1];
-	struct patch *next;
-
-	/* three-way fallback result */
-	struct object_id threeway_stage[3];
-};
-
 static void free_fragment_list(struct fragment *list)
 {
 	while (list) {
@@ -1321,11 +1287,13 @@ static int check_header_line(int linenr, struct patch *patch)
 }
 
 /* Verify that we recognize the lines following a git header */
-static int parse_git_header(struct apply_state *state,
-			    const char *line,
-			    int len,
-			    unsigned int size,
-			    struct patch *patch)
+int parse_git_header(struct strbuf *root,
+		     int *linenr,
+		     int p_value,
+		     const char *line,
+		     int len,
+		     unsigned int size,
+		     struct patch *patch)
 {
 	unsigned long offset;
 	struct parse_git_header_state parse_hdr_state;
@@ -1340,21 +1308,21 @@ static int parse_git_header(struct apply_state *state,
 	 * or removing or adding empty files), so we get
 	 * the default name from the header.
 	 */
-	patch->def_name = git_header_name(state->p_value, line, len);
-	if (patch->def_name && state->root.len) {
-		char *s = xstrfmt("%s%s", state->root.buf, patch->def_name);
+	patch->def_name = git_header_name(p_value, line, len);
+	if (patch->def_name && root->len) {
+		char *s = xstrfmt("%s%s", root->buf, patch->def_name);
 		free(patch->def_name);
 		patch->def_name = s;
 	}
 
 	line += len;
 	size -= len;
-	state->linenr++;
-	parse_hdr_state.root = &state->root;
-	parse_hdr_state.linenr = state->linenr;
-	parse_hdr_state.p_value = state->p_value;
+	(*linenr)++;
+	parse_hdr_state.root = root;
+	parse_hdr_state.linenr = *linenr;
+	parse_hdr_state.p_value = p_value;
 
-	for (offset = len ; size > 0 ; offset += len, size -= len, line += len, state->linenr++) {
+	for (offset = len ; size > 0 ; offset += len, size -= len, line += len, (*linenr)++) {
 		static const struct opentry {
 			const char *str;
 			int (*fn)(struct parse_git_header_state *, const char *, struct patch *);
@@ -1391,7 +1359,7 @@ static int parse_git_header(struct apply_state *state,
 			res = p->fn(&parse_hdr_state, line + oplen, patch);
 			if (res < 0)
 				return -1;
-			if (check_header_line(state->linenr, patch))
+			if (check_header_line(*linenr, patch))
 				return -1;
 			if (res > 0)
 				return offset;
@@ -1572,7 +1540,9 @@ static int find_header(struct apply_state *state,
 		 * or mode change, so we handle that specially
 		 */
 		if (!memcmp("diff --git ", line, 11)) {
-			int git_hdr_len = parse_git_header(state, line, len, size, patch);
+			int git_hdr_len = parse_git_header(&state->root, &state->linenr,
+							   state->p_value, line, len,
+							   size, patch);
 			if (git_hdr_len < 0)
 				return -128;
 			if (git_hdr_len <= len)
diff --git a/apply.h b/apply.h
index 5948348133..e7a1586b17 100644
--- a/apply.h
+++ b/apply.h
@@ -117,6 +117,40 @@ struct apply_state {
 	int applied_after_fixing_ws;
 };
 
+/*
+ * This represents a "patch" to a file, both metainfo changes
+ * such as creation/deletion, filemode and content changes represented
+ * as a series of fragments.
+ */
+struct patch {
+	char *new_name, *old_name, *def_name;
+	unsigned int old_mode, new_mode;
+	int is_new, is_delete;	/* -1 = unknown, 0 = false, 1 = true */
+	int rejected;
+	unsigned ws_rule;
+	int lines_added, lines_deleted;
+	int score;
+	int extension_linenr; /* first line specifying delete/new/rename/copy */
+	unsigned int is_toplevel_relative:1;
+	unsigned int inaccurate_eof:1;
+	unsigned int is_binary:1;
+	unsigned int is_copy:1;
+	unsigned int is_rename:1;
+	unsigned int recount:1;
+	unsigned int conflicted_threeway:1;
+	unsigned int direct_to_threeway:1;
+	unsigned int crlf_in_old:1;
+	struct fragment *fragments;
+	char *result;
+	size_t resultsize;
+	char old_oid_prefix[GIT_MAX_HEXSZ + 1];
+	char new_oid_prefix[GIT_MAX_HEXSZ + 1];
+	struct patch *next;
+
+	/* three-way fallback result */
+	struct object_id threeway_stage[3];
+};
+
 int apply_parse_options(int argc, const char **argv,
 			struct apply_state *state,
 			int *force_apply, int *options,
@@ -127,6 +161,20 @@ int init_apply_state(struct apply_state *state,
 void clear_apply_state(struct apply_state *state);
 int check_apply_state(struct apply_state *state, int force_apply);
 
+/*
+ * Parse a get header, starting at line.  Fills the relevant metadata
+ * information in 'struct patch'.
+ *
+ * Returns -1 on failure, the length of the parsed header otherwise.
+ */
+int parse_git_header(struct strbuf *root,
+		     int *linenr,
+		     int p_value,
+		     const char *line,
+		     int len,
+		     unsigned int size,
+		     struct patch *patch);
+
 /*
  * Some aspects of the apply behavior are controlled by the following
  * bits in the "options" parameter passed to apply_all_patches().
-- 
2.22.0.510.g264f2c817a
Previous: Thomas GummererNext: Johannes Schindelin
Message 7 of 61 in “output improvements for git range-diff”
  1. 00/14 output improvements for git range-diffThomas Gummerer, Jul 5, 2019
  2. 01/14 apply: replace marc.info link with public-inboxThomas Gummerer, Jul 5, 2019
  3. 03/14 apply: only pass required data to git_header_nameThomas Gummerer, Jul 5, 2019
  4. 02/14 apply: only pass required data to skip_tree_prefixThomas Gummerer, Jul 5, 2019
  5. 04/14 apply: only pass required data to check_header_lineThomas Gummerer, Jul 5, 2019
  6. 05/14 apply: only pass required data to find_name_*Thomas Gummerer, Jul 5, 2019
  7. 07/14 apply: make parse_git_header publicThomas Gummerer, Jul 5, 2019
  8. Johannes SchindelinJul 5, 2019
  9. 06/14 apply: only pass required data to gitdiff_* functionsThomas Gummerer, Jul 5, 2019
  10. Johannes SchindelinJul 5, 2019
  11. 08/14 range-diff: fix function parameter indentationThomas Gummerer, Jul 5, 2019
  12. 09/14 range-diff: split lines manuallyThomas Gummerer, Jul 5, 2019
  13. Johannes SchindelinJul 5, 2019
  14. Thomas GummererJul 8, 2019
  15. 10/14 range-diff: don't remove funcname from inner diffThomas Gummerer, Jul 5, 2019
  16. Johannes SchindelinJul 5, 2019
  17. 11/14 range-diff: suppress line count in outer diffThomas Gummerer, Jul 5, 2019
  18. 12/14 range-diff: add section header instead of diff headerThomas Gummerer, Jul 5, 2019
  19. Johannes SchindelinJul 5, 2019
  20. Thomas GummererJul 8, 2019
  21. Johannes SchindelinJul 8, 2019
  22. 13/14 range-diff: add filename to inner diffThomas Gummerer, Jul 5, 2019
  23. 14/14 range-diff: add headers to the outer hunk headerThomas Gummerer, Jul 5, 2019
  24. Johannes SchindelinJul 5, 2019
  25. Thomas GummererJul 8, 2019
  26. 00/14 output improvements for git range-diffThomas Gummerer, Jul 8, 2019
  27. 01/14 apply: replace marc.info link with public-inboxThomas Gummerer, Jul 8, 2019
  28. 02/14 apply: only pass required data to skip_tree_prefixThomas Gummerer, Jul 8, 2019
  29. 03/14 apply: only pass required data to git_header_nameThomas Gummerer, Jul 8, 2019
  30. 04/14 apply: only pass required data to check_header_lineThomas Gummerer, Jul 8, 2019
  31. 05/14 apply: only pass required data to find_name_*Thomas Gummerer, Jul 8, 2019
  32. 06/14 apply: only pass required data to gitdiff_* functionsThomas Gummerer, Jul 8, 2019
  33. 07/14 apply: make parse_git_header publicThomas Gummerer, Jul 8, 2019
  34. Junio C HamanoJul 9, 2019
  35. Thomas GummererJul 9, 2019
  36. Junio C HamanoJul 9, 2019
  37. Thomas GummererJul 10, 2019
  38. 09/14 range-diff: split lines manuallyThomas Gummerer, Jul 8, 2019
  39. 08/14 range-diff: fix function parameter indentationThomas Gummerer, Jul 8, 2019
  40. 10/14 range-diff: don't remove funcname from inner diffThomas Gummerer, Jul 8, 2019
  41. 11/14 range-diff: suppress line count in outer diffThomas Gummerer, Jul 8, 2019
  42. 12/14 range-diff: add section header instead of diff headerThomas Gummerer, Jul 8, 2019
  43. 14/14 range-diff: add headers to the outer hunk headerThomas Gummerer, Jul 8, 2019
  44. 13/14 range-diff: add filename to inner diffThomas Gummerer, Jul 8, 2019
  45. 00/14 output improvements for git range-diffThomas Gummerer, Jul 11, 2019
  46. 01/14 apply: replace marc.info link with public-inboxThomas Gummerer, Jul 11, 2019
  47. 02/14 apply: only pass required data to skip_tree_prefixThomas Gummerer, Jul 11, 2019
  48. 04/14 apply: only pass required data to check_header_lineThomas Gummerer, Jul 11, 2019
  49. 03/14 apply: only pass required data to git_header_nameThomas Gummerer, Jul 11, 2019
  50. 05/14 apply: only pass required data to find_name_*Thomas Gummerer, Jul 11, 2019
  51. 06/14 apply: only pass required data to gitdiff_* functionsThomas Gummerer, Jul 11, 2019
  52. 08/14 range-diff: fix function parameter indentationThomas Gummerer, Jul 11, 2019
  53. 07/14 apply: make parse_git_diff_header publicThomas Gummerer, Jul 11, 2019
  54. 09/14 range-diff: split lines manuallyThomas Gummerer, Jul 11, 2019
  55. 10/14 range-diff: don't remove funcname from inner diffThomas Gummerer, Jul 11, 2019
  56. 12/14 range-diff: add section header instead of diff headerThomas Gummerer, Jul 11, 2019
  57. 11/14 range-diff: suppress line count in outer diffThomas Gummerer, Jul 11, 2019
  58. 13/14 range-diff: add filename to inner diffThomas Gummerer, Jul 11, 2019
  59. 14/14 range-diff: add headers to the outer hunk headerThomas Gummerer, Jul 11, 2019
  60. Ramsay JonesJul 11, 2019
  61. Johannes SchindelinJul 12, 2019

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.