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

[PATCH v3 40/49] builtin/apply: move 'ws_error_action' into 'struct apply_state'

From
Christian Couder <christian.couder@gmail.com>
Date
May 24, 2016, 08:11 UTC
Message-ID
<20160524081126.16973-41-chriscool@tuxfamily.org>
In-Reply-To
<20160524081126.16973-1-chriscool@tuxfamily.org>

To libify the apply functionality the 'ws_error_action' variable should not be static and global to the file. Let's move it into 'struct apply_state'.

Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
---
 builtin/apply.c | 61 +++++++++++++++++++++++++++++++--------------------------
 1 file changed, 33 insertions(+), 28 deletions(-)
diff --git a/builtin/apply.c b/builtin/apply.c
index 91b6283..61d809a 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -21,6 +21,13 @@
 #include "ll-merge.h"
 #include "rerere.h"
 
+enum ws_error_action {
+	nowarn_ws_error,
+	warn_on_ws_error,
+	die_on_ws_error,
+	correct_ws_error
+};
+
 struct apply_state {
 	const char *prefix;
 	int prefix_length;
@@ -61,6 +68,7 @@ struct apply_state {
 	int has_include;
 
 	/* These control whitespace errors */
+	enum ws_error_action ws_error_action;
 	const char *whitespace_option;
 	int whitespace_error;
 	int squelch_whitespace_errors;
@@ -74,12 +82,6 @@ static const char * const apply_usage[] = {
 	NULL
 };
 
-static enum ws_error_action {
-	nowarn_ws_error,
-	warn_on_ws_error,
-	die_on_ws_error,
-	correct_ws_error
-} ws_error_action = warn_on_ws_error;
 
 static enum ws_ignore {
 	ignore_ws_none,
@@ -90,28 +92,28 @@ static enum ws_ignore {
 static void parse_whitespace_option(struct apply_state *state, const char *option)
 {
 	if (!option) {
-		ws_error_action = warn_on_ws_error;
+		state->ws_error_action = warn_on_ws_error;
 		return;
 	}
 	if (!strcmp(option, "warn")) {
-		ws_error_action = warn_on_ws_error;
+		state->ws_error_action = warn_on_ws_error;
 		return;
 	}
 	if (!strcmp(option, "nowarn")) {
-		ws_error_action = nowarn_ws_error;
+		state->ws_error_action = nowarn_ws_error;
 		return;
 	}
 	if (!strcmp(option, "error")) {
-		ws_error_action = die_on_ws_error;
+		state->ws_error_action = die_on_ws_error;
 		return;
 	}
 	if (!strcmp(option, "error-all")) {
-		ws_error_action = die_on_ws_error;
+		state->ws_error_action = die_on_ws_error;
 		state->squelch_whitespace_errors = 0;
 		return;
 	}
 	if (!strcmp(option, "strip") || !strcmp(option, "fix")) {
-		ws_error_action = correct_ws_error;
+		state->ws_error_action = correct_ws_error;
 		return;
 	}
 	die(_("unrecognized whitespace option '%s'"), option);
@@ -135,7 +137,7 @@ static void parse_ignorewhitespace_option(const char *option)
 static void set_default_whitespace_mode(struct apply_state *state)
 {
 	if (!state->whitespace_option && !apply_default_whitespace)
-		ws_error_action = (state->apply ? warn_on_ws_error : nowarn_ws_error);
+		state->ws_error_action = (state->apply ? warn_on_ws_error : nowarn_ws_error);
 }
 
 /*
@@ -1670,12 +1672,12 @@ static int parse_fragment(struct apply_state *state,
 				leading++;
 			trailing++;
 			if (!state->apply_in_reverse &&
-			    ws_error_action == correct_ws_error)
+			    state->ws_error_action == correct_ws_error)
 				check_whitespace(state, line, len, patch->ws_rule);
 			break;
 		case '-':
 			if (state->apply_in_reverse &&
-			    ws_error_action != nowarn_ws_error)
+			    state->ws_error_action != nowarn_ws_error)
 				check_whitespace(state, line, len, patch->ws_rule);
 			deleted++;
 			oldlines--;
@@ -1683,7 +1685,7 @@ static int parse_fragment(struct apply_state *state,
 			break;
 		case '+':
 			if (!state->apply_in_reverse &&
-			    ws_error_action != nowarn_ws_error)
+			    state->ws_error_action != nowarn_ws_error)
 				check_whitespace(state, line, len, patch->ws_rule);
 			added++;
 			newlines--;
@@ -2396,7 +2398,8 @@ static int line_by_line_fuzzy_match(struct image *img,
 	return 1;
 }
 
-static int match_fragment(struct image *img,
+static int match_fragment(struct apply_state *state,
+			  struct image *img,
 			  struct image *preimage,
 			  struct image *postimage,
 			  unsigned long try,
@@ -2417,7 +2420,7 @@ static int match_fragment(struct image *img,
 		preimage_limit = preimage->nr;
 		if (match_end && (preimage->nr + try_lno != img->nr))
 			return 0;
-	} else if (ws_error_action == correct_ws_error &&
+	} else if (state->ws_error_action == correct_ws_error &&
 		   (ws_rule & WS_BLANK_AT_EOF)) {
 		/*
 		 * This hunk extends beyond the end of img, and we are
@@ -2489,7 +2492,7 @@ static int match_fragment(struct image *img,
 		return line_by_line_fuzzy_match(img, preimage, postimage,
 						try, try_lno, preimage_limit);
 
-	if (ws_error_action != correct_ws_error)
+	if (state->ws_error_action != correct_ws_error)
 		return 0;
 
 	/*
@@ -2601,7 +2604,8 @@ static int match_fragment(struct image *img,
 	return 0;
 }
 
-static int find_pos(struct image *img,
+static int find_pos(struct apply_state *state,
+		    struct image *img,
 		    struct image *preimage,
 		    struct image *postimage,
 		    int line,
@@ -2645,7 +2649,7 @@ static int find_pos(struct image *img,
 	try_lno = line;
 
 	for (i = 0; ; i++) {
-		if (match_fragment(img, preimage, postimage,
+		if (match_fragment(state, img, preimage, postimage,
 				   try, try_lno, ws_rule,
 				   match_beginning, match_end))
 			return try_lno;
@@ -2858,7 +2862,7 @@ static int apply_one_fragment(struct apply_state *state,
 			start = newlines.len;
 			if (first != '+' ||
 			    !state->whitespace_error ||
-			    ws_error_action != correct_ws_error) {
+			    state->ws_error_action != correct_ws_error) {
 				strbuf_add(&newlines, patch + 1, plen);
 			}
 			else {
@@ -2936,7 +2940,7 @@ static int apply_one_fragment(struct apply_state *state,
 
 	for (;;) {
 
-		applied_pos = find_pos(img, &preimage, &postimage, pos,
+		applied_pos = find_pos(state, img, &preimage, &postimage, pos,
 				       ws_rule, match_beginning, match_end);
 
 		if (applied_pos >= 0)
@@ -2972,10 +2976,10 @@ static int apply_one_fragment(struct apply_state *state,
 		if (new_blank_lines_at_end &&
 		    preimage.nr + applied_pos >= img->nr &&
 		    (ws_rule & WS_BLANK_AT_EOF) &&
-		    ws_error_action != nowarn_ws_error) {
+		    state->ws_error_action != nowarn_ws_error) {
 			record_ws_error(state, WS_BLANK_AT_EOF, "+", 1,
 					found_new_blank_lines_at_end);
-			if (ws_error_action == correct_ws_error) {
+			if (state->ws_error_action == correct_ws_error) {
 				while (new_blank_lines_at_end--)
 					remove_last_line(&postimage);
 			}
@@ -2986,7 +2990,7 @@ static int apply_one_fragment(struct apply_state *state,
 			 * apply_patch->check_patch_list->check_patch->
 			 * apply_data->apply_fragments->apply_one_fragment
 			 */
-			if (ws_error_action == die_on_ws_error)
+			if (state->ws_error_action == die_on_ws_error)
 				state->apply = 0;
 		}
 
@@ -4530,7 +4534,7 @@ static int apply_patch(struct apply_state *state,
 	if (!list && !skipped_patch)
 		die(_("unrecognized input"));
 
-	if (state->whitespace_error && (ws_error_action == die_on_ws_error))
+	if (state->whitespace_error && (state->ws_error_action == die_on_ws_error))
 		state->apply = 0;
 
 	state->update_index = state->check_index && state->apply;
@@ -4645,6 +4649,7 @@ static void init_apply_state(struct apply_state *state, const char *prefix)
 	state->p_value = 1;
 	state->p_context = UINT_MAX;
 	state->squelch_whitespace_errors = 5;
+	state->ws_error_action = warn_on_ws_error;
 	strbuf_init(&state->root, 0);
 
 	git_apply_config();
@@ -4801,7 +4806,7 @@ int cmd_apply(int argc, const char **argv, const char *prefix)
 				   squelched),
 				squelched);
 		}
-		if (ws_error_action == die_on_ws_error)
+		if (state.ws_error_action == die_on_ws_error)
 			die(Q_("%d line adds whitespace errors.",
 			       "%d lines add whitespace errors.",
 			       state.whitespace_error),
-- 
2.8.3.443.gaeee61e
Previous: Christian CouderNext: Christian Couder
Message 41 of 53 in “libify apply and use lib in am, part 1”
  1. 00/49 libify apply and use lib in am, part 1Christian Couder, May 24, 2016
  2. 01/49 builtin/apply: make gitdiff_verify_name() return voidChristian Couder, May 24, 2016
  3. 02/49 builtin/apply: avoid parameter shadowing 'p_value' globalChristian Couder, May 24, 2016
  4. 03/49 builtin/apply: avoid parameter shadowing 'linenr' globalChristian Couder, May 24, 2016
  5. 04/49 builtin/apply: avoid local variable shadowing 'len' parameterChristian Couder, May 24, 2016
  6. 05/49 builtin/apply: extract line_by_line_fuzzy_match() from match_fragment()Christian Couder, May 24, 2016
  7. 06/49 builtin/apply: move 'options' variable into cmd_apply()Christian Couder, May 24, 2016
  8. 07/49 builtin/apply: move 'read_stdin' global into cmd_apply()Christian Couder, May 24, 2016
  9. 08/49 builtin/apply: introduce 'struct apply_state' to start libifyingChristian Couder, May 24, 2016
  10. 09/49 builtin/apply: move 'state' init into init_apply_state()Christian Couder, May 24, 2016
  11. 10/49 builtin/apply: move 'unidiff_zero' global into 'struct apply_state'Christian Couder, May 24, 2016
  12. 11/49 builtin/apply: move 'check' global into 'struct apply_state'Christian Couder, May 24, 2016
  13. 12/49 builtin/apply: move 'check_index' global into 'struct apply_state'Christian Couder, May 24, 2016
  14. 13/49 builtin/apply: move 'apply_in_reverse' global into 'struct apply_state'Christian Couder, May 24, 2016
  15. 14/49 builtin/apply: move 'apply_with_reject' global into 'struct apply_state'Christian Couder, May 24, 2016
  16. 15/49 builtin/apply: move 'apply_verbosely' global into 'struct apply_state'Christian Couder, May 24, 2016
  17. 16/49 builtin/apply: move 'update_index' global into 'struct apply_state'Christian Couder, May 24, 2016
  18. 17/49 builtin/apply: move 'allow_overlap' global into 'struct apply_state'Christian Couder, May 24, 2016
  19. 18/49 builtin/apply: move 'cached' global into 'struct apply_state'Christian Couder, May 24, 2016
  20. 19/49 builtin/apply: move 'diffstat' global into 'struct apply_state'Christian Couder, May 24, 2016
  21. 20/49 builtin/apply: move 'numstat' global into 'struct apply_state'Christian Couder, May 24, 2016
  22. 21/49 builtin/apply: move 'summary' global into 'struct apply_state'Christian Couder, May 24, 2016
  23. 22/49 builtin/apply: move 'threeway' global into 'struct apply_state'Christian Couder, May 24, 2016
  24. 23/49 builtin/apply: move 'no_add' global into 'struct apply_state'Christian Couder, May 24, 2016
  25. 24/49 builtin/apply: move 'unsafe_paths' global into 'struct apply_state'Christian Couder, May 24, 2016
  26. 25/49 builtin/apply: move 'line_termination' global into 'struct apply_state'Christian Couder, May 24, 2016
  27. 26/49 builtin/apply: move 'fake_ancestor' global into 'struct apply_state'Christian Couder, May 24, 2016
  28. 27/49 builtin/apply: move 'p_context' global into 'struct apply_state'Christian Couder, May 24, 2016
  29. 28/49 builtin/apply: move 'apply' global into 'struct apply_state'Christian Couder, May 24, 2016
  30. 29/49 builtin/apply: move 'patch_input_file' global into 'struct apply_state'Christian Couder, May 24, 2016
  31. 30/49 builtin/apply: move 'limit_by_name' global into 'struct apply_state'Christian Couder, May 24, 2016
  32. 31/49 builtin/apply: move 'has_include' global into 'struct apply_state'Christian Couder, May 24, 2016
  33. 32/49 builtin/apply: move 'p_value' global into 'struct apply_state'Christian Couder, May 24, 2016
  34. 33/49 builtin/apply: move 'p_value_known' global into 'struct apply_state'Christian Couder, May 24, 2016
  35. 34/49 builtin/apply: move 'root' global into 'struct apply_state'Christian Couder, May 24, 2016
  36. 35/49 builtin/apply: move 'whitespace_error' global into 'struct apply_state'Christian Couder, May 24, 2016
  37. 36/49 builtin/apply: move 'whitespace_option' into 'struct apply_state'Christian Couder, May 24, 2016
  38. 37/49 builtin/apply: remove whitespace_option arg from set_default_whitespace_mode()Christian Couder, May 24, 2016
  39. 38/49 builtin/apply: move 'squelch_whitespace_errors' into 'struct apply_state'Christian Couder, May 24, 2016
  40. 39/49 builtin/apply: move 'applied_after_fixing_ws' into 'struct apply_state'Christian Couder, May 24, 2016
  41. 40/49 builtin/apply: move 'ws_error_action' into 'struct apply_state'Christian Couder, May 24, 2016
  42. 41/49 builtin/apply: move 'ws_ignore_action' into 'struct apply_state'Christian Couder, May 24, 2016
  43. 42/49 builtin/apply: move 'max_change' and 'max_len' into 'struct apply_state'Christian Couder, May 24, 2016
  44. 43/49 builtin/apply: move 'state_linenr' global into 'struct apply_state'Christian Couder, May 24, 2016
  45. 44/49 builtin/apply: move 'fn_table' global into 'struct apply_state'Christian Couder, May 24, 2016
  46. 45/49 builtin/apply: move 'symlink_changes' global into 'struct apply_state'Christian Couder, May 24, 2016
  47. 46/49 builtin/apply: move 'state' check into check_apply_state()Christian Couder, May 24, 2016
  48. 47/49 builtin/apply: move applying patches into apply_all_patches()Christian Couder, May 24, 2016
  49. 48/49 builtin/apply: move 'lock_file' global into 'struct apply_state'Christian Couder, May 24, 2016
  50. Junio C HamanoJun 1, 2016
  51. Christian CouderJun 3, 2016
  52. 49/49 builtin/apply: move 'newfd' global into 'struct apply_state'Christian Couder, May 24, 2016
  53. Christian CouderMay 24, 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.