Skip to content

Commit

Permalink
branch: add --unset-upstream option
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
Carlos Martín Nieto authored and Junio C Hamano committed Aug 30, 2012
1 parent 6183d82 commit b84869e
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 3 deletions.
5 changes: 5 additions & 0 deletions Documentation/git-branch.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ SYNOPSIS
[(--merged | --no-merged | --contains) [<commit>]] [<pattern>...]
'git branch' [--set-upstream | --track | --no-track] [-l] [-f] <branchname> [<start-point>]
'git branch' (--set-upstream-to=<upstream> | -u <upstream>) [<branchname>]
'git branch' --unset-upstream [<branchname>]
'git branch' (-m | -M) [<oldbranch>] <newbranch>
'git branch' (-d | -D) [-r] <branchname>...
'git branch' --edit-description [<branchname>]
Expand Down Expand Up @@ -180,6 +181,10 @@ start-point is either a local or remote-tracking branch.
considered <branchname>'s upstream branch. If no <branchname>
is specified, then it defaults to the current branch.

--unset-upstream::
Remove the upstream information for <branchname>. If no branch
is specified it defaults to the current branch.

--edit-description::
Open an editor and edit the text to explain what the branch is
for, to be used by various other commands (e.g. `request-pull`).
Expand Down
21 changes: 18 additions & 3 deletions builtin/branch.c
Original file line number Diff line number Diff line change
Expand Up @@ -712,7 +712,7 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
int delete = 0, rename = 0, force_create = 0, list = 0;
int verbose = 0, abbrev = -1, detached = 0;
int reflog = 0, edit_description = 0;
int quiet = 0;
int quiet = 0, unset_upstream = 0;
const char *new_upstream = NULL;
enum branch_track track;
int kinds = REF_LOCAL_BRANCH;
Expand All @@ -728,6 +728,7 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
OPT_SET_INT( 0, "set-upstream", &track, "change upstream info",
BRANCH_TRACK_OVERRIDE),
OPT_STRING('u', "set-upstream-to", &new_upstream, "upstream", "change the upstream info"),
OPT_BOOLEAN(0, "unset-upstream", &unset_upstream, "Unset the upstream info"),
OPT__COLOR(&branch_use_color, "use colored output"),
OPT_SET_INT('r', "remotes", &kinds, "act on remote-tracking branches",
REF_REMOTE_BRANCH),
Expand Down Expand Up @@ -796,10 +797,10 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
argc = parse_options(argc, argv, prefix, options, builtin_branch_usage,
0);

if (!delete && !rename && !edit_description && !new_upstream && argc == 0)
if (!delete && !rename && !edit_description && !new_upstream && !unset_upstream && argc == 0)
list = 1;

if (!!delete + !!rename + !!force_create + !!list + !!new_upstream > 1)
if (!!delete + !!rename + !!force_create + !!list + !!new_upstream + !!unset_upstream > 1)
usage_with_options(builtin_branch_usage, options);

if (abbrev == -1)
Expand Down Expand Up @@ -865,6 +866,20 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
* info and making sure new_upstream is correct
*/
create_branch(head, branch->name, new_upstream, 0, 0, 0, quiet, BRANCH_TRACK_OVERRIDE);
} else if (unset_upstream) {
struct branch *branch = branch_get(argv[0]);
struct strbuf buf = STRBUF_INIT;

if (!branch_has_merge_config(branch)) {
die(_("Branch '%s' has no upstream information"), branch->name);
}

strbuf_addf(&buf, "branch.%s.remote", branch->name);
git_config_set_multivar(buf.buf, NULL, NULL, 1);
strbuf_reset(&buf);
strbuf_addf(&buf, "branch.%s.merge", branch->name);
git_config_set_multivar(buf.buf, NULL, NULL, 1);
strbuf_release(&buf);
} else if (argc > 0 && argc <= 2) {
if (kinds != REF_LOCAL_BRANCH)
die(_("-a and -r options to 'git branch' do not make sense with a branch name"));
Expand Down
22 changes: 22 additions & 0 deletions t/t3200-branch.sh
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,28 @@ test_expect_success 'use --set-upstream-to modify a particular branch' \
test "$(git config branch.my13.remote)" = "." &&
test "$(git config branch.my13.merge)" = "refs/heads/master"'

test_expect_success '--unset-upstream should fail if given a non-existent branch' \
'test_must_fail git branch --unset-upstream i-dont-exist'

test_expect_success 'test --unset-upstream on HEAD' \
'git branch my14
test_config branch.master.remote foo &&
test_config branch.master.merge foo &&
git branch --set-upstream-to my14 &&
git branch --unset-upstream &&
test_must_fail git config branch.master.remote &&
test_must_fail git config branch.master.merge &&
# fail for a branch without upstream set
test_must_fail git branch --unset-upstream
'

test_expect_success 'test --unset-upstream on a particular branch' \
'git branch my15
git branch --set-upstream-to master my14 &&
git branch --unset-upstream my14 &&
test_must_fail git config branch.my14.remote &&
test_must_fail git config branch.my14.merge'

# Keep this test last, as it changes the current branch
cat >expect <<EOF
$_z40 $HEAD $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150200 +0000 branch: Created from master
Expand Down

0 comments on commit b84869e

Please sign in to comment.