All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 0/3] Improve branch UI for setting upstream information
@ 2012-08-20 13:47 Carlos Martín Nieto
  2012-08-20 13:47 ` [PATCH 1/3] branch: introduce --set-upstream-to Carlos Martín Nieto
                   ` (2 more replies)
  0 siblings, 3 replies; 14+ messages in thread
From: Carlos Martín Nieto @ 2012-08-20 13:47 UTC (permalink / raw)
  To: git

Hi all,

After way too long, here's the next iteration of the concept that
began with swapping arguments in --set-upstream like -m does.

After the feedback from the list, --set-upstream-to was born and
--set-upstream is being deprecated in favour of either --track or
--set-upstream-to depening on which of the behaviours the user wants
to have.

Using --set-upsteam with one argument now also leads to a message
explaining how to undo it. For that, branch has learnt
--unset-upstream which will remove the upstream information for the
given branch (or HEAD).

Carlos Martín Nieto (3):
  branch: introduce --set-upstream-to
  branch: add --unset-upstream option
  branch: suggest how to undo a --set-upstream when given one branch

 Documentation/git-branch.txt | 14 +++++++-
 builtin/branch.c             | 65 +++++++++++++++++++++++++++++++++--
 t/t3200-branch.sh            | 82 ++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 157 insertions(+), 4 deletions(-)

-- 
1.7.11.1.104.ge7b44f1

^ permalink raw reply	[flat|nested] 14+ messages in thread

* [PATCH 1/3] branch: introduce --set-upstream-to
  2012-08-20 13:47 [PATCH 0/3] Improve branch UI for setting upstream information Carlos Martín Nieto
@ 2012-08-20 13:47 ` Carlos Martín Nieto
  2012-08-20 13:47 ` [PATCH 2/3] branch: add --unset-upstream option Carlos Martín Nieto
  2012-08-20 13:47 ` [PATCH 3/3] branch: suggest how to undo a --set-upstream when given one branch Carlos Martín Nieto
  2 siblings, 0 replies; 14+ messages in thread
From: Carlos Martín Nieto @ 2012-08-20 13:47 UTC (permalink / raw)
  To: git

The existing --set-uptream option can cause confusion, as it uses the
usual branch convention of assuming a starting point of HEAD if none
is specified, causing

    git branch --set-upstream origin/master

to create a new local branch 'origin/master' that tracks the current
branch. As --set-upstream already exists, we can't simply change its
behaviour. To work around this, introduce --set-upstream-to which
accepts a compulsory argument indicating what the new upstream branch
should be and one optinal argument indicating which branch to change,
defaulting to HEAD.

The new options allows us to type

    git branch --set-upstream-to origin/master

to set the current branch's upstream to be origin's master.

Signed-off-by: Carlos Martín Nieto <cmn@elego.de>
---
 Documentation/git-branch.txt |  9 ++++++++-
 builtin/branch.c             | 17 +++++++++++++++--
 t/t3200-branch.sh            | 14 ++++++++++++++
 3 files changed, 37 insertions(+), 3 deletions(-)

diff --git a/Documentation/git-branch.txt b/Documentation/git-branch.txt
index 47235be..e41c4b5 100644
--- a/Documentation/git-branch.txt
+++ b/Documentation/git-branch.txt
@@ -13,6 +13,7 @@ SYNOPSIS
 	[--column[=<options>] | --no-column]
 	[(--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' (-m | -M) [<oldbranch>] <newbranch>
 'git branch' (-d | -D) [-r] <branchname>...
 'git branch' --edit-description [<branchname>]
@@ -48,7 +49,7 @@ branch so that 'git pull' will appropriately merge from
 the remote-tracking branch. This behavior may be changed via the global
 `branch.autosetupmerge` configuration flag. That setting can be
 overridden by using the `--track` and `--no-track` options, and
-changed later using `git branch --set-upstream`.
+changed later using `git branch --set-upstream-to`.
 
 With a `-m` or `-M` option, <oldbranch> will be renamed to <newbranch>.
 If <oldbranch> had a corresponding reflog, it is renamed to match
@@ -173,6 +174,12 @@ start-point is either a local or remote-tracking branch.
 	like `--track` would when creating the branch, except that where
 	branch points to is not changed.
 
+-u <upstream>::
+--set-upstream-to=<upstream>::
+	Set up <branchname>'s tracking information so <upstream> is
+	considered <branchname>'s upstream branch. If no <branchname>
+	is specified, then 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`).
diff --git a/builtin/branch.c b/builtin/branch.c
index 0e060f2..3c978eb 100644
--- a/builtin/branch.c
+++ b/builtin/branch.c
@@ -713,6 +713,7 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
 	int verbose = 0, abbrev = -1, detached = 0;
 	int reflog = 0, edit_description = 0;
 	int quiet = 0;
+	const char *new_upstream = NULL;
 	enum branch_track track;
 	int kinds = REF_LOCAL_BRANCH;
 	struct commit_list *with_commit = NULL;
@@ -726,6 +727,7 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
 			BRANCH_TRACK_EXPLICIT),
 		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__COLOR(&branch_use_color, "use colored output"),
 		OPT_SET_INT('r', "remotes",     &kinds, "act on remote-tracking branches",
 			REF_REMOTE_BRANCH),
@@ -794,10 +796,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 && argc == 0)
+	if (!delete && !rename && !edit_description && !new_upstream && argc == 0)
 		list = 1;
 
-	if (!!delete + !!rename + !!force_create + !!list > 1)
+	if (!!delete + !!rename + !!force_create + !!list + !!new_upstream > 1)
 		usage_with_options(builtin_branch_usage, options);
 
 	if (abbrev == -1)
@@ -852,6 +854,17 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
 			rename_branch(argv[0], argv[1], rename > 1);
 		else
 			usage_with_options(builtin_branch_usage, options);
+	} else if (new_upstream) {
+		struct branch *branch = branch_get(argv[0]);
+
+		if (!ref_exists(branch->refname))
+			die(_("branch '%s' does not exist"), branch->name);
+
+		/*
+		 * create_branch takes care of setting up the tracking
+		 * info and making sure new_upstream is correct
+		 */
+		create_branch(head, branch->name, new_upstream, 0, 0, 0, quiet, BRANCH_TRACK_OVERRIDE);
 	} 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"));
diff --git a/t/t3200-branch.sh b/t/t3200-branch.sh
index a17f8b2..e9019ac 100755
--- a/t/t3200-branch.sh
+++ b/t/t3200-branch.sh
@@ -369,6 +369,20 @@ test_expect_success \
     'git tag foobar &&
      test_must_fail git branch --track my11 foobar'
 
+test_expect_success 'use --set-upstream-to modify HEAD' \
+    'test_config branch.master.remote foo &&
+     test_config branch.master.merge foo &&
+     git branch my12
+     git branch --set-upstream-to my12 &&
+     test "$(git config branch.master.remote)" = "." &&
+     test "$(git config branch.master.merge)" = "refs/heads/my12"'
+
+test_expect_success 'use --set-upstream-to modify a particular branch' \
+    'git branch my13
+     git branch --set-upstream-to master my13 &&
+     test "$(git config branch.my13.remote)" = "." &&
+     test "$(git config branch.my13.merge)" = "refs/heads/master"'
+
 # 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
-- 
1.7.11.1.104.ge7b44f1

^ permalink raw reply related	[flat|nested] 14+ messages in thread

* [PATCH 2/3] branch: add --unset-upstream option
  2012-08-20 13:47 [PATCH 0/3] Improve branch UI for setting upstream information Carlos Martín Nieto
  2012-08-20 13:47 ` [PATCH 1/3] branch: introduce --set-upstream-to Carlos Martín Nieto
@ 2012-08-20 13:47 ` Carlos Martín Nieto
  2012-08-23 21:20   ` Junio C Hamano
  2012-08-20 13:47 ` [PATCH 3/3] branch: suggest how to undo a --set-upstream when given one branch Carlos Martín Nieto
  2 siblings, 1 reply; 14+ messages in thread
From: Carlos Martín Nieto @ 2012-08-20 13:47 UTC (permalink / raw)
  To: git

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>
---
 Documentation/git-branch.txt |  5 +++++
 builtin/branch.c             | 17 ++++++++++++++---
 t/t3200-branch.sh            | 16 ++++++++++++++++
 3 files changed, 35 insertions(+), 3 deletions(-)

diff --git a/Documentation/git-branch.txt b/Documentation/git-branch.txt
index e41c4b5..9c1d2f1 100644
--- a/Documentation/git-branch.txt
+++ b/Documentation/git-branch.txt
@@ -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>]
@@ -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`).
diff --git a/builtin/branch.c b/builtin/branch.c
index 3c978eb..08068f7 100644
--- a/builtin/branch.c
+++ b/builtin/branch.c
@@ -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;
@@ -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),
@@ -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)
@@ -865,6 +866,16 @@ 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;
+
+		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"));
diff --git a/t/t3200-branch.sh b/t/t3200-branch.sh
index e9019ac..93e5d6e 100755
--- a/t/t3200-branch.sh
+++ b/t/t3200-branch.sh
@@ -383,6 +383,22 @@ 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 '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'
+
+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
-- 
1.7.11.1.104.ge7b44f1

^ permalink raw reply related	[flat|nested] 14+ messages in thread

* [PATCH 3/3] branch: suggest how to undo a --set-upstream when given one branch
  2012-08-20 13:47 [PATCH 0/3] Improve branch UI for setting upstream information Carlos Martín Nieto
  2012-08-20 13:47 ` [PATCH 1/3] branch: introduce --set-upstream-to Carlos Martín Nieto
  2012-08-20 13:47 ` [PATCH 2/3] branch: add --unset-upstream option Carlos Martín Nieto
@ 2012-08-20 13:47 ` Carlos Martín Nieto
  2012-08-20 18:50   ` Junio C Hamano
  2 siblings, 1 reply; 14+ messages in thread
From: Carlos Martín Nieto @ 2012-08-20 13:47 UTC (permalink / raw)
  To: git

This interface is error prone, and a better one (--set-upstream-to)
exists. Suggest how to fix a --set-upstream invocation in case the
user only gives one argument, which makes it likely that he meant to
do the opposite, like with

    git branch --set-upstream origin/master

when they meant one of

    git branch --set-upstream origin/master master
    git branch --set-upstream-to origin/master

While we're at it, add a notice that the --set-upstream flag is
deprecated and will be removed at some point.

Signed-off-by: Carlos Martín Nieto <cmn@elego.de>

---

This produces suboptimal output in case that A tracks B and we do

    git checkout B
    git branch --set-upstream A

as it will suggest

    git branch --set-upstream A B

as a way of undoing what we just did. Avoiding it becomes a bit messy
(yet another layer of ifs), so I've left it out. Anybody reckon it's
worth recognising this?
---
 builtin/branch.c  | 35 +++++++++++++++++++++++++++++++++++
 t/t3200-branch.sh | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 87 insertions(+)

diff --git a/builtin/branch.c b/builtin/branch.c
index 08068f7..33641d9 100644
--- a/builtin/branch.c
+++ b/builtin/branch.c
@@ -707,6 +707,21 @@ static int edit_branch_description(const char *branch_name)
 	return status;
 }
 
+static void print_set_upstream_warning(const char *branch, int branch_existed, const char *old_upstream)
+{
+	fprintf(stderr, _("If you wanted to make '%s' track '%s', do this:\n\n"), head, branch);
+	if (branch_existed) {
+		if (old_upstream)
+			fprintf(stderr, _("    git branch --set-upstream %s %s\n"), old_upstream, branch);
+		else
+			fprintf(stderr, _("    git branch --unset-upstream %s\n"), branch);
+	} else {
+		fprintf(stderr, _("    git branch -d %s\n"), branch);
+	}
+
+	fprintf(stderr, _("    git branch --set-upstream-to %s\n"), branch);
+}
+
 int cmd_branch(int argc, const char **argv, const char *prefix)
 {
 	int delete = 0, rename = 0, force_create = 0, list = 0;
@@ -877,10 +892,30 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
 		git_config_set_multivar(buf.buf, NULL, NULL, 1);
 		strbuf_release(&buf);
 	} else if (argc > 0 && argc <= 2) {
+		struct branch *branch = branch_get(argv[0]);
+		const char *old_upstream = NULL;
+		int branch_existed = 0;
+
 		if (kinds != REF_LOCAL_BRANCH)
 			die(_("-a and -r options to 'git branch' do not make sense with a branch name"));
+
+		if (track == BRANCH_TRACK_OVERRIDE)
+			fprintf(stderr, _("The --set-upstream flag is deprecated and will be removed. Consider using --track or --set-upstream-to\n"));
+
+		/*
+		 * Save what argv[0] was pointing to so we can give
+		 * the --set-upstream-to hint
+		 */
+		if (branch_has_merge_config(branch))
+			old_upstream = shorten_unambiguous_ref(branch->merge[0]->dst, 0);
+
+		branch_existed = ref_exists(branch->refname);
 		create_branch(head, argv[0], (argc == 2) ? argv[1] : head,
 			      force_create, reflog, 0, quiet, track);
+
+		if (argc == 1 && track == BRANCH_TRACK_OVERRIDE)
+			print_set_upstream_warning(argv[0], branch_existed, old_upstream);
+
 	} else
 		usage_with_options(builtin_branch_usage, options);
 
diff --git a/t/t3200-branch.sh b/t/t3200-branch.sh
index 93e5d6e..702bffa 100755
--- a/t/t3200-branch.sh
+++ b/t/t3200-branch.sh
@@ -399,6 +399,58 @@ test_expect_success 'test --unset-upstream on a particular branch' \
      test_must_fail git config branch.my14.remote &&
      test_must_fail git config branch.my14.merge'
 
+test_expect_success 'test --set-upstream help message with one arg' \
+    'git branch --set-upstream origin/master 2>actual &&
+     test_when_finished git branch -d origin/master &&
+     cat >expected <<EOF &&
+The --set-upstream flag is deprecated and will be removed. Consider using --track or --set-upstream-to
+If you wanted to make '"'master'"' track '"'origin/master'"', do this:
+
+    git branch -d origin/master
+    git branch --set-upstream-to origin/master
+EOF
+     test_cmp expected actual
+'
+
+test_expect_success '--set-upstream with a branch that already has an upstream' \
+    'git branch --set-upstream-to my12 master &&
+     git branch --set-upstream-to my13 my12 &&
+     test_when_finished git branch --unset-upstream my12 &&
+     test_when_finished git branch --unset-upstream my13 &&
+     git branch --set-upstream my12 2>actual &&
+     cat actual &&
+     cat >expected <<EOF &&
+The --set-upstream flag is deprecated and will be removed. Consider using --track or --set-upstream-to
+If you wanted to make '"'master'"' track '"'my12'"', do this:
+
+    git branch --set-upstream my13 my12
+    git branch --set-upstream-to my12
+EOF
+     test_cmp expected actual
+'
+
+test_expect_success '--set-upstream with a branch that has no upstream' \
+    'git branch --set-upstream my12 2>actual &&
+     test_when_finished git branch --unset-upstream my12
+     cat >expected <<EOF &&
+The --set-upstream flag is deprecated and will be removed. Consider using --track or --set-upstream-to
+If you wanted to make '"'master'"' track '"'my12'"', do this:
+
+    git branch --unset-upstream my12
+    git branch --set-upstream-to my12
+EOF
+     test_cmp expected actual
+'
+
+test_expect_success '--set-upstream with two args should only show the deprecation message' \
+    'git branch --set-upstream master my13 2>actual &&
+     test_when_finished git branch --unset-upstream master &&
+     cat >expected <<EOF &&
+The --set-upstream flag is deprecated and will be removed. Consider using --track or --set-upstream-to
+EOF
+     test_cmp expected actual
+'
+
 # 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
-- 
1.7.11.1.104.ge7b44f1

^ permalink raw reply related	[flat|nested] 14+ messages in thread

* Re: [PATCH 3/3] branch: suggest how to undo a --set-upstream when given one branch
  2012-08-20 13:47 ` [PATCH 3/3] branch: suggest how to undo a --set-upstream when given one branch Carlos Martín Nieto
@ 2012-08-20 18:50   ` Junio C Hamano
  2012-08-22  1:26     ` Carlos Martín Nieto
  2012-08-23 18:56     ` [PATCHv2 3/3] branch: deprecate --set-upstream and show help if we detect possible mistaken use Carlos Martín Nieto
  0 siblings, 2 replies; 14+ messages in thread
From: Junio C Hamano @ 2012-08-20 18:50 UTC (permalink / raw)
  To: Carlos Martín Nieto; +Cc: git

Carlos Martín Nieto <cmn@elego.de> writes:

> This interface is error prone, and a better one (--set-upstream-to)
> exists. Suggest how to fix a --set-upstream invocation in case the
> user only gives one argument, which makes it likely that he meant to
> do the opposite, like with
>
>     git branch --set-upstream origin/master
>
> when they meant one of
>
>     git branch --set-upstream origin/master master
>     git branch --set-upstream-to origin/master

In "git branch --set-upstream $A", if $A did not exist (i.e. we
ended up creating $A that is forked from the current branch), and if
refs/remotes/$A exists, I agree it is very likely that the user
wanted to set the upstream of the current branch to remotes/$A
instead.

But I am not sure about other cases.  If $A already existed, it is
equally likely that "git branch --set-upstream $A" wanted to make
the existing $A track our current branch, or make our branch track
that existing $A, isn't it?  We would end up giving unwanted advice
that is *wrong* for the user's situation 50% of the time, which does
not sound like an acceptable thing to do.

So I would really prefer to see the condition this advice is offered
limited to very specific cases (namely, only one parameter $A was
given to cause us use "HEAD" as the other branch, refs/heads/$A did
not exist and refs/remotes/$A did; there may be others but I think
this is the one we most care about) in which we know the advice is
correct with certainty.

> While we're at it, add a notice that the --set-upstream flag is
> deprecated and will be removed at some point.

This part is unquestionably good.

> Signed-off-by: Carlos Martín Nieto <cmn@elego.de>
>
> ---
>
> This produces suboptimal output in case that A tracks B and we do
>
>     git checkout B
>     git branch --set-upstream A
>
> as it will suggest
>
>     git branch --set-upstream A B
>
> as a way of undoing what we just did.

The literal meaning of what the user did was to create B and then
made A that existed (and used to build upon B) to build upon B,
which is a no-op.  And undoing can be done with the same command.

Which is funny.

I am sure you will get complaint from the real users.  To avoid it,
you would need to know what the old value was (if any), and skip the
"undo" part, but I think it is probably worth it.  You already have
code to learn what the old value was anyway, so the ideal is just a
single comparison away, no?

By the way, are you assuming that what the user wanted to do was to
make B build on top of A (i.e. set branch.B.up to A) in this
example?  For that setting to make sense, perhaps B should already
be a descendant of A, shouldn't it?  If that is the case, that is
another clue you can use in your logic to decide if you want to tell
the user "you told me to make $A build on the current branch, but I
think you meant the other way around" with more confidence.

> diff --git a/builtin/branch.c b/builtin/branch.c
> index 08068f7..33641d9 100644
> --- a/builtin/branch.c
> +++ b/builtin/branch.c
> @@ -707,6 +707,21 @@ static int edit_branch_description(const char *branch_name)
>  	return status;
>  }
>  
> +static void print_set_upstream_warning(const char *branch, int branch_existed, const char *old_upstream)
> +{
> +	fprintf(stderr, _("If you wanted to make '%s' track '%s', do this:\n\n"), head, branch);

I would suggest strongly against using the verb "track" here, as it
is overused and has caused confusion "is it tracking branch, remote
tracking branch, do I merge in one but fetch to update the other?".

Regardless of the verb, I think there should be a line _before_ the
above to tell the user what the command actually _did_, to make the
distinction stand out to help the user decide.

That is, tell the user "I set upstream for that other branch to the
current branch, making that other branch build on top of the current
branch.  If that was a mistake, and what you really wanted to do was
to make the current branch build on top of the other branch, here
are the things you need to do to recover".  Without saying anything
before "If that was a mistake, " part and giving "to do X, do Y"
would leave the user confused why he is being given a way to do X in
the first place, whether what he really wanted to do was X or
something else.

> +	if (branch_existed) {
> +		if (old_upstream)
> +			fprintf(stderr, _("    git branch --set-upstream %s %s\n"), old_upstream, branch);
> +		else
> +			fprintf(stderr, _("    git branch --unset-upstream %s\n"), branch);
> +	} else {
> +		fprintf(stderr, _("    git branch -d %s\n"), branch);
> +	}
> +
> +	fprintf(stderr, _("    git branch --set-upstream-to %s\n"), branch);
> +}

And I suspect the logic to call into this function needs to be
tightened a lot.  It may even turn out that we may not need messages
when "branch_existed" is true.

>  int cmd_branch(int argc, const char **argv, const char *prefix)
>  {
>  	int delete = 0, rename = 0, force_create = 0, list = 0;
> @@ -877,10 +892,30 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
>  		git_config_set_multivar(buf.buf, NULL, NULL, 1);
>  		strbuf_release(&buf);
>  	} else if (argc > 0 && argc <= 2) {
> +		struct branch *branch = branch_get(argv[0]);
> +		const char *old_upstream = NULL;
> +		int branch_existed = 0;
> +
>  		if (kinds != REF_LOCAL_BRANCH)
>  			die(_("-a and -r options to 'git branch' do not make sense with a branch name"));
> +
> +		if (track == BRANCH_TRACK_OVERRIDE)
> +			fprintf(stderr, _("The --set-upstream flag is deprecated and will be removed. Consider using --track or --set-upstream-to\n"));
> +
> +		/*
> +		 * Save what argv[0] was pointing to so we can give
> +		 * the --set-upstream-to hint
> +		 */
> +		if (branch_has_merge_config(branch))
> +			old_upstream = shorten_unambiguous_ref(branch->merge[0]->dst, 0);
> +
> +		branch_existed = ref_exists(branch->refname);
>  		create_branch(head, argv[0], (argc == 2) ? argv[1] : head,
>  			      force_create, reflog, 0, quiet, track);
> +
> +		if (argc == 1 && track == BRANCH_TRACK_OVERRIDE)
> +			print_set_upstream_warning(argv[0], branch_existed, old_upstream);
> +

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [PATCH 3/3] branch: suggest how to undo a --set-upstream when given one branch
  2012-08-20 18:50   ` Junio C Hamano
@ 2012-08-22  1:26     ` Carlos Martín Nieto
  2012-08-23 18:56     ` [PATCHv2 3/3] branch: deprecate --set-upstream and show help if we detect possible mistaken use Carlos Martín Nieto
  1 sibling, 0 replies; 14+ messages in thread
From: Carlos Martín Nieto @ 2012-08-22  1:26 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: git

On Mon, 2012-08-20 at 11:50 -0700, Junio C Hamano wrote:
> Carlos Martín Nieto <cmn@elego.de> writes:
> 
> > This interface is error prone, and a better one (--set-upstream-to)
> > exists. Suggest how to fix a --set-upstream invocation in case the
> > user only gives one argument, which makes it likely that he meant to
> > do the opposite, like with
> >
> >     git branch --set-upstream origin/master
> >
> > when they meant one of
> >
> >     git branch --set-upstream origin/master master
> >     git branch --set-upstream-to origin/master
> 
> In "git branch --set-upstream $A", if $A did not exist (i.e. we
> ended up creating $A that is forked from the current branch), and if
> refs/remotes/$A exists, I agree it is very likely that the user
> wanted to set the upstream of the current branch to remotes/$A
> instead.
> 
> But I am not sure about other cases.  If $A already existed, it is
> equally likely that "git branch --set-upstream $A" wanted to make
> the existing $A track our current branch, or make our branch track
> that existing $A, isn't it?  We would end up giving unwanted advice
> that is *wrong* for the user's situation 50% of the time, which does
> not sound like an acceptable thing to do.
> 
> So I would really prefer to see the condition this advice is offered
> limited to very specific cases (namely, only one parameter $A was
> given to cause us use "HEAD" as the other branch, refs/heads/$A did
> not exist and refs/remotes/$A did; there may be others but I think
> this is the one we most care about) in which we know the advice is
> correct with certainty.

I'm not sure about the 50%, I've personally never used the one-argument
version (correctly), but the restriction does make sense as
--set-upstream origin/master is the main thing we want to "protect"
against.

> 
> > While we're at it, add a notice that the --set-upstream flag is
> > deprecated and will be removed at some point.
> 
> This part is unquestionably good.
> 
> > Signed-off-by: Carlos Martín Nieto <cmn@elego.de>
> >
> > ---
> >
> > This produces suboptimal output in case that A tracks B and we do
> >
> >     git checkout B
> >     git branch --set-upstream A
> >
> > as it will suggest
> >
> >     git branch --set-upstream A B
> >
> > as a way of undoing what we just did.
> 
> The literal meaning of what the user did was to create B and then
> made A that existed (and used to build upon B) to build upon B,
> which is a no-op.  And undoing can be done with the same command.
> 
> Which is funny.
> 
> I am sure you will get complaint from the real users.  To avoid it,
> you would need to know what the old value was (if any), and skip the
> "undo" part, but I think it is probably worth it.  You already have
> code to learn what the old value was anyway, so the ideal is just a
> single comparison away, no?

Yes, but this comparison is already two or three levels deep in ifs,
which is why I didn't put it in this version, as it was unlikely to be
triggered anyway. As per the next paragraph, this probably won't be an
issue.

> 
> By the way, are you assuming that what the user wanted to do was to
> make B build on top of A (i.e. set branch.B.up to A) in this
> example?  For that setting to make sense, perhaps B should already
> be a descendant of A, shouldn't it?  If that is the case, that is
> another clue you can use in your logic to decide if you want to tell
> the user "you told me to make $A build on the current branch, but I
> think you meant the other way around" with more confidence.

I am pretty much assuming that every use of --set-upstream with a single
argument was meant to use the --set-upstream-to semantics. This
particular examples is an edge-case I found when trying to figure out
the possible combinations, not a sane one I expect to see in the wild.

> 
> > diff --git a/builtin/branch.c b/builtin/branch.c
> > index 08068f7..33641d9 100644
> > --- a/builtin/branch.c
> > +++ b/builtin/branch.c
> > @@ -707,6 +707,21 @@ static int edit_branch_description(const char *branch_name)
> >  	return status;
> >  }
> >  
> > +static void print_set_upstream_warning(const char *branch, int branch_existed, const char *old_upstream)
> > +{
> > +	fprintf(stderr, _("If you wanted to make '%s' track '%s', do this:\n\n"), head, branch);
> 
> I would suggest strongly against using the verb "track" here, as it
> is overused and has caused confusion "is it tracking branch, remote
> tracking branch, do I merge in one but fetch to update the other?".
> 
> Regardless of the verb, I think there should be a line _before_ the
> above to tell the user what the command actually _did_, to make the
> distinction stand out to help the user decide.

I don't like the word either, but that's what 'branch --set-upstream'
and 'checkout -t' use (or anything that ends up calling
install_branch_config()). There is such a message (see below) and that's
how it says it. I worry that changing the wording might be even more
confusing, but I'll play with some wording (unless you want to change
the message in install_branch_config()).

> 
> That is, tell the user "I set upstream for that other branch to the
> current branch, making that other branch build on top of the current
> branch.  If that was a mistake, and what you really wanted to do was
> to make the current branch build on top of the other branch, here
> are the things you need to do to recover".  Without saying anything
> before "If that was a mistake, " part and giving "to do X, do Y"
> would leave the user confused why he is being given a way to do X in
> the first place, whether what he really wanted to do was X or
> something else.

This message is always printed as a result of create_branch() at the
bottom of cmd_branch() and it's printed to stdout. The rest is printed
to stderr and that's what the tests are interested in checking, so you
can't really tell from there. The whole output looks like
        
    % ./bin-wrappers/git branch --set-upstream master
    The --set-upstream flag is deprecated and will be removed. Consider using --track or --set-upstream-to
    Branch master set up to track local branch set-upstream-to.
    If you wanted to make 'set-upstream-to' track 'master', do this:
    
        git branch --set-upstream master set-upstream-to
        git branch --set-upstream-to master

It may need an extra LF after the deprecation warning for readability.

> 
> > +	if (branch_existed) {
> > +		if (old_upstream)
> > +			fprintf(stderr, _("    git branch --set-upstream %s %s\n"), old_upstream, branch);
> > +		else
> > +			fprintf(stderr, _("    git branch --unset-upstream %s\n"), branch);
> > +	} else {
> > +		fprintf(stderr, _("    git branch -d %s\n"), branch);
> > +	}
> > +
> > +	fprintf(stderr, _("    git branch --set-upstream-to %s\n"), branch);
> > +}
> 
> And I suspect the logic to call into this function needs to be
> tightened a lot.  It may even turn out that we may not need messages
> when "branch_existed" is true.

Without that check, this function will be much simpler, yeah. I'll go
for showing the help only for a new branch in the next iteration and
then we can see if we want to check how existing branches relate.

> 
> >  int cmd_branch(int argc, const char **argv, const char *prefix)
> >  {
> >  	int delete = 0, rename = 0, force_create = 0, list = 0;
> > @@ -877,10 +892,30 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
> >  		git_config_set_multivar(buf.buf, NULL, NULL, 1);
> >  		strbuf_release(&buf);
> >  	} else if (argc > 0 && argc <= 2) {
> > +		struct branch *branch = branch_get(argv[0]);
> > +		const char *old_upstream = NULL;
> > +		int branch_existed = 0;
> > +
> >  		if (kinds != REF_LOCAL_BRANCH)
> >  			die(_("-a and -r options to 'git branch' do not make sense with a branch name"));
> > +
> > +		if (track == BRANCH_TRACK_OVERRIDE)
> > +			fprintf(stderr, _("The --set-upstream flag is deprecated and will be removed. Consider using --track or --set-upstream-to\n"));
> > +
> > +		/*
> > +		 * Save what argv[0] was pointing to so we can give
> > +		 * the --set-upstream-to hint
> > +		 */
> > +		if (branch_has_merge_config(branch))
> > +			old_upstream = shorten_unambiguous_ref(branch->merge[0]->dst, 0);
> > +
> > +		branch_existed = ref_exists(branch->refname);
> >  		create_branch(head, argv[0], (argc == 2) ? argv[1] : head,
> >  			      force_create, reflog, 0, quiet, track);
> > +
> > +		if (argc == 1 && track == BRANCH_TRACK_OVERRIDE)
> > +			print_set_upstream_warning(argv[0], branch_existed, old_upstream);
> > +
> 

^ permalink raw reply	[flat|nested] 14+ messages in thread

* [PATCHv2 3/3] branch: deprecate --set-upstream and show help if we detect possible mistaken use
  2012-08-20 18:50   ` Junio C Hamano
  2012-08-22  1:26     ` Carlos Martín Nieto
@ 2012-08-23 18:56     ` Carlos Martín Nieto
  2012-08-23 21:16       ` Junio C Hamano
  1 sibling, 1 reply; 14+ messages in thread
From: Carlos Martín Nieto @ 2012-08-23 18:56 UTC (permalink / raw)
  To: gitster; +Cc: git

This interface is error prone, and a better one (--set-upstream-to)
exists. Add a message listing the alternatives and suggest how to fix
a --set-upstream invocation in case the user only gives one argument
which causes a local branch with the same name as a remote-tracking
one to be created. The typical case is

    git branch --set-upstream origin/master

when the user meant

    git branch --set-upstream master origin/master

assuming that the current branch is master. Show a message telling the
user how to undo their action and get what they wanted. For the
command above, the message would be

The --set-upstream flag is deprecated and will be removed. Consider using --track or --set-upstream-to
Branch origin/master set up to track local branch master.

If you wanted to make 'master' track 'origin/master', do this:

    git branch -d origin/master
    git branch --set-upstream-to origin/master

Signed-off-by: Carlos Martín Nieto <cmn@elego.de>
---

The 'track' in the message is still not great, but it does fit with
the one above. Maybe if we make it say "If youw wanted [...] track the
remote-tracking branch 'origin/master'" it would be clearer?

I've simplified and tightened the logic. Now it will only show the
undo message if the branch didn't exist locally and there is a
remote-tracking branch of the same name.

 builtin/branch.c  | 26 ++++++++++++++++++++++++++
 t/t3200-branch.sh | 34 ++++++++++++++++++++++++++++++++++
 2 files changed, 60 insertions(+)

diff --git a/builtin/branch.c b/builtin/branch.c
index 08068f7..a0302a2 100644
--- a/builtin/branch.c
+++ b/builtin/branch.c
@@ -877,10 +877,36 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
 		git_config_set_multivar(buf.buf, NULL, NULL, 1);
 		strbuf_release(&buf);
 	} else if (argc > 0 && argc <= 2) {
+		struct branch *branch = branch_get(argv[0]);
+		int branch_existed = 0, remote_tracking = 0;
+		struct strbuf buf = STRBUF_INIT;
+
 		if (kinds != REF_LOCAL_BRANCH)
 			die(_("-a and -r options to 'git branch' do not make sense with a branch name"));
+
+		if (track == BRANCH_TRACK_OVERRIDE)
+			fprintf(stderr, _("The --set-upstream flag is deprecated and will be removed. Consider using --track or --set-upstream-to\n"));
+
+		strbuf_addf(&buf, "refs/remotes/%s", branch->name);
+		remote_tracking = ref_exists(buf.buf);
+		strbuf_release(&buf);
+
+		branch_existed = ref_exists(branch->refname);
 		create_branch(head, argv[0], (argc == 2) ? argv[1] : head,
 			      force_create, reflog, 0, quiet, track);
+
+		/*
+		 * We only show the instructions if the user gave us
+		 * one branch which doesn't exist locally, but is the
+		 * name of a remote-tracking branch.
+		 */
+		if (argc == 1 && track == BRANCH_TRACK_OVERRIDE &&
+		    !branch_existed && remote_tracking) {
+			fprintf(stderr, _("\nIf you wanted to make '%s' track '%s', do this:\n\n"), head, branch->name);
+			fprintf(stderr, _("    git branch -d %s\n"), branch->name);
+			fprintf(stderr, _("    git branch --set-upstream-to %s\n"), branch->name);
+		}
+
 	} else
 		usage_with_options(builtin_branch_usage, options);
 
diff --git a/t/t3200-branch.sh b/t/t3200-branch.sh
index 93e5d6e..e9e11cf 100755
--- a/t/t3200-branch.sh
+++ b/t/t3200-branch.sh
@@ -399,6 +399,40 @@ test_expect_success 'test --unset-upstream on a particular branch' \
      test_must_fail git config branch.my14.remote &&
      test_must_fail git config branch.my14.merge'
 
+test_expect_success '--set-upstream shows message when creating a new branch that exists as remote-tracking' \
+    'git update-ref refs/remotes/origin/master HEAD &&
+     git branch --set-upstream origin/master 2>actual &&
+     test_when_finished git update-ref -d refs/remotes/origin/master &&
+     test_when_finished git branch -d origin/master &&
+     cat >expected <<EOF &&
+The --set-upstream flag is deprecated and will be removed. Consider using --track or --set-upstream-to
+
+If you wanted to make '"'master'"' track '"'origin/master'"', do this:
+
+    git branch -d origin/master
+    git branch --set-upstream-to origin/master
+EOF
+     test_cmp expected actual
+'
+
+test_expect_success '--set-upstream with two args only shows the deprecation message' \
+    'git branch --set-upstream master my13 2>actual &&
+     test_when_finished git branch --unset-upstream master &&
+     cat >expected <<EOF &&
+The --set-upstream flag is deprecated and will be removed. Consider using --track or --set-upstream-to
+EOF
+     test_cmp expected actual
+'
+
+test_expect_success '--set-upstream with one arg only shows the deprecation message if the branch existed' \
+    'git branch --set-upstream my13 2>actual &&
+     test_when_finished git branch --unset-upstream master &&
+     cat >expected <<EOF &&
+The --set-upstream flag is deprecated and will be removed. Consider using --track or --set-upstream-to
+EOF
+     test_cmp expected actual
+'
+
 # 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
-- 
1.7.12

^ permalink raw reply related	[flat|nested] 14+ messages in thread

* Re: [PATCHv2 3/3] branch: deprecate --set-upstream and show help if we detect possible mistaken use
  2012-08-23 18:56     ` [PATCHv2 3/3] branch: deprecate --set-upstream and show help if we detect possible mistaken use Carlos Martín Nieto
@ 2012-08-23 21:16       ` Junio C Hamano
  0 siblings, 0 replies; 14+ messages in thread
From: Junio C Hamano @ 2012-08-23 21:16 UTC (permalink / raw)
  To: Carlos Martín Nieto; +Cc: git

Carlos Martín Nieto <cmn@elego.de> writes:

> The 'track' in the message is still not great, but it does fit with
> the one above. Maybe if we make it say "If youw wanted [...] track the
> remote-tracking branch 'origin/master'" it would be clearer?

The verb "track" in the phrase "remote-tracking branch" means "keep
track of the branch at the remote, by storing a copy of the last
observed state of it".  In the same sentence, the verb "track"
elsewhere is used to describe what the branch B whose upstream is
set to B@{upstream} does against B@{upstream}, but that is not
"keeping a copy"---it is doing an entirely different thing.

If we say that the branch B whose upstream is set to B@{upstream} is
"building on top of B@{upstream}", "integrating with B@{upstream}",
"forked from B@{upstream}", etc., without using the verb "track"
that already means something else (i.e. keeps track of the copy of
last observed state), it would reduce the confusion, but I do not
think it would clarify anything if the verb "track" is used for
that.

As usual, because I am not the best source of phrasing, others may
find a better verb than "builds", "forks", or "integrates", though.

> I've simplified and tightened the logic. Now it will only show the
> undo message if the branch didn't exist locally and there is a
> remote-tracking branch of the same name.

The updated and simplified logic reads quite straight-forward, and
looks good.  It is likely that the message will be reworded and also
localized in the future, so it would make sense to use test_i18ncmp
from the day one, though.

Thanks.  Will queue.

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [PATCH 2/3] branch: add --unset-upstream option
  2012-08-20 13:47 ` [PATCH 2/3] branch: add --unset-upstream option Carlos Martín Nieto
@ 2012-08-23 21:20   ` Junio C Hamano
  2012-08-27 17:30     ` Carlos Martín Nieto
  0 siblings, 1 reply; 14+ messages in thread
From: Junio C Hamano @ 2012-08-23 21:20 UTC (permalink / raw)
  To: Carlos Martín Nieto; +Cc: git

Carlos Martín Nieto <cmn@elego.de> writes:

> diff --git a/t/t3200-branch.sh b/t/t3200-branch.sh
> index e9019ac..93e5d6e 100755
> --- a/t/t3200-branch.sh
> +++ b/t/t3200-branch.sh
> @@ -383,6 +383,22 @@ 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 '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'
> +
> +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'
> +

What should happen when you say "--unset-upstream" on a branch B
that does not have B@{upstream}?  Should it fail?  Should it be
silently ignored?  Is it undefined that we do not want to test?

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [PATCH 2/3] branch: add --unset-upstream option
  2012-08-23 21:20   ` Junio C Hamano
@ 2012-08-27 17:30     ` Carlos Martín Nieto
  2012-08-27 18:01       ` Junio C Hamano
  0 siblings, 1 reply; 14+ messages in thread
From: Carlos Martín Nieto @ 2012-08-27 17:30 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: git

Junio C Hamano <gitster@pobox.com> writes:

> Carlos Martín Nieto <cmn@elego.de> writes:
>
>> diff --git a/t/t3200-branch.sh b/t/t3200-branch.sh
>> index e9019ac..93e5d6e 100755
>> --- a/t/t3200-branch.sh
>> +++ b/t/t3200-branch.sh
>> @@ -383,6 +383,22 @@ 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 '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'
>> +
>> +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'
>> +
>
> What should happen when you say "--unset-upstream" on a branch B
> that does not have B@{upstream}?  Should it fail?  Should it be
> silently ignored?  Is it undefined that we do not want to test?

I'd say it should be ignored, as the end result we want is for there to
be no upstream information. What we do underneath is remove a couple of
config options, wich doesn't fail if they didn't insist in the first
place.

Would you like a test that makes sure two --unset-upstream in a row
don't cause any errors?

   cmn

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [PATCH 2/3] branch: add --unset-upstream option
  2012-08-27 17:30     ` Carlos Martín Nieto
@ 2012-08-27 18:01       ` Junio C Hamano
  2012-08-27 18:14         ` Junio C Hamano
  0 siblings, 1 reply; 14+ messages in thread
From: Junio C Hamano @ 2012-08-27 18:01 UTC (permalink / raw)
  To: Carlos Martín Nieto; +Cc: git

cmn@elego.de (Carlos Martín Nieto) writes:

> Junio C Hamano <gitster@pobox.com> writes:
>
>> Carlos Martín Nieto <cmn@elego.de> writes:
>>
>>> diff --git a/t/t3200-branch.sh b/t/t3200-branch.sh
>>> index e9019ac..93e5d6e 100755
>>> --- a/t/t3200-branch.sh
>>> +++ b/t/t3200-branch.sh
>>> @@ -383,6 +383,22 @@ 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 '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'
>>> +
>>> +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'
>>> +
>>
>> What should happen when you say "--unset-upstream" on a branch B
>> that does not have B@{upstream}?  Should it fail?  Should it be
>> silently ignored?  Is it undefined that we do not want to test?
>
> I'd say it should be ignored, as the end result we want is for there to
> be no upstream information. What we do underneath is remove a couple of
> config options, wich doesn't fail if they didn't insist in the first
> place.

I am not sure about that reasoning.

    $ git config --unset core.nosuchvariable ; echo $?
    5

looks like a failure to me.

More importantly, wouldn't we want to catch typo in

	git branch --unset-upstream mext

which admittedly should come from a different codepath (I do not
think your patch checks if "mext" branch exists before going ahead
to the config--unset dance)?

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [PATCH 2/3] branch: add --unset-upstream option
  2012-08-27 18:01       ` Junio C Hamano
@ 2012-08-27 18:14         ` Junio C Hamano
  2012-08-27 21:33           ` Carlos Martín Nieto
  0 siblings, 1 reply; 14+ messages in thread
From: Junio C Hamano @ 2012-08-27 18:14 UTC (permalink / raw)
  To: Carlos Martín Nieto; +Cc: git

Junio C Hamano <gitster@pobox.com> writes:

> cmn@elego.de (Carlos Martín Nieto) writes:
>
>> Junio C Hamano <gitster@pobox.com> writes:
>>
>>> Carlos Martín Nieto <cmn@elego.de> writes:
>>>
>>>> diff --git a/t/t3200-branch.sh b/t/t3200-branch.sh
>>>> index e9019ac..93e5d6e 100755
>>>> --- a/t/t3200-branch.sh
>>>> +++ b/t/t3200-branch.sh
>>>> @@ -383,6 +383,22 @@ 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 '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'
>>>> +
>>>> +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'
>>>> +
>>>
>>> What should happen when you say "--unset-upstream" on a branch B
>>> that does not have B@{upstream}?  Should it fail?  Should it be
>>> silently ignored?  Is it undefined that we do not want to test?
>>
>> I'd say it should be ignored, as the end result we want is for there to
>> be no upstream information. What we do underneath is remove a couple of
>> config options, wich doesn't fail if they didn't insist in the first
>> place.
>
> I am not sure about that reasoning.
>
>     $ git config --unset core.nosuchvariable ; echo $?
>     5
>
> looks like a failure to me.
>
> More importantly, wouldn't we want to catch typo in
>
> 	git branch --unset-upstream mext
>
> which admittedly should come from a different codepath (I do not
> think your patch checks if "mext" branch exists before going ahead
> to the config--unset dance)?

Sorry, the last paragraph was incomplete.

In general, we should detect errors and allow the user to choose to
ignore.

A script that wants to make sure that B does not have an upstream,
without knowing if it already has one, can say "--unset-upstream B"
and choose to ignore the error if B does not have an upstream.  

If the script is carefully written, it would try to check if B has
one and call "--unset-upstream B" only when it doesn't.  A casually
written loose script would say "--unset-upstream B 2>/dev/null"
and keep going (it would not notice other kinds of errors, but that
is what makes it "casual and loose").

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [PATCH 2/3] branch: add --unset-upstream option
  2012-08-27 18:14         ` Junio C Hamano
@ 2012-08-27 21:33           ` Carlos Martín Nieto
  0 siblings, 0 replies; 14+ messages in thread
From: Carlos Martín Nieto @ 2012-08-27 21:33 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: git

Junio C Hamano <gitster@pobox.com> writes:

> Junio C Hamano <gitster@pobox.com> writes:
>
>> cmn@elego.de (Carlos Martín Nieto) writes:
>>
>>> Junio C Hamano <gitster@pobox.com> writes:
>>>
>>>> Carlos Martín Nieto <cmn@elego.de> writes:
>>>>
>>>>> diff --git a/t/t3200-branch.sh b/t/t3200-branch.sh
>>>>> index e9019ac..93e5d6e 100755
>>>>> --- a/t/t3200-branch.sh
>>>>> +++ b/t/t3200-branch.sh
>>>>> @@ -383,6 +383,22 @@ 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 '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'
>>>>> +
>>>>> +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'
>>>>> +
>>>>
>>>> What should happen when you say "--unset-upstream" on a branch B
>>>> that does not have B@{upstream}?  Should it fail?  Should it be
>>>> silently ignored?  Is it undefined that we do not want to test?
>>>
>>> I'd say it should be ignored, as the end result we want is for there to
>>> be no upstream information. What we do underneath is remove a couple of
>>> config options, wich doesn't fail if they didn't insist in the first
>>> place.
>>
>> I am not sure about that reasoning.
>>
>>     $ git config --unset core.nosuchvariable ; echo $?
>>     5
>>
>> looks like a failure to me.
>>
>> More importantly, wouldn't we want to catch typo in
>>
>> 	git branch --unset-upstream mext
>>
>> which admittedly should come from a different codepath (I do not
>> think your patch checks if "mext" branch exists before going ahead
>> to the config--unset dance)?
>
> Sorry, the last paragraph was incomplete.
>
> In general, we should detect errors and allow the user to choose to
> ignore.
>
> A script that wants to make sure that B does not have an upstream,
> without knowing if it already has one, can say "--unset-upstream B"
> and choose to ignore the error if B does not have an upstream.  
>
> If the script is carefully written, it would try to check if B has
> one and call "--unset-upstream B" only when it doesn't.  A casually
> written loose script would say "--unset-upstream B 2>/dev/null"
> and keep going (it would not notice other kinds of errors, but that
> is what makes it "casual and loose").

OK, that's a good point, I'll update the patch.

   cmn

^ permalink raw reply	[flat|nested] 14+ messages in thread

* [PATCH 2/3] branch: add --unset-upstream option
  2012-08-30 17:23 [PATCHv2 0/3] Improve branch UI for setting upstream information Carlos Martín Nieto
@ 2012-08-30 17:23 ` Carlos Martín Nieto
  0 siblings, 0 replies; 14+ messages in thread
From: Carlos Martín Nieto @ 2012-08-30 17:23 UTC (permalink / raw)
  To: git; +Cc: gitster

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>
---
 Documentation/git-branch.txt |  5 +++++
 builtin/branch.c             | 21 ++++++++++++++++++---
 t/t3200-branch.sh            | 19 +++++++++++++++++++
 3 files changed, 42 insertions(+), 3 deletions(-)

diff --git a/Documentation/git-branch.txt b/Documentation/git-branch.txt
index e41c4b5..9c1d2f1 100644
--- a/Documentation/git-branch.txt
+++ b/Documentation/git-branch.txt
@@ -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>]
@@ -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`).
diff --git a/builtin/branch.c b/builtin/branch.c
index 3c978eb..557995d 100644
--- a/builtin/branch.c
+++ b/builtin/branch.c
@@ -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;
@@ -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),
@@ -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)
@@ -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"));
diff --git a/t/t3200-branch.sh b/t/t3200-branch.sh
index e9019ac..1018e8b 100755
--- a/t/t3200-branch.sh
+++ b/t/t3200-branch.sh
@@ -383,6 +383,25 @@ 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'
+
+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
-- 
1.7.12.3.g0dd8ef6

^ permalink raw reply related	[flat|nested] 14+ messages in thread

end of thread, other threads:[~2012-08-30 17:23 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-08-20 13:47 [PATCH 0/3] Improve branch UI for setting upstream information Carlos Martín Nieto
2012-08-20 13:47 ` [PATCH 1/3] branch: introduce --set-upstream-to Carlos Martín Nieto
2012-08-20 13:47 ` [PATCH 2/3] branch: add --unset-upstream option Carlos Martín Nieto
2012-08-23 21:20   ` Junio C Hamano
2012-08-27 17:30     ` Carlos Martín Nieto
2012-08-27 18:01       ` Junio C Hamano
2012-08-27 18:14         ` Junio C Hamano
2012-08-27 21:33           ` Carlos Martín Nieto
2012-08-20 13:47 ` [PATCH 3/3] branch: suggest how to undo a --set-upstream when given one branch Carlos Martín Nieto
2012-08-20 18:50   ` Junio C Hamano
2012-08-22  1:26     ` Carlos Martín Nieto
2012-08-23 18:56     ` [PATCHv2 3/3] branch: deprecate --set-upstream and show help if we detect possible mistaken use Carlos Martín Nieto
2012-08-23 21:16       ` Junio C Hamano
2012-08-30 17:23 [PATCHv2 0/3] Improve branch UI for setting upstream information Carlos Martín Nieto
2012-08-30 17:23 ` [PATCH 2/3] branch: add --unset-upstream option Carlos Martín Nieto

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.