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

5 messages from 2008-02-18 to 2008-02-18. Participants: Jay Soffian, Johannes Schindelin, Junio C Hamano.
Thread: https://gitlist.dev/t/12177

## Jay Soffian, 2008-02-18 13:53

Subject: [PATCH] branch: optionally setup branch.*.merge from upstream local branches
Message-ID: <1203342817-19653-1-git-send-email-jaysoffian@gmail.com>
URL: https://gitlist.dev/e/1203342817-19653-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>
---
 Incorporated Johannes' second round of feedback. This should be all for awhile.

 Documentation/config.txt       |   13 ++++++++-----
 Documentation/git-branch.txt   |   24 ++++++++++++------------
 Documentation/git-checkout.txt |   25 ++++++++++++-------------
 branch.c                       |   19 ++++++++++++-------
 branch.h                       |    2 +-
 builtin-branch.c               |   13 ++++---------
 builtin-checkout.c             |   13 ++++---------
 cache.h                        |    8 ++++++++
 config.c                       |    8 ++++++++
 environment.c                  |    1 +
 10 files changed, 70 insertions(+), 56 deletions(-)

diff --git a/Documentation/config.txt b/Documentation/config.txt
index fb6dae0..f15fe0a 100644
--- a/Documentation/config.txt
+++ b/Documentation/config.txt
@@ -378,11 +378,14 @@ apply.whitespace::
 	as the '--whitespace' option. See linkgit:git-apply[1].
 
 branch.autosetupmerge::
-	Tells `git-branch` and `git-checkout` to setup new branches
-	so that linkgit:git-pull[1] will appropriately merge from that
-	remote branch.  Note that even if this option is not set,
-	this behavior can be chosen per-branch using the `--track`
-	and `--no-track` options.  This option defaults to true.
+	Tells `git-branch` and `git-checkout` to setup `branch.*.remote`
+	and `branch.*.merge` for new branches so that linkgit:git-pull[1]
+	will appropriately merge from that upstream branch. Note that even
+	if this option is not set, this behavior can be chosen per-branch
+	using the `--track`	and `--no-track` options. This option defaults
+	to true, which will setup tracking for remote branches. Set it to
+	`always` to automatically setup the aforementioned options for local
+	upstream branches as well.
 
 branch.<name>.remote::
 	When in branch <name>, it tells `git fetch` which remote to fetch.
diff --git a/Documentation/git-branch.txt b/Documentation/git-branch.txt
index 7e8874a..ce2fc64 100644
--- a/Documentation/git-branch.txt
+++ b/Documentation/git-branch.txt
@@ -105,20 +105,20 @@ OPTIONS
 	Display the full sha1s in output listing rather than abbreviating them.
 
 --track::
-	Set up configuration so that git-pull will automatically
-	retrieve data from the remote branch.  Use this if you always
-	pull from the same remote branch into the new branch, or if you
-	don't want to use "git pull <repository> <refspec>" explicitly.
-	This behavior is the default.  Set the
-	branch.autosetupmerge configuration variable to false if you
-	want git-checkout and git-branch to always behave as if
-	'--no-track' were given.
+	Set up configuration so that git-pull will
+	automatically retrieve data from the upstream branch.  Use this if
+	you always pull from the same upstream branch into the new branch,
+	or if you don't want to use "git pull <repository> <refspec>"
+	explicitly. This behavior is the default for remote branches.
+	Set the branch.autosetupmerge configuration variable to false if you
+	want git-checkout and git-branch to always behave as if '--no-track'
+	were given. Set it to 'always' if you want git-checkout and git-branch
+	to always behave as if '--track' were given.
 
 --no-track::
-	When a branch is created off a remote branch,
-	set up configuration so that git-pull will not retrieve data
-	from the remote branch, ignoring the branch.autosetupmerge
-	configuration variable.
+	Ignore the branch.autosetupmerge configuration variable. When
+	using git pull with the new branch, a <refspec> will have to be
+	given explicitly.
 
 <branchname>::
 	The name of the branch to create or delete.
diff --git a/Documentation/git-checkout.txt b/Documentation/git-checkout.txt
index b4cfa04..ad2ab51 100644
--- a/Documentation/git-checkout.txt
+++ b/Documentation/git-checkout.txt
@@ -48,21 +48,20 @@ OPTIONS
 	may restrict the characters allowed in a branch name.
 
 --track::
-	When -b is given and a branch is created off a remote branch,
-	set up configuration so that git-pull will automatically
-	retrieve data from the remote branch.  Use this if you always
-	pull from the same remote branch into the new branch, or if you
-	don't want to use "git pull <repository> <refspec>" explicitly.
-	This behavior is the default.  Set the
-	branch.autosetupmerge configuration variable to false if you
-	want git-checkout and git-branch to always behave as if
-	'--no-track' were given.
+	When -b is given set up configuration so that git-pull will
+	automatically retrieve data from the upstream branch.  Use this if
+	you always pull from the same upstream branch into the new branch,
+	or if you don't want to use "git pull <repository> <refspec>"
+	explicitly. This behavior is the default for remote branches.
+	Set the branch.autosetupmerge configuration variable to false if you
+	want git-checkout and git-branch to always behave as if '--no-track'
+	were given. Set it to 'always' if you want git-checkout and git-branch
+	to always behave as if '--track' were given.
 
 --no-track::
-	When -b is given and a branch is created off a remote branch,
-	set up configuration so that git-pull will not retrieve data
-	from the remote branch, ignoring the branch.autosetupmerge
-	configuration variable.
+	Ignore the branch.autosetupmerge configuration variable. When
+	using git pull with the new branch, a <refspec> will have to be
+	given explicitly.
 
 -l::
 	Create the new branch's reflog.  This activates recording of
diff --git a/branch.c b/branch.c
index 1fc8788..64f0a4a 100644
--- a/branch.c
+++ b/branch.c
@@ -37,7 +37,7 @@ static int find_tracked_branch(struct remote *remote, void *priv)
  * to infer the settings for branch.<new_ref>.{remote,merge} from the
  * config.
  */
-static int setup_tracking(const char *new_ref, const char *orig_ref)
+static int setup_tracking(const char *new_ref, const char *orig_ref, int always)
 {
 	char key[1024];
 	struct tracking tracking;
@@ -49,8 +49,12 @@ static int setup_tracking(const char *new_ref, const char *orig_ref)
 	memset(&tracking, 0, sizeof(tracking));
 	tracking.spec.dst = (char *)orig_ref;
 	if (for_each_remote(find_tracked_branch, &tracking) ||
-			!tracking.matches)
-		return 1;
+			!tracking.matches) {
+		if (!always)
+			return 1;
+		tracking.matches = 1;
+		tracking.src = xstrdup(orig_ref);
+	}
 
 	if (tracking.matches > 1)
 		return error("Not tracking: ambiguous information for ref %s",
@@ -62,8 +66,9 @@ static int setup_tracking(const char *new_ref, const char *orig_ref)
 		sprintf(key, "branch.%s.merge", new_ref);
 		git_config_set(key, tracking.src);
 		free(tracking.src);
-		printf("Branch %s set up to track remote branch %s.\n",
-			       new_ref, orig_ref);
+		printf("Branch %s set up to track %s branch %s.\n",
+			new_ref, tracking.remote ? "remote" : "local", 
+			orig_ref);
 	}
 
 	return 0;
@@ -71,7 +76,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 +135,7 @@ 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);
+		setup_tracking(name, real_ref, (track == BRANCH_TRACK_ALWAYS));
 
 	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..9f0c2a2 100644
--- a/branch.h
+++ b/branch.h
@@ -13,7 +13,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 7e99103..3b53bb7 100644
--- a/builtin-branch.c
+++ b/builtin-branch.c
@@ -30,8 +30,6 @@ static const char * const builtin_branch_usage[] = {
 static const char *head;
 static unsigned char head_sha1[20];
 
-static int branch_track = 1;
-
 static int branch_use_color = -1;
 static char branch_colors[][COLOR_MAXLEN] = {
 	"\033[m",	/* reset */
@@ -76,10 +74,6 @@ static int git_branch_config(const char *var, const char *value)
 		color_parse(value, var, branch_colors[slot]);
 		return 0;
 	}
-	if (!strcmp(var, "branch.autosetupmerge")) {
-		branch_track = git_config_bool(var, value);
-		return 0;
-	}
 	return git_color_default_config(var, value);
 }
 
@@ -420,14 +414,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),
@@ -458,7 +453,7 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
 	if (branch_use_color == -1)
 		branch_use_color = git_use_color_default;
 
-	track = branch_track;
+	track = git_branch_track;
 	argc = parse_options(argc, argv, options, builtin_branch_usage, 0);
 	if (!!delete + !!rename + !!force_create > 1)
 		usage_with_options(builtin_branch_usage, options);
diff --git a/builtin-checkout.c b/builtin-checkout.c
index 0d19835..cf26e10 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,8 @@ static int switch_branches(struct checkout_opts *opts,
 	return post_checkout_hook(old.commit, new->commit, 1);
 }
 
-static int branch_track = 0;
-
 static int git_checkout_config(const char *var, const char *value)
 {
-	if (!strcmp(var, "branch.autosetupmerge"))
-		branch_track = git_config_bool(var, value);
-
 	return git_default_config(var, value);
 }
 
@@ -542,7 +537,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(),
@@ -553,7 +548,7 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
 
 	git_config(git_checkout_config);
 
-	opts.track = branch_track;
+	opts.track = git_branch_track;
 
 	argc = parse_options(argc, argv, options, checkout_usage, 0);
 	if (argc) {
@@ -582,7 +577,7 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
 		argc--;
 	}
 
-	if (!opts.new_branch && (opts.track != branch_track))
+	if (!opts.new_branch && (opts.track != git_branch_track))
 		die("git checkout: --track and --no-track require -b");
 
 	if (opts.force && opts.merge)
diff --git a/cache.h b/cache.h
index 2b59c44..90b3a9e 100644
--- a/cache.h
+++ b/cache.h
@@ -393,6 +393,14 @@ enum safe_crlf {
 
 extern enum safe_crlf safe_crlf;
 
+enum branch_track {
+	BRANCH_TRACK_REMOTES_FALSE = 0,
+	BRANCH_TRACK_REMOTES_TRUE,
+	BRANCH_TRACK_ALWAYS,
+};
+
+extern enum branch_track git_branch_track;
+
 #define GIT_REPO_VERSION 0
 extern int repository_format_version;
 extern int check_repository_format(void);
diff --git a/config.c b/config.c
index 8064cae..556b2ec 100644
--- a/config.c
+++ b/config.c
@@ -464,6 +464,14 @@ int git_default_config(const char *var, const char *value)
 		whitespace_rule_cfg = parse_whitespace_rule(value);
 		return 0;
 	}
+	if (!strcmp(var, "branch.autosetupmerge")) {
+		if (value && !strcasecmp(value, "always")) {
+			git_branch_track = BRANCH_TRACK_ALWAYS;
+			return 0;
+		}
+		git_branch_track = git_config_bool(var, value);
+		return 0;
+	}
 
 	/* Add other config variables here and to Documentation/config.txt. */
 	return 0;
diff --git a/environment.c b/environment.c
index 3527f16..24334a2 100644
--- a/environment.c
+++ b/environment.c
@@ -37,6 +37,7 @@ const char *excludes_file;
 int auto_crlf = 0;	/* 1: both ways, -1: only when adding git objects */
 enum safe_crlf safe_crlf = SAFE_CRLF_WARN;
 unsigned whitespace_rule_cfg = WS_DEFAULT_RULE;
+enum branch_track git_branch_track = BRANCH_TRACK_REMOTES_TRUE;
 
 /* This is set by setup_git_dir_gently() and/or git_default_config() */
 char *git_work_tree_cfg;
-- 
1.5.4.2.203.gf8d86

```

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

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

```
Hi,

On Mon, 18 Feb 2008, Jay Soffian wrote:

>  Incorporated Johannes' second round of feedback.

Not exactly.

> diff --git a/branch.c b/branch.c
> index 1fc8788..64f0a4a 100644
> --- a/branch.c
> +++ b/branch.c
> @@ -37,7 +37,7 @@ static int find_tracked_branch(struct remote *remote, void *priv)
>   * to infer the settings for branch.<new_ref>.{remote,merge} from the
>   * config.
>   */
> -static int setup_tracking(const char *new_ref, const char *orig_ref)
> +static int setup_tracking(const char *new_ref, const char *orig_ref, int always)
>  {
>  	char key[1024];
>  	struct tracking tracking;
> @@ -49,8 +49,12 @@ static int setup_tracking(const char *new_ref, const char *orig_ref)
>  	memset(&tracking, 0, sizeof(tracking));
>  	tracking.spec.dst = (char *)orig_ref;
>  	if (for_each_remote(find_tracked_branch, &tracking) ||
> -			!tracking.matches)
> -		return 1;
> +			!tracking.matches) {
> +		if (!always)
> +			return 1;
> +		tracking.matches = 1;
> +		tracking.src = xstrdup(orig_ref);
> +	}
>  

This looks completely different than what I commented on.  And my comments 
suggested a different solution.

Unfortunately, I will not have time for the rest of the day to review that 
new thing (it is not at all obvious for me why this works as intended, and 
does not break anything else).

But you can use the time to write some tests ;-)

Ciao,
Dscho

```

## Jay Soffian, 2008-02-18 14:38

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

```
On Feb 18, 2008 9:05 AM, Johannes Schindelin <Johannes.Schindelin@gmx.de> wrote:
> Hi,
>
> On Mon, 18 Feb 2008, Jay Soffian wrote:
>
> >  Incorporated Johannes' second round of feedback.
>
> Not exactly.

Heh...

> This looks completely different than what I commented on.  And my comments
> suggested a different solution.

Thought process went like this:

- fix sprintf() to use snprintf()
- notice setup_tracking() uses sprintf() and think about fixing it too
- realize setup_tracking() does bounds checking first and its use of sprintf()
  is therefor safe.
- go to copy the bounds checking code from setup_tracking() to the area you
  commented on.
- realized duplicating the code was dumb and I could just re-use what was
  already in setup_tracking() via what was just sent.

> But you can use the time to write some tests ;-)

Sure I can. I really hope after all this work the patch gets accepted.

j.

```

## Johannes Schindelin, 2008-02-18 18:47

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

```
Hi,

On Mon, 18 Feb 2008, Jay Soffian wrote:

> diff --git a/branch.c b/branch.c
> index 1fc8788..64f0a4a 100644
> --- a/branch.c
> +++ b/branch.c
> @@ -49,8 +49,12 @@ static int setup_tracking(const char *new_ref, const char *orig_ref)
>  	memset(&tracking, 0, sizeof(tracking));
>  	tracking.spec.dst = (char *)orig_ref;
>  	if (for_each_remote(find_tracked_branch, &tracking) ||
> -			!tracking.matches)
> -		return 1;
> +			!tracking.matches) {
> +		if (!always)
> +			return 1;
> +		tracking.matches = 1;
> +		tracking.src = xstrdup(orig_ref);
> +	}

I think you need to split this into

	if (for_each_remote(...))
		return 1;
	if (!tracking.matches && !always)
			return 1;

because an error in for_each_remote() is a proper error, and should not be 
ignored (speaking of which, the return value is unchecked...).  And then, 
later,

        if (tracking.matches == 1) {
                sprintf(key, "branch.%s.remote", new_ref);
                git_config_set(key, tracking.remote ?  tracking.remote : ".");
                sprintf(key, "branch.%s.merge", new_ref);
-               git_config_set(key, tracking.src);
+               git_config_set(key, tracking.src ? tracking.src : orig_ref);
                free(tracking.src);

> @@ -130,7 +135,7 @@ 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);
> +		setup_tracking(name, real_ref, (track == BRANCH_TRACK_ALWAYS));

It is a matter of taste, I guess, but I would remove the parens around the 
boolean expression.

FWIW a very good argument in favour of your patch is IMHO that now, 
"--track" works as a user would expect, even for local branch-offs.

And of course that you were very responsive, and really put in a lot of 
effort.

Thanks,
Dscho

```

## Junio C Hamano, 2008-02-18 20:59

Subject: Re: [PATCH] branch: optionally setup branch.*.merge from upstream local branches
Message-ID: <7vir0mnfq6.fsf@gitster.siamese.dyndns.org>
URL: https://gitlist.dev/e/7vir0mnfq6.fsf%40gitster.siamese.dyndns.org
In-Reply-To: <1203342817-19653-1-git-send-email-jaysoffian@gmail.com>

```
Jay Soffian <jaysoffian@gmail.com> writes:

> diff --git a/Documentation/config.txt b/Documentation/config.txt
> index fb6dae0..f15fe0a 100644
> --- a/Documentation/config.txt
> +++ b/Documentation/config.txt
> @@ -378,11 +378,14 @@ apply.whitespace::
>  	as the '--whitespace' option. See linkgit:git-apply[1].
>  
>  branch.autosetupmerge::
> ...
> +	Tells `git-branch` and `git-checkout` to setup `branch.*.remote`
> +	and `branch.*.merge` for new branches so that linkgit:git-pull[1]
> +	will appropriately merge from that upstream branch. Note that even
> +	if this option is not set, this behavior can be chosen per-branch
> +	using the `--track`	and `--no-track` options. This option defaults

You have an unintended tab there.

> +	to true, which will setup tracking for remote branches. Set it to
> +	`always` to automatically setup the aforementioned options for local
> +	upstream branches as well.

I somehow find it easier to read if the default is always
described at the end of the paragraph, i.e. "Setting this to
value X does foo, Y does bar, Z does baz. Defaults to X.", but
that may be just me.

> diff --git a/Documentation/git-branch.txt b/Documentation/git-branch.txt
> index 7e8874a..ce2fc64 100644
> --- a/Documentation/git-branch.txt
> +++ b/Documentation/git-branch.txt
> @@ -105,20 +105,20 @@ OPTIONS
>  	Display the full sha1s in output listing rather than abbreviating them.
>  
>  --track::
> ...
> +	Set up configuration so that git-pull will
> +	automatically retrieve data from the upstream branch.  Use this if
> +	you always pull from the same upstream branch into the new branch,
> +	or if you don't want to use "git pull <repository> <refspec>"
> +	explicitly. This behavior is the default for remote branches.
> +	Set the branch.autosetupmerge configuration variable to false if you
> +	want git-checkout and git-branch to always behave as if '--no-track'
> +	were given. Set it to 'always' if you want git-checkout and git-branch
> +	to always behave as if '--track' were given.

Re-wrapping the
paragraph at an unusual place.  It is Ok to do so if it is done
to keep the unchanged part intact to minimize the diff text, but
otherwise, please don't.

You reworded "remote branch" to "upstream branch" here and other
places.  I think that makes the intention easier to understand.
It is "the branch this new branch intends to build on top of",
so the earlier text should have said "remote tracking branch"
but the intention has always been to mean "on top of whom will I
be building".

This is not a flaw in your patch, but I suspect "or if you
don't want to" should have been "and if you don't want to".

"This behaviour is the default for remote branches" feels a bit
odd.  It confuses the first time reader as if you are saying "if
you are creating a remote branch with "git-branch" command, this
behaviour is the default", which is not what you are saying.  I
think "... the default when forking off of a remote tracking
branch" is what you meant.

>  --no-track::
> -	When a branch is created off a remote branch,
> -	set up configuration so that git-pull will not retrieve data
> -	from the remote branch, ignoring the branch.autosetupmerge
> -	configuration variable.
> +	Ignore the branch.autosetupmerge configuration variable. When
> +	using git pull with the new branch, a <refspec> will have to be
> +	given explicitly.

This rewrite probably has made the description easier to understand.

> diff --git a/Documentation/git-checkout.txt b/Documentation/git-checkout.txt
> index b4cfa04..ad2ab51 100644
> --- a/Documentation/git-checkout.txt
> +++ b/Documentation/git-checkout.txt
> @@ -48,21 +48,20 @@ OPTIONS
>  	may restrict the characters allowed in a branch name.
>  
>  --track::
> ...
> +	When -b is given set up configuration so that git-pull will

s/given/&,/;

> +	automatically retrieve data from the upstream branch.  Use this if
> +	you always pull from the same upstream branch into the new branch,
> +	or if you don't want to use "git pull <repository> <refspec>"
> +	explicitly. This behavior is the default for remote branches.

The same comment on "or if you don't want to" applies.

> @@ -49,8 +49,12 @@ static int setup_tracking(const char *new_ref, const char *orig_ref)
>  	memset(&tracking, 0, sizeof(tracking));
>  	tracking.spec.dst = (char *)orig_ref;
>  	if (for_each_remote(find_tracked_branch, &tracking) ||
> -			!tracking.matches)
> -		return 1;
> +			!tracking.matches) {
> +		if (!always)
> +			return 1;
> +		tracking.matches = 1;
> +		tracking.src = xstrdup(orig_ref);
> +	}

I suspect this is a clever fallback to pretend as if you have
[remote "."] fetch=refs/*:refs/* when nothing else matches, but
I think it needs an explanation why this is a good idea and
would not break others.  Also I suspect this should be moved
after you see for-each-remote returns without error and did not
find any match.

> @@ -71,7 +76,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 +135,7 @@ 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);
> +		setup_tracking(name, real_ref, (track == BRANCH_TRACK_ALWAYS));

It probably is better to pass 'track' itself and have the
special case logic inside setup_tracking() instead.  Maybe we
would want to add new tracking mode that needs different
matching logic there later.

The move to default_config() is probably a good idea.

> diff --git a/cache.h b/cache.h
> index 2b59c44..90b3a9e 100644
> --- a/cache.h
> +++ b/cache.h
> @@ -393,6 +393,14 @@ enum safe_crlf {
>  
>  extern enum safe_crlf safe_crlf;
>  
> +enum branch_track {
> +	BRANCH_TRACK_REMOTES_FALSE = 0,
> +	BRANCH_TRACK_REMOTES_TRUE,
> +	BRANCH_TRACK_ALWAYS,
> +};

Perhaps BRANCH_TRACK_{NEVER,REMOTE,ALWAYS} would be easier
(although I cannot decide if s/NEVER/FALSE/ would be better)?

```
