# get upstream branch

7 messages from 2008-12-09 to 2008-12-09. Participants: Jeff Whiteside, Junio C Hamano, Jeff King, Sverre Rabbelier, Peter Harris, Santi Béjar.
Thread: https://gitlist.dev/t/16645

## Jeff Whiteside, 2008-12-09 04:52

Subject: get upstream branch
Message-ID: <3ab397d0812082052j6a45d05dr1c863aa260826f4@mail.gmail.com>
URL: https://gitlist.dev/e/3ab397d0812082052j6a45d05dr1c863aa260826f4%40mail.gmail.com

```
Hi,

Does anyone know how to programmatically get the upstream/parent
branch of a branch?  I'm trying to write a gui, but looking at gitk's
tcl code isn't helping me much.

Thanks,

Whiteside

```

## Junio C Hamano, 2008-12-09 05:35

Subject: Re: get upstream branch
Message-ID: <7vljup6hdf.fsf@gitster.siamese.dyndns.org>
URL: https://gitlist.dev/e/7vljup6hdf.fsf%40gitster.siamese.dyndns.org
In-Reply-To: <3ab397d0812082052j6a45d05dr1c863aa260826f4@mail.gmail.com>

```
"Jeff Whiteside" <jeff.m.whiteside@gmail.com> writes:

> Does anyone know how to programmatically get the upstream/parent
> branch of a branch?

I do not think there is any plumbing facility to get that information, as
such "upstream/parent" concept did not exist back then when building the
whole Porcelain by scripting was the norm.

This should give "git branch -v -v it" to show the remote tracking
branch that is merged when "git pull" without any other parameters is
issued while on branch "it".

Obviously untested.

 builtin-branch.c |   26 +++++++++++++++-----------
 1 files changed, 15 insertions(+), 11 deletions(-)

diff --git c/builtin-branch.c w/builtin-branch.c
index 494cbac..565d99c 100644
--- c/builtin-branch.c
+++ w/builtin-branch.c
@@ -279,11 +279,15 @@ static int ref_cmp(const void *r1, const void *r2)
 	return strcmp(c1->name, c2->name);
 }
 
-static void fill_tracking_info(struct strbuf *stat, const char *branch_name)
+static void fill_tracking_info(struct strbuf *stat, const char *branch_name,
+			       int verbosity)
 {
 	int ours, theirs;
 	struct branch *branch = branch_get(branch_name);
 
+	if (verbosity > 1 && branch->merge[0]->dst)
+		strbuf_addf(stat, "(follows %s) ", branch->merge[0]->dst);
+
 	if (!stat_tracking_info(branch, &ours, &theirs) || (!ours && !theirs))
 		return;
 	if (!ours)
@@ -305,7 +309,7 @@ static int matches_merge_filter(struct commit *commit)
 	return (is_merged == (merge_filter == SHOW_MERGED));
 }
 
-static void print_ref_item(struct ref_item *item, int maxwidth, int verbose,
+static void print_ref_item(struct ref_item *item, int maxwidth, int verbosity,
 			   int abbrev, int current)
 {
 	char c;
@@ -333,7 +337,7 @@ static void print_ref_item(struct ref_item *item, int maxwidth, int verbose,
 		color = COLOR_BRANCH_CURRENT;
 	}
 
-	if (verbose) {
+	if (verbosity > 0) {
 		struct strbuf subject = STRBUF_INIT, stat = STRBUF_INIT;
 		const char *sub = " **** invalid ref ****";
 
@@ -345,7 +349,7 @@ static void print_ref_item(struct ref_item *item, int maxwidth, int verbose,
 		}
 
 		if (item->kind == REF_LOCAL_BRANCH)
-			fill_tracking_info(&stat, item->name);
+			fill_tracking_info(&stat, item->name, verbosity);
 
 		printf("%c %s%-*s%s %s %s%s\n", c, branch_get_color(color),
 		       maxwidth, item->name,
@@ -373,7 +377,7 @@ static int calc_maxwidth(struct ref_list *refs)
 	return w;
 }
 
-static void print_ref_list(int kinds, int detached, int verbose, int abbrev, struct commit_list *with_commit)
+static void print_ref_list(int kinds, int detached, int verbosity, int abbrev, struct commit_list *with_commit)
 {
 	int i;
 	struct ref_list ref_list;
@@ -393,7 +397,7 @@ static void print_ref_list(int kinds, int detached, int verbose, int abbrev, str
 				   (struct object *) filter, "");
 		ref_list.revs.limited = 1;
 		prepare_revision_walk(&ref_list.revs);
-		if (verbose)
+		if (verbosity > 0)
 			ref_list.maxwidth = calc_maxwidth(&ref_list);
 	}
 
@@ -407,7 +411,7 @@ static void print_ref_list(int kinds, int detached, int verbose, int abbrev, str
 		item.commit = head_commit;
 		if (strlen(item.name) > ref_list.maxwidth)
 			ref_list.maxwidth = strlen(item.name);
-		print_ref_item(&item, ref_list.maxwidth, verbose, abbrev, 1);
+		print_ref_item(&item, ref_list.maxwidth, verbosity, abbrev, 1);
 		free(item.name);
 	}
 
@@ -415,7 +419,7 @@ static void print_ref_list(int kinds, int detached, int verbose, int abbrev, str
 		int current = !detached &&
 			(ref_list.list[i].kind == REF_LOCAL_BRANCH) &&
 			!strcmp(ref_list.list[i].name, head);
-		print_ref_item(&ref_list.list[i], ref_list.maxwidth, verbose,
+		print_ref_item(&ref_list.list[i], ref_list.maxwidth, verbosity,
 			       abbrev, current);
 	}
 
@@ -498,7 +502,7 @@ static int opt_parse_merge_filter(const struct option *opt, const char *arg, int
 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 verbosity = 0, abbrev = DEFAULT_ABBREV, detached = 0;
 	int reflog = 0;
 	enum branch_track track;
 	int kinds = REF_LOCAL_BRANCH;
@@ -506,7 +510,7 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
 
 	struct option options[] = {
 		OPT_GROUP("Generic options"),
-		OPT__VERBOSE(&verbose),
+		OPT__VERBOSITY(&verbosity),
 		OPT_SET_INT( 0 , "track",  &track, "set up tracking mode (see git-pull(1))",
 			BRANCH_TRACK_EXPLICIT),
 		OPT_BOOLEAN( 0 , "color",  &branch_use_color, "use colored output"),
@@ -577,7 +581,7 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
 	if (delete)
 		return delete_branches(argc, argv, delete > 1, kinds);
 	else if (argc == 0)
-		print_ref_list(kinds, detached, verbose, abbrev, with_commit);
+		print_ref_list(kinds, detached, verbosity, abbrev, with_commit);
 	else if (rename && (argc == 1))
 		rename_branch(head, argv[0], rename > 1);
 	else if (rename && (argc == 2))

```

## Jeff King, 2008-12-09 05:56

Subject: Re: get upstream branch
Message-ID: <20081209055629.GB2972@coredump.intra.peff.net>
URL: https://gitlist.dev/e/20081209055629.GB2972%40coredump.intra.peff.net
In-Reply-To: <7vljup6hdf.fsf@gitster.siamese.dyndns.org>

```
On Mon, Dec 08, 2008 at 09:35:08PM -0800, Junio C Hamano wrote:

> I do not think there is any plumbing facility to get that information, as
> such "upstream/parent" concept did not exist back then when building the
> whole Porcelain by scripting was the norm.

In one of my scripts I do something like this (actually this is not
straight from my script, as the operation there is "find all pairs of
local/remote branches" and this is "find the current upstream"):

  ref=`git symbolic-ref HEAD`
  head=${ref#refs/heads/}
  remote=`git config branch.$head.remote`
  branch=`git config branch.$head.merge`
  echo refs/remote/$remote/${branch#refs/heads/}

And obviously this is missing error checking for the detached HEAD
(symbolic-ref should fail) and no tracking branch ($remote and/or $branch
will be empty) cases.

-Peff

```

## Junio C Hamano, 2008-12-09 06:43

Subject: Re: get upstream branch
Message-ID: <7v8wqp6e89.fsf@gitster.siamese.dyndns.org>
URL: https://gitlist.dev/e/7v8wqp6e89.fsf%40gitster.siamese.dyndns.org
In-Reply-To: <20081209055629.GB2972@coredump.intra.peff.net>

```
Jeff King <peff@peff.net> writes:

> In one of my scripts I do something like this (actually this is not
> straight from my script, as the operation there is "find all pairs of
> local/remote branches" and this is "find the current upstream"):
>
>   ref=`git symbolic-ref HEAD`
>   head=${ref#refs/heads/}
>   remote=`git config branch.$head.remote`
>   branch=`git config branch.$head.merge`
>   echo refs/remote/$remote/${branch#refs/heads/}
>
> And obviously this is missing error checking for the detached HEAD
> (symbolic-ref should fail) and no tracking branch ($remote and/or $branch
> will be empty) cases.

Yeah, add any nonstandard layout to that set of things that are missing,
but in practice it should not matter.

```

## Sverre Rabbelier, 2008-12-09 06:46

Subject: Re: get upstream branch
Message-ID: <bd6139dc0812082246u59164cf6tb20f8c98ede7b666@mail.gmail.com>
URL: https://gitlist.dev/e/bd6139dc0812082246u59164cf6tb20f8c98ede7b666%40mail.gmail.com
In-Reply-To: <7vljup6hdf.fsf@gitster.siamese.dyndns.org>

```
On Tue, Dec 9, 2008 at 06:35, Junio C Hamano <gitster@pobox.com> wrote:
> This should give "git branch -v -v it" to show the remote tracking
> branch that is merged when "git pull" without any other parameters is
> issued while on branch "it".

But won't that leave Jeff Whiteside in the same position he was
already? As he is writing a GUI having the functionality in 'git
branch' is not useful, what with it being porcelain and all.

-- 
Cheers,

Sverre Rabbelier

```

## Peter Harris, 2008-12-09 15:25

Subject: Re: get upstream branch
Message-ID: <eaa105840812090725p6158697au2177e0de3d4e5966@mail.gmail.com>
URL: https://gitlist.dev/e/eaa105840812090725p6158697au2177e0de3d4e5966%40mail.gmail.com
In-Reply-To: <3ab397d0812082052j6a45d05dr1c863aa260826f4@mail.gmail.com>

```
On Mon, Dec 8, 2008 at 11:52 PM, Jeff Whiteside wrote:
>
> Does anyone know how to programmatically get the upstream/parent
> branch of a branch?  I'm trying to write a gui, but looking at gitk's
> tcl code isn't helping me much.

Would C++ code be a better help?

qgit is somewhat similar to gitk:
http://digilander.libero.it/mcostalba/
git://git.kernel.org/pub/scm/qgit/qgit.git
git://git.kernel.org/pub/scm/qgit/qgit4.git

Peter Harris

```

## Santi Béjar, 2008-12-09 15:51

Subject: Re: get upstream branch
Message-ID: <adf1fd3d0812090751r5d5a1c49k199e5e92316e0f9d@mail.gmail.com>
URL: https://gitlist.dev/e/adf1fd3d0812090751r5d5a1c49k199e5e92316e0f9d%40mail.gmail.com
In-Reply-To: <7v8wqp6e89.fsf@gitster.siamese.dyndns.org>

```
2008/12/9 Junio C Hamano <gitster@pobox.com>:
> Jeff King <peff@peff.net> writes:
>
>> In one of my scripts I do something like this (actually this is not
>> straight from my script, as the operation there is "find all pairs of
>> local/remote branches" and this is "find the current upstream"):
>>
>>   ref=`git symbolic-ref HEAD`
>>   head=${ref#refs/heads/}
>>   remote=`git config branch.$head.remote`
>>   branch=`git config branch.$head.merge`
>>   echo refs/remote/$remote/${branch#refs/heads/}
>>
>> And obviously this is missing error checking for the detached HEAD
>> (symbolic-ref should fail) and no tracking branch ($remote and/or $branch
>> will be empty) cases.
>
> Yeah, add any nonstandard layout to that set of things that are missing,
> but in practice it should not matter.

In "git pull --rebase" this is used to know the hash of the tracking branch:

        . git-parse-remote &&
        origin="$1"
        test -z "$origin" && origin=$(get_default_remote)
        reflist="$(get_remote_refs_for_fetch "$@" 2>/dev/null |
                sed "s|refs/heads/\(.*\):|\1|")" &&
        oldremoteref="$(git rev-parse -q --verify \
                "refs/remotes/$origin/$reflist")"

Santi

```
