Code

sha1_file.c (write_sha1_from_fd): Detect close failure.
[git.git] / builtin-fetch--tool.c
index 3075ba04af7b9904911be188d827ddf35d756f46..e9d676455078b40138ad4716a912c4b2b5e10c2e 100644 (file)
@@ -2,9 +2,31 @@
 #include "refs.h"
 #include "commit.h"
 
-static void show_new(char *type, unsigned char *sha1_new)
+#define CHUNK_SIZE 1024
+
+static char *get_stdin(void)
 {
-       fprintf(stderr, "  %s: %s\n", type,
+       int offset = 0;
+       char *data = xmalloc(CHUNK_SIZE);
+
+       while (1) {
+               int cnt = xread(0, data + offset, CHUNK_SIZE);
+               if (cnt < 0)
+                       die("error reading standard input: %s",
+                           strerror(errno));
+               if (cnt == 0) {
+                       data[offset] = 0;
+                       break;
+               }
+               offset += cnt;
+               data = xrealloc(data, offset + CHUNK_SIZE);
+       }
+       return data;
+}
+
+static void show_new(enum object_type type, unsigned char *sha1_new)
+{
+       fprintf(stderr, "  %s: %s\n", typename(type),
                find_unique_abbrev(sha1_new, DEFAULT_ABBREV));
 }
 
@@ -36,14 +58,16 @@ static int update_local_ref(const char *name,
                            const char *note,
                            int verbose, int force)
 {
-       char type[20];
        unsigned char sha1_old[20], sha1_new[20];
        char oldh[41], newh[41];
        struct commit *current, *updated;
+       enum object_type type;
 
        if (get_sha1_hex(new_head, sha1_new))
                die("malformed object name %s", new_head);
-       if (sha1_object_info(sha1_new, type, NULL))
+
+       type = sha1_object_info(sha1_new, NULL);
+       if (type < 0)
                die("object %s not found", new_head);
 
        if (!*name) {
@@ -157,19 +181,261 @@ static int append_fetch_head(FILE *fp,
        remote_len = i + 1;
        if (4 < i && !strncmp(".git", remote + i - 3, 4))
                remote_len = i - 3;
-       note_len = sprintf(note, "%s\t%s\t",
-                          sha1_to_hex(commit ? commit->object.sha1 : sha1),
-                          not_for_merge ? "not-for-merge" : "");
+
+       note_len = 0;
        if (*what) {
                if (*kind)
                        note_len += sprintf(note + note_len, "%s ", kind);
                note_len += sprintf(note + note_len, "'%s' of ", what);
        }
        note_len += sprintf(note + note_len, "%.*s", remote_len, remote);
-       fprintf(fp, "%s\n", note);
+       fprintf(fp, "%s\t%s\t%s\n",
+               sha1_to_hex(commit ? commit->object.sha1 : sha1),
+               not_for_merge ? "not-for-merge" : "",
+               note);
        return update_local_ref(local_name, head, note, verbose, force);
 }
 
+static char *keep;
+static void remove_keep(void)
+{
+       if (keep && *keep)
+               unlink(keep);
+}
+
+static void remove_keep_on_signal(int signo)
+{
+       remove_keep();
+       signal(SIGINT, SIG_DFL);
+       raise(signo);
+}
+
+static char *find_local_name(const char *remote_name, const char *refs,
+                            int *force_p, int *not_for_merge_p)
+{
+       const char *ref = refs;
+       int len = strlen(remote_name);
+
+       while (ref) {
+               const char *next;
+               int single_force, not_for_merge;
+
+               while (*ref == '\n')
+                       ref++;
+               if (!*ref)
+                       break;
+               next = strchr(ref, '\n');
+
+               single_force = not_for_merge = 0;
+               if (*ref == '+') {
+                       single_force = 1;
+                       ref++;
+               }
+               if (*ref == '.') {
+                       not_for_merge = 1;
+                       ref++;
+                       if (*ref == '+') {
+                               single_force = 1;
+                               ref++;
+                       }
+               }
+               if (!strncmp(remote_name, ref, len) && ref[len] == ':') {
+                       const char *local_part = ref + len + 1;
+                       char *ret;
+                       int retlen;
+
+                       if (!next)
+                               retlen = strlen(local_part);
+                       else
+                               retlen = next - local_part;
+                       ret = xmalloc(retlen + 1);
+                       memcpy(ret, local_part, retlen);
+                       ret[retlen] = 0;
+                       *force_p = single_force;
+                       *not_for_merge_p = not_for_merge;
+                       return ret;
+               }
+               ref = next;
+       }
+       return NULL;
+}
+
+static int fetch_native_store(FILE *fp,
+                             const char *remote,
+                             const char *remote_nick,
+                             const char *refs,
+                             int verbose, int force)
+{
+       char buffer[1024];
+       int err = 0;
+
+       signal(SIGINT, remove_keep_on_signal);
+       atexit(remove_keep);
+
+       while (fgets(buffer, sizeof(buffer), stdin)) {
+               int len;
+               char *cp;
+               char *local_name;
+               int single_force, not_for_merge;
+
+               for (cp = buffer; *cp && !isspace(*cp); cp++)
+                       ;
+               if (*cp)
+                       *cp++ = 0;
+               len = strlen(cp);
+               if (len && cp[len-1] == '\n')
+                       cp[--len] = 0;
+               if (!strcmp(buffer, "failed"))
+                       die("Fetch failure: %s", remote);
+               if (!strcmp(buffer, "pack"))
+                       continue;
+               if (!strcmp(buffer, "keep")) {
+                       char *od = get_object_directory();
+                       int len = strlen(od) + strlen(cp) + 50;
+                       keep = xmalloc(len);
+                       sprintf(keep, "%s/pack/pack-%s.keep", od, cp);
+                       continue;
+               }
+
+               local_name = find_local_name(cp, refs,
+                                            &single_force, &not_for_merge);
+               if (!local_name)
+                       continue;
+               err |= append_fetch_head(fp,
+                                        buffer, remote, cp, remote_nick,
+                                        local_name, not_for_merge,
+                                        verbose, force || single_force);
+       }
+       return err;
+}
+
+static int parse_reflist(const char *reflist)
+{
+       const char *ref;
+
+       printf("refs='");
+       for (ref = reflist; ref; ) {
+               const char *next;
+               while (*ref && isspace(*ref))
+                       ref++;
+               if (!*ref)
+                       break;
+               for (next = ref; *next && !isspace(*next); next++)
+                       ;
+               printf("\n%.*s", (int)(next - ref), ref);
+               ref = next;
+       }
+       printf("'\n");
+
+       printf("rref='");
+       for (ref = reflist; ref; ) {
+               const char *next, *colon;
+               while (*ref && isspace(*ref))
+                       ref++;
+               if (!*ref)
+                       break;
+               for (next = ref; *next && !isspace(*next); next++)
+                       ;
+               if (*ref == '.')
+                       ref++;
+               if (*ref == '+')
+                       ref++;
+               colon = strchr(ref, ':');
+               putchar('\n');
+               printf("%.*s", (int)((colon ? colon : next) - ref), ref);
+               ref = next;
+       }
+       printf("'\n");
+       return 0;
+}
+
+static int expand_refs_wildcard(const char *ls_remote_result, int numrefs,
+                               const char **refs)
+{
+       int i, matchlen, replacelen;
+       int found_one = 0;
+       const char *remote = *refs++;
+       numrefs--;
+
+       if (numrefs == 0) {
+               fprintf(stderr, "Nothing specified for fetching with remote.%s.fetch\n",
+                       remote);
+               printf("empty\n");
+       }
+
+       for (i = 0; i < numrefs; i++) {
+               const char *ref = refs[i];
+               const char *lref = ref;
+               const char *colon;
+               const char *tail;
+               const char *ls;
+               const char *next;
+
+               if (*lref == '+')
+                       lref++;
+               colon = strchr(lref, ':');
+               tail = lref + strlen(lref);
+               if (!(colon &&
+                     2 < colon - lref &&
+                     colon[-1] == '*' &&
+                     colon[-2] == '/' &&
+                     2 < tail - (colon + 1) &&
+                     tail[-1] == '*' &&
+                     tail[-2] == '/')) {
+                       /* not a glob */
+                       if (!found_one++)
+                               printf("explicit\n");
+                       printf("%s\n", ref);
+                       continue;
+               }
+
+               /* glob */
+               if (!found_one++)
+                       printf("glob\n");
+
+               /* lref to colon-2 is remote hierarchy name;
+                * colon+1 to tail-2 is local.
+                */
+               matchlen = (colon-1) - lref;
+               replacelen = (tail-1) - (colon+1);
+               for (ls = ls_remote_result; ls; ls = next) {
+                       const char *eol;
+                       unsigned char sha1[20];
+                       int namelen;
+
+                       while (*ls && isspace(*ls))
+                               ls++;
+                       next = strchr(ls, '\n');
+                       eol = !next ? (ls + strlen(ls)) : next;
+                       if (!memcmp("^{}", eol-3, 3))
+                               continue;
+                       if (eol - ls < 40)
+                               continue;
+                       if (get_sha1_hex(ls, sha1))
+                               continue;
+                       ls += 40;
+                       while (ls < eol && isspace(*ls))
+                               ls++;
+                       /* ls to next (or eol) is the name.
+                        * is it identical to lref to colon-2?
+                        */
+                       if ((eol - ls) <= matchlen ||
+                           strncmp(ls, lref, matchlen))
+                               continue;
+
+                       /* Yes, it is a match */
+                       namelen = eol - ls;
+                       if (lref != ref)
+                               putchar('+');
+                       printf("%.*s:%.*s%.*s\n",
+                              namelen, ls,
+                              replacelen, colon + 1,
+                              namelen - matchlen, ls + matchlen);
+               }
+       }
+       return 0;
+}
+
 int cmd_fetch__tool(int argc, const char **argv, const char *prefix)
 {
        int verbose = 0;
@@ -204,11 +470,36 @@ int cmd_fetch__tool(int argc, const char **argv, const char *prefix)
                fclose(fp);
                return result;
        }
-       if (!strcmp("update-local-ref", argv[1])) {
+       if (!strcmp("native-store", argv[1])) {
+               int result;
+               FILE *fp;
+
                if (argc != 5)
-                       return error("update-local-ref takes 3 args");
-               return update_local_ref(argv[2], argv[3], argv[4],
-                                       verbose, force);
+                       return error("fetch-native-store takes 3 args");
+               fp = fopen(git_path("FETCH_HEAD"), "a");
+               result = fetch_native_store(fp, argv[2], argv[3], argv[4],
+                                           verbose, force);
+               fclose(fp);
+               return result;
+       }
+       if (!strcmp("parse-reflist", argv[1])) {
+               const char *reflist;
+               if (argc != 3)
+                       return error("parse-reflist takes 1 arg");
+               reflist = argv[2];
+               if (!strcmp(reflist, "-"))
+                       reflist = get_stdin();
+               return parse_reflist(reflist);
        }
+       if (!strcmp("expand-refs-wildcard", argv[1])) {
+               const char *reflist;
+               if (argc < 4)
+                       return error("expand-refs-wildcard takes at least 2 args");
+               reflist = argv[2];
+               if (!strcmp(reflist, "-"))
+                       reflist = get_stdin();
+               return expand_refs_wildcard(reflist, argc - 3, argv + 3);
+       }
+
        return error("Unknown subcommand: %s", argv[1]);
 }