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

[PATCH v8 10/41] builtin/apply: move init_apply_state() to apply.c

From
Christian Couder <christian.couder@gmail.com>
Date
Jun 27, 2016, 18:23 UTC
Message-ID
<20160627182429.31550-11-chriscool@tuxfamily.org>
In-Reply-To
<20160627182429.31550-1-chriscool@tuxfamily.org>

To libify `git apply` functionality we must make init_apply_state() usable outside "builtin/apply.c".

Let's do that by moving it into a new "apply.c".
Helped-by: Eric Sunshine <sunshine@sunshineco.com>
Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
---
 Makefile        |  1 +
 apply.c         | 91 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 apply.h         | 10 +++++++
 builtin/apply.c | 88 -------------------------------------------------------
 4 files changed, 102 insertions(+), 88 deletions(-)
 create mode 100644 apply.c
diff --git a/Makefile b/Makefile
index de5a030..51f8583 100644
--- a/Makefile
+++ b/Makefile
@@ -680,6 +680,7 @@ LIB_OBJS += abspath.o
 LIB_OBJS += advice.o
 LIB_OBJS += alias.o
 LIB_OBJS += alloc.o
+LIB_OBJS += apply.o
 LIB_OBJS += archive.o
 LIB_OBJS += archive-tar.o
 LIB_OBJS += archive-zip.o
diff --git a/apply.c b/apply.c
new file mode 100644
index 0000000..1f31bb4
--- /dev/null
+++ b/apply.c
@@ -0,0 +1,91 @@
+#include "cache.h"
+#include "lockfile.h"
+#include "apply.h"
+
+static void git_apply_config(void)
+{
+	git_config_get_string_const("apply.whitespace", &apply_default_whitespace);
+	git_config_get_string_const("apply.ignorewhitespace", &apply_default_ignorewhitespace);
+	git_config(git_default_config, NULL);
+}
+
+int parse_whitespace_option(struct apply_state *state, const char *option)
+{
+	if (!option) {
+		state->ws_error_action = warn_on_ws_error;
+		return 0;
+	}
+	if (!strcmp(option, "warn")) {
+		state->ws_error_action = warn_on_ws_error;
+		return 0;
+	}
+	if (!strcmp(option, "nowarn")) {
+		state->ws_error_action = nowarn_ws_error;
+		return 0;
+	}
+	if (!strcmp(option, "error")) {
+		state->ws_error_action = die_on_ws_error;
+		return 0;
+	}
+	if (!strcmp(option, "error-all")) {
+		state->ws_error_action = die_on_ws_error;
+		state->squelch_whitespace_errors = 0;
+		return 0;
+	}
+	if (!strcmp(option, "strip") || !strcmp(option, "fix")) {
+		state->ws_error_action = correct_ws_error;
+		return 0;
+	}
+	return error(_("unrecognized whitespace option '%s'"), option);
+}
+
+int parse_ignorewhitespace_option(struct apply_state *state,
+				  const char *option)
+{
+	if (!option || !strcmp(option, "no") ||
+	    !strcmp(option, "false") || !strcmp(option, "never") ||
+	    !strcmp(option, "none")) {
+		state->ws_ignore_action = ignore_ws_none;
+		return 0;
+	}
+	if (!strcmp(option, "change")) {
+		state->ws_ignore_action = ignore_ws_change;
+		return 0;
+	}
+	return error(_("unrecognized whitespace ignore option '%s'"), option);
+}
+
+void init_apply_state(struct apply_state *state,
+		      const char *prefix,
+		      struct lock_file *lock_file)
+{
+	memset(state, 0, sizeof(*state));
+	state->prefix = prefix;
+	state->prefix_length = state->prefix ? strlen(state->prefix) : 0;
+	state->lock_file = lock_file;
+	state->newfd = -1;
+	state->apply = 1;
+	state->line_termination = '\n';
+	state->p_value = 1;
+	state->p_context = UINT_MAX;
+	state->squelch_whitespace_errors = 5;
+	state->ws_error_action = warn_on_ws_error;
+	state->ws_ignore_action = ignore_ws_none;
+	state->linenr = 1;
+	strbuf_init(&state->root, 0);
+
+	git_apply_config();
+	if (apply_default_whitespace && parse_whitespace_option(state, apply_default_whitespace))
+		exit(1);
+	if (apply_default_ignorewhitespace && parse_ignorewhitespace_option(state, apply_default_ignorewhitespace))
+		exit(1);
+}
+
+void clear_apply_state(struct apply_state *state)
+{
+	string_list_clear(&state->limit_by_name, 0);
+	string_list_clear(&state->symlink_changes, 0);
+	strbuf_release(&state->root);
+
+	/* &state->fn_table is cleared at the end of apply_patch() */
+}
diff --git a/apply.h b/apply.h
index 7493a40..08c0a25 100644
--- a/apply.h
+++ b/apply.h
@@ -97,4 +97,14 @@ struct apply_state {
 	int applied_after_fixing_ws;
 };
 
+extern int parse_whitespace_option(struct apply_state *state,
+				   const char *option);
+extern int parse_ignorewhitespace_option(struct apply_state *state,
+					 const char *option);
+
+extern void init_apply_state(struct apply_state *state,
+			     const char *prefix,
+			     struct lock_file *lock_file);
+extern void clear_apply_state(struct apply_state *state);
+
 #endif
diff --git a/builtin/apply.c b/builtin/apply.c
index 7d0bf66..4fbd993 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -27,52 +27,6 @@ static const char * const apply_usage[] = {
 	NULL
 };
 
-static int parse_whitespace_option(struct apply_state *state, const char *option)
-{
-	if (!option) {
-		state->ws_error_action = warn_on_ws_error;
-		return 0;
-	}
-	if (!strcmp(option, "warn")) {
-		state->ws_error_action = warn_on_ws_error;
-		return 0;
-	}
-	if (!strcmp(option, "nowarn")) {
-		state->ws_error_action = nowarn_ws_error;
-		return 0;
-	}
-	if (!strcmp(option, "error")) {
-		state->ws_error_action = die_on_ws_error;
-		return 0;
-	}
-	if (!strcmp(option, "error-all")) {
-		state->ws_error_action = die_on_ws_error;
-		state->squelch_whitespace_errors = 0;
-		return 0;
-	}
-	if (!strcmp(option, "strip") || !strcmp(option, "fix")) {
-		state->ws_error_action = correct_ws_error;
-		return 0;
-	}
-	return error(_("unrecognized whitespace option '%s'"), option);
-}
-
-static int parse_ignorewhitespace_option(struct apply_state *state,
-					 const char *option)
-{
-	if (!option || !strcmp(option, "no") ||
-	    !strcmp(option, "false") || !strcmp(option, "never") ||
-	    !strcmp(option, "none")) {
-		state->ws_ignore_action = ignore_ws_none;
-		return 0;
-	}
-	if (!strcmp(option, "change")) {
-		state->ws_ignore_action = ignore_ws_change;
-		return 0;
-	}
-	return error(_("unrecognized whitespace ignore option '%s'"), option);
-}
-
 static void set_default_whitespace_mode(struct apply_state *state)
 {
 	if (!state->whitespace_option && !apply_default_whitespace)
@@ -4539,13 +4493,6 @@ end:
 	return res;
 }
 
-static void git_apply_config(void)
-{
-	git_config_get_string_const("apply.whitespace", &apply_default_whitespace);
-	git_config_get_string_const("apply.ignorewhitespace", &apply_default_ignorewhitespace);
-	git_config(git_default_config, NULL);
-}
-
 static int option_parse_exclude(const struct option *opt,
 				const char *arg, int unset)
 {
@@ -4604,41 +4551,6 @@ static int option_parse_directory(const struct option *opt,
 	return 0;
 }
 
-static void init_apply_state(struct apply_state *state,
-			     const char *prefix,
-			     struct lock_file *lock_file)
-{
-	memset(state, 0, sizeof(*state));
-	state->prefix = prefix;
-	state->prefix_length = state->prefix ? strlen(state->prefix) : 0;
-	state->lock_file = lock_file;
-	state->newfd = -1;
-	state->apply = 1;
-	state->line_termination = '\n';
-	state->p_value = 1;
-	state->p_context = UINT_MAX;
-	state->squelch_whitespace_errors = 5;
-	state->ws_error_action = warn_on_ws_error;
-	state->ws_ignore_action = ignore_ws_none;
-	state->linenr = 1;
-	strbuf_init(&state->root, 0);
-
-	git_apply_config();
-	if (apply_default_whitespace && parse_whitespace_option(state, apply_default_whitespace))
-		exit(1);
-	if (apply_default_ignorewhitespace && parse_ignorewhitespace_option(state, apply_default_ignorewhitespace))
-		exit(1);
-}
-
-static void clear_apply_state(struct apply_state *state)
-{
-	string_list_clear(&state->limit_by_name, 0);
-	string_list_clear(&state->symlink_changes, 0);
-	strbuf_release(&state->root);
-
-	/* &state->fn_table is cleared at the end of apply_patch() */
-}
-
 static void check_apply_state(struct apply_state *state, int force_apply)
 {
 	int is_not_gitdir = !startup_info->have_repository;
-- 
2.9.0.172.gfb57a78
Previous: Christian CouderNext: Christian Couder
Message 54 of 59 in “libify apply and use lib in am, part 2”
  1. 00/41 libify apply and use lib in am, part 2Christian Couder, Jun 27, 2016
  2. 03/41 builtin/apply: make apply_patch() return -1 or -128 instead of die()ingChristian Couder, Jun 27, 2016
  3. 06/41 builtin/apply: make parse_chunk() return a negative integer on errorChristian Couder, Jun 27, 2016
  4. 07/41 builtin/apply: make parse_single_patch() return -1 on errorChristian Couder, Jun 27, 2016
  5. 01/41 apply: make some names more specificChristian Couder, Jun 27, 2016
  6. 12/41 builtin/apply: make check_apply_state() return -1 instead of die()ingChristian Couder, Jun 27, 2016
  7. 19/41 builtin/apply: make build_fake_ancestor() return -1 on errorChristian Couder, Jun 27, 2016
  8. 14/41 builtin/apply: make apply_all_patches() return 128 or 1 on errorChristian Couder, Jun 27, 2016
  9. 16/41 builtin/apply: make gitdiff_*() return 1 at end of headerChristian Couder, Jun 27, 2016
  10. 28/41 builtin/apply: rename option parsing functionsChristian Couder, Jun 27, 2016
  11. 29/41 apply: rename and move opt constants to apply.hChristian Couder, Jun 27, 2016
  12. 23/41 builtin/apply: make create_file() return -1 on errorChristian Couder, Jun 27, 2016
  13. 24/41 builtin/apply: make write_out_one_result() return -1 on errorChristian Couder, Jun 27, 2016
  14. 21/41 builtin/apply: make add_conflicted_stages_file() return -1 on errorChristian Couder, Jun 27, 2016
  15. 20/41 builtin/apply: make remove_file() return -1 on errorChristian Couder, Jun 27, 2016
  16. 26/41 builtin/apply: make try_create_file() return -1 on errorChristian Couder, Jun 27, 2016
  17. 32/41 environment: add set_index_file()Christian Couder, Jun 27, 2016
  18. Junio C HamanoJul 26, 2016
  19. Duy NguyenJul 27, 2016
  20. Christian CouderJul 29, 2016
  21. Duy NguyenJul 29, 2016
  22. Christian CouderJul 29, 2016
  23. Duy NguyenJul 29, 2016
  24. Junio C HamanoJul 29, 2016
  25. 31/41 apply: make some parsing functions static againChristian Couder, Jun 27, 2016
  26. 38/41 usage: add get_error_routine() and get_warn_routine()Christian Couder, Jun 27, 2016
  27. 35/41 apply: make 'be_silent' incompatible with 'apply_verbosely'Christian Couder, Jun 27, 2016
  28. Junio C HamanoJul 26, 2016
  29. 39/41 apply: change error_routine when be_silent is setChristian Couder, Jun 27, 2016
  30. 33/41 write_or_die: use warning() instead of fprintf(stderr, ...)Christian Couder, Jun 27, 2016
  31. Junio C HamanoJun 28, 2016
  32. Christian CouderJun 30, 2016
  33. 40/41 builtin/am: use apply api in run_apply()Christian Couder, Jun 27, 2016
  34. Junio C HamanoJul 26, 2016
  35. 34/41 apply: add 'be_silent' variable to 'struct apply_state'Christian Couder, Jun 27, 2016
  36. Junio C HamanoJul 26, 2016
  37. 41/41 apply: use error_errno() where possibleChristian Couder, Jun 27, 2016
  38. 37/41 usage: add set_warn_routine()Christian Couder, Jun 27, 2016
  39. 36/41 apply: don't print on stdout when be_silent is setChristian Couder, Jun 27, 2016
  40. Junio C HamanoJul 26, 2016
  41. 22/41 builtin/apply: make add_index_file() return -1 on errorChristian Couder, Jun 27, 2016
  42. 13/41 builtin/apply: move check_apply_state() to apply.cChristian Couder, Jun 27, 2016
  43. 27/41 builtin/apply: make create_one_file() return -1 on errorChristian Couder, Jun 27, 2016
  44. 25/41 builtin/apply: make write_out_results() return -1 on errorChristian Couder, Jun 27, 2016
  45. 15/41 builtin/apply: make parse_traditional_patch() return -1 on errorChristian Couder, Jun 27, 2016
  46. 11/41 apply: make init_apply_state() return -1 instead of exit()ingChristian Couder, Jun 27, 2016
  47. 02/41 apply: move 'struct apply_state' to apply.hChristian Couder, Jun 27, 2016
  48. 04/41 builtin/apply: read_patch_file() return -1 instead of die()ingChristian Couder, Jun 27, 2016
  49. 08/41 builtin/apply: make parse_whitespace_option() return -1 instead of die()ingChristian Couder, Jun 27, 2016
  50. 09/41 builtin/apply: make parse_ignorewhitespace_option() return -1 instead of die()ingChristian Couder, Jun 27, 2016
  51. 05/41 builtin/apply: make find_header() return -128 instead of die()ingChristian Couder, Jun 27, 2016
  52. 18/41 builtin/apply: change die_on_unsafe_path() to check_unsafe_path()Christian Couder, Jun 27, 2016
  53. 17/41 builtin/apply: make gitdiff_*() return -1 on errorChristian Couder, Jun 27, 2016
  54. 10/41 builtin/apply: move init_apply_state() to apply.cChristian Couder, Jun 27, 2016
  55. Christian CouderJun 27, 2016
  56. Junio C HamanoJul 26, 2016
  57. Christian CouderJul 27, 2016
  58. Junio C HamanoJul 27, 2016
  59. Christian CouderJul 30, 2016

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.