checkout: pass "struct checkout_opts *" as const pointer
This struct contains various switches to system and it feels somewhat safer to have the compiler reassure us that nowhere else changes it. One field that is changed, writeout_error, is split out and passed as another argument. Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Nguyễn Thái Ngọc Duy committed
Aug 29, 2012 at 20:55 UTC
a2b4994c826b40b42f395824dcadc082df77959b
1 file changed
+21
-16
builtin/checkout.c
+21
-16
@@ -33,7 +33,6 @@ struct checkout_opts {
33
int force;
34
int force_detach;
35
int writeout_stage;
36
- int writeout_error;
36
int overwrite_ignore;
37
38
/* not set by parse_options */
@@ -216,7 +215,7 @@ static int checkout_merged(int pos, struct checkout *state)
215
}
216
217
static int checkout_paths(struct tree *source_tree, const char **pathspec,
219
- const char *prefix, struct checkout_opts *opts)
218
+ const char *prefix, const struct checkout_opts *opts)
219
{
220
int pos;
221
struct checkout state;
@@ -309,7 +308,8 @@ static int checkout_paths(struct tree *source_tree, const char **pathspec,
308
return errs;
309
}
310
312
-static void show_local_changes(struct object *head, struct diff_options *opts)
311
+static void show_local_changes(struct object *head,
312
+ const struct diff_options *opts)
313
{
314
struct rev_info rev;
315
/* I think we want full paths, even if we're in a subdirectory. */
@@ -331,7 +331,8 @@ static void describe_detached_head(const char *msg, struct commit *commit)
331
strbuf_release(&sb);
332
}
333
334
-static int reset_tree(struct tree *tree, struct checkout_opts *o, int worktree)
334
+static int reset_tree(struct tree *tree, const struct checkout_opts *o,
335
+ int worktree, int *writeout_error)
336
{
337
struct unpack_trees_options opts;
338
struct tree_desc tree_desc;
@@ -350,7 +351,7 @@ static int reset_tree(struct tree *tree, struct checkout_opts *o, int worktree)
351
init_tree_desc(&tree_desc, tree->buffer, tree->size);
352
switch (unpack_trees(1, &tree_desc, &opts)) {
353
case -2:
353
- o->writeout_error = 1;
354
+ *writeout_error = 1;
355
/*
356
* We return 0 nevertheless, as the index is all right
357
* and more importantly we have made best efforts to
@@ -381,8 +382,10 @@ static void setup_branch_path(struct branch_info *branch)
382
branch->path = strbuf_detach(&buf, NULL);
383
}
384
384
-static int merge_working_tree(struct checkout_opts *opts,
385
- struct branch_info *old, struct branch_info *new)
385
+static int merge_working_tree(const struct checkout_opts *opts,
386
+ struct branch_info *old,
387
+ struct branch_info *new,
388
+ int *writeout_error)
389
{
390
int ret;
391
struct lock_file *lock_file = xcalloc(1, sizeof(struct lock_file));
@@ -393,7 +396,7 @@ static int merge_working_tree(struct checkout_opts *opts,
396
397
resolve_undo_clear();
398
if (opts->force) {
396
- ret = reset_tree(new->commit->tree, opts, 1);
399
+ ret = reset_tree(new->commit->tree, opts, 1, writeout_error);
400
if (ret)
401
return ret;
402
} else {
@@ -479,7 +482,8 @@ static int merge_working_tree(struct checkout_opts *opts,
482
o.verbosity = 0;
483
work = write_tree_from_memory(&o);
484
482
- ret = reset_tree(new->commit->tree, opts, 1);
485
+ ret = reset_tree(new->commit->tree, opts, 1,
486
+ writeout_error);
487
if (ret)
488
return ret;
489
o.ancestor = old->name;
@@ -487,7 +491,8 @@ static int merge_working_tree(struct checkout_opts *opts,
491
o.branch2 = "local";
492
merge_trees(&o, new->commit->tree, work,
493
old->commit->tree, &result);
490
- ret = reset_tree(new->commit->tree, opts, 0);
494
+ ret = reset_tree(new->commit->tree, opts, 0,
495
+ writeout_error);
496
if (ret)
497
return ret;
498
}
@@ -514,7 +519,7 @@ static void report_tracking(struct branch_info *new)
519
strbuf_release(&sb);
520
}
521
517
-static void update_refs_for_switch(struct checkout_opts *opts,
522
+static void update_refs_for_switch(const struct checkout_opts *opts,
523
struct branch_info *old,
524
struct branch_info *new)
525
{
@@ -701,13 +706,13 @@ static void orphaned_commit_warning(struct commit *old, struct commit *new)
706
free(refs.objects);
707
}
708
704
-static int switch_branches(struct checkout_opts *opts, struct branch_info *new)
709
+static int switch_branches(const struct checkout_opts *opts, struct branch_info *new)
710
{
711
int ret = 0;
712
struct branch_info old;
713
void *path_to_free;
714
unsigned char rev[20];
710
- int flag;
715
+ int flag, writeout_error = 0;
716
memset(&old, 0, sizeof(old));
717
old.path = path_to_free = resolve_refdup("HEAD", rev, 0, &flag);
718
old.commit = lookup_commit_reference_gently(rev, 1);
@@ -725,7 +730,7 @@ static int switch_branches(struct checkout_opts *opts, struct branch_info *new)
730
parse_commit(new->commit);
731
}
732
728
- ret = merge_working_tree(opts, &old, new);
733
+ ret = merge_working_tree(opts, &old, new, &writeout_error);
734
if (ret) {
735
free(path_to_free);
736
return ret;
@@ -738,7 +743,7 @@ static int switch_branches(struct checkout_opts *opts, struct branch_info *new)
743
744
ret = post_checkout_hook(old.commit, new->commit, 1);
745
free(path_to_free);
741
- return ret || opts->writeout_error;
746
+ return ret || writeout_error;
747
}
748
749
static int git_checkout_config(const char *var, const char *value, void *cb)
@@ -910,7 +915,7 @@ static int parse_branchname_arg(int argc, const char **argv,
915
return argcount;
916
}
917
913
-static int switch_unborn_to_new_branch(struct checkout_opts *opts)
918
+static int switch_unborn_to_new_branch(const struct checkout_opts *opts)
919
{
920
int status;
921
struct strbuf branch_ref = STRBUF_INIT;