repack_without_refs(): make the refnames argument a string_list
[gitweb.git] / builtin / remote.c
index c9102e8fe94b41af5136a9cc23db939effae147a..63a67098a074bd8df7d62e681b4608511b197373 100644 (file)
@@ -250,9 +250,7 @@ static struct string_list branch_list;
 
 static const char *abbrev_ref(const char *name, const char *prefix)
 {
-       const char *abbrev = skip_prefix(name, prefix);
-       if (abbrev)
-               return abbrev;
+       skip_prefix(name, prefix, &name);
        return name;
 }
 #define abbrev_branch(name) abbrev_ref((name), "refs/heads/")
@@ -265,16 +263,17 @@ static int config_read_branches(const char *key, const char *value, void *cb)
                struct string_list_item *item;
                struct branch_info *info;
                enum { REMOTE, MERGE, REBASE } type;
+               size_t key_len;
 
                key += 7;
-               if (ends_with(key, ".remote")) {
-                       name = xstrndup(key, strlen(key) - 7);
+               if (strip_suffix(key, ".remote", &key_len)) {
+                       name = xmemdupz(key, key_len);
                        type = REMOTE;
-               } else if (ends_with(key, ".merge")) {
-                       name = xstrndup(key, strlen(key) - 6);
+               } else if (strip_suffix(key, ".merge", &key_len)) {
+                       name = xmemdupz(key, key_len);
                        type = MERGE;
-               } else if (ends_with(key, ".rebase")) {
-                       name = xstrndup(key, strlen(key) - 7);
+               } else if (strip_suffix(key, ".rebase", &key_len)) {
+                       name = xmemdupz(key, key_len);
                        type = REBASE;
                } else
                        return 0;
@@ -568,7 +567,8 @@ static int read_remote_branches(const char *refname,
        strbuf_addf(&buf, "refs/remotes/%s/", rename->old);
        if (starts_with(refname, buf.buf)) {
                item = string_list_append(rename->remote_branches, xstrdup(refname));
-               symref = resolve_ref_unsafe(refname, orig_sha1, 1, &flag);
+               symref = resolve_ref_unsafe(refname, RESOLVE_REF_READING,
+                                           orig_sha1, &flag);
                if (flag & REF_ISSYMREF)
                        item->util = xstrdup(symref);
                else
@@ -704,7 +704,7 @@ static int mv(int argc, const char **argv)
                int flag = 0;
                unsigned char sha1[20];
 
-               read_ref_full(item->string, sha1, 1, &flag);
+               read_ref_full(item->string, RESOLVE_REF_READING, sha1, &flag);
                if (!(flag & REF_ISSYMREF))
                        continue;
                if (delete_ref(item->string, NULL, REF_NODEREF))
@@ -749,14 +749,12 @@ static int mv(int argc, const char **argv)
 
 static int remove_branches(struct string_list *branches)
 {
-       const char **branch_names;
+       struct strbuf err = STRBUF_INIT;
        int i, result = 0;
 
-       branch_names = xmalloc(branches->nr * sizeof(*branch_names));
-       for (i = 0; i < branches->nr; i++)
-               branch_names[i] = branches->items[i].string;
-       result |= repack_without_refs(branch_names, branches->nr);
-       free(branch_names);
+       if (repack_without_refs(branches, &err))
+               result |= error("%s", err.buf);
+       strbuf_release(&err);
 
        for (i = 0; i < branches->nr; i++) {
                struct string_list_item *item = branches->items + i;
@@ -1314,7 +1312,6 @@ static int prune_remote(const char *remote, int dry_run)
        int result = 0, i;
        struct ref_states states;
        struct string_list delete_refs_list = STRING_LIST_INIT_NODUP;
-       const char **delete_refs;
        const char *dangling_msg = dry_run
                ? _(" %s will become dangling!")
                : _(" %s has become dangling!");
@@ -1322,25 +1319,33 @@ static int prune_remote(const char *remote, int dry_run)
        memset(&states, 0, sizeof(states));
        get_remote_ref_states(remote, &states, GET_REF_STATES);
 
-       if (states.stale.nr) {
-               printf_ln(_("Pruning %s"), remote);
-               printf_ln(_("URL: %s"),
-                      states.remote->url_nr
-                      ? states.remote->url[0]
-                      : _("(no URL)"));
-
-               delete_refs = xmalloc(states.stale.nr * sizeof(*delete_refs));
-               for (i = 0; i < states.stale.nr; i++)
-                       delete_refs[i] = states.stale.items[i].util;
-               if (!dry_run)
-                       result |= repack_without_refs(delete_refs, states.stale.nr);
-               free(delete_refs);
+       if (!states.stale.nr) {
+               free_remote_ref_states(&states);
+               return 0;
        }
 
+       printf_ln(_("Pruning %s"), remote);
+       printf_ln(_("URL: %s"),
+                 states.remote->url_nr
+                 ? states.remote->url[0]
+                 : _("(no URL)"));
+
        for (i = 0; i < states.stale.nr; i++) {
                const char *refname = states.stale.items[i].util;
 
-               string_list_insert(&delete_refs_list, refname);
+               string_list_append(&delete_refs_list, refname);
+       }
+       sort_string_list(&delete_refs_list);
+
+       if (!dry_run) {
+               struct strbuf err = STRBUF_INIT;
+               if (repack_without_refs(&delete_refs_list, &err))
+                       result |= error("%s", err.buf);
+               strbuf_release(&err);
+       }
+
+       for (i = 0; i < states.stale.nr; i++) {
+               const char *refname = states.stale.items[i].util;
 
                if (!dry_run)
                        result |= delete_ref(refname, NULL, 0);
@@ -1354,8 +1359,8 @@ static int prune_remote(const char *remote, int dry_run)
        }
 
        warn_dangling_symrefs(stdout, dangling_msg, &delete_refs_list);
-       string_list_clear(&delete_refs_list, 0);
 
+       string_list_clear(&delete_refs_list, 0);
        free_remote_ref_states(&states);
        return result;
 }