branch: add --unset-upstream option

We have ways of setting the upstream information, but if we want to unset it, we need to resort to modifying the configuration manually. Teach branch an --unset-upstream option that unsets this information. Signed-off-by: Carlos Martín Nieto <cmn@elego.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Carlos Martín Nieto committed Aug 30, 2012 at 19:23 UTC b84869ef14081b298a4ab825219221ccfcb2a3ba
3 files changed +45 -3
Documentation/git-branch.txt
+5
@@ -14,6 +14,7 @@ SYNOPSIS
14 [(--merged | --no-merged | --contains) [<commit>]] [<pattern>...]
15 'git branch' [--set-upstream | --track | --no-track] [-l] [-f] <branchname> [<start-point>]
16 'git branch' (--set-upstream-to=<upstream> | -u <upstream>) [<branchname>]
17 +'git branch' --unset-upstream [<branchname>]
18 'git branch' (-m | -M) [<oldbranch>] <newbranch>
19 'git branch' (-d | -D) [-r] <branchname>...
20 'git branch' --edit-description [<branchname>]
@@ -180,6 +181,10 @@ start-point is either a local or remote-tracking branch.
181 considered <branchname>'s upstream branch. If no <branchname>
182 is specified, then it defaults to the current branch.
183
184 +--unset-upstream::
185 + Remove the upstream information for <branchname>. If no branch
186 + is specified it defaults to the current branch.
187 +
188 --edit-description::
189 Open an editor and edit the text to explain what the branch is
190 for, to be used by various other commands (e.g. `request-pull`).
builtin/branch.c
+18 -3
@@ -712,7 +712,7 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
712 int delete = 0, rename = 0, force_create = 0, list = 0;
713 int verbose = 0, abbrev = -1, detached = 0;
714 int reflog = 0, edit_description = 0;
715 - int quiet = 0;
715 + int quiet = 0, unset_upstream = 0;
716 const char *new_upstream = NULL;
717 enum branch_track track;
718 int kinds = REF_LOCAL_BRANCH;
@@ -728,6 +728,7 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
728 OPT_SET_INT( 0, "set-upstream", &track, "change upstream info",
729 BRANCH_TRACK_OVERRIDE),
730 OPT_STRING('u', "set-upstream-to", &new_upstream, "upstream", "change the upstream info"),
731 + OPT_BOOLEAN(0, "unset-upstream", &unset_upstream, "Unset the upstream info"),
732 OPT__COLOR(&branch_use_color, "use colored output"),
733 OPT_SET_INT('r', "remotes", &kinds, "act on remote-tracking branches",
734 REF_REMOTE_BRANCH),
@@ -796,10 +797,10 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
797 argc = parse_options(argc, argv, prefix, options, builtin_branch_usage,
798 0);
799
799 - if (!delete && !rename && !edit_description && !new_upstream && argc == 0)
800 + if (!delete && !rename && !edit_description && !new_upstream && !unset_upstream && argc == 0)
801 list = 1;
802
802 - if (!!delete + !!rename + !!force_create + !!list + !!new_upstream > 1)
803 + if (!!delete + !!rename + !!force_create + !!list + !!new_upstream + !!unset_upstream > 1)
804 usage_with_options(builtin_branch_usage, options);
805
806 if (abbrev == -1)
@@ -865,6 +866,20 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
866 * info and making sure new_upstream is correct
867 */
868 create_branch(head, branch->name, new_upstream, 0, 0, 0, quiet, BRANCH_TRACK_OVERRIDE);
869 + } else if (unset_upstream) {
870 + struct branch *branch = branch_get(argv[0]);
871 + struct strbuf buf = STRBUF_INIT;
872 +
873 + if (!branch_has_merge_config(branch)) {
874 + die(_("Branch '%s' has no upstream information"), branch->name);
875 + }
876 +
877 + strbuf_addf(&buf, "branch.%s.remote", branch->name);
878 + git_config_set_multivar(buf.buf, NULL, NULL, 1);
879 + strbuf_reset(&buf);
880 + strbuf_addf(&buf, "branch.%s.merge", branch->name);
881 + git_config_set_multivar(buf.buf, NULL, NULL, 1);
882 + strbuf_release(&buf);
883 } else if (argc > 0 && argc <= 2) {
884 if (kinds != REF_LOCAL_BRANCH)
885 die(_("-a and -r options to 'git branch' do not make sense with a branch name"));
t/t3200-branch.sh
+22
@@ -383,6 +383,28 @@ test_expect_success 'use --set-upstream-to modify a particular branch' \
383 test "$(git config branch.my13.remote)" = "." &&
384 test "$(git config branch.my13.merge)" = "refs/heads/master"'
385
386 +test_expect_success '--unset-upstream should fail if given a non-existent branch' \
387 + 'test_must_fail git branch --unset-upstream i-dont-exist'
388 +
389 +test_expect_success 'test --unset-upstream on HEAD' \
390 + 'git branch my14
391 + test_config branch.master.remote foo &&
392 + test_config branch.master.merge foo &&
393 + git branch --set-upstream-to my14 &&
394 + git branch --unset-upstream &&
395 + test_must_fail git config branch.master.remote &&
396 + test_must_fail git config branch.master.merge &&
397 + # fail for a branch without upstream set
398 + test_must_fail git branch --unset-upstream
399 +'
400 +
401 +test_expect_success 'test --unset-upstream on a particular branch' \
402 + 'git branch my15
403 + git branch --set-upstream-to master my14 &&
404 + git branch --unset-upstream my14 &&
405 + test_must_fail git config branch.my14.remote &&
406 + test_must_fail git config branch.my14.merge'
407 +
408 # Keep this test last, as it changes the current branch
409 cat >expect <<EOF
410 $_z40 $HEAD $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150200 +0000 branch: Created from master