pull: support pull.ff config
[gitweb.git] / remote.c
index 1b7051a18722700907f969e6c88f355b91f5ec3b..26504b744786c65ea4d6e1e0abbf5c6409af5358 100644 (file)
--- a/remote.c
+++ b/remote.c
@@ -1721,22 +1721,114 @@ 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 (!branch->merge || !branch->merge[0]) {
+               /*
+                * no merge config; is it because the user didn't define any,
+                * or because it is not a real branch, and get_branch
+                * auto-vivified it?
+                */
                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,
+                                _("no upstream configured for branch '%s'"),
+                                branch->name);
+       }
+
+       if (!branch->merge[0]->dst)
                return error_buf(err,
                                 _("upstream branch '%s' not stored as a remote-tracking branch"),
                                 branch->merge[0]->src);
-       }
 
        return branch->merge[0]->dst;
 }
 
+static const char *tracking_for_push_dest(struct remote *remote,
+                                         const char *refname,
+                                         struct strbuf *err)
+{
+       char *ret;
+
+       ret = apply_refspecs(remote->fetch, remote->fetch_refspec_nr, refname);
+       if (!ret)
+               return error_buf(err,
+                                _("push destination '%s' on remote '%s' has no local tracking branch"),
+                                refname, remote->name);
+       return ret;
+}
+
+static const char *branch_get_push_1(struct branch *branch, struct strbuf *err)
+{
+       struct remote *remote;
+
+       if (!branch)
+               return error_buf(err, _("HEAD does not point to a branch"));
+
+       remote = remote_get(pushremote_for_branch(branch, NULL));
+       if (!remote)
+               return error_buf(err,
+                                _("branch '%s' has no remote for pushing"),
+                                branch->name);
+
+       if (remote->push_refspec_nr) {
+               char *dst;
+               const char *ret;
+
+               dst = apply_refspecs(remote->push, remote->push_refspec_nr,
+                                    branch->refname);
+               if (!dst)
+                       return error_buf(err,
+                                        _("push refspecs for '%s' do not include '%s'"),
+                                        remote->name, branch->name);
+
+               ret = tracking_for_push_dest(remote, dst, err);
+               free(dst);
+               return ret;
+       }
+
+       if (remote->mirror)
+               return tracking_for_push_dest(remote, branch->refname, err);
+
+       switch (push_default) {
+       case PUSH_DEFAULT_NOTHING:
+               return error_buf(err, _("push has no destination (push.default is 'nothing')"));
+
+       case PUSH_DEFAULT_MATCHING:
+       case PUSH_DEFAULT_CURRENT:
+               return tracking_for_push_dest(remote, branch->refname, err);
+
+       case PUSH_DEFAULT_UPSTREAM:
+               return branch_get_upstream(branch, err);
+
+       case PUSH_DEFAULT_UNSPECIFIED:
+       case PUSH_DEFAULT_SIMPLE:
+               {
+                       const char *up, *cur;
+
+                       up = branch_get_upstream(branch, err);
+                       if (!up)
+                               return NULL;
+                       cur = tracking_for_push_dest(remote, branch->refname, err);
+                       if (!cur)
+                               return NULL;
+                       if (strcmp(cur, up))
+                               return error_buf(err,
+                                                _("cannot resolve 'simple' push to a single destination"));
+                       return cur;
+               }
+       }
+
+       die("BUG: unhandled push situation");
+}
+
+const char *branch_get_push(struct branch *branch, struct strbuf *err)
+{
+       if (!branch->push_tracking_ref)
+               branch->push_tracking_ref = branch_get_push_1(branch, err);
+       return branch->push_tracking_ref;
+}
+
 static int ignore_symref_update(const char *refname)
 {
        unsigned char sha1[20];
@@ -1931,12 +2023,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;
@@ -1947,8 +2042,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))
@@ -1966,7 +2063,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... */
@@ -2002,7 +2099,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;
 }
 
 /*
@@ -2011,23 +2108,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"),
@@ -2077,7 +2168,8 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb)
        return 1;
 }
 
-static int one_local_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
+static int one_local_ref(const char *refname, const struct object_id *oid,
+                        int flag, void *cb_data)
 {
        struct ref ***local_tail = cb_data;
        struct ref *ref;
@@ -2089,7 +2181,7 @@ static int one_local_ref(const char *refname, const unsigned char *sha1, int fla
 
        len = strlen(refname) + 1;
        ref = xcalloc(1, sizeof(*ref) + len);
-       hashcpy(ref->new_sha1, sha1);
+       hashcpy(ref->new_sha1, oid->hash);
        memcpy(ref->name, refname, len);
        **local_tail = ref;
        *local_tail = &ref->next;
@@ -2099,6 +2191,7 @@ static int one_local_ref(const char *refname, const unsigned char *sha1, int fla
 struct ref *get_local_heads(void)
 {
        struct ref *local_refs = NULL, **local_tail = &local_refs;
+
        for_each_ref(one_local_ref, &local_tail);
        return local_refs;
 }
@@ -2151,8 +2244,8 @@ struct stale_heads_info {
        int ref_count;
 };
 
-static int get_stale_heads_cb(const char *refname,
-       const unsigned char *sha1, int flags, void *cb_data)
+static int get_stale_heads_cb(const char *refname, const struct object_id *oid,
+                             int flags, void *cb_data)
 {
        struct stale_heads_info *info = cb_data;
        struct string_list matches = STRING_LIST_INIT_DUP;
@@ -2181,7 +2274,7 @@ static int get_stale_heads_cb(const char *refname,
 
        if (stale) {
                struct ref *ref = make_linked_ref(refname, &info->stale_refs_tail);
-               hashcpy(ref->new_sha1, sha1);
+               hashcpy(ref->new_sha1, oid->hash);
        }
 
 clean_exit:
@@ -2194,6 +2287,7 @@ struct ref *get_stale_heads(struct refspec *refs, int ref_count, struct ref *fet
        struct ref *ref, *stale_refs = NULL;
        struct string_list ref_names = STRING_LIST_INIT_NODUP;
        struct stale_heads_info info;
+
        info.ref_names = &ref_names;
        info.stale_refs_tail = &stale_refs;
        info.refs = refs;