]> asedeno.scripts.mit.edu Git - git.git/blobdiff - builtin-fetch.c
Do not rely on the exit status of "unset" for unset variables
[git.git] / builtin-fetch.c
index 31e138eab82a4a710916ff88386bd82bcf2f11d6..f6d16fe9662c01f44795837606d48ddf7c9d414e 100644 (file)
@@ -14,7 +14,7 @@ static const char fetch_usage[] = "git-fetch [-a | --append] [--upload-pack <upl
 
 static int append, force, tags, no_tags, update_head_ok, verbose, quiet;
 static const char *depth;
-static char *default_rla = NULL;
+static struct strbuf default_rla = STRBUF_INIT;
 static struct transport *transport;
 
 static void unlock_pack(void)
@@ -142,7 +142,7 @@ static int s_update_ref(const char *action,
        static struct ref_lock *lock;
 
        if (!rla)
-               rla = default_rla;
+               rla = default_rla.buf;
        snprintf(msg, sizeof(msg), "%s: %s", rla, action);
        lock = lock_any_ref_for_update(ref->name,
                                       check_old ? ref->old_sha1 : NULL, 0);
@@ -255,7 +255,7 @@ static int update_local_ref(struct ref *ref,
        }
 }
 
-static void store_updated_refs(const char *url, struct ref *ref_map)
+static int store_updated_refs(const char *url, struct ref *ref_map)
 {
        FILE *fp;
        struct commit *commit;
@@ -263,8 +263,11 @@ static void store_updated_refs(const char *url, struct ref *ref_map)
        char note[1024];
        const char *what, *kind;
        struct ref *rm;
+       char *filename = git_path("FETCH_HEAD");
 
-       fp = fopen(git_path("FETCH_HEAD"), "a");
+       fp = fopen(filename, "a");
+       if (!fp)
+               return error("cannot open %s: %s\n", filename, strerror(errno));
        for (rm = ref_map; rm; rm = rm->next) {
                struct ref *ref = NULL;
 
@@ -335,6 +338,7 @@ static void store_updated_refs(const char *url, struct ref *ref_map)
                }
        }
        fclose(fp);
+       return 0;
 }
 
 /*
@@ -404,7 +408,7 @@ static int fetch_refs(struct transport *transport, struct ref *ref_map)
        if (ret)
                ret = transport_fetch_refs(transport, ref_map);
        if (!ret)
-               store_updated_refs(transport->url, ref_map);
+               ret |= store_updated_refs(transport->url, ref_map);
        transport_unlock_pack(transport);
        return ret;
 }
@@ -487,8 +491,13 @@ static int do_fetch(struct transport *transport,
                die("Don't know how to fetch from %s", transport->url);
 
        /* if not appending, truncate FETCH_HEAD */
-       if (!append)
-               fclose(fopen(git_path("FETCH_HEAD"), "w"));
+       if (!append) {
+               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);
+       }
 
        ref_map = get_ref_map(transport, refs, ref_count, tags, &autotags);
 
@@ -534,16 +543,19 @@ static void set_option(const char *name, const char *value)
 int cmd_fetch(int argc, const char **argv, const char *prefix)
 {
        struct remote *remote;
-       int i, j, rla_offset;
+       int i;
        static const char **refs = NULL;
        int ref_nr = 0;
-       int cmd_len = 0;
        const char *upload_pack = NULL;
        int keep = 0;
 
+       /* Record the command line for the reflog */
+       strbuf_addstr(&default_rla, "fetch");
+       for (i = 1; i < argc; i++)
+               strbuf_addf(&default_rla, " %s", argv[i]);
+
        for (i = 1; i < argc; i++) {
                const char *arg = argv[i];
-               cmd_len += strlen(arg);
 
                if (arg[0] != '-')
                        break;
@@ -604,17 +616,6 @@ int cmd_fetch(int argc, const char **argv, const char *prefix)
                usage(fetch_usage);
        }
 
-       for (j = i; j < argc; j++)
-               cmd_len += strlen(argv[j]);
-
-       default_rla = xmalloc(cmd_len + 5 + argc + 1);
-       sprintf(default_rla, "fetch");
-       rla_offset = strlen(default_rla);
-       for (j = 1; j < argc; j++) {
-               sprintf(default_rla + rla_offset, " %s", argv[j]);
-               rla_offset += strlen(argv[j]) + 1;
-       }
-
        if (i == argc)
                remote = remote_get(NULL);
        else