git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: get upstream branch

From
Junio C Hamano <gitster@pobox.com>
Date
Dec 9, 2008, 05:35 UTC
Message-ID
<7vljup6hdf.fsf@gitster.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))
Previous: Jeff WhitesideNext: Jeff King
Message 2 of 7 in “get upstream branch”
  1. Jeff WhitesideDec 9, 2008
  2. Junio C HamanoDec 9, 2008
  3. Jeff KingDec 9, 2008
  4. Junio C HamanoDec 9, 2008
  5. Santi BéjarDec 9, 2008
  6. Sverre RabbelierDec 9, 2008
  7. Peter HarrisDec 9, 2008

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.