fetch: convert prune_refs to take a struct refspec
[gitweb.git] / builtin / fetch.c
index 18704c6cd6c58add7d05a7d134efa3d1dfc86d81..5e46df70ccb235e9c15ca91290a7c196868dfc88 100644 (file)
@@ -60,8 +60,7 @@ static const char *submodule_prefix = "";
 static int recurse_submodules = RECURSE_SUBMODULES_DEFAULT;
 static int recurse_submodules_default = RECURSE_SUBMODULES_ON_DEMAND;
 static int shown_url = 0;
-static int refmap_alloc, refmap_nr;
-static const char **refmap_array;
+static struct refspec refmap = REFSPEC_INIT_FETCH;
 static struct list_objects_filter_options filter_options;
 
 static int git_fetch_config(const char *k, const char *v, void *cb)
@@ -108,14 +107,12 @@ static int gitmodules_fetch_config(const char *var, const char *value, void *cb)
 
 static int parse_refmap_arg(const struct option *opt, const char *arg, int unset)
 {
-       ALLOC_GROW(refmap_array, refmap_nr + 1, refmap_alloc);
-
        /*
         * "git fetch --refmap='' origin foo"
         * can be used to tell the command not to store anywhere
         */
-       if (*arg)
-               refmap_array[refmap_nr++] = arg;
+       refspec_append(&refmap, arg);
+
        return 0;
 }
 
@@ -340,7 +337,7 @@ static void find_non_local_tags(struct transport *transport,
 }
 
 static struct ref *get_ref_map(struct transport *transport,
-                              struct refspec_item *refspecs, int refspec_count,
+                              struct refspec *rs,
                               int tags, int *autotags)
 {
        int i;
@@ -354,15 +351,16 @@ static struct ref *get_ref_map(struct transport *transport,
 
        const struct ref *remote_refs;
 
-       for (i = 0; i < refspec_count; i++) {
-               if (!refspecs[i].exact_sha1) {
-                       const char *glob = strchr(refspecs[i].src, '*');
+       for (i = 0; i < rs->nr; i++) {
+               const struct refspec_item *item = &rs->items[i];
+               if (!item->exact_sha1) {
+                       const char *glob = strchr(item->src, '*');
                        if (glob)
                                argv_array_pushf(&ref_prefixes, "%.*s",
-                                                (int)(glob - refspecs[i].src),
-                                                refspecs[i].src);
+                                                (int)(glob - item->src),
+                                                item->src);
                        else
-                               expand_ref_prefix(&ref_prefixes, refspecs[i].src);
+                               expand_ref_prefix(&ref_prefixes, item->src);
                }
        }
 
@@ -370,13 +368,12 @@ static struct ref *get_ref_map(struct transport *transport,
 
        argv_array_clear(&ref_prefixes);
 
-       if (refspec_count) {
-               struct refspec_item *fetch_refspec;
-               int fetch_refspec_nr;
+       if (rs->nr) {
+               struct refspec *fetch_refspec;
 
-               for (i = 0; i < refspec_count; i++) {
-                       get_fetch_map(remote_refs, &refspecs[i], &tail, 0);
-                       if (refspecs[i].dst && refspecs[i].dst[0])
+               for (i = 0; i < rs->nr; i++) {
+                       get_fetch_map(remote_refs, &rs->items[i], &tail, 0);
+                       if (rs->items[i].dst && rs->items[i].dst[0])
                                *autotags = 1;
                }
                /* Merge everything on the command line (but not --tags) */
@@ -403,17 +400,14 @@ static struct ref *get_ref_map(struct transport *transport,
                 * by ref_remove_duplicates() in favor of one of these
                 * opportunistic entries with FETCH_HEAD_IGNORE.
                 */
-               if (refmap_array) {
-                       fetch_refspec = parse_fetch_refspec(refmap_nr, refmap_array);
-                       fetch_refspec_nr = refmap_nr;
-               } else {
-                       fetch_refspec = transport->remote->fetch.items;
-                       fetch_refspec_nr = transport->remote->fetch.nr;
-               }
+               if (refmap.nr)
+                       fetch_refspec = &refmap;
+               else
+                       fetch_refspec = &transport->remote->fetch;
 
-               for (i = 0; i < fetch_refspec_nr; i++)
-                       get_fetch_map(ref_map, &fetch_refspec[i], &oref_tail, 1);
-       } else if (refmap_array) {
+               for (i = 0; i < fetch_refspec->nr; i++)
+                       get_fetch_map(ref_map, &fetch_refspec->items[i], &oref_tail, 1);
+       } else if (refmap.nr) {
                die("--refmap option is only meaningful with command-line refspec(s).");
        } else {
                /* Use the defaults */
@@ -965,11 +959,11 @@ static int fetch_refs(struct transport *transport, struct ref *ref_map)
        return ret;
 }
 
-static int prune_refs(struct refspec_item *refs, int ref_count, struct ref *ref_map,
-               const char *raw_url)
+static int prune_refs(struct refspec *rs, struct ref *ref_map,
+                     const char *raw_url)
 {
        int url_len, i, result = 0;
-       struct ref *ref, *stale_refs = get_stale_heads(refs, ref_count, ref_map);
+       struct ref *ref, *stale_refs = get_stale_heads(rs->items, rs->nr, ref_map);
        char *url;
        int summary_width = transport_summary_width(stale_refs);
        const char *dangling_msg = dry_run
@@ -1115,7 +1109,7 @@ static void backfill_tags(struct transport *transport, struct ref *ref_map)
 }
 
 static int do_fetch(struct transport *transport,
-                   struct refspec_item *refs, int ref_count)
+                   struct refspec *rs)
 {
        struct string_list existing_refs = STRING_LIST_INIT_DUP;
        struct ref *ref_map;
@@ -1139,7 +1133,7 @@ static int do_fetch(struct transport *transport,
                        goto cleanup;
        }
 
-       ref_map = get_ref_map(transport, refs, ref_count, tags, &autotags);
+       ref_map = get_ref_map(transport, rs, tags, &autotags);
        if (!update_head_ok)
                check_not_current_branch(ref_map);
 
@@ -1163,11 +1157,10 @@ static int do_fetch(struct transport *transport,
                 * explicitly (via command line or configuration); we
                 * don't care whether --tags was specified.
                 */
-               if (ref_count) {
-                       prune_refs(refs, ref_count, ref_map, transport->url);
+               if (rs->nr) {
+                       prune_refs(rs, ref_map, transport->url);
                } else {
-                       prune_refs(transport->remote->fetch.items,
-                                  transport->remote->fetch.nr,
+                       prune_refs(&transport->remote->fetch,
                                   ref_map,
                                   transport->url);
                }
@@ -1413,7 +1406,7 @@ static int fetch_one(struct remote *remote, int argc, const char **argv, int pru
 
        sigchain_push_common(unlock_pack_on_signal);
        atexit(unlock_pack);
-       exit_code = do_fetch(gtransport, rs.items, rs.nr);
+       exit_code = do_fetch(gtransport, &rs);
        refspec_clear(&rs);
        transport_disconnect(gtransport);
        gtransport = NULL;