threads / discuss / 16645

get upstream branch

Subject: get upstream branch

## tl;dr

7 messages between Dec 9, 2008 and Dec 9, 2008.

replies: 6people: 6as markdown or json

Jeff Whiteside· Dec 9, 2008, 04:52 UTC · lore
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· Dec 9, 2008, 05:35 UTC · re: Jeff Whiteside · lore

Re: get upstream branch

"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· Dec 9, 2008, 05:56 UTC · re: Junio C Hamano · lore

Re: get upstream branch

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· Dec 9, 2008, 06:43 UTC · re: Jeff King · lore

Re: get upstream branch

Jeff King <peff@peff.net> writes:
Show 13 quoted lines
> 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.

Santi Béjar· Dec 9, 2008, 15:51 UTC · re: Junio C Hamano · lore

Re: get upstream branch

2008/12/9 Junio C Hamano <gitster@pobox.com>:
Show 18 quoted lines
> 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
Sverre Rabbelier· Dec 9, 2008, 06:46 UTC · re: Junio C Hamano · lore

Re: get upstream branch

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· Dec 9, 2008, 15:25 UTC · re: Jeff Whiteside · lore

Re: get upstream branch

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

← back to recent threads