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

[RFC/PATCH 48/48] builtin/apply: move applying patches into apply_all_patches()

From
Christian Couder <christian.couder@gmail.com>
Date
Mar 9, 2016, 17:49 UTC
Message-ID
<1457545756-20616-49-git-send-email-chriscool@tuxfamily.org>
In-Reply-To
<1457545756-20616-1-git-send-email-chriscool@tuxfamily.org>
Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
---
 builtin/apply.c | 128 ++++++++++++++++++++++++++++++--------------------------
 1 file changed, 69 insertions(+), 59 deletions(-)
diff --git a/builtin/apply.c b/builtin/apply.c
index 3f76469..9611b1a 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -4718,13 +4718,79 @@ static void check_apply_state(struct apply_state *state, int force_apply)
 		state->unsafe_paths = 0;
 }
 
-int cmd_apply(int argc, const char **argv, const char *prefix_)
+static int apply_all_patches(struct apply_state *state,
+			     int argc,
+			     const char **argv,
+			     int options)
 {
 	int i;
 	int errs = 0;
+	int read_stdin = 1;
+
+	for (i = 0; i < argc; i++) {
+		const char *arg = argv[i];
+		int fd;
+
+		if (!strcmp(arg, "-")) {
+			errs |= apply_patch(state, 0, "<stdin>", options);
+			read_stdin = 0;
+			continue;
+		} else if (0 < state->prefix_length)
+			arg = prefix_filename(state->prefix,
+					      state->prefix_length,
+					      arg);
+
+		fd = open(arg, O_RDONLY);
+		if (fd < 0)
+			die_errno(_("can't open patch '%s'"), arg);
+		read_stdin = 0;
+		set_default_whitespace_mode(state);
+		errs |= apply_patch(state, fd, arg, options);
+		close(fd);
+	}
+	set_default_whitespace_mode(state);
+	if (read_stdin)
+		errs |= apply_patch(state, 0, "<stdin>", options);
+
+	if (state->whitespace_error) {
+		if (state->squelch_whitespace_errors &&
+		    state->squelch_whitespace_errors < state->whitespace_error) {
+			int squelched =
+				state->whitespace_error - state->squelch_whitespace_errors;
+			warning(Q_("squelched %d whitespace error",
+				   "squelched %d whitespace errors",
+				   squelched),
+				squelched);
+		}
+		if (state->ws_error_action == die_on_ws_error)
+			die(Q_("%d line adds whitespace errors.",
+			       "%d lines add whitespace errors.",
+			       state->whitespace_error),
+			    state->whitespace_error);
+		if (state->applied_after_fixing_ws && state->apply)
+			warning("%d line%s applied after"
+				" fixing whitespace errors.",
+				state->applied_after_fixing_ws,
+				state->applied_after_fixing_ws == 1 ? "" : "s");
+		else if (state->whitespace_error)
+			warning(Q_("%d line adds whitespace errors.",
+				   "%d lines add whitespace errors.",
+				   state->whitespace_error),
+				state->whitespace_error);
+	}
+
+	if (state->update_index) {
+		if (write_locked_index(&the_index, &state->lock_file, COMMIT_LOCK))
+			die(_("Unable to write new index file"));
+	}
+
+	return !!errs;
+}
+
+int cmd_apply(int argc, const char **argv, const char *prefix_)
+{
 	int force_apply = 0;
 	int options = 0;
-	int read_stdin = 1;
 	struct apply_state state;
 
 	struct option builtin_apply_options[] = {
@@ -4803,61 +4869,5 @@ int cmd_apply(int argc, const char **argv, const char *prefix_)
 
 	check_apply_state(&state, force_apply);
 
-	for (i = 0; i < argc; i++) {
-		const char *arg = argv[i];
-		int fd;
-
-		if (!strcmp(arg, "-")) {
-			errs |= apply_patch(&state, 0, "<stdin>", options);
-			read_stdin = 0;
-			continue;
-		} else if (0 < state.prefix_length)
-			arg = prefix_filename(state.prefix,
-					      state.prefix_length,
-					      arg);
-
-		fd = open(arg, O_RDONLY);
-		if (fd < 0)
-			die_errno(_("can't open patch '%s'"), arg);
-		read_stdin = 0;
-		set_default_whitespace_mode(&state);
-		errs |= apply_patch(&state, fd, arg, options);
-		close(fd);
-	}
-	set_default_whitespace_mode(&state);
-	if (read_stdin)
-		errs |= apply_patch(&state, 0, "<stdin>", options);
-	if (state.whitespace_error) {
-		if (state.squelch_whitespace_errors &&
-		    state.squelch_whitespace_errors < state.whitespace_error) {
-			int squelched =
-				state.whitespace_error - state.squelch_whitespace_errors;
-			warning(Q_("squelched %d whitespace error",
-				   "squelched %d whitespace errors",
-				   squelched),
-				squelched);
-		}
-		if (state.ws_error_action == die_on_ws_error)
-			die(Q_("%d line adds whitespace errors.",
-			       "%d lines add whitespace errors.",
-			       state.whitespace_error),
-			    state.whitespace_error);
-		if (state.applied_after_fixing_ws && state.apply)
-			warning("%d line%s applied after"
-				" fixing whitespace errors.",
-				state.applied_after_fixing_ws,
-				state.applied_after_fixing_ws == 1 ? "" : "s");
-		else if (state.whitespace_error)
-			warning(Q_("%d line adds whitespace errors.",
-				   "%d lines add whitespace errors.",
-				   state.whitespace_error),
-				state.whitespace_error);
-	}
-
-	if (state.update_index) {
-		if (write_locked_index(&the_index, &state.lock_file, COMMIT_LOCK))
-			die(_("Unable to write new index file"));
-	}
-
-	return !!errs;
+	return apply_all_patches(&state, argc, argv, options);
 }
-- 
2.8.0.rc1.49.gca61272
Previous: Christian CouderNext: Junio C Hamano
Message 54 of 58 in “Libifying git apply”
  1. 00/48 Libifying git applyChristian Couder, Mar 9, 2016
  2. 01/48 builtin/apply: avoid parameter shadowing 'p_value' globalChristian Couder, Mar 9, 2016
  3. Junio C HamanoMar 9, 2016
  4. Duy NguyenMar 10, 2016
  5. Christian CouderMar 10, 2016
  6. 02/48 builtin/apply: avoid parameter shadowing 'linenr' globalChristian Couder, Mar 9, 2016
  7. 03/48 builtin/apply: avoid local variable shadowing 'len' parameterChristian Couder, Mar 9, 2016
  8. 04/48 builtin/apply: extract line_by_line_fuzzy_match() from match_fragment()Christian Couder, Mar 9, 2016
  9. Stefan BellerMar 9, 2016
  10. Christian CouderMar 10, 2016
  11. 05/48 builtin/apply: move 'options' variable into cmd_apply()Christian Couder, Mar 9, 2016
  12. 06/48 builtin/apply: introduce 'struct apply_state' to start libifyingChristian Couder, Mar 9, 2016
  13. 07/48 builtin/apply: move 'newfd' global into 'struct apply_state'Christian Couder, Mar 9, 2016
  14. 08/48 builtin/apply: move 'unidiff_zero' global into 'struct apply_state'Christian Couder, Mar 9, 2016
  15. 09/48 builtin/apply: move 'check' global into 'struct apply_state'Christian Couder, Mar 9, 2016
  16. 10/48 builtin/apply: move 'check_index' global into 'struct apply_state'Christian Couder, Mar 9, 2016
  17. 11/48 builtin/apply: move 'apply_in_reverse' global into 'struct apply_state'Christian Couder, Mar 9, 2016
  18. 12/48 builtin/apply: move 'apply_with_reject' global into 'struct apply_state'Christian Couder, Mar 9, 2016
  19. 13/48 builtin/apply: move 'apply_verbosely' global into 'struct apply_state'Christian Couder, Mar 9, 2016
  20. 14/48 builtin/apply: move 'update_index' global into 'struct apply_state'Christian Couder, Mar 9, 2016
  21. 15/48 builtin/apply: move 'allow_overlap' global into 'struct apply_state'Christian Couder, Mar 9, 2016
  22. 16/48 builtin/apply: move 'cached' global into 'struct apply_state'Christian Couder, Mar 9, 2016
  23. 17/48 builtin/apply: move 'diffstat' global into 'struct apply_state'Christian Couder, Mar 9, 2016
  24. 18/48 builtin/apply: move 'numstat' global into 'struct apply_state'Christian Couder, Mar 9, 2016
  25. 19/48 builtin/apply: move 'summary' global into 'struct apply_state'Christian Couder, Mar 9, 2016
  26. 20/48 builtin/apply: move 'threeway' global into 'struct apply_state'Christian Couder, Mar 9, 2016
  27. 21/48 builtin/apply: move 'no-add' global into 'struct apply_state'Christian Couder, Mar 9, 2016
  28. 22/48 builtin/apply: move 'unsafe_paths' global into 'struct apply_state'Christian Couder, Mar 9, 2016
  29. 23/48 builtin/apply: move 'line_termination' global into 'struct apply_state'Christian Couder, Mar 9, 2016
  30. 24/48 builtin/apply: move 'fake_ancestor' global into 'struct apply_state'Christian Couder, Mar 9, 2016
  31. 25/48 builtin/apply: move 'p_context' global into 'struct apply_state'Christian Couder, Mar 9, 2016
  32. 26/48 builtin/apply: move 'apply' global into 'struct apply_state'Christian Couder, Mar 9, 2016
  33. 27/48 builtin/apply: move 'read_stdin' global into cmd_apply()Christian Couder, Mar 9, 2016
  34. 28/48 builtin/apply: move 'lock_file' global into 'struct apply_state'Christian Couder, Mar 9, 2016
  35. 29/48 builtin/apply: move 'patch_input_file' global into 'struct apply_state'Christian Couder, Mar 9, 2016
  36. 30/48 builtin/apply: move 'limit_by_name' global into 'struct apply_state'Christian Couder, Mar 9, 2016
  37. 31/48 builtin/apply: move 'has_include' global into 'struct apply_state'Christian Couder, Mar 9, 2016
  38. 32/48 builtin/apply: move 'p_value' global into 'struct apply_state'Christian Couder, Mar 9, 2016
  39. 33/48 builtin/apply: move 'p_value_known' global into 'struct apply_state'Christian Couder, Mar 9, 2016
  40. 34/48 builtin/apply: move 'root' global into 'struct apply_state'Christian Couder, Mar 9, 2016
  41. 35/48 builtin/apply: move 'whitespace_error' global into 'struct apply_state'Christian Couder, Mar 9, 2016
  42. 36/48 builtin/apply: move 'whitespace_option' into 'struct apply_state'Christian Couder, Mar 9, 2016
  43. 37/48 builtin/apply: remove whitespace_option arg from set_default_whitespace_mode()Christian Couder, Mar 9, 2016
  44. 38/48 builtin/apply: move 'squelch_whitespace_errors' into 'struct apply_state'Christian Couder, Mar 9, 2016
  45. 39/48 builtin/apply: move 'applied_after_fixing_ws' into 'struct apply_state'Christian Couder, Mar 9, 2016
  46. 40/48 builtin/apply: move 'ws_error_action' into 'struct apply_state'Christian Couder, Mar 9, 2016
  47. 41/48 builtin/apply: move 'ws_ignore_action' into 'struct apply_state'Christian Couder, Mar 9, 2016
  48. 42/48 builtin/apply: move 'max_change' and 'max_len' into 'struct apply_state'Christian Couder, Mar 9, 2016
  49. 43/48 builtin/apply: move 'linenr' global into 'struct apply_state'Christian Couder, Mar 9, 2016
  50. 44/48 builtin/apply: move 'fn_table' global into 'struct apply_state'Christian Couder, Mar 9, 2016
  51. 45/48 builtin/apply: move 'symlink_changes' global into 'struct apply_state'Christian Couder, Mar 9, 2016
  52. 46/48 builtin/apply: move 'state' init into init_apply_state()Christian Couder, Mar 9, 2016
  53. 47/48 builtin/apply: move 'state' check into check_apply_state()Christian Couder, Mar 9, 2016
  54. 48/48 builtin/apply: move applying patches into apply_all_patches()Christian Couder, Mar 9, 2016
  55. Junio C HamanoMar 9, 2016
  56. Christian CouderMar 11, 2016
  57. Duy NguyenMar 10, 2016
  58. Christian CouderMar 11, 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.