# [PATCH] branch: optionally setup branch.*.merge from upstream local branches

4 messages from 2008-02-18 to 2008-02-18. Participants: Jay Soffian, Johannes Schindelin.
Thread: https://gitlist.dev/t/12174

## Jay Soffian, 2008-02-18 12:04

Subject: [PATCH] branch: optionally setup branch.*.merge from upstream local branches
Message-ID: <1203336248-9308-1-git-send-email-jaysoffian@gmail.com>
URL: https://gitlist.dev/e/1203336248-9308-1-git-send-email-jaysoffian%40gmail.com

```
git branch / git checkout -b now honor --track when the upstream branch
is local. Previously --track was silently ignored for local upstream
branches.

The configuration setting branch.autosetupmerge can now be set to
"always" which is equivalent to using --track. Setting
branch.autosetupmerge to boolean true will retains the former behavior
of only setting up branch.*.merge for remote upstream branches.

Signed-off-by: Jay Soffian <jaysoffian@gmail.com>
---
On Feb 16, 2008 6:45 AM, Johannes Schindelin <Johannes.Schindelin@gmx.de> wrote:
> Hi,
> 
> On Fri, 15 Feb 2008, Junio C Hamano wrote:
> 
> > This area was last cleaned up in 6f084a5 (branch --track: code cleanup
> > and saner handling of local branches).  I do not know if the original
> > intention of the code was to allow a hack like this to work, or it is
> > just an unintended accident that it happens to work.  Dscho, any ideas?
> 
> AFAIR the problem was that you were (rightfully) annoyed when you were
> setting up your local branches, and all of a sudden, they were set up with
> loads of tracking information, cluttering your config.
> 
> Basically, that is the reason why we disallowed tracking information to be
> set up for local branching.

Hopefully this is an acceptable compromise? Junio suggested making this 
implicit:

   [remote "."]
      fetch = refs/*:refs/*

But I thought that might affect other areas, and I don't think this is too bad.

 branch.c           |   12 ++++++++++--
 branch.h           |    8 +++++++-
 builtin-branch.c   |   11 ++++++++---
 builtin-checkout.c |   15 ++++++++++-----
 4 files changed, 35 insertions(+), 11 deletions(-)

diff --git a/branch.c b/branch.c
index 1fc8788..24e98dd 100644
--- a/branch.c
+++ b/branch.c
@@ -71,7 +71,7 @@ static int setup_tracking(const char *new_ref, const char *orig_ref)
 
 void create_branch(const char *head,
 		   const char *name, const char *start_name,
-		   int force, int reflog, int track)
+		   int force, int reflog, enum branch_track track)
 {
 	struct ref_lock *lock;
 	struct commit *commit;
@@ -130,7 +130,15 @@ void create_branch(const char *head,
 	   automatically merges from there.  So far, this is only done for
 	   remotes registered via .git/config.  */
 	if (real_ref && track)
-		setup_tracking(name, real_ref);
+		if (setup_tracking(name, real_ref) == 1 && track == BRANCH_TRACK_ALWAYS) {
+			char key[1024];
+			sprintf(key, "branch.%s.remote", name);
+			git_config_set(key, ".");
+			sprintf(key, "branch.%s.merge", name);
+			git_config_set(key, real_ref);
+			printf("Branch %s set up with upstream branch %s.\n",
+			       name, real_ref);
+		}
 
 	if (write_ref_sha1(lock, sha1, msg) < 0)
 		die("Failed to write ref: %s.", strerror(errno));
diff --git a/branch.h b/branch.h
index d30abe0..771d6ce 100644
--- a/branch.h
+++ b/branch.h
@@ -1,6 +1,12 @@
 #ifndef BRANCH_H
 #define BRANCH_H
 
+enum branch_track {
+	BRANCH_TRACK_FALSE = 0,
+	BRANCH_TRACK_TRUE = 1,
+	BRANCH_TRACK_ALWAYS = 2,
+};
+
 /* Functions for acting on the information about branches. */
 
 /*
@@ -13,7 +19,7 @@
  * branch for (if any).
  */
 void create_branch(const char *head, const char *name, const char *start_name,
-		   int force, int reflog, int track);
+		   int force, int reflog, enum branch_track track);
 
 /*
  * Remove information about the state of working on the current
diff --git a/builtin-branch.c b/builtin-branch.c
index 5094e0d..231bff4 100644
--- a/builtin-branch.c
+++ b/builtin-branch.c
@@ -30,7 +30,7 @@ static const char * const builtin_branch_usage[] = {
 static const char *head;
 static unsigned char head_sha1[20];
 
-static int branch_track = 1;
+static enum branch_track branch_track = BRANCH_TRACK_FALSE;
 
 static int branch_use_color;
 static char branch_colors[][COLOR_MAXLEN] = {
@@ -77,6 +77,10 @@ static int git_branch_config(const char *var, const char *value)
 		return 0;
 	}
 	if (!strcmp(var, "branch.autosetupmerge")) {
+		if (value && !strcasecmp(value, "always")) {
+			branch_track = BRANCH_TRACK_ALWAYS;
+			return 0;
+		}
 		branch_track = git_config_bool(var, value);
 		return 0;
 	}
@@ -420,14 +424,15 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
 {
 	int delete = 0, rename = 0, force_create = 0;
 	int verbose = 0, abbrev = DEFAULT_ABBREV, detached = 0;
-	int reflog = 0, track;
+	int reflog = 0;
+	enum branch_track track;
 	int kinds = REF_LOCAL_BRANCH;
 	struct commit_list *with_commit = NULL;
 
 	struct option options[] = {
 		OPT_GROUP("Generic options"),
 		OPT__VERBOSE(&verbose),
-		OPT_BOOLEAN( 0 , "track",  &track, "set up tracking mode (see git-pull(1))"),
+		OPT_SET_INT( 0 , "track",  &track, "set up tracking mode (see git-pull(1))", BRANCH_TRACK_ALWAYS),
 		OPT_BOOLEAN( 0 , "color",  &branch_use_color, "use colored output"),
 		OPT_SET_INT('r', NULL,     &kinds, "act on remote-tracking branches",
 			REF_REMOTE_BRANCH),
diff --git a/builtin-checkout.c b/builtin-checkout.c
index 0d19835..d77ee9c 100644
--- a/builtin-checkout.c
+++ b/builtin-checkout.c
@@ -186,7 +186,7 @@ struct checkout_opts {
 
 	char *new_branch;
 	int new_branch_log;
-	int track;
+	enum branch_track track;
 };
 
 struct branch_info {
@@ -521,13 +521,18 @@ static int switch_branches(struct checkout_opts *opts,
 	return post_checkout_hook(old.commit, new->commit, 1);
 }
 
-static int branch_track = 0;
+static enum branch_track branch_track = BRANCH_TRACK_FALSE;
 
 static int git_checkout_config(const char *var, const char *value)
 {
-	if (!strcmp(var, "branch.autosetupmerge"))
+	if (!strcmp(var, "branch.autosetupmerge")) {
+		if (value && !strcasecmp(value, "always")) {
+			branch_track = BRANCH_TRACK_ALWAYS;
+			return 0;
+		}
 		branch_track = git_config_bool(var, value);
-
+		return 0;
+	}
 	return git_default_config(var, value);
 }
 
@@ -542,7 +547,7 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
 		OPT__QUIET(&opts.quiet),
 		OPT_STRING('b', NULL, &opts.new_branch, "new branch", "branch"),
 		OPT_BOOLEAN('l', NULL, &opts.new_branch_log, "log for new branch"),
-		OPT_BOOLEAN( 0 , "track", &opts.track, "track"),
+		OPT_SET_INT( 0 , "track",  &opts.track, "track", BRANCH_TRACK_ALWAYS),
 		OPT_BOOLEAN('f', NULL, &opts.force, "force"),
 		OPT_BOOLEAN('m', NULL, &opts.merge, "merge"),
 		OPT_END(),
-- 
1.5.4.2.183.g9fe5b

```

## Johannes Schindelin, 2008-02-18 12:14

Subject: Re: [PATCH] branch: optionally setup branch.*.merge from upstream local branches
Message-ID: <alpine.LSU.1.00.0802181209210.30505@racer.site>
URL: https://gitlist.dev/e/alpine.LSU.1.00.0802181209210.30505%40racer.site
In-Reply-To: <1203336248-9308-1-git-send-email-jaysoffian@gmail.com>

```
Hi,

On Mon, 18 Feb 2008, Jay Soffian wrote:

> diff --git a/branch.h b/branch.h
> index d30abe0..771d6ce 100644
> --- a/branch.h
> +++ b/branch.h
> @@ -1,6 +1,12 @@
>  #ifndef BRANCH_H
>  #define BRANCH_H
>  
> +enum branch_track {
> +	BRANCH_TRACK_FALSE = 0,
> +	BRANCH_TRACK_TRUE = 1,

BRANCH_TRACK_REMOTES would be a better name here.  And you do not need the 
"= 1" and "= 2".

> +	BRANCH_TRACK_ALWAYS = 2,
> +};
> +
>  /* Functions for acting on the information about branches. */
>  
>  /*
> diff --git a/builtin-branch.c b/builtin-branch.c
> index 5094e0d..231bff4 100644
> --- a/builtin-branch.c
> +++ b/builtin-branch.c
> @@ -30,7 +30,7 @@ static const char * const builtin_branch_usage[] = {
>  static const char *head;
>  static unsigned char head_sha1[20];
>  
> -static int branch_track = 1;
> +static enum branch_track branch_track = BRANCH_TRACK_FALSE;

That is a clear regression.

> @@ -77,6 +77,10 @@ static int git_branch_config(const char *var, const char *value)
>  		return 0;
>  	}
>  	if (!strcmp(var, "branch.autosetupmerge")) {
> +		if (value && !strcasecmp(value, "always")) {
> +			branch_track = BRANCH_TRACK_ALWAYS;
> +			return 0;
> +		}
>  		branch_track = git_config_bool(var, value);
>  		return 0;
>  	}

You have this in builtin-branch.c and builtin-checkout.c.  Duplicated 
code.  IMHO it is time to move this into the git_default_config() function 
(with "branch_track" being renamed to "git_branch_track", and moved to 
environment.c).

Personally, I have no problem with typing "git merge <branch>" in your 
workflow.  I would even avoid saying "git pull" for obviously-local 
branches, because I would have forgotten which branch it tracked 
originally.

But hey, if you really want to... ;-)

Ciao,
Dscho

```

## Jay Soffian, 2008-02-18 12:40

Subject: Re: [PATCH] branch: optionally setup branch.*.merge from upstream local branches
Message-ID: <76718490802180440o3041b1ffh4764199e2aeaa52b@mail.gmail.com>
URL: https://gitlist.dev/e/76718490802180440o3041b1ffh4764199e2aeaa52b%40mail.gmail.com
In-Reply-To: <alpine.LSU.1.00.0802181209210.30505@racer.site>

```
On Feb 18, 2008 7:14 AM, Johannes Schindelin <Johannes.Schindelin@gmx.de> wrote:

> On Mon, 18 Feb 2008, Jay Soffian wrote:
>
> > +enum branch_track {
> > +     BRANCH_TRACK_FALSE = 0,
> > +     BRANCH_TRACK_TRUE = 1,
>
> BRANCH_TRACK_REMOTES would be a better name here.  And you do not need the
> "= 1" and "= 2".

I was just following along. Plenty of enum examples in the current code
do this. Am I missing something subtle about when assigning explicit
values should be done?

> >
> > -static int branch_track = 1;
> > +static enum branch_track branch_track = BRANCH_TRACK_FALSE;
>
> That is a clear regression.

Perhaps. It's consistent with builtin-checkout.c though (which was
initializing it to 0). Who to believe?

> > @@ -77,6 +77,10 @@ static int git_branch_config(const char *var, const char *value)
> >               return 0;
> >       }
> >       if (!strcmp(var, "branch.autosetupmerge")) {
> > +             if (value && !strcasecmp(value, "always")) {
> > +                     branch_track = BRANCH_TRACK_ALWAYS;
> > +                     return 0;
> > +             }
> >               branch_track = git_config_bool(var, value);
> >               return 0;
> >       }
>
> You have this in builtin-branch.c and builtin-checkout.c.  Duplicated
> code.  IMHO it is time to move this into the git_default_config() function
> (with "branch_track" being renamed to "git_branch_track", and moved to
> environment.c).

Mkay, builtin-checkout.c author didn't do it, so...

> Personally, I have no problem with typing "git merge <branch>" in your
> workflow.  I would even avoid saying "git pull" for obviously-local
> branches, because I would have forgotten which branch it tracked
> originally.

Um, well, apply this patch, set branch.autosetupmerge=always and then
branch.*.merge will tell you which branch it tracked originally. :-)

Aside, then how do you figure out the upstream branch is if you've
forgotten?

j.

```

## Johannes Schindelin, 2008-02-18 13:24

Subject: Re: [PATCH] branch: optionally setup branch.*.merge from upstream local branches
Message-ID: <alpine.LSU.1.00.0802181320200.30505@racer.site>
URL: https://gitlist.dev/e/alpine.LSU.1.00.0802181320200.30505%40racer.site
In-Reply-To: <76718490802180440o3041b1ffh4764199e2aeaa52b@mail.gmail.com>

```
Hi,

On Mon, 18 Feb 2008, Jay Soffian wrote:

> On Feb 18, 2008 7:14 AM, Johannes Schindelin <Johannes.Schindelin@gmx.de> wrote:
> 
> > On Mon, 18 Feb 2008, Jay Soffian wrote:
> >
> > > -static int branch_track = 1;
> > > +static enum branch_track branch_track = BRANCH_TRACK_FALSE;
> >
> > That is a clear regression.
> 
> Perhaps. It's consistent with builtin-checkout.c though (which was 
> initializing it to 0). Who to believe?

Well, in the way _you_ implemented it, it is a clear regression, since 
_your_ patch changes the 1 to a 0.

> > Personally, I have no problem with typing "git merge <branch>" in your 
> > workflow.  I would even avoid saying "git pull" for obviously-local 
> > branches, because I would have forgotten which branch it tracked 
> > originally.
> 
> Um, well, apply this patch, set branch.autosetupmerge=always and then 
> branch.*.merge will tell you which branch it tracked originally. :-)

That's what I meant.  Rather than _looking it up_, and then saying "git 
pull", I'd do "git merge <branch>" _directly_.  One command.  Not two.

> Aside, then how do you figure out the upstream branch is if you've 
> forgotten?

Well, that's easy.  I would look at the output of "git show-branch --all".

But then, I usually do not care too much which branch I branched off, but 
which branch to merge with/rebase on.

Ciao,
Dscho

```
