builtin/apply: move 'update_index' global into 'struct apply_state'
To libify the apply functionality the 'update_index' variable should not be static and global to the file. Let's move it into 'struct 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:10 UTC
901f9c6d42dd9857ecdac76413014c1497d34614
1 file changed
+26
-19
builtin/apply.c
+26
-19
@@ -28,6 +28,7 @@ struct apply_state {
28
/* These control what gets looked at and modified */
29
int check; /* preimage must match working tree, don't actually apply */
30
int check_index; /* preimage must match the indexed version */
31
+ int update_index; /* check_index && apply */
32
33
/* These boolean parameters control how the apply is done */
34
int apply_in_reverse;
@@ -46,7 +47,6 @@ static int newfd = -1;
47
48
static int state_p_value = 1;
49
static int p_value_known;
49
-static int update_index;
50
static int cached;
51
static int diffstat;
52
static int numstat;
@@ -4090,9 +4090,9 @@ static void patch_stats(struct patch *patch)
4090
}
4091
}
4092
4093
-static void remove_file(struct patch *patch, int rmdir_empty)
4093
+static void remove_file(struct apply_state *state, struct patch *patch, int rmdir_empty)
4094
{
4095
- if (update_index) {
4095
+ if (state->update_index) {
4096
if (remove_file_from_cache(patch->old_name) < 0)
4097
die(_("unable to remove %s from index"), patch->old_name);
4098
}
@@ -4103,14 +4103,18 @@ static void remove_file(struct patch *patch, int rmdir_empty)
4103
}
4104
}
4105
4106
-static void add_index_file(const char *path, unsigned mode, void *buf, unsigned long size)
4106
+static void add_index_file(struct apply_state *state,
4107
+ const char *path,
4108
+ unsigned mode,
4109
+ void *buf,
4110
+ unsigned long size)
4111
{
4112
struct stat st;
4113
struct cache_entry *ce;
4114
int namelen = strlen(path);
4115
unsigned ce_size = cache_entry_size(namelen);
4116
4113
- if (!update_index)
4117
+ if (!state->update_index)
4118
return;
4119
4120
ce = xcalloc(1, ce_size);
@@ -4220,13 +4224,14 @@ static void create_one_file(char *path, unsigned mode, const char *buf, unsigned
4224
die_errno(_("unable to write file '%s' mode %o"), path, mode);
4225
}
4226
4223
-static void add_conflicted_stages_file(struct patch *patch)
4227
+static void add_conflicted_stages_file(struct apply_state *state,
4228
+ struct patch *patch)
4229
{
4230
int stage, namelen;
4231
unsigned ce_size, mode;
4232
struct cache_entry *ce;
4233
4229
- if (!update_index)
4234
+ if (!state->update_index)
4235
return;
4236
namelen = strlen(patch->new_name);
4237
ce_size = cache_entry_size(namelen);
@@ -4247,7 +4252,7 @@ static void add_conflicted_stages_file(struct patch *patch)
4252
}
4253
}
4254
4250
-static void create_file(struct patch *patch)
4255
+static void create_file(struct apply_state *state, struct patch *patch)
4256
{
4257
char *path = patch->new_name;
4258
unsigned mode = patch->new_mode;
@@ -4259,22 +4264,24 @@ static void create_file(struct patch *patch)
4264
create_one_file(path, mode, buf, size);
4265
4266
if (patch->conflicted_threeway)
4262
- add_conflicted_stages_file(patch);
4267
+ add_conflicted_stages_file(state, patch);
4268
else
4264
- add_index_file(path, mode, buf, size);
4269
+ add_index_file(state, path, mode, buf, size);
4270
}
4271
4272
/* phase zero is to remove, phase one is to create */
4268
-static void write_out_one_result(struct patch *patch, int phase)
4273
+static void write_out_one_result(struct apply_state *state,
4274
+ struct patch *patch,
4275
+ int phase)
4276
{
4277
if (patch->is_delete > 0) {
4278
if (phase == 0)
4272
- remove_file(patch, 1);
4279
+ remove_file(state, patch, 1);
4280
return;
4281
}
4282
if (patch->is_new > 0 || patch->is_copy) {
4283
if (phase == 1)
4277
- create_file(patch);
4284
+ create_file(state, patch);
4285
return;
4286
}
4287
/*
@@ -4282,9 +4289,9 @@ static void write_out_one_result(struct patch *patch, int phase)
4289
* thing: remove the old, write the new
4290
*/
4291
if (phase == 0)
4285
- remove_file(patch, patch->is_rename);
4292
+ remove_file(state, patch, patch->is_rename);
4293
if (phase == 1)
4287
- create_file(patch);
4294
+ create_file(state, patch);
4295
}
4296
4297
static int write_out_one_reject(struct apply_state *state, struct patch *patch)
@@ -4371,7 +4378,7 @@ static int write_out_results(struct apply_state *state, struct patch *list)
4378
if (l->rejected)
4379
errs = 1;
4380
else {
4374
- write_out_one_result(l, phase);
4381
+ write_out_one_result(state, l, phase);
4382
if (phase == 1) {
4383
if (write_out_one_reject(state, l))
4384
errs = 1;
@@ -4451,8 +4458,8 @@ static int apply_patch(struct apply_state *state,
4458
if (whitespace_error && (ws_error_action == die_on_ws_error))
4459
apply = 0;
4460
4454
- update_index = state->check_index && apply;
4455
- if (update_index && newfd < 0)
4461
+ state->update_index = state->check_index && apply;
4462
+ if (state->update_index && newfd < 0)
4463
newfd = hold_locked_index(&lock_file, 1);
4464
4465
if (state->check_index) {
@@ -4727,7 +4734,7 @@ int cmd_apply(int argc, const char **argv, const char *prefix)
4734
whitespace_error);
4735
}
4736
4730
- if (update_index) {
4737
+ if (state.update_index) {
4738
if (write_locked_index(&the_index, &lock_file, COMMIT_LOCK))
4739
die(_("Unable to write new index file"));
4740
}