builtin/apply: move 'fn_table' global into 'struct apply_state'
To libify the apply functionality the 'fn_table' variable should not be static and global to the file. Let's move it into 'struct apply_state'. As fn_table is cleared at the end of apply_patch(), it is not necessary to clear it in clear_apply_state(). Reviewed-by: Stefan Beller <sbeller@google.com> Signed-off-by: Christian Couder <chriscool@tuxfamily.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Christian Couder committed
May 24, 2016 at 10:11 UTC
71dac5cef57daacb35a56b4b54e1a30bc6417968
1 file changed
+25
-22
builtin/apply.c
+25
-22
@@ -84,6 +84,12 @@ struct apply_state {
84
int max_change;
85
int max_len;
86
87
+ /*
88
+ * Records filenames that have been touched, in order to handle
89
+ * the case where more than one patches touch the same file.
90
+ */
91
+ struct string_list fn_table;
92
+
93
/* These control whitespace errors */
94
enum ws_error_action ws_error_action;
95
enum ws_ignore ws_ignore_action;
@@ -271,13 +277,6 @@ struct image {
277
struct line *line;
278
};
279
274
-/*
275
- * Records filenames that have been touched, in order to handle
276
- * the case where more than one patches touch the same file.
277
- */
278
-
279
-static struct string_list fn_table;
280
-
280
static uint32_t hash_line(const char *cp, size_t len)
281
{
282
size_t i;
@@ -3207,14 +3206,14 @@ static int read_file_or_gitlink(const struct cache_entry *ce, struct strbuf *buf
3206
return read_blob_object(buf, ce->sha1, ce->ce_mode);
3207
}
3208
3210
-static struct patch *in_fn_table(const char *name)
3209
+static struct patch *in_fn_table(struct apply_state *state, const char *name)
3210
{
3211
struct string_list_item *item;
3212
3213
if (name == NULL)
3214
return NULL;
3215
3217
- item = string_list_lookup(&fn_table, name);
3216
+ item = string_list_lookup(&state->fn_table, name);
3217
if (item != NULL)
3218
return (struct patch *)item->util;
3219
@@ -3246,7 +3245,7 @@ static int was_deleted(struct patch *patch)
3245
return patch == PATH_WAS_DELETED;
3246
}
3247
3249
-static void add_to_fn_table(struct patch *patch)
3248
+static void add_to_fn_table(struct apply_state *state, struct patch *patch)
3249
{
3250
struct string_list_item *item;
3251
@@ -3256,7 +3255,7 @@ static void add_to_fn_table(struct patch *patch)
3255
* file creations and copies
3256
*/
3257
if (patch->new_name != NULL) {
3259
- item = string_list_insert(&fn_table, patch->new_name);
3258
+ item = string_list_insert(&state->fn_table, patch->new_name);
3259
item->util = patch;
3260
}
3261
@@ -3265,12 +3264,12 @@ static void add_to_fn_table(struct patch *patch)
3264
* later chunks shouldn't patch old names
3265
*/
3266
if ((patch->new_name == NULL) || (patch->is_rename)) {
3268
- item = string_list_insert(&fn_table, patch->old_name);
3267
+ item = string_list_insert(&state->fn_table, patch->old_name);
3268
item->util = PATH_WAS_DELETED;
3269
}
3270
}
3271
3273
-static void prepare_fn_table(struct patch *patch)
3272
+static void prepare_fn_table(struct apply_state *state, struct patch *patch)
3273
{
3274
/*
3275
* store information about incoming file deletion
@@ -3278,7 +3277,7 @@ static void prepare_fn_table(struct patch *patch)
3277
while (patch) {
3278
if ((patch->new_name == NULL) || (patch->is_rename)) {
3279
struct string_list_item *item;
3281
- item = string_list_insert(&fn_table, patch->old_name);
3280
+ item = string_list_insert(&state->fn_table, patch->old_name);
3281
item->util = PATH_TO_BE_DELETED;
3282
}
3283
patch = patch->next;
@@ -3299,7 +3298,9 @@ static int checkout_target(struct index_state *istate,
3298
return 0;
3299
}
3300
3302
-static struct patch *previous_patch(struct patch *patch, int *gone)
3301
+static struct patch *previous_patch(struct apply_state *state,
3302
+ struct patch *patch,
3303
+ int *gone)
3304
{
3305
struct patch *previous;
3306
@@ -3307,7 +3308,7 @@ static struct patch *previous_patch(struct patch *patch, int *gone)
3308
if (patch->is_copy || patch->is_rename)
3309
return NULL; /* "git" patches do not depend on the order */
3310
3310
- previous = in_fn_table(patch->old_name);
3311
+ previous = in_fn_table(state, patch->old_name);
3312
if (!previous)
3313
return NULL;
3314
@@ -3376,7 +3377,7 @@ static int load_preimage(struct apply_state *state,
3377
struct patch *previous;
3378
int status;
3379
3379
- previous = previous_patch(patch, &status);
3380
+ previous = previous_patch(state, patch, &status);
3381
if (status)
3382
return error(_("path %s has been renamed/deleted"),
3383
patch->old_name);
@@ -3572,7 +3573,7 @@ static int apply_data(struct apply_state *state, struct patch *patch,
3573
}
3574
patch->result = image.buf;
3575
patch->resultsize = image.len;
3575
- add_to_fn_table(patch);
3576
+ add_to_fn_table(state, patch);
3577
free(image.line_allocated);
3578
3579
if (0 < patch->is_delete && patch->resultsize)
@@ -3606,7 +3607,7 @@ static int check_preimage(struct apply_state *state,
3607
return 0;
3608
3609
assert(patch->is_new <= 0);
3609
- previous = previous_patch(patch, &status);
3610
+ previous = previous_patch(state, patch, &status);
3611
3612
if (status)
3613
return error(_("path %s has been renamed/deleted"), old_name);
@@ -3852,7 +3853,7 @@ static int check_patch(struct apply_state *state, struct patch *patch)
3853
* B and rename from A to B is handled the same way by asking
3854
* was_deleted().
3855
*/
3855
- if ((tpatch = in_fn_table(new_name)) &&
3856
+ if ((tpatch = in_fn_table(state, new_name)) &&
3857
(was_deleted(tpatch) || to_be_deleted(tpatch)))
3858
ok_if_exists = 1;
3859
else
@@ -3930,7 +3931,7 @@ static int check_patch_list(struct apply_state *state, struct patch *patch)
3931
int err = 0;
3932
3933
prepare_symlink_changes(patch);
3933
- prepare_fn_table(patch);
3934
+ prepare_fn_table(state, patch);
3935
while (patch) {
3936
if (state->apply_verbosely)
3937
say_patch_name(stderr,
@@ -4574,7 +4575,7 @@ static int apply_patch(struct apply_state *state,
4575
4576
free_patch_list(list);
4577
strbuf_release(&buf);
4577
- string_list_clear(&fn_table, 0);
4578
+ string_list_clear(&state->fn_table, 0);
4579
return 0;
4580
}
4581
@@ -4668,6 +4669,8 @@ static void clear_apply_state(struct apply_state *state)
4669
{
4670
string_list_clear(&state->limit_by_name, 0);
4671
strbuf_release(&state->root);
4672
+
4673
+ /* &state->fn_table is cleared at the end of apply_patch() */
4674
}
4675
4676
int cmd_apply(int argc, const char **argv, const char *prefix)