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

[PATCH 10/83] builtin/apply: move 'check_index' global into 'struct apply_state'

From
Christian Couder <christian.couder@gmail.com>
Date
Apr 24, 2016, 13:33 UTC
Message-ID
<1461504863-15946-11-git-send-email-chriscool@tuxfamily.org>
In-Reply-To
<1461504863-15946-1-git-send-email-chriscool@tuxfamily.org>
Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
---
 builtin/apply.c | 69 +++++++++++++++++++++++++++++++++------------------------
 1 file changed, 40 insertions(+), 29 deletions(-)
diff --git a/builtin/apply.c b/builtin/apply.c
index 6c628f6..3f8671c 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -30,6 +30,10 @@ struct apply_state {
 	 *    files that are being modified, but doesn't apply the patch
 	 */
 	int check;
+
+	/* --index updates the cache as well. */
+	int check_index;
+
 	int unidiff_zero;
 };
 
@@ -37,14 +41,12 @@ struct apply_state {
  *  --stat does just a diffstat, and doesn't actually apply
  *  --numstat does numeric diffstat, and doesn't actually apply
  *  --index-info shows the old and new index info for paths if available.
- *  --index updates the cache as well.
  *  --cached updates only the cache without ever touching the working tree.
  */
 static int newfd = -1;
 
 static int state_p_value = 1;
 static int p_value_known;
-static int check_index;
 static int update_index;
 static int cached;
 static int diffstat;
@@ -3248,13 +3250,14 @@ static int verify_index_match(const struct cache_entry *ce, struct stat *st)
 
 #define SUBMODULE_PATCH_WITHOUT_INDEX 1
 
-static int load_patch_target(struct strbuf *buf,
+static int load_patch_target(struct apply_state *state,
+			     struct strbuf *buf,
 			     const struct cache_entry *ce,
 			     struct stat *st,
 			     const char *name,
 			     unsigned expected_mode)
 {
-	if (cached || check_index) {
+	if (cached || state->check_index) {
 		if (read_file_or_gitlink(ce, buf))
 			return error(_("read of %s failed"), name);
 	} else if (name) {
@@ -3280,7 +3283,8 @@ static int load_patch_target(struct strbuf *buf,
  * applying a non-git patch that incrementally updates the tree,
  * we read from the result of a previous diff.
  */
-static int load_preimage(struct image *image,
+static int load_preimage(struct apply_state *state,
+			 struct image *image,
 			 struct patch *patch, struct stat *st,
 			 const struct cache_entry *ce)
 {
@@ -3298,7 +3302,7 @@ static int load_preimage(struct image *image,
 		/* We have a patched copy in memory; use that. */
 		strbuf_add(&buf, previous->result, previous->resultsize);
 	} else {
-		status = load_patch_target(&buf, ce, st,
+		status = load_patch_target(state, &buf, ce, st,
 					   patch->old_name, patch->old_mode);
 		if (status < 0)
 			return status;
@@ -3357,7 +3361,9 @@ static int three_way_merge(struct image *image,
  * the current contents of the new_name.  In no cases other than that
  * this function will be called.
  */
-static int load_current(struct image *image, struct patch *patch)
+static int load_current(struct apply_state *state,
+			struct image *image,
+			struct patch *patch)
 {
 	struct strbuf buf = STRBUF_INIT;
 	int status, pos;
@@ -3384,7 +3390,7 @@ static int load_current(struct image *image, struct patch *patch)
 	if (verify_index_match(ce, &st))
 		return error(_("%s: does not match index"), name);
 
-	status = load_patch_target(&buf, ce, &st, name, mode);
+	status = load_patch_target(state, &buf, ce, &st, name, mode);
 	if (status < 0)
 		return status;
 	else if (status)
@@ -3434,11 +3440,11 @@ static int try_threeway(struct apply_state *state,
 
 	/* our_sha1[] is ours */
 	if (patch->is_new) {
-		if (load_current(&tmp_image, patch))
+		if (load_current(state, &tmp_image, patch))
 			return error("cannot read the current contents of '%s'",
 				     patch->new_name);
 	} else {
-		if (load_preimage(&tmp_image, patch, st, ce))
+		if (load_preimage(state, &tmp_image, patch, st, ce))
 			return error("cannot read the current contents of '%s'",
 				     patch->old_name);
 	}
@@ -3473,7 +3479,7 @@ static int apply_data(struct apply_state *state, struct patch *patch,
 {
 	struct image image;
 
-	if (load_preimage(&image, patch, st, ce) < 0)
+	if (load_preimage(state, &image, patch, st, ce) < 0)
 		return -1;
 
 	if (patch->direct_to_threeway ||
@@ -3504,7 +3510,10 @@ static int apply_data(struct apply_state *state, struct patch *patch,
  * check_patch() separately makes sure (and errors out otherwise) that
  * the path the patch creates does not exist in the current tree.
  */
-static int check_preimage(struct patch *patch, struct cache_entry **ce, struct stat *st)
+static int check_preimage(struct apply_state *state,
+			  struct patch *patch,
+			  struct cache_entry **ce,
+			  struct stat *st)
 {
 	const char *old_name = patch->old_name;
 	struct patch *previous = NULL;
@@ -3527,7 +3536,7 @@ static int check_preimage(struct patch *patch, struct cache_entry **ce, struct s
 			return error(_("%s: %s"), old_name, strerror(errno));
 	}
 
-	if (check_index && !previous) {
+	if (state->check_index && !previous) {
 		int pos = cache_name_pos(old_name, strlen(old_name));
 		if (pos < 0) {
 			if (patch->is_new < 0)
@@ -3577,11 +3586,13 @@ static int check_preimage(struct patch *patch, struct cache_entry **ce, struct s
 #define EXISTS_IN_INDEX 1
 #define EXISTS_IN_WORKTREE 2
 
-static int check_to_create(const char *new_name, int ok_if_exists)
+static int check_to_create(struct apply_state *state,
+			   const char *new_name,
+			   int ok_if_exists)
 {
 	struct stat nst;
 
-	if (check_index &&
+	if (state->check_index &&
 	    cache_name_pos(new_name, strlen(new_name)) >= 0 &&
 	    !ok_if_exists)
 		return EXISTS_IN_INDEX;
@@ -3657,7 +3668,7 @@ static void prepare_symlink_changes(struct patch *patch)
 	}
 }
 
-static int path_is_beyond_symlink_1(struct strbuf *name)
+static int path_is_beyond_symlink_1(struct apply_state *state, struct strbuf *name)
 {
 	do {
 		unsigned int change;
@@ -3678,7 +3689,7 @@ static int path_is_beyond_symlink_1(struct strbuf *name)
 			continue;
 
 		/* otherwise, check the preimage */
-		if (check_index) {
+		if (state->check_index) {
 			struct cache_entry *ce;
 
 			ce = cache_file_exists(name->buf, name->len, ignore_case);
@@ -3693,14 +3704,14 @@ static int path_is_beyond_symlink_1(struct strbuf *name)
 	return 0;
 }
 
-static int path_is_beyond_symlink(const char *name_)
+static int path_is_beyond_symlink(struct apply_state *state, const char *name_)
 {
 	int ret;
 	struct strbuf name = STRBUF_INIT;
 
 	assert(*name_ != '\0');
 	strbuf_addstr(&name, name_);
-	ret = path_is_beyond_symlink_1(&name);
+	ret = path_is_beyond_symlink_1(state, &name);
 	strbuf_release(&name);
 
 	return ret;
@@ -3740,7 +3751,7 @@ static int check_patch(struct apply_state *state, struct patch *patch)
 
 	patch->rejected = 1; /* we will drop this after we succeed */
 
-	status = check_preimage(patch, &ce, &st);
+	status = check_preimage(state, patch, &ce, &st);
 	if (status)
 		return status;
 	old_name = patch->old_name;
@@ -3767,7 +3778,7 @@ static int check_patch(struct apply_state *state, struct patch *patch)
 
 	if (new_name &&
 	    ((0 < patch->is_new) || patch->is_rename || patch->is_copy)) {
-		int err = check_to_create(new_name, ok_if_exists);
+		int err = check_to_create(state, new_name, ok_if_exists);
 
 		if (err && threeway) {
 			patch->direct_to_threeway = 1;
@@ -3822,7 +3833,7 @@ static int check_patch(struct apply_state *state, struct patch *patch)
 	 * is not deposited to a path that is beyond a symbolic link
 	 * here.
 	 */
-	if (!patch->is_delete && path_is_beyond_symlink(patch->new_name))
+	if (!patch->is_delete && path_is_beyond_symlink(state, patch->new_name))
 		return error(_("affected file '%s' is beyond a symbolic link"),
 			     patch->new_name);
 
@@ -4434,11 +4445,11 @@ static int apply_patch(struct apply_state *state,
 	if (whitespace_error && (ws_error_action == die_on_ws_error))
 		apply = 0;
 
-	update_index = check_index && apply;
+	update_index = state->check_index && apply;
 	if (update_index && newfd < 0)
 		newfd = hold_locked_index(&lock_file, 1);
 
-	if (check_index) {
+	if (state->check_index) {
 		if (read_cache() < 0)
 			die(_("unable to read index file"));
 	}
@@ -4565,7 +4576,7 @@ int cmd_apply(int argc, const char **argv, const char *prefix_)
 			N_("instead of applying the patch, output a summary for the input")),
 		OPT_BOOL(0, "check", &state.check,
 			N_("instead of applying the patch, see if the patch is applicable")),
-		OPT_BOOL(0, "index", &check_index,
+		OPT_BOOL(0, "index", &state.check_index,
 			N_("make sure the patch is applicable to the current index")),
 		OPT_BOOL(0, "cached", &cached,
 			N_("apply a patch without touching the working tree")),
@@ -4632,20 +4643,20 @@ int cmd_apply(int argc, const char **argv, const char *prefix_)
 	if (threeway) {
 		if (is_not_gitdir)
 			die(_("--3way outside a repository"));
-		check_index = 1;
+		state.check_index = 1;
 	}
 	if (apply_with_reject)
 		apply = apply_verbosely = 1;
 	if (!force_apply && (diffstat || numstat || summary || state.check || fake_ancestor))
 		apply = 0;
-	if (check_index && is_not_gitdir)
+	if (state.check_index && is_not_gitdir)
 		die(_("--index outside a repository"));
 	if (cached) {
 		if (is_not_gitdir)
 			die(_("--cached outside a repository"));
-		check_index = 1;
+		state.check_index = 1;
 	}
-	if (check_index)
+	if (state.check_index)
 		unsafe_paths = 0;
 
 	for (i = 0; i < argc; i++) {
-- 
2.8.1.300.g5fed0c0
Previous: Stefan BellerNext: Junio C Hamano
Message 20 of 156 in “libify apply and use lib in am”
  1. 00/83 libify apply and use lib in amChristian Couder, Apr 24, 2016
  2. 01/83 builtin/apply: make gitdiff_verify_name() return voidChristian Couder, Apr 24, 2016
  3. 02/83 builtin/apply: avoid parameter shadowing 'p_value' globalChristian Couder, Apr 24, 2016
  4. 03/83 builtin/apply: avoid parameter shadowing 'linenr' globalChristian Couder, Apr 24, 2016
  5. Junio C HamanoApr 27, 2016
  6. Christian CouderApr 27, 2016
  7. 04/83 builtin/apply: avoid local variable shadowing 'len' parameterChristian Couder, Apr 24, 2016
  8. 05/83 builtin/apply: extract line_by_line_fuzzy_match() from match_fragment()Christian Couder, Apr 24, 2016
  9. Stefan BellerApr 25, 2016
  10. Christian CouderApr 26, 2016
  11. Junio C HamanoApr 26, 2016
  12. Christian CouderApr 27, 2016
  13. 06/83 builtin/apply: move 'options' variable into cmd_apply()Christian Couder, Apr 24, 2016
  14. 07/83 builtin/apply: introduce 'struct apply_state' to start libifyingChristian Couder, Apr 24, 2016
  15. 08/83 builtin/apply: move 'unidiff_zero' global into 'struct apply_state'Christian Couder, Apr 24, 2016
  16. 09/83 builtin/apply: move 'check' global into 'struct apply_state'Christian Couder, Apr 24, 2016
  17. Stefan BellerApr 25, 2016
  18. Christian CouderApr 26, 2016
  19. Stefan BellerApr 26, 2016
  20. 10/83 builtin/apply: move 'check_index' global into 'struct apply_state'Christian Couder, Apr 24, 2016
  21. Junio C HamanoApr 26, 2016
  22. 11/83 builtin/apply: move 'apply_in_reverse' global into 'struct apply_state'Christian Couder, Apr 24, 2016
  23. 12/83 builtin/apply: move 'apply_with_reject' global into 'struct apply_state'Christian Couder, Apr 24, 2016
  24. 13/83 builtin/apply: move 'apply_verbosely' global into 'struct apply_state'Christian Couder, Apr 24, 2016
  25. 14/83 builtin/apply: move 'update_index' global into 'struct apply_state'Christian Couder, Apr 24, 2016
  26. 15/83 builtin/apply: move 'allow_overlap' global into 'struct apply_state'Christian Couder, Apr 24, 2016
  27. 16/83 builtin/apply: move 'cached' global into 'struct apply_state'Christian Couder, Apr 24, 2016
  28. 17/83 builtin/apply: move 'diffstat' global into 'struct apply_state'Christian Couder, Apr 24, 2016
  29. 18/83 builtin/apply: move 'numstat' global into 'struct apply_state'Christian Couder, Apr 24, 2016
  30. Stefan BellerApr 25, 2016
  31. Christian CouderApr 26, 2016
  32. 19/83 builtin/apply: move 'summary' global into 'struct apply_state'Christian Couder, Apr 24, 2016
  33. 20/83 builtin/apply: move 'threeway' global into 'struct apply_state'Christian Couder, Apr 24, 2016
  34. 21/83 builtin/apply: move 'no-add' global into 'struct apply_state'Christian Couder, Apr 24, 2016
  35. 22/83 builtin/apply: move 'unsafe_paths' global into 'struct apply_state'Christian Couder, Apr 24, 2016
  36. Junio C HamanoApr 26, 2016
  37. Christian CouderApr 27, 2016
  38. Junio C HamanoApr 27, 2016
  39. 23/83 builtin/apply: move 'line_termination' global into 'struct apply_state'Christian Couder, Apr 24, 2016
  40. 24/83 builtin/apply: move 'fake_ancestor' global into 'struct apply_state'Christian Couder, Apr 24, 2016
  41. 25/83 builtin/apply: move 'p_context' global into 'struct apply_state'Christian Couder, Apr 24, 2016
  42. 26/83 builtin/apply: move 'apply' global into 'struct apply_state'Christian Couder, Apr 24, 2016
  43. 27/83 builtin/apply: move 'read_stdin' global into cmd_apply()Christian Couder, Apr 24, 2016
  44. Junio C HamanoApr 26, 2016
  45. Christian CouderApr 27, 2016
  46. 28/83 builtin/apply: move 'patch_input_file' global into 'struct apply_state'Christian Couder, Apr 24, 2016
  47. 29/83 builtin/apply: move 'limit_by_name' global into 'struct apply_state'Christian Couder, Apr 24, 2016
  48. 30/83 builtin/apply: move 'has_include' global into 'struct apply_state'Christian Couder, Apr 24, 2016
  49. 31/83 builtin/apply: move 'p_value' global into 'struct apply_state'Christian Couder, Apr 24, 2016
  50. 32/83 builtin/apply: move 'p_value_known' global into 'struct apply_state'Christian Couder, Apr 24, 2016
  51. 33/83 builtin/apply: move 'root' global into 'struct apply_state'Christian Couder, Apr 24, 2016
  52. Stefan BellerApr 25, 2016
  53. Stefan BellerApr 25, 2016
  54. Christian CouderApr 26, 2016
  55. 34/83 builtin/apply: move 'whitespace_error' global into 'struct apply_state'Christian Couder, Apr 24, 2016
  56. 35/83 builtin/apply: move 'whitespace_option' into 'struct apply_state'Christian Couder, Apr 24, 2016
  57. 36/83 builtin/apply: remove whitespace_option arg from set_default_whitespace_mode()Christian Couder, Apr 24, 2016
  58. 37/83 builtin/apply: move 'squelch_whitespace_errors' into 'struct apply_state'Christian Couder, Apr 24, 2016
  59. 38/83 builtin/apply: move 'applied_after_fixing_ws' into 'struct apply_state'Christian Couder, Apr 24, 2016
  60. 39/83 builtin/apply: move 'ws_error_action' into 'struct apply_state'Christian Couder, Apr 24, 2016
  61. Junio C HamanoApr 26, 2016
  62. Junio C HamanoApr 26, 2016
  63. Christian CouderApr 27, 2016
  64. 40/83 builtin/apply: move 'ws_ignore_action' into 'struct apply_state'Christian Couder, Apr 24, 2016
  65. 41/83 builtin/apply: move 'max_change' and 'max_len' into 'struct apply_state'Christian Couder, Apr 24, 2016
  66. 42/83 builtin/apply: move 'linenr' global into 'struct apply_state'Christian Couder, Apr 24, 2016
  67. 43/83 builtin/apply: move 'fn_table' global into 'struct apply_state'Christian Couder, Apr 24, 2016
  68. 44/83 builtin/apply: move 'symlink_changes' global into 'struct apply_state'Christian Couder, Apr 24, 2016
  69. 45/83 builtin/apply: move 'state' init into init_apply_state()Christian Couder, Apr 24, 2016
  70. Eric SunshineApr 25, 2016
  71. Christian CouderApr 27, 2016
  72. 46/83 builtin/apply: move 'state' check into check_apply_state()Christian Couder, Apr 24, 2016
  73. 47/83 builtin/apply: move applying patches into apply_all_patches()Christian Couder, Apr 24, 2016
  74. Stefan BellerApr 25, 2016
  75. Christian CouderApr 27, 2016
  76. 48/83 builtin/apply: rename 'prefix_' parameter to 'prefix'Christian Couder, Apr 24, 2016
  77. 49/83 builtin/apply: move 'lock_file' global into 'struct apply_state'Christian Couder, Apr 24, 2016
  78. Eric SunshineApr 25, 2016
  79. Junio C HamanoApr 25, 2016
  80. Christian CouderApr 28, 2016
  81. Junio C HamanoApr 28, 2016
  82. Christian CouderApr 30, 2016
  83. 50/83 builtin/apply: get rid of the 'newfd' globalChristian Couder, Apr 24, 2016
  84. 51/83 builtin/apply: make apply_patch() return -1 instead of die()ingChristian Couder, Apr 24, 2016
  85. Eric SunshineApr 26, 2016
  86. Christian CouderApr 30, 2016
  87. 52/83 builtin/apply: read_patch_file() return -1 instead of die()ingChristian Couder, Apr 24, 2016
  88. 53/83 builtin/apply: make find_header() return -1 instead of die()ingChristian Couder, Apr 24, 2016
  89. Duy NguyenApr 25, 2016
  90. Eric SunshineApr 27, 2016
  91. Christian CouderMay 1, 2016
  92. Eric SunshineApr 27, 2016
  93. Christian CouderMay 1, 2016
  94. 54/83 builtin/apply: make parse_chunk() return a negative integer on errorChristian Couder, Apr 24, 2016
  95. Eric SunshineMay 1, 2016
  96. Christian CouderMay 1, 2016
  97. 55/83 builtin/apply: make parse_single_patch() return -1 on errorChristian Couder, Apr 24, 2016
  98. Eric SunshineMay 1, 2016
  99. 56/83 apply: move 'struct apply_state' to apply.hChristian Couder, Apr 24, 2016
  100. 57/83 builtin/apply: libify parse_whitespace_option()Christian Couder, Apr 24, 2016
  101. 58/83 builtin/apply: libify parse_ignorewhitespace_option()Christian Couder, Apr 24, 2016
  102. 59/83 builtin/apply: move init_apply_state() to apply.cChristian Couder, Apr 24, 2016
  103. Eric SunshineMay 1, 2016
  104. Christian CouderMay 1, 2016
  105. 60/83 apply: libify init_apply_state()Christian Couder, Apr 24, 2016
  106. 61/83 builtin/apply: libify check_apply_state()Christian Couder, Apr 24, 2016
  107. Duy NguyenApr 25, 2016
  108. Christian CouderMay 1, 2016
  109. 62/83 builtin/apply: move check_apply_state() to apply.cChristian Couder, Apr 24, 2016
  110. 63/83 builtin/apply: make apply_all_patches() return -1 on errorChristian Couder, Apr 24, 2016
  111. Duy NguyenApr 25, 2016
  112. Christian CouderMay 1, 2016
  113. Eric SunshineMay 1, 2016
  114. Johannes SchindelinMay 2, 2016
  115. Christian CouderMay 3, 2016
  116. 64/83 builtin/apply: make parse_traditional_patch() return -1 on errorChristian Couder, Apr 24, 2016
  117. 65/83 builtin/apply: make gitdiff_verify_name() return -1 on errorChristian Couder, Apr 24, 2016
  118. Duy NguyenApr 25, 2016
  119. Christian CouderMay 1, 2016
  120. 66/83 builtin/apply: change die_on_unsafe_path() to check_unsafe_path()Christian Couder, Apr 24, 2016
  121. 67/83 builtin/apply: make build_fake_ancestor() return -1 on errorChristian Couder, Apr 24, 2016
  122. Eric SunshineMay 2, 2016
  123. Christian CouderMay 3, 2016
  124. 68/83 builtin/apply: make remove_file() return -1 on errorChristian Couder, Apr 24, 2016
  125. 69/83 builtin/apply: make add_conflicted_stages_file() return -1 on errorChristian Couder, Apr 24, 2016
  126. Eric SunshineMay 2, 2016
  127. Christian CouderMay 3, 2016
  128. 70/83 builtin/apply: make add_index_file() return -1 on errorChristian Couder, Apr 24, 2016
  129. 71/83 builtin/apply: make create_file() return -1 on errorChristian Couder, Apr 24, 2016
  130. 72/83 builtin/apply: make write_out_one_result() return -1 on errorChristian Couder, Apr 24, 2016
  131. 73/83 builtin/apply: make write_out_results() return -1 on errorChristian Couder, Apr 24, 2016
  132. Eric SunshineMay 2, 2016
  133. Christian CouderMay 3, 2016
  134. 74/83 builtin/apply: make try_create_file() return -1 on errorChristian Couder, Apr 24, 2016
  135. Eric SunshineMay 2, 2016
  136. Christian CouderMay 3, 2016
  137. 75/83 builtin/apply: make create_one_file() return -1 on errorChristian Couder, Apr 24, 2016
  138. 76/83 builtin/apply: rename option parsing functionsChristian Couder, Apr 24, 2016
  139. 77/83 apply: rename and move opt constants to apply.hChristian Couder, Apr 24, 2016
  140. Ramsay JonesApr 24, 2016
  141. Christian CouderApr 24, 2016
  142. Christian CouderApr 24, 2016
  143. Ramsay JonesApr 24, 2016
  144. Duy NguyenApr 25, 2016
  145. Christian CouderApr 25, 2016
  146. Johannes SchindelinApr 25, 2016
  147. Duy NguyenApr 25, 2016
  148. Christian CouderApr 25, 2016
  149. Duy NguyenApr 25, 2016
  150. Duy NguyenApr 25, 2016
  151. Christian CouderMay 4, 2016
  152. Duy NguyenMay 4, 2016
  153. Christian CouderMay 5, 2016
  154. Christian CouderMay 6, 2016
  155. Junio C HamanoMay 6, 2016
  156. Christian CouderMay 8, 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.