@matheusbsbs / matheus-bagliano / commits / 02ac98374e
Fork 0

builtin/checkout: learn -B

Internally, --track and --orphan still use the 'safe' -b, not -B. Signed-off-by: Tay Ray Chuan <rctay89@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Tay Ray Chuan committed Jun 24, 2010 at 03:29 UTC 02ac98374eefbe4a46d4b53a8a78057ad8ad39b7
3 files changed +88 -7
Documentation/git-checkout.txt
+19 -2
@@ -9,7 +9,7 @@ SYNOPSIS
9 --------
10 [verse]
11 'git checkout' [-q] [-f] [-m] [<branch>]
12 -'git checkout' [-q] [-f] [-m] [[-b|--orphan] <new_branch>] [<start_point>]
12 +'git checkout' [-q] [-f] [-m] [[-b|-B|--orphan] <new_branch>] [<start_point>]
13 'git checkout' [-f|--ours|--theirs|-m|--conflict=<style>] [<tree-ish>] [--] <paths>...
14 'git checkout' --patch [<tree-ish>] [--] [<paths>...]
15
@@ -21,7 +21,7 @@ also update `HEAD` to set the specified branch as the current
21 branch.
22
23 'git checkout' [<branch>]::
24 -'git checkout' -b <new branch> [<start point>]::
24 +'git checkout' -b|-B <new_branch> [<start point>]::
25
26 This form switches branches by updating the index, working
27 tree, and HEAD to reflect the specified branch.
@@ -31,6 +31,17 @@ were called and then checked out; in this case you can
31 use the `--track` or `--no-track` options, which will be passed to
32 'git branch'. As a convenience, `--track` without `-b` implies branch
33 creation; see the description of `--track` below.
34 ++
35 +If `-B` is given, <new_branch> is created if it doesn't exist; otherwise, it
36 +is reset. This is the transactional equivalent of
37 ++
38 +------------
39 +$ git branch -f <branch> [<start point>]
40 +$ git checkout <branch>
41 +------------
42 ++
43 +that is to say, the branch is not reset/created unless "git checkout" is
44 +successful.
45
46 'git checkout' [--patch] [<tree-ish>] [--] <pathspec>...::
47
@@ -75,6 +86,12 @@ entries; instead, unmerged entries are ignored.
86 Create a new branch named <new_branch> and start it at
87 <start_point>; see linkgit:git-branch[1] for details.
88
89 +-B::
90 + Creates the branch <new_branch> and start it at <start_point>;
91 + if it already exists, then reset it to <start_point>. This is
92 + equivalent to running "git branch" with "-f"; see
93 + linkgit:git-branch[1] for details.
94 +
95 -t::
96 --track::
97 When creating a new branch, set up "upstream" configuration. See
builtin/checkout.c
+24 -5
@@ -32,7 +32,11 @@ struct checkout_opts {
32 int writeout_stage;
33 int writeout_error;
34
35 + /* not set by parse_options */
36 + int branch_exists;
37 +
38 const char *new_branch;
39 + const char *new_branch_force;
40 const char *new_orphan_branch;
41 int new_branch_log;
42 enum branch_track track;
@@ -511,7 +515,8 @@ static void update_refs_for_switch(struct checkout_opts *opts,
515 }
516 }
517 else
514 - create_branch(old->name, opts->new_branch, new->name, 0,
518 + create_branch(old->name, opts->new_branch, new->name,
519 + opts->new_branch_force ? 1 : 0,
520 opts->new_branch_log, opts->track);
521 new->name = opts->new_branch;
522 setup_branch_path(new);
@@ -531,7 +536,7 @@ static void update_refs_for_switch(struct checkout_opts *opts,
536 new->name);
537 else
538 fprintf(stderr, "Switched to%s branch '%s'\n",
534 - opts->new_branch ? " a new" : "",
539 + opts->branch_exists ? " and reset" : " a new",
540 new->name);
541 }
542 if (old->path && old->name) {
@@ -659,6 +664,8 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
664 OPT__QUIET(&opts.quiet),
665 OPT_STRING('b', NULL, &opts.new_branch, "branch",
666 "create and checkout a new branch"),
667 + OPT_STRING('B', NULL, &opts.new_branch_force, "branch",
668 + "create/reset and checkout a branch"),
669 OPT_BOOLEAN('l', NULL, &opts.new_branch_log, "log for new branch"),
670 OPT_SET_INT('t', "track", &opts.track, "track",
671 BRANCH_TRACK_EXPLICIT),
@@ -689,6 +696,14 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
696 argc = parse_options(argc, argv, prefix, options, checkout_usage,
697 PARSE_OPT_KEEP_DASHDASH);
698
699 + /* we can assume from now on new_branch = !new_branch_force */
700 + if (opts.new_branch && opts.new_branch_force)
701 + die("-B cannot be used with -b");
702 +
703 + /* copy -B over to -b, so that we can just check the latter */
704 + if (opts.new_branch_force)
705 + opts.new_branch = opts.new_branch_force;
706 +
707 if (patch_mode && (opts.track > 0 || opts.new_branch
708 || opts.new_branch_log || opts.merge || opts.force))
709 die ("--patch is incompatible with all other options");
@@ -710,7 +725,7 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
725
726 if (opts.new_orphan_branch) {
727 if (opts.new_branch)
713 - die("--orphan and -b are mutually exclusive");
728 + die("--orphan and -b|-B are mutually exclusive");
729 if (opts.track > 0)
730 die("--orphan cannot be used with -t");
731 opts.new_branch = opts.new_orphan_branch;
@@ -859,8 +874,12 @@ no_reference:
874 if (strbuf_check_branch_ref(&buf, opts.new_branch))
875 die("git checkout: we do not like '%s' as a branch name.",
876 opts.new_branch);
862 - if (!get_sha1(buf.buf, rev))
863 - die("git checkout: branch %s already exists", opts.new_branch);
877 + if (!get_sha1(buf.buf, rev)) {
878 + opts.branch_exists = 1;
879 + if (!opts.new_branch_force)
880 + die("git checkout: branch %s already exists",
881 + opts.new_branch);
882 + }
883 strbuf_release(&buf);
884 }
885
t/t2018-checkout-branch.sh
+45
@@ -118,4 +118,49 @@ test_expect_success 'checkout -b to an existing branch fails' '
118 test_must_fail do_checkout branch2 $HEAD2
119 '
120
121 +test_expect_success 'checkout -B to an existing branch resets branch to HEAD' '
122 + git checkout branch1 &&
123 +
124 + do_checkout branch2 "" -B
125 +'
126 +
127 +test_expect_success 'checkout -B to an existing branch with an explicit ref resets branch to that ref' '
128 + git checkout branch1 &&
129 +
130 + do_checkout branch2 $HEAD1 -B
131 +'
132 +
133 +test_expect_success 'checkout -B to an existing branch with unmergeable changes fails' '
134 + git checkout branch1 &&
135 +
136 + setup_dirty_unmergeable &&
137 + test_must_fail do_checkout branch2 $HEAD1 -B &&
138 + test_dirty_unmergeable
139 +'
140 +
141 +test_expect_success 'checkout -f -B to an existing branch with unmergeable changes discards changes' '
142 + # still dirty and on branch1
143 + do_checkout branch2 $HEAD1 "-f -B" &&
144 + test_must_fail test_dirty_unmergeable
145 +'
146 +
147 +test_expect_success 'checkout -B to an existing branch preserves mergeable changes' '
148 + git checkout branch1 &&
149 +
150 + setup_dirty_mergeable &&
151 + do_checkout branch2 $HEAD1 -B &&
152 + test_dirty_mergeable
153 +'
154 +
155 +test_expect_success 'checkout -f -B to an existing branch with mergeable changes discards changes' '
156 + # clean up from previous test
157 + git reset --hard &&
158 +
159 + git checkout branch1 &&
160 +
161 + setup_dirty_mergeable &&
162 + do_checkout branch2 $HEAD1 "-f -B" &&
163 + test_must_fail test_dirty_mergeable
164 +'
165 +
166 test_done