remote.c: return upstream name from stat_tracking_info
authorJeff King <peff@peff.net>
Fri, 22 May 2015 00:49:11 +0000 (20:49 -0400)
committerJunio C Hamano <gitster@pobox.com>
Fri, 22 May 2015 16:32:34 +0000 (09:32 -0700)
After calling stat_tracking_info, callers often want to
print the name of the upstream branch (in addition to the
tracking count). To do this, they have to access
branch->merge->dst[0] themselves. This is not wrong, as the
return value from stat_tracking_info tells us whether we
have an upstream branch or not. But it is a bit leaky, as we
make an assumption about how it calculated the upstream
name.

Instead, let's add an out-parameter that lets the caller
know the upstream name we found.

As a bonus, we can get rid of the unusual tri-state return
from the function. We no longer need to use it to
differentiate between "no tracking config" and "tracking ref
does not exist" (since you can check the upstream_name for
that), so we can just use the usual 0/-1 convention for
success/error.

Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/branch.c
builtin/for-each-ref.c
remote.c
remote.h
wt-status.c
index b7202b33990a68cfa41b1a302c286c38ddfb713e..e4d184d69a883d2c6a683f837f9c91aa9286c0a9 100644 (file)
@@ -425,25 +425,19 @@ static void fill_tracking_info(struct strbuf *stat, const char *branch_name,
        int ours, theirs;
        char *ref = NULL;
        struct branch *branch = branch_get(branch_name);
+       const char *upstream;
        struct strbuf fancy = STRBUF_INIT;
        int upstream_is_gone = 0;
        int added_decoration = 1;
 
-       switch (stat_tracking_info(branch, &ours, &theirs)) {
-       case 0:
-               /* no base */
-               return;
-       case -1:
-               /* with "gone" base */
+       if (stat_tracking_info(branch, &ours, &theirs, &upstream) < 0) {
+               if (!upstream)
+                       return;
                upstream_is_gone = 1;
-               break;
-       default:
-               /* with base */
-               break;
        }
 
        if (show_upstream_ref) {
-               ref = shorten_unambiguous_ref(branch->merge[0]->dst, 0);
+               ref = shorten_unambiguous_ref(upstream, 0);
                if (want_color(branch_use_color))
                        strbuf_addf(&fancy, "%s%s%s",
                                        branch_get_color(BRANCH_COLOR_UPSTREAM),
index 18d209bc9a2c49909d891a5190e51f2dd0ea9156..92bd2b26654262da24523dd5f98f3741df64de1e 100644 (file)
@@ -716,7 +716,7 @@ static void populate_value(struct refinfo *ref)
                                char buf[40];
 
                                if (stat_tracking_info(branch, &num_ours,
-                                                      &num_theirs) != 1)
+                                                      &num_theirs, NULL))
                                        continue;
 
                                if (!num_ours && !num_theirs)
@@ -738,7 +738,7 @@ static void populate_value(struct refinfo *ref)
                                assert(branch);
 
                                if (stat_tracking_info(branch, &num_ours,
-                                                       &num_theirs) != 1)
+                                                       &num_theirs, NULL))
                                        continue;
 
                                if (!num_ours && !num_theirs)
index d2519c22ce85921ae5c231b099ac0735bb09814e..c8845746b61dbceb1074813bb7e6298f1aebf61e 100644 (file)
--- a/remote.c
+++ b/remote.c
@@ -1938,12 +1938,15 @@ int ref_newer(const unsigned char *new_sha1, const unsigned char *old_sha1)
 
 /*
  * Compare a branch with its upstream, and save their differences (number
- * of commits) in *num_ours and *num_theirs.
+ * of commits) in *num_ours and *num_theirs. The name of the upstream branch
+ * (or NULL if no upstream is defined) is returned via *upstream_name, if it
+ * is not itself NULL.
  *
- * Return 0 if branch has no upstream (no base), -1 if upstream is missing
- * (with "gone" base), otherwise 1 (with base).
+ * Returns -1 if num_ours and num_theirs could not be filled in (e.g., no
+ * upstream defined, or ref does not exist), 0 otherwise.
  */
-int stat_tracking_info(struct branch *branch, int *num_ours, int *num_theirs)
+int stat_tracking_info(struct branch *branch, int *num_ours, int *num_theirs,
+                      const char **upstream_name)
 {
        unsigned char sha1[20];
        struct commit *ours, *theirs;
@@ -1954,8 +1957,10 @@ int stat_tracking_info(struct branch *branch, int *num_ours, int *num_theirs)
 
        /* Cannot stat unless we are marked to build on top of somebody else. */
        base = branch_get_upstream(branch, NULL);
+       if (upstream_name)
+               *upstream_name = base;
        if (!base)
-               return 0;
+               return -1;
 
        /* Cannot stat if what we used to build on no longer exists */
        if (read_ref(base, sha1))
@@ -1973,7 +1978,7 @@ int stat_tracking_info(struct branch *branch, int *num_ours, int *num_theirs)
        /* are we the same? */
        if (theirs == ours) {
                *num_theirs = *num_ours = 0;
-               return 1;
+               return 0;
        }
 
        /* Run "rev-list --left-right ours...theirs" internally... */
@@ -2009,7 +2014,7 @@ int stat_tracking_info(struct branch *branch, int *num_ours, int *num_theirs)
        /* clear object flags smudged by the above traversal */
        clear_commit_marks(ours, ALL_REV_FLAGS);
        clear_commit_marks(theirs, ALL_REV_FLAGS);
-       return 1;
+       return 0;
 }
 
 /*
@@ -2018,23 +2023,17 @@ int stat_tracking_info(struct branch *branch, int *num_ours, int *num_theirs)
 int format_tracking_info(struct branch *branch, struct strbuf *sb)
 {
        int ours, theirs;
+       const char *full_base;
        char *base;
        int upstream_is_gone = 0;
 
-       switch (stat_tracking_info(branch, &ours, &theirs)) {
-       case 0:
-               /* no base */
-               return 0;
-       case -1:
-               /* with "gone" base */
+       if (stat_tracking_info(branch, &ours, &theirs, &full_base) < 0) {
+               if (!full_base)
+                       return 0;
                upstream_is_gone = 1;
-               break;
-       default:
-               /* with base */
-               break;
        }
 
-       base = shorten_unambiguous_ref(branch->merge[0]->dst, 0);
+       base = shorten_unambiguous_ref(full_base, 0);
        if (upstream_is_gone) {
                strbuf_addf(sb,
                        _("Your branch is based on '%s', but the upstream is gone.\n"),
index 03ca0058fe67c29f2d948aaa39d9a19bc6d59465..357a90963db926b328d3b7df1b9bd3b8515b08b9 100644 (file)
--- a/remote.h
+++ b/remote.h
@@ -239,7 +239,8 @@ enum match_refs_flags {
 };
 
 /* Reporting of tracking info */
-int stat_tracking_info(struct branch *branch, int *num_ours, int *num_theirs);
+int stat_tracking_info(struct branch *branch, int *num_ours, int *num_theirs,
+                      const char **upstream_name);
 int format_tracking_info(struct branch *branch, struct strbuf *sb);
 
 struct ref *get_local_heads(void);
index 853419f05f232045cf4ad3f8a8a2af04be9329a0..4313b9d8457037bcaef03399a852ef91198861ba 100644 (file)
@@ -1532,21 +1532,15 @@ static void wt_shortstatus_print_tracking(struct wt_status *s)
 
        color_fprintf(s->fp, branch_color_local, "%s", branch_name);
 
-       switch (stat_tracking_info(branch, &num_ours, &num_theirs)) {
-       case 0:
-               /* no base */
-               fputc(s->null_termination ? '\0' : '\n', s->fp);
-               return;
-       case -1:
-               /* with "gone" base */
+       if (stat_tracking_info(branch, &num_ours, &num_theirs, &base) < 0) {
+               if (!base) {
+                       fputc(s->null_termination ? '\0' : '\n', s->fp);
+                       return;
+               }
+
                upstream_is_gone = 1;
-               break;
-       default:
-               /* with base */
-               break;
        }
 
-       base = branch->merge[0]->dst;
        base = shorten_unambiguous_ref(base, 0);
        color_fprintf(s->fp, header_color, "...");
        color_fprintf(s->fp, branch_color_remote, "%s", base);