fetch: Check for a "^{}" suffix with suffixcmp()
[gitweb.git] / builtin-fetch.c
index 5b7db616dcf5cc3bb178b2c4dbb989322c6b2374..0acf80991b57226d44305fd51f2c32544130dc91 100644 (file)
@@ -322,7 +322,10 @@ static int store_updated_refs(const char *raw_url, const char *remote_name,
        if (!fp)
                return error("cannot open %s: %s\n", filename, strerror(errno));
 
-       url = transport_anonymize_url(raw_url);
+       if (raw_url)
+               url = transport_anonymize_url(raw_url);
+       else
+               url = xstrdup("foreign");
        for (rm = ref_map; rm; rm = rm->next) {
                struct ref *ref = NULL;
 
@@ -585,7 +588,7 @@ static void find_non_local_tags(struct transport *transport,
                 * to fetch then we can mark the ref entry in the list
                 * as one to ignore by setting util to NULL.
                 */
-               if (!strcmp(ref->name + strlen(ref->name) - 3, "^{}")) {
+               if (!suffixcmp(ref->name, "^{}")) {
                        if (item && !has_sha1_file(ref->old_sha1) &&
                            !will_fetch(head, ref->old_sha1) &&
                            !has_sha1_file(item->util) &&
@@ -648,6 +651,17 @@ static void check_not_current_branch(struct ref *ref_map)
                            "of non-bare repository", current_branch->refname);
 }
 
+static int truncate_fetch_head(void)
+{
+       char *filename = git_path("FETCH_HEAD");
+       FILE *fp = fopen(filename, "w");
+
+       if (!fp)
+               return error("cannot open %s: %s\n", filename, strerror(errno));
+       fclose(fp);
+       return 0;
+}
+
 static int do_fetch(struct transport *transport,
                    struct refspec *refs, int ref_count)
 {
@@ -669,11 +683,9 @@ static int do_fetch(struct transport *transport,
 
        /* if not appending, truncate FETCH_HEAD */
        if (!append && !dry_run) {
-               char *filename = git_path("FETCH_HEAD");
-               FILE *fp = fopen(filename, "w");
-               if (!fp)
-                       return error("cannot open %s: %s\n", filename, strerror(errno));
-               fclose(fp);
+               int errcode = truncate_fetch_head();
+               if (errcode)
+                       return errcode;
        }
 
        ref_map = get_ref_map(transport, refs, ref_count, tags, &autotags);
@@ -781,13 +793,19 @@ static int add_remote_or_group(const char *name, struct string_list *list)
 static int fetch_multiple(struct string_list *list)
 {
        int i, result = 0;
-       const char *argv[] = { "fetch", NULL, NULL, NULL, NULL, NULL, NULL };
-       int argc = 1;
+       const char *argv[11] = { "fetch", "--append" };
+       int argc = 2;
 
        if (dry_run)
                argv[argc++] = "--dry-run";
        if (prune)
                argv[argc++] = "--prune";
+       if (update_head_ok)
+               argv[argc++] = "--update-head-ok";
+       if (force)
+               argv[argc++] = "--force";
+       if (keep)
+               argv[argc++] = "--keep";
        if (verbosity >= 2)
                argv[argc++] = "-v";
        if (verbosity >= 1)
@@ -795,9 +813,16 @@ static int fetch_multiple(struct string_list *list)
        else if (verbosity < 0)
                argv[argc++] = "-q";
 
+       if (!append && !dry_run) {
+               int errcode = truncate_fetch_head();
+               if (errcode)
+                       return errcode;
+       }
+
        for (i = 0; i < list->nr; i++) {
                const char *name = list->items[i].string;
                argv[argc] = name;
+               argv[argc + 1] = NULL;
                if (verbosity >= 0)
                        printf("Fetching %s\n", name);
                if (run_command_v_opt(argv, RUN_GIT_CMD)) {
@@ -819,7 +844,7 @@ static int fetch_one(struct remote *remote, int argc, const char **argv)
        if (!remote)
                die("Where do you want to fetch from today?");
 
-       transport = transport_get(remote, remote->url[0]);
+       transport = transport_get(remote, NULL);
        if (verbosity >= 2)
                transport->verbose = verbosity <= 3 ? verbosity : 3;
        if (verbosity < 0)