checkout: move more parameters to struct checkout_opts

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 e51e30577a9056f5ae7195c694a00ee71200a9d5
1 file changed +35 -33
builtin/checkout.c
+35 -33
@@ -28,6 +28,7 @@ static const char * const checkout_usage[] = {
28 };
29
30 struct checkout_opts {
31 + int patch_mode;
32 int quiet;
33 int merge;
34 int force;
@@ -35,15 +36,17 @@ struct checkout_opts {
36 int writeout_stage;
37 int overwrite_ignore;
38
38 - /* not set by parse_options */
39 - int branch_exists;
40 -
39 const char *new_branch;
40 const char *new_branch_force;
41 const char *new_orphan_branch;
42 int new_branch_log;
43 enum branch_track track;
44 struct diff_options diff_options;
45 +
46 + int branch_exists;
47 + const char *prefix;
48 + const char **pathspec;
49 + struct tree *source_tree;
50 };
51
52 static int post_checkout_hook(struct commit *old, struct commit *new,
@@ -214,8 +217,7 @@ static int checkout_merged(int pos, struct checkout *state)
217 return status;
218 }
219
217 -static int checkout_paths(struct tree *source_tree, const char **pathspec,
218 - const char *prefix, const struct checkout_opts *opts)
220 +static int checkout_paths(const struct checkout_opts *opts)
221 {
222 int pos;
223 struct checkout state;
@@ -230,34 +232,34 @@ static int checkout_paths(struct tree *source_tree, const char **pathspec,
232 struct lock_file *lock_file = xcalloc(1, sizeof(struct lock_file));
233
234 newfd = hold_locked_index(lock_file, 1);
233 - if (read_cache_preload(pathspec) < 0)
235 + if (read_cache_preload(opts->pathspec) < 0)
236 return error(_("corrupt index file"));
237
236 - if (source_tree)
237 - read_tree_some(source_tree, pathspec);
238 + if (opts->source_tree)
239 + read_tree_some(opts->source_tree, opts->pathspec);
240
239 - for (pos = 0; pathspec[pos]; pos++)
241 + for (pos = 0; opts->pathspec[pos]; pos++)
242 ;
243 ps_matched = xcalloc(1, pos);
244
245 for (pos = 0; pos < active_nr; pos++) {
246 struct cache_entry *ce = active_cache[pos];
245 - if (source_tree && !(ce->ce_flags & CE_UPDATE))
247 + if (opts->source_tree && !(ce->ce_flags & CE_UPDATE))
248 continue;
247 - match_pathspec(pathspec, ce->name, ce_namelen(ce), 0, ps_matched);
249 + match_pathspec(opts->pathspec, ce->name, ce_namelen(ce), 0, ps_matched);
250 }
251
250 - if (report_path_error(ps_matched, pathspec, prefix))
252 + if (report_path_error(ps_matched, opts->pathspec, opts->prefix))
253 return 1;
254
255 /* "checkout -m path" to recreate conflicted state */
256 if (opts->merge)
255 - unmerge_cache(pathspec);
257 + unmerge_cache(opts->pathspec);
258
259 /* Any unmerged paths? */
260 for (pos = 0; pos < active_nr; pos++) {
261 struct cache_entry *ce = active_cache[pos];
260 - if (match_pathspec(pathspec, ce->name, ce_namelen(ce), 0, NULL)) {
262 + if (match_pathspec(opts->pathspec, ce->name, ce_namelen(ce), 0, NULL)) {
263 if (!ce_stage(ce))
264 continue;
265 if (opts->force) {
@@ -282,9 +284,9 @@ static int checkout_paths(struct tree *source_tree, const char **pathspec,
284 state.refresh_cache = 1;
285 for (pos = 0; pos < active_nr; pos++) {
286 struct cache_entry *ce = active_cache[pos];
285 - if (source_tree && !(ce->ce_flags & CE_UPDATE))
287 + if (opts->source_tree && !(ce->ce_flags & CE_UPDATE))
288 continue;
287 - if (match_pathspec(pathspec, ce->name, ce_namelen(ce), 0, NULL)) {
289 + if (match_pathspec(opts->pathspec, ce->name, ce_namelen(ce), 0, NULL)) {
290 if (!ce_stage(ce)) {
291 errs |= checkout_entry(ce, &state, NULL);
292 continue;
@@ -706,7 +708,8 @@ static void orphaned_commit_warning(struct commit *old, struct commit *new)
708 free(refs.objects);
709 }
710
709 -static int switch_branches(const struct checkout_opts *opts, struct branch_info *new)
711 +static int switch_branches(const struct checkout_opts *opts,
712 + struct branch_info *new)
713 {
714 int ret = 0;
715 struct branch_info old;
@@ -760,8 +763,7 @@ static int git_checkout_config(const char *var, const char *value, void *cb)
763 return git_xmerge_config(var, value, NULL);
764 }
765
763 -static int interactive_checkout(const char *revision, const char **pathspec,
764 - struct checkout_opts *opts)
766 +static int interactive_checkout(const char *revision, const char **pathspec)
767 {
768 return run_add_interactive(revision, "--patch=checkout", pathspec);
769 }
@@ -931,11 +933,8 @@ static int switch_unborn_to_new_branch(const struct checkout_opts *opts)
933 int cmd_checkout(int argc, const char **argv, const char *prefix)
934 {
935 struct checkout_opts opts;
934 - unsigned char rev[20];
936 struct branch_info new;
936 - struct tree *source_tree = NULL;
937 char *conflict_style = NULL;
938 - int patch_mode = 0;
938 int dwim_new_local_branch = 1;
939 struct option options[] = {
940 OPT__QUIET(&opts.quiet, "suppress progress reporting"),
@@ -957,7 +956,7 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
956 OPT_BOOLEAN(0, "overwrite-ignore", &opts.overwrite_ignore, "update ignored files (default)"),
957 OPT_STRING(0, "conflict", &conflict_style, "style",
958 "conflict style (merge or diff3)"),
960 - OPT_BOOLEAN('p', "patch", &patch_mode, "select hunks interactively"),
959 + OPT_BOOLEAN('p', "patch", &opts.patch_mode, "select hunks interactively"),
960 { OPTION_BOOLEAN, 0, "guess", &dwim_new_local_branch, NULL,
961 "second guess 'git checkout no-such-branch'",
962 PARSE_OPT_NOARG | PARSE_OPT_HIDDEN },
@@ -967,6 +966,7 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
966 memset(&opts, 0, sizeof(opts));
967 memset(&new, 0, sizeof(new));
968 opts.overwrite_ignore = 1;
969 + opts.prefix = prefix;
970
971 gitmodules_config();
972 git_config(git_checkout_config, &opts);
@@ -984,7 +984,7 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
984 if (opts.new_branch_force)
985 opts.new_branch = opts.new_branch_force;
986
987 - if (patch_mode && (opts.track > 0 || opts.new_branch
987 + if (opts.patch_mode && (opts.track > 0 || opts.new_branch
988 || opts.new_branch_log || opts.merge || opts.force
989 || opts.force_detach))
990 die (_("--patch is incompatible with all other options"));
@@ -1039,13 +1039,15 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
1039 * remote branches, erroring out for invalid or ambiguous cases.
1040 */
1041 if (argc) {
1042 + unsigned char rev[20];
1043 int dwim_ok =
1043 - !patch_mode &&
1044 + !opts.patch_mode &&
1045 dwim_new_local_branch &&
1046 opts.track == BRANCH_TRACK_UNSPECIFIED &&
1047 !opts.new_branch;
1048 int n = parse_branchname_arg(argc, argv, dwim_ok,
1048 - &new, &source_tree, rev, &opts.new_branch);
1049 + &new, &opts.source_tree,
1050 + rev, &opts.new_branch);
1051 argv += n;
1052 argc -= n;
1053 }
@@ -1054,13 +1056,13 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
1056 opts.track = git_branch_track;
1057
1058 if (argc) {
1057 - const char **pathspec = get_pathspec(prefix, argv);
1059 + opts.pathspec = get_pathspec(prefix, argv);
1060
1059 - if (!pathspec)
1061 + if (!opts.pathspec)
1062 die(_("invalid path specification"));
1063
1062 - if (patch_mode)
1063 - return interactive_checkout(new.name, pathspec, &opts);
1064 + if (opts.patch_mode)
1065 + return interactive_checkout(new.name, opts.pathspec);
1066
1067 /* Checkout paths */
1068 if (opts.new_branch) {
@@ -1077,11 +1079,11 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
1079 if (1 < !!opts.writeout_stage + !!opts.force + !!opts.merge)
1080 die(_("git checkout: --ours/--theirs, --force and --merge are incompatible when\nchecking out of the index."));
1081
1080 - return checkout_paths(source_tree, pathspec, prefix, &opts);
1082 + return checkout_paths(&opts);
1083 }
1084
1083 - if (patch_mode)
1084 - return interactive_checkout(new.name, NULL, &opts);
1085 + if (opts.patch_mode)
1086 + return interactive_checkout(new.name, NULL);
1087
1088 if (opts.new_branch) {
1089 struct strbuf buf = STRBUF_INIT;