2b3145bc725bc8c7948cc2229441130b8fe4027c
   1/*
   2 * GIT - the stupid content tracker
   3 *
   4 * Copyright (c) Junio C Hamano, 2006, 2009
   5 */
   6#include "builtin.h"
   7#include "quote.h"
   8#include "tree.h"
   9#include "parse-options.h"
  10
  11static struct treeent {
  12        unsigned mode;
  13        unsigned char sha1[20];
  14        int len;
  15        char name[FLEX_ARRAY];
  16} **entries;
  17static int alloc, used;
  18
  19static void append_to_tree(unsigned mode, unsigned char *sha1, char *path)
  20{
  21        struct treeent *ent;
  22        int len = strlen(path);
  23        if (strchr(path, '/'))
  24                die("path %s contains slash", path);
  25
  26        if (alloc <= used) {
  27                alloc = alloc_nr(used);
  28                entries = xrealloc(entries, sizeof(*entries) * alloc);
  29        }
  30        ent = entries[used++] = xmalloc(sizeof(**entries) + len + 1);
  31        ent->mode = mode;
  32        ent->len = len;
  33        hashcpy(ent->sha1, sha1);
  34        memcpy(ent->name, path, len+1);
  35}
  36
  37static int ent_compare(const void *a_, const void *b_)
  38{
  39        struct treeent *a = *(struct treeent **)a_;
  40        struct treeent *b = *(struct treeent **)b_;
  41        return base_name_compare(a->name, a->len, a->mode,
  42                                 b->name, b->len, b->mode);
  43}
  44
  45static void write_tree(unsigned char *sha1)
  46{
  47        struct strbuf buf;
  48        size_t size;
  49        int i;
  50
  51        qsort(entries, used, sizeof(*entries), ent_compare);
  52        for (size = i = 0; i < used; i++)
  53                size += 32 + entries[i]->len;
  54
  55        strbuf_init(&buf, size);
  56        for (i = 0; i < used; i++) {
  57                struct treeent *ent = entries[i];
  58                strbuf_addf(&buf, "%o %s%c", ent->mode, ent->name, '\0');
  59                strbuf_add(&buf, ent->sha1, 20);
  60        }
  61
  62        write_sha1_file(buf.buf, buf.len, tree_type, sha1);
  63}
  64
  65static const char *mktree_usage[] = {
  66        "git mktree [-z]",
  67        NULL
  68};
  69
  70int cmd_mktree(int ac, const char **av, const char *prefix)
  71{
  72        struct strbuf sb = STRBUF_INIT;
  73        struct strbuf p_uq = STRBUF_INIT;
  74        unsigned char sha1[20];
  75        int line_termination = '\n';
  76        const struct option option[] = {
  77                OPT_SET_INT('z', NULL, &line_termination, "input is NUL terminated", '\0'),
  78                OPT_END()
  79        };
  80
  81        ac = parse_options(ac, av, option, mktree_usage, 0);
  82
  83        while (strbuf_getline(&sb, stdin, line_termination) != EOF) {
  84                char *ptr, *ntr;
  85                unsigned mode;
  86                enum object_type type;
  87                char *path;
  88
  89                ptr = sb.buf;
  90                /*
  91                 * Read non-recursive ls-tree output format:
  92                 *     mode SP type SP sha1 TAB name
  93                 */
  94                mode = strtoul(ptr, &ntr, 8);
  95                if (ptr == ntr || !ntr || *ntr != ' ')
  96                        die("input format error: %s", sb.buf);
  97                ptr = ntr + 1; /* type */
  98                ntr = strchr(ptr, ' ');
  99                if (!ntr || sb.buf + sb.len <= ntr + 40 ||
 100                    ntr[41] != '\t' ||
 101                    get_sha1_hex(ntr + 1, sha1))
 102                        die("input format error: %s", sb.buf);
 103                type = sha1_object_info(sha1, NULL);
 104                if (type < 0)
 105                        die("object %s unavailable", sha1_to_hex(sha1));
 106                *ntr++ = 0; /* now at the beginning of SHA1 */
 107                if (type != type_from_string(ptr))
 108                        die("object type %s mismatch (%s)", ptr, typename(type));
 109
 110                path = ntr + 41;  /* at the beginning of name */
 111                if (line_termination && path[0] == '"') {
 112                        strbuf_reset(&p_uq);
 113                        if (unquote_c_style(&p_uq, path, NULL)) {
 114                                die("invalid quoting");
 115                        }
 116                        path = p_uq.buf;
 117                }
 118
 119                append_to_tree(mode, sha1, path);
 120        }
 121        strbuf_release(&p_uq);
 122        strbuf_release(&sb);
 123
 124        write_tree(sha1);
 125        puts(sha1_to_hex(sha1));
 126        exit(0);
 127}