Rewrite Tcl/Tk interpreter path for the GUI tools.
[gitweb.git] / builtin-fetch--tool.c
index 705a6649a996202f6f7a9da52ddd0292a55d6637..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)
+{
+       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", type,
+       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,16 +181,18 @@ 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);
 }
 
@@ -323,6 +349,93 @@ static int parse_reflist(const char *reflist)
        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;
@@ -357,12 +470,6 @@ int cmd_fetch__tool(int argc, const char **argv, const char *prefix)
                fclose(fp);
                return result;
        }
-       if (!strcmp("update-local-ref", argv[1])) {
-               if (argc != 5)
-                       return error("update-local-ref takes 3 args");
-               return update_local_ref(argv[2], argv[3], argv[4],
-                                       verbose, force);
-       }
        if (!strcmp("native-store", argv[1])) {
                int result;
                FILE *fp;
@@ -376,9 +483,22 @@ int cmd_fetch__tool(int argc, const char **argv, const char *prefix)
                return result;
        }
        if (!strcmp("parse-reflist", argv[1])) {
+               const char *reflist;
                if (argc != 3)
                        return error("parse-reflist takes 1 arg");
-               return parse_reflist(argv[2]);
+               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]);