]> git.ipfire.org Git - thirdparty/git.git/blobdiff - remote.c
t5536: new test of refspec conflicts when fetching
[thirdparty/git.git] / remote.c
index efcba931eca963bd6a5fd13f01a4859e0ae9e14d..1fb87de01c6a3e7cede213b64a99408060eef33a 100644 (file)
--- a/remote.c
+++ b/remote.c
@@ -148,6 +148,7 @@ static struct remote *make_remote(const char *name, int len)
        }
 
        ret = xcalloc(1, sizeof(struct remote));
+       ret->prune = -1;  /* unspecified */
        ALLOC_GROW(remotes, remotes_nr + 1, remotes_alloc);
        remotes[remotes_nr++] = ret;
        if (len)
@@ -404,6 +405,8 @@ static int handle_config(const char *key, const char *value, void *cb)
                remote->skip_default_update = git_config_bool(key, value);
        else if (!strcmp(subkey, ".skipfetchall"))
                remote->skip_default_update = git_config_bool(key, value);
+       else if (!strcmp(subkey, ".prune"))
+               remote->prune = git_config_bool(key, value);
        else if (!strcmp(subkey, ".url")) {
                const char *v;
                if (git_config_string(&v, key, value))
@@ -747,13 +750,15 @@ void ref_remove_duplicates(struct ref *ref_map)
        struct string_list refs = STRING_LIST_INIT_NODUP;
        struct string_list_item *item = NULL;
        struct ref *prev = NULL, *next = NULL;
+
        for (; ref_map; prev = ref_map, ref_map = next) {
                next = ref_map->next;
                if (!ref_map->peer_ref)
                        continue;
 
-               item = string_list_lookup(&refs, ref_map->peer_ref->name);
-               if (item) {
+               item = string_list_insert(&refs, ref_map->peer_ref->name);
+               if (item->util) {
+                       /* Entry already existed */
                        if (strcmp(((struct ref *)item->util)->name,
                                   ref_map->name))
                                die("%s tracks both %s and %s",
@@ -764,11 +769,9 @@ void ref_remove_duplicates(struct ref *ref_map)
                        free(ref_map->peer_ref);
                        free(ref_map);
                        ref_map = prev; /* skip this; we freed it */
-                       continue;
+               } else {
+                       item->util = ref_map;
                }
-
-               item = string_list_insert(&refs, ref_map->peer_ref->name);
-               item->util = ref_map;
        }
        string_list_clear(&refs, 0);
 }
@@ -822,6 +825,8 @@ static int query_refspecs(struct refspec *refs, int ref_count, struct refspec *q
 {
        int i;
        int find_src = !query->src;
+       const char *needle = find_src ? query->dst : query->src;
+       char **result = find_src ? &query->src : &query->dst;
 
        if (find_src && !query->dst)
                return error("query_refspecs: need either src or dst");
@@ -830,8 +835,6 @@ static int query_refspecs(struct refspec *refs, int ref_count, struct refspec *q
                struct refspec *refspec = &refs[i];
                const char *key = find_src ? refspec->dst : refspec->src;
                const char *value = find_src ? refspec->src : refspec->dst;
-               const char *needle = find_src ? query->dst : query->src;
-               char **result = find_src ? &query->src : &query->dst;
 
                if (!refspec->dst)
                        continue;
@@ -1302,6 +1305,14 @@ static void add_missing_tags(struct ref *src, struct ref **dst, struct ref ***ds
        free(sent_tips.tip);
 }
 
+struct ref *find_ref_by_name(const struct ref *list, const char *name)
+{
+       for ( ; list; list = list->next)
+               if (!strcmp(list->name, name))
+                       return (struct ref *)list;
+       return NULL;
+}
+
 static void prepare_ref_index(struct string_list *ref_index, struct ref *ref)
 {
        for ( ; ref; ref = ref->next)
@@ -1411,12 +1422,13 @@ int match_push_refs(struct ref *src, struct ref **dst,
 }
 
 void set_ref_status_for_push(struct ref *remote_refs, int send_mirror,
-       int force_update)
+                            int force_update)
 {
        struct ref *ref;
 
        for (ref = remote_refs; ref; ref = ref->next) {
                int force_ref_update = ref->force || force_update;
+               int reject_reason = 0;
 
                if (ref->peer_ref)
                        hashcpy(ref->new_sha1, ref->peer_ref->new_sha1);
@@ -1431,6 +1443,26 @@ void set_ref_status_for_push(struct ref *remote_refs, int send_mirror,
                }
 
                /*
+                * Bypass the usual "must fast-forward" check but
+                * replace it with a weaker "the old value must be
+                * this value we observed".  If the remote ref has
+                * moved and is now different from what we expect,
+                * reject any push.
+                *
+                * It also is an error if the user told us to check
+                * with the remote-tracking branch to find the value
+                * to expect, but we did not have such a tracking
+                * branch.
+                */
+               if (ref->expect_old_sha1) {
+                       if (ref->expect_old_no_trackback ||
+                           hashcmp(ref->old_sha1, ref->old_sha1_expect))
+                               reject_reason = REF_STATUS_REJECT_STALE;
+               }
+
+               /*
+                * The usual "must fast-forward" rules.
+                *
                 * Decide whether an individual refspec A:B can be
                 * pushed.  The push will succeed if any of the
                 * following are true:
@@ -1448,24 +1480,26 @@ void set_ref_status_for_push(struct ref *remote_refs, int send_mirror,
                 *     passing the --force argument
                 */
 
-               if (!ref->deletion && !is_null_sha1(ref->old_sha1)) {
-                       int why = 0; /* why would this push require --force? */
-
+               else if (!ref->deletion && !is_null_sha1(ref->old_sha1)) {
                        if (!prefixcmp(ref->name, "refs/tags/"))
-                               why = REF_STATUS_REJECT_ALREADY_EXISTS;
+                               reject_reason = REF_STATUS_REJECT_ALREADY_EXISTS;
                        else if (!has_sha1_file(ref->old_sha1))
-                               why = REF_STATUS_REJECT_FETCH_FIRST;
+                               reject_reason = REF_STATUS_REJECT_FETCH_FIRST;
                        else if (!lookup_commit_reference_gently(ref->old_sha1, 1) ||
                                 !lookup_commit_reference_gently(ref->new_sha1, 1))
-                               why = REF_STATUS_REJECT_NEEDS_FORCE;
+                               reject_reason = REF_STATUS_REJECT_NEEDS_FORCE;
                        else if (!ref_newer(ref->new_sha1, ref->old_sha1))
-                               why = REF_STATUS_REJECT_NONFASTFORWARD;
-
-                       if (!force_ref_update)
-                               ref->status = why;
-                       else if (why)
-                               ref->forced_update = 1;
+                               reject_reason = REF_STATUS_REJECT_NONFASTFORWARD;
                }
+
+               /*
+                * "--force" will defeat any rejection implemented
+                * by the rules above.
+                */
+               if (!force_ref_update)
+                       ref->status = reject_reason;
+               else if (reject_reason)
+                       ref->forced_update = 1;
        }
 }
 
@@ -1519,6 +1553,13 @@ static int ignore_symref_update(const char *refname)
        return (flag & REF_ISSYMREF);
 }
 
+/*
+ * Create and return a list of (struct ref) consisting of copies of
+ * each remote_ref that matches refspec.  refspec must be a pattern.
+ * Fill in the copies' peer_ref to describe the local tracking refs to
+ * which they map.  Omit any references that would map to an existing
+ * local symbolic ref.
+ */
 static struct ref *get_expanded_map(const struct ref *remote_refs,
                                    const struct refspec *refspec)
 {
@@ -1526,9 +1567,9 @@ static struct ref *get_expanded_map(const struct ref *remote_refs,
        struct ref *ret = NULL;
        struct ref **tail = &ret;
 
-       char *expn_name;
-
        for (ref = remote_refs; ref; ref = ref->next) {
+               char *expn_name = NULL;
+
                if (strchr(ref->name, '^'))
                        continue; /* a dereference item */
                if (match_name_with_pattern(refspec->src, ref->name,
@@ -1537,12 +1578,12 @@ static struct ref *get_expanded_map(const struct ref *remote_refs,
                        struct ref *cpy = copy_ref(ref);
 
                        cpy->peer_ref = alloc_ref(expn_name);
-                       free(expn_name);
                        if (refspec->force)
                                cpy->peer_ref->force = 1;
                        *tail = cpy;
                        tail = &cpy->next;
                }
+               free(expn_name);
        }
 
        return ret;
@@ -1695,7 +1736,11 @@ int ref_newer(const unsigned char *new_sha1, const unsigned char *old_sha1)
 }
 
 /*
- * Return true if there is anything to report, otherwise false.
+ * Compare a branch with its upstream, and save their differences (number
+ * of commits) in *num_ours and *num_theirs.
+ *
+ * Return 0 if branch has no upstream (no base), -1 if upstream is missing
+ * (with "gone" base), otherwise 1 (with base).
  */
 int stat_tracking_info(struct branch *branch, int *num_ours, int *num_theirs)
 {
@@ -1706,34 +1751,30 @@ int stat_tracking_info(struct branch *branch, int *num_ours, int *num_theirs)
        const char *rev_argv[10], *base;
        int rev_argc;
 
-       /*
-        * Nothing to report unless we are marked to build on top of
-        * somebody else.
-        */
+       /* Cannot stat unless we are marked to build on top of somebody else. */
        if (!branch ||
            !branch->merge || !branch->merge[0] || !branch->merge[0]->dst)
                return 0;
 
-       /*
-        * If what we used to build on no longer exists, there is
-        * nothing to report.
-        */
+       /* Cannot stat if what we used to build on no longer exists */
        base = branch->merge[0]->dst;
        if (read_ref(base, sha1))
-               return 0;
+               return -1;
        theirs = lookup_commit_reference(sha1);
        if (!theirs)
-               return 0;
+               return -1;
 
        if (read_ref(branch->refname, sha1))
-               return 0;
+               return -1;
        ours = lookup_commit_reference(sha1);
        if (!ours)
-               return 0;
+               return -1;
 
        /* are we the same? */
-       if (theirs == ours)
-               return 0;
+       if (theirs == ours) {
+               *num_theirs = *num_ours = 0;
+               return 1;
+       }
 
        /* Run "rev-list --left-right ours...theirs" internally... */
        rev_argc = 0;
@@ -1775,31 +1816,53 @@ int stat_tracking_info(struct branch *branch, int *num_ours, int *num_theirs)
  */
 int format_tracking_info(struct branch *branch, struct strbuf *sb)
 {
-       int num_ours, num_theirs;
+       int ours, theirs;
        const char *base;
+       int upstream_is_gone = 0;
 
-       if (!stat_tracking_info(branch, &num_ours, &num_theirs))
+       switch (stat_tracking_info(branch, &ours, &theirs)) {
+       case 0:
+               /* no base */
                return 0;
+       case -1:
+               /* with "gone" base */
+               upstream_is_gone = 1;
+               break;
+       default:
+               /* with base */
+               break;
+       }
 
        base = branch->merge[0]->dst;
        base = shorten_unambiguous_ref(base, 0);
-       if (!num_theirs) {
+       if (upstream_is_gone) {
+               strbuf_addf(sb,
+                       _("Your branch is based on '%s', but the upstream is gone.\n"),
+                       base);
+               if (advice_status_hints)
+                       strbuf_addf(sb,
+                               _("  (use \"git branch --unset-upstream\" to fixup)\n"));
+       } else if (!ours && !theirs) {
+               strbuf_addf(sb,
+                       _("Your branch is up-to-date with '%s'.\n"),
+                       base);
+       } else if (!theirs) {
                strbuf_addf(sb,
                        Q_("Your branch is ahead of '%s' by %d commit.\n",
                           "Your branch is ahead of '%s' by %d commits.\n",
-                          num_ours),
-                       base, num_ours);
+                          ours),
+                       base, ours);
                if (advice_status_hints)
                        strbuf_addf(sb,
                                _("  (use \"git push\" to publish your local commits)\n"));
-       } else if (!num_ours) {
+       } else if (!ours) {
                strbuf_addf(sb,
                        Q_("Your branch is behind '%s' by %d commit, "
                               "and can be fast-forwarded.\n",
                           "Your branch is behind '%s' by %d commits, "
                               "and can be fast-forwarded.\n",
-                          num_theirs),
-                       base, num_theirs);
+                          theirs),
+                       base, theirs);
                if (advice_status_hints)
                        strbuf_addf(sb,
                                _("  (use \"git pull\" to update your local branch)\n"));
@@ -1811,8 +1874,8 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb)
                           "Your branch and '%s' have diverged,\n"
                               "and have %d and %d different commits each, "
                               "respectively.\n",
-                          num_theirs),
-                       base, num_ours, num_theirs);
+                          theirs),
+                       base, ours, theirs);
                if (advice_status_hints)
                        strbuf_addf(sb,
                                _("  (use \"git pull\" to merge the remote branch into yours)\n"));
@@ -1936,3 +1999,121 @@ struct ref *get_stale_heads(struct refspec *refs, int ref_count, struct ref *fet
        string_list_clear(&ref_names, 0);
        return stale_refs;
 }
+
+/*
+ * Compare-and-swap
+ */
+void clear_cas_option(struct push_cas_option *cas)
+{
+       int i;
+
+       for (i = 0; i < cas->nr; i++)
+               free(cas->entry[i].refname);
+       free(cas->entry);
+       memset(cas, 0, sizeof(*cas));
+}
+
+static struct push_cas *add_cas_entry(struct push_cas_option *cas,
+                                     const char *refname,
+                                     size_t refnamelen)
+{
+       struct push_cas *entry;
+       ALLOC_GROW(cas->entry, cas->nr + 1, cas->alloc);
+       entry = &cas->entry[cas->nr++];
+       memset(entry, 0, sizeof(*entry));
+       entry->refname = xmemdupz(refname, refnamelen);
+       return entry;
+}
+
+int parse_push_cas_option(struct push_cas_option *cas, const char *arg, int unset)
+{
+       const char *colon;
+       struct push_cas *entry;
+
+       if (unset) {
+               /* "--no-<option>" */
+               clear_cas_option(cas);
+               return 0;
+       }
+
+       if (!arg) {
+               /* just "--<option>" */
+               cas->use_tracking_for_rest = 1;
+               return 0;
+       }
+
+       /* "--<option>=refname" or "--<option>=refname:value" */
+       colon = strchrnul(arg, ':');
+       entry = add_cas_entry(cas, arg, colon - arg);
+       if (!*colon)
+               entry->use_tracking = 1;
+       else if (get_sha1(colon + 1, entry->expect))
+               return error("cannot parse expected object name '%s'", colon + 1);
+       return 0;
+}
+
+int parseopt_push_cas_option(const struct option *opt, const char *arg, int unset)
+{
+       return parse_push_cas_option(opt->value, arg, unset);
+}
+
+int is_empty_cas(const struct push_cas_option *cas)
+{
+       return !cas->use_tracking_for_rest && !cas->nr;
+}
+
+/*
+ * Look at remote.fetch refspec and see if we have a remote
+ * tracking branch for the refname there.  Fill its current
+ * value in sha1[].
+ * If we cannot do so, return negative to signal an error.
+ */
+static int remote_tracking(struct remote *remote, const char *refname,
+                          unsigned char sha1[20])
+{
+       char *dst;
+
+       dst = apply_refspecs(remote->fetch, remote->fetch_refspec_nr, refname);
+       if (!dst)
+               return -1; /* no tracking ref for refname at remote */
+       if (read_ref(dst, sha1))
+               return -1; /* we know what the tracking ref is but we cannot read it */
+       return 0;
+}
+
+static void apply_cas(struct push_cas_option *cas,
+                     struct remote *remote,
+                     struct ref *ref)
+{
+       int i;
+
+       /* Find an explicit --<option>=<name>[:<value>] entry */
+       for (i = 0; i < cas->nr; i++) {
+               struct push_cas *entry = &cas->entry[i];
+               if (!refname_match(entry->refname, ref->name, ref_rev_parse_rules))
+                       continue;
+               ref->expect_old_sha1 = 1;
+               if (!entry->use_tracking)
+                       hashcpy(ref->old_sha1_expect, cas->entry[i].expect);
+               else if (remote_tracking(remote, ref->name, ref->old_sha1_expect))
+                       ref->expect_old_no_trackback = 1;
+               return;
+       }
+
+       /* Are we using "--<option>" to cover all? */
+       if (!cas->use_tracking_for_rest)
+               return;
+
+       ref->expect_old_sha1 = 1;
+       if (remote_tracking(remote, ref->name, ref->old_sha1_expect))
+               ref->expect_old_no_trackback = 1;
+}
+
+void apply_push_cas(struct push_cas_option *cas,
+                   struct remote *remote,
+                   struct ref *remote_refs)
+{
+       struct ref *ref;
+       for (ref = remote_refs; ref; ref = ref->next)
+               apply_cas(cas, remote, ref);
+}