worktree: add 'list' command
[gitweb.git] / builtin / fetch.c
index 7910419c93275c58f4881202905ae1f24bfe8c91..9a3869f4ffd81f84f738e54c6405aaedb2514f18 100644 (file)
@@ -11,6 +11,7 @@
 #include "run-command.h"
 #include "parse-options.h"
 #include "sigchain.h"
+#include "submodule-config.h"
 #include "submodule.h"
 #include "connected.h"
 #include "argv-array.h"
@@ -179,13 +180,15 @@ static void add_merge_config(struct ref **head,
        }
 }
 
-static int add_existing(const char *refname, const unsigned char *sha1,
+static int add_existing(const char *refname, const struct object_id *oid,
                        int flag, void *cbdata)
 {
        struct string_list *list = (struct string_list *)cbdata;
        struct string_list_item *item = string_list_insert(list, refname);
-       item->util = xmalloc(20);
-       hashcpy(item->util, sha1);
+       struct object_id *old_oid = xmalloc(sizeof(*old_oid));
+
+       oidcpy(old_oid, oid);
+       item->util = old_oid;
        return 0;
 }
 
@@ -589,7 +592,7 @@ static int store_updated_refs(const char *raw_url, const char *remote_name,
        const char *what, *kind;
        struct ref *rm;
        char *url;
-       const char *filename = dry_run ? "/dev/null" : git_path("FETCH_HEAD");
+       const char *filename = dry_run ? "/dev/null" : git_path_fetch_head();
        int want_status;
 
        fp = fopen(filename, "a");
@@ -788,20 +791,29 @@ static int prune_refs(struct refspec *refs, int ref_count, struct ref *ref_map,
        if (4 < i && !strncmp(".git", url + i - 3, 4))
                url_len = i - 3;
 
-       for (ref = stale_refs; ref; ref = ref->next) {
-               if (!dry_run)
-                       result |= delete_ref(ref->name, NULL, 0);
-               if (verbosity >= 0 && !shown_url) {
-                       fprintf(stderr, _("From %.*s\n"), url_len, url);
-                       shown_url = 1;
-               }
-               if (verbosity >= 0) {
+       if (!dry_run) {
+               struct string_list refnames = STRING_LIST_INIT_NODUP;
+
+               for (ref = stale_refs; ref; ref = ref->next)
+                       string_list_append(&refnames, ref->name);
+
+               result = delete_refs(&refnames);
+               string_list_clear(&refnames, 0);
+       }
+
+       if (verbosity >= 0) {
+               for (ref = stale_refs; ref; ref = ref->next) {
+                       if (!shown_url) {
+                               fprintf(stderr, _("From %.*s\n"), url_len, url);
+                               shown_url = 1;
+                       }
                        fprintf(stderr, " x %-*s %-*s -> %s\n",
                                TRANSPORT_SUMMARY(_("[deleted]")),
                                REFCOL_WIDTH, _("(none)"), prettify_refname(ref->name));
                        warn_dangling_symref(stderr, dangling_msg, ref->name);
                }
        }
+
        free(url);
        free_refs(stale_refs);
        return result;
@@ -823,7 +835,7 @@ static void check_not_current_branch(struct ref *ref_map)
 
 static int truncate_fetch_head(void)
 {
-       const char *filename = git_path("FETCH_HEAD");
+       const char *filename = git_path_fetch_head();
        FILE *fp = fopen(filename, "w");
 
        if (!fp)
@@ -913,9 +925,10 @@ static int do_fetch(struct transport *transport,
                        struct string_list_item *peer_item =
                                string_list_lookup(&existing_refs,
                                                   rm->peer_ref->name);
-                       if (peer_item)
-                               hashcpy(rm->peer_ref->old_sha1,
-                                       peer_item->util);
+                       if (peer_item) {
+                               struct object_id *old_oid = peer_item->util;
+                               hashcpy(rm->peer_ref->old_sha1, old_oid->hash);
+                       }
                }
        }
 
@@ -976,17 +989,15 @@ static int get_remote_group(const char *key, const char *value, void *priv)
 {
        struct remote_group_data *g = priv;
 
-       if (starts_with(key, "remotes.") &&
-                       !strcmp(key + 8, g->name)) {
+       if (skip_prefix(key, "remotes.", &key) && !strcmp(key, g->name)) {
                /* split list by white space */
-               int space = strcspn(value, " \t\n");
                while (*value) {
-                       if (space > 1) {
+                       size_t wordlen = strcspn(value, " \t\n");
+
+                       if (wordlen >= 1)
                                string_list_append(g->list,
-                                                  xstrndup(value, space));
-                       }
-                       value += space + (value[space] != '\0');
-                       space = strcspn(value, " \t\n");
+                                                  xstrndup(value, wordlen));
+                       value += wordlen + (value[wordlen] != '\0');
                }
        }