Skip to content

Commit 71dac5c

Browse files
chriscoolgitster
authored andcommitted
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>
1 parent d7263d0 commit 71dac5c

File tree

1 file changed

+25
-22
lines changed

1 file changed

+25
-22
lines changed

builtin/apply.c

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,12 @@ struct apply_state {
8484
int max_change;
8585
int max_len;
8686

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+
8793
/* These control whitespace errors */
8894
enum ws_error_action ws_error_action;
8995
enum ws_ignore ws_ignore_action;
@@ -271,13 +277,6 @@ struct image {
271277
struct line *line;
272278
};
273279

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-
281280
static uint32_t hash_line(const char *cp, size_t len)
282281
{
283282
size_t i;
@@ -3207,14 +3206,14 @@ static int read_file_or_gitlink(const struct cache_entry *ce, struct strbuf *buf
32073206
return read_blob_object(buf, ce->sha1, ce->ce_mode);
32083207
}
32093208

3210-
static struct patch *in_fn_table(const char *name)
3209+
static struct patch *in_fn_table(struct apply_state *state, const char *name)
32113210
{
32123211
struct string_list_item *item;
32133212

32143213
if (name == NULL)
32153214
return NULL;
32163215

3217-
item = string_list_lookup(&fn_table, name);
3216+
item = string_list_lookup(&state->fn_table, name);
32183217
if (item != NULL)
32193218
return (struct patch *)item->util;
32203219

@@ -3246,7 +3245,7 @@ static int was_deleted(struct patch *patch)
32463245
return patch == PATH_WAS_DELETED;
32473246
}
32483247

3249-
static void add_to_fn_table(struct patch *patch)
3248+
static void add_to_fn_table(struct apply_state *state, struct patch *patch)
32503249
{
32513250
struct string_list_item *item;
32523251

@@ -3256,7 +3255,7 @@ static void add_to_fn_table(struct patch *patch)
32563255
* file creations and copies
32573256
*/
32583257
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);
32603259
item->util = patch;
32613260
}
32623261

@@ -3265,20 +3264,20 @@ static void add_to_fn_table(struct patch *patch)
32653264
* later chunks shouldn't patch old names
32663265
*/
32673266
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);
32693268
item->util = PATH_WAS_DELETED;
32703269
}
32713270
}
32723271

3273-
static void prepare_fn_table(struct patch *patch)
3272+
static void prepare_fn_table(struct apply_state *state, struct patch *patch)
32743273
{
32753274
/*
32763275
* store information about incoming file deletion
32773276
*/
32783277
while (patch) {
32793278
if ((patch->new_name == NULL) || (patch->is_rename)) {
32803279
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);
32823281
item->util = PATH_TO_BE_DELETED;
32833282
}
32843283
patch = patch->next;
@@ -3299,15 +3298,17 @@ static int checkout_target(struct index_state *istate,
32993298
return 0;
33003299
}
33013300

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)
33033304
{
33043305
struct patch *previous;
33053306

33063307
*gone = 0;
33073308
if (patch->is_copy || patch->is_rename)
33083309
return NULL; /* "git" patches do not depend on the order */
33093310

3310-
previous = in_fn_table(patch->old_name);
3311+
previous = in_fn_table(state, patch->old_name);
33113312
if (!previous)
33123313
return NULL;
33133314

@@ -3376,7 +3377,7 @@ static int load_preimage(struct apply_state *state,
33763377
struct patch *previous;
33773378
int status;
33783379

3379-
previous = previous_patch(patch, &status);
3380+
previous = previous_patch(state, patch, &status);
33803381
if (status)
33813382
return error(_("path %s has been renamed/deleted"),
33823383
patch->old_name);
@@ -3572,7 +3573,7 @@ static int apply_data(struct apply_state *state, struct patch *patch,
35723573
}
35733574
patch->result = image.buf;
35743575
patch->resultsize = image.len;
3575-
add_to_fn_table(patch);
3576+
add_to_fn_table(state, patch);
35763577
free(image.line_allocated);
35773578

35783579
if (0 < patch->is_delete && patch->resultsize)
@@ -3606,7 +3607,7 @@ static int check_preimage(struct apply_state *state,
36063607
return 0;
36073608

36083609
assert(patch->is_new <= 0);
3609-
previous = previous_patch(patch, &status);
3610+
previous = previous_patch(state, patch, &status);
36103611

36113612
if (status)
36123613
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)
38523853
* B and rename from A to B is handled the same way by asking
38533854
* was_deleted().
38543855
*/
3855-
if ((tpatch = in_fn_table(new_name)) &&
3856+
if ((tpatch = in_fn_table(state, new_name)) &&
38563857
(was_deleted(tpatch) || to_be_deleted(tpatch)))
38573858
ok_if_exists = 1;
38583859
else
@@ -3930,7 +3931,7 @@ static int check_patch_list(struct apply_state *state, struct patch *patch)
39303931
int err = 0;
39313932

39323933
prepare_symlink_changes(patch);
3933-
prepare_fn_table(patch);
3934+
prepare_fn_table(state, patch);
39343935
while (patch) {
39353936
if (state->apply_verbosely)
39363937
say_patch_name(stderr,
@@ -4574,7 +4575,7 @@ static int apply_patch(struct apply_state *state,
45744575

45754576
free_patch_list(list);
45764577
strbuf_release(&buf);
4577-
string_list_clear(&fn_table, 0);
4578+
string_list_clear(&state->fn_table, 0);
45784579
return 0;
45794580
}
45804581

@@ -4668,6 +4669,8 @@ static void clear_apply_state(struct apply_state *state)
46684669
{
46694670
string_list_clear(&state->limit_by_name, 0);
46704671
strbuf_release(&state->root);
4672+
4673+
/* &state->fn_table is cleared at the end of apply_patch() */
46714674
}
46724675

46734676
int cmd_apply(int argc, const char **argv, const char *prefix)

0 commit comments

Comments
 (0)