remote.c: report specific errors from branch_get_upstream
[gitweb.git] / remote.c
index a91d06396e5973f6bfe090c5a672f1b70c3b65bb..1b7051a18722700907f969e6c88f355b91f5ec3b 100644 (file)
--- a/remote.c
+++ b/remote.c
@@ -718,6 +718,8 @@ static struct remote *remote_get_1(const char *name,
        struct remote *ret;
        int name_given = 0;
 
+       read_config();
+
        if (name)
                name_given = 1;
        else
@@ -741,13 +743,11 @@ static struct remote *remote_get_1(const char *name,
 
 struct remote *remote_get(const char *name)
 {
-       read_config();
        return remote_get_1(name, remote_for_branch);
 }
 
 struct remote *pushremote_get(const char *name)
 {
-       read_config();
        return remote_get_1(name, pushremote_for_branch);
 }
 
@@ -1705,6 +1705,38 @@ int branch_merge_matches(struct branch *branch,
        return refname_match(branch->merge[i]->src, refname);
 }
 
+__attribute((format (printf,2,3)))
+static const char *error_buf(struct strbuf *err, const char *fmt, ...)
+{
+       if (err) {
+               va_list ap;
+               va_start(ap, fmt);
+               strbuf_vaddf(err, fmt, ap);
+               va_end(ap);
+       }
+       return NULL;
+}
+
+const char *branch_get_upstream(struct branch *branch, struct strbuf *err)
+{
+       if (!branch)
+               return error_buf(err, _("HEAD does not point to a branch"));
+       if (!branch->merge || !branch->merge[0] || !branch->merge[0]->dst) {
+               if (!ref_exists(branch->refname))
+                       return error_buf(err, _("no such branch: '%s'"),
+                                        branch->name);
+               if (!branch->merge)
+                       return error_buf(err,
+                                        _("no upstream configured for branch '%s'"),
+                                        branch->name);
+               return error_buf(err,
+                                _("upstream branch '%s' not stored as a remote-tracking branch"),
+                                branch->merge[0]->src);
+       }
+
+       return branch->merge[0]->dst;
+}
+
 static int ignore_symref_update(const char *refname)
 {
        unsigned char sha1[20];
@@ -1914,12 +1946,11 @@ int stat_tracking_info(struct branch *branch, int *num_ours, int *num_theirs)
        int rev_argc;
 
        /* Cannot stat unless we are marked to build on top of somebody else. */
-       if (!branch ||
-           !branch->merge || !branch->merge[0] || !branch->merge[0]->dst)
+       base = branch_get_upstream(branch, NULL);
+       if (!base)
                return 0;
 
        /* Cannot stat if what we used to build on no longer exists */
-       base = branch->merge[0]->dst;
        if (read_ref(base, sha1))
                return -1;
        theirs = lookup_commit_reference(sha1);