1/*2* GIT - the stupid content tracker3*4* Copyright (c) Junio C Hamano, 20065*/6#include "cache.h"7#include "strbuf.h"8#include "quote.h"9#include "tree.h"1011static struct treeent {12unsigned mode;13unsigned char sha1[20];14int len;15char name[FLEX_ARRAY];16} **entries;17static int alloc, used;1819static void append_to_tree(unsigned mode, unsigned char *sha1, char *path)20{21struct treeent *ent;22int len = strlen(path);23if (strchr(path, '/'))24die("path %s contains slash", path);2526if (alloc <= used) {27alloc = alloc_nr(used);28entries = xrealloc(entries, sizeof(*entries) * alloc);29}30ent = entries[used++] = xmalloc(sizeof(**entries) + len + 1);31ent->mode = mode;32ent->len = len;33memcpy(ent->sha1, sha1, 20);34memcpy(ent->name, path, len+1);35}3637static int ent_compare(const void *a_, const void *b_)38{39struct treeent *a = *(struct treeent **)a_;40struct treeent *b = *(struct treeent **)b_;41return base_name_compare(a->name, a->len, a->mode,42b->name, b->len, b->mode);43}4445static void write_tree(unsigned char *sha1)46{47char *buffer;48unsigned long size, offset;49int i;5051qsort(entries, used, sizeof(*entries), ent_compare);52size = 100;53for (size = i = 0; i < used; i++)54size += 32 + entries[i]->len;55buffer = xmalloc(size);56offset = 0;5758for (i = 0; i < used; i++) {59struct treeent *ent = entries[i];6061if (offset + ent->len + 100 < size) {62size = alloc_nr(offset + ent->len + 100);63buffer = xrealloc(buffer, size);64}65offset += sprintf(buffer + offset, "%o ", ent->mode);66offset += sprintf(buffer + offset, "%s", ent->name);67buffer[offset++] = 0;68memcpy(buffer + offset, ent->sha1, 20);69offset += 20;70}71write_sha1_file(buffer, offset, tree_type, sha1);72}7374static const char mktree_usage[] = "mktree [-z]";7576int main(int ac, char **av)77{78struct strbuf sb;79unsigned char sha1[20];80int line_termination = '\n';8182setup_git_directory();8384while ((1 < ac) && av[1][0] == '-') {85char *arg = av[1];86if (!strcmp("-z", arg))87line_termination = 0;88else89usage(mktree_usage);90ac--;91av++;92}9394strbuf_init(&sb);95while (1) {96int len;97char *ptr, *ntr;98unsigned mode;99char type[20];100char *path;101102read_line(&sb, stdin, line_termination);103if (sb.eof)104break;105len = sb.len;106ptr = sb.buf;107/* Input is non-recursive ls-tree output format108* mode SP type SP sha1 TAB name109*/110mode = strtoul(ptr, &ntr, 8);111if (ptr == ntr || !ntr || *ntr != ' ')112die("input format error: %s", sb.buf);113ptr = ntr + 1; /* type */114ntr = strchr(ptr, ' ');115if (!ntr || sb.buf + len <= ntr + 41 ||116ntr[41] != '\t' ||117get_sha1_hex(ntr + 1, sha1))118die("input format error: %s", sb.buf);119if (sha1_object_info(sha1, type, NULL))120die("object %s unavailable", sha1_to_hex(sha1));121*ntr++ = 0; /* now at the beginning of SHA1 */122if (strcmp(ptr, type))123die("object type %s mismatch (%s)", ptr, type);124ntr += 41; /* at the beginning of name */125if (line_termination && ntr[0] == '"')126path = unquote_c_style(ntr, NULL);127else128path = ntr;129130append_to_tree(mode, sha1, path);131132if (path != ntr)133free(path);134}135write_tree(sha1);136puts(sha1_to_hex(sha1));137exit(0);138}