mktree.con commit Rework pretty_print_commit to use strbufs instead of custom buffers. (674d172)
   1/*
   2 * GIT - the stupid content tracker
   3 *
   4 * Copyright (c) Junio C Hamano, 2006
   5 */
   6#include "cache.h"
   7#include "strbuf.h"
   8#include "quote.h"
   9#include "tree.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[] = "git-mktree [-z]";
  66
  67int main(int ac, char **av)
  68{
  69        struct strbuf sb;
  70        unsigned char sha1[20];
  71        int line_termination = '\n';
  72
  73        setup_git_directory();
  74
  75        while ((1 < ac) && av[1][0] == '-') {
  76                char *arg = av[1];
  77                if (!strcmp("-z", arg))
  78                        line_termination = 0;
  79                else
  80                        usage(mktree_usage);
  81                ac--;
  82                av++;
  83        }
  84
  85        strbuf_init(&sb, 0);
  86        while (1) {
  87                char *ptr, *ntr;
  88                unsigned mode;
  89                enum object_type type;
  90                char *path;
  91
  92                read_line(&sb, stdin, line_termination);
  93                if (sb.eof)
  94                        break;
  95                ptr = sb.buf;
  96                /* Input is non-recursive ls-tree output format
  97                 * mode SP type SP sha1 TAB name
  98                 */
  99                mode = strtoul(ptr, &ntr, 8);
 100                if (ptr == ntr || !ntr || *ntr != ' ')
 101                        die("input format error: %s", sb.buf);
 102                ptr = ntr + 1; /* type */
 103                ntr = strchr(ptr, ' ');
 104                if (!ntr || sb.buf + sb.len <= ntr + 40 ||
 105                    ntr[41] != '\t' ||
 106                    get_sha1_hex(ntr + 1, sha1))
 107                        die("input format error: %s", sb.buf);
 108                type = sha1_object_info(sha1, NULL);
 109                if (type < 0)
 110                        die("object %s unavailable", sha1_to_hex(sha1));
 111                *ntr++ = 0; /* now at the beginning of SHA1 */
 112                if (type != type_from_string(ptr))
 113                        die("object type %s mismatch (%s)", ptr, typename(type));
 114                ntr += 41; /* at the beginning of name */
 115                if (line_termination && ntr[0] == '"')
 116                        path = unquote_c_style(ntr, NULL);
 117                else
 118                        path = ntr;
 119
 120                append_to_tree(mode, sha1, path);
 121
 122                if (path != ntr)
 123                        free(path);
 124        }
 125        write_tree(sha1);
 126        puts(sha1_to_hex(sha1));
 127        exit(0);
 128}