blob: e0da110a98d3a7376dc78df71fabc10fc5664296 [file] [log] [blame]
Junio C Hamano83f50532006-02-20 21:50:01 -08001/*
2 * GIT - the stupid content tracker
3 *
4 * Copyright (c) Junio C Hamano, 2006
5 */
6#include "cache.h"
Junio C Hamano83f50532006-02-20 21:50:01 -08007#include "quote.h"
Peter Eriksen8e440252006-04-02 14:44:09 +02008#include "tree.h"
Junio C Hamano83f50532006-02-20 21:50:01 -08009
10static struct treeent {
11 unsigned mode;
12 unsigned char sha1[20];
13 int len;
14 char name[FLEX_ARRAY];
15} **entries;
16static int alloc, used;
17
18static void append_to_tree(unsigned mode, unsigned char *sha1, char *path)
19{
20 struct treeent *ent;
21 int len = strlen(path);
22 if (strchr(path, '/'))
23 die("path %s contains slash", path);
24
25 if (alloc <= used) {
26 alloc = alloc_nr(used);
27 entries = xrealloc(entries, sizeof(*entries) * alloc);
28 }
29 ent = entries[used++] = xmalloc(sizeof(**entries) + len + 1);
30 ent->mode = mode;
31 ent->len = len;
Shawn Pearcee7024962006-08-23 02:49:00 -040032 hashcpy(ent->sha1, sha1);
Junio C Hamano83f50532006-02-20 21:50:01 -080033 memcpy(ent->name, path, len+1);
34}
35
36static int ent_compare(const void *a_, const void *b_)
37{
38 struct treeent *a = *(struct treeent **)a_;
39 struct treeent *b = *(struct treeent **)b_;
40 return base_name_compare(a->name, a->len, a->mode,
41 b->name, b->len, b->mode);
42}
43
44static void write_tree(unsigned char *sha1)
45{
Pierre Habouzitd52bc662007-09-06 13:20:08 +020046 struct strbuf buf;
47 size_t size;
Junio C Hamano83f50532006-02-20 21:50:01 -080048 int i;
49
50 qsort(entries, used, sizeof(*entries), ent_compare);
Junio C Hamano83f50532006-02-20 21:50:01 -080051 for (size = i = 0; i < used; i++)
52 size += 32 + entries[i]->len;
Junio C Hamano83f50532006-02-20 21:50:01 -080053
Pierre Habouzitf1696ee2007-09-10 12:35:04 +020054 strbuf_init(&buf, size);
Junio C Hamano83f50532006-02-20 21:50:01 -080055 for (i = 0; i < used; i++) {
56 struct treeent *ent = entries[i];
Pierre Habouzitd52bc662007-09-06 13:20:08 +020057 strbuf_addf(&buf, "%o %s%c", ent->mode, ent->name, '\0');
58 strbuf_add(&buf, ent->sha1, 20);
Junio C Hamano83f50532006-02-20 21:50:01 -080059 }
Pierre Habouzitd52bc662007-09-06 13:20:08 +020060
61 write_sha1_file(buf.buf, buf.len, tree_type, sha1);
Junio C Hamano83f50532006-02-20 21:50:01 -080062}
63
Ramsay Allan Jones15e593e2006-08-03 16:38:39 +010064static const char mktree_usage[] = "git-mktree [-z]";
Junio C Hamano83f50532006-02-20 21:50:01 -080065
66int main(int ac, char **av)
67{
68 struct strbuf sb;
Pierre Habouzit7fb10112007-09-20 00:42:14 +020069 struct strbuf p_uq;
Junio C Hamano83f50532006-02-20 21:50:01 -080070 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
Pierre Habouzitf1696ee2007-09-10 12:35:04 +020085 strbuf_init(&sb, 0);
Pierre Habouzit7fb10112007-09-20 00:42:14 +020086 strbuf_init(&p_uq, 0);
87 while (strbuf_getline(&sb, stdin, line_termination) != EOF) {
Junio C Hamano83f50532006-02-20 21:50:01 -080088 char *ptr, *ntr;
89 unsigned mode;
Nicolas Pitre21666f12007-02-26 14:55:59 -050090 enum object_type type;
Junio C Hamano83f50532006-02-20 21:50:01 -080091 char *path;
92
Junio C Hamano83f50532006-02-20 21:50:01 -080093 ptr = sb.buf;
94 /* Input is non-recursive ls-tree output format
95 * mode SP type SP sha1 TAB name
96 */
97 mode = strtoul(ptr, &ntr, 8);
98 if (ptr == ntr || !ntr || *ntr != ' ')
99 die("input format error: %s", sb.buf);
100 ptr = ntr + 1; /* type */
101 ntr = strchr(ptr, ' ');
Pierre Habouzitb449f4c2007-09-06 13:20:05 +0200102 if (!ntr || sb.buf + sb.len <= ntr + 40 ||
Junio C Hamano83f50532006-02-20 21:50:01 -0800103 ntr[41] != '\t' ||
104 get_sha1_hex(ntr + 1, sha1))
105 die("input format error: %s", sb.buf);
Nicolas Pitre21666f12007-02-26 14:55:59 -0500106 type = sha1_object_info(sha1, NULL);
107 if (type < 0)
Junio C Hamano83f50532006-02-20 21:50:01 -0800108 die("object %s unavailable", sha1_to_hex(sha1));
109 *ntr++ = 0; /* now at the beginning of SHA1 */
Nicolas Pitre21666f12007-02-26 14:55:59 -0500110 if (type != type_from_string(ptr))
111 die("object type %s mismatch (%s)", ptr, typename(type));
Pierre Habouzit7fb10112007-09-20 00:42:14 +0200112
113 path = ntr + 41; /* at the beginning of name */
114 if (line_termination && path[0] == '"') {
115 strbuf_reset(&p_uq);
116 if (unquote_c_style(&p_uq, path, NULL)) {
117 die("invalid quoting");
118 }
119 path = p_uq.buf;
120 }
Junio C Hamano83f50532006-02-20 21:50:01 -0800121
122 append_to_tree(mode, sha1, path);
Junio C Hamano83f50532006-02-20 21:50:01 -0800123 }
Pierre Habouzit7fb10112007-09-20 00:42:14 +0200124 strbuf_release(&p_uq);
Pierre Habouzite6c019d2007-09-17 11:19:04 +0200125 strbuf_release(&sb);
Pierre Habouzit7fb10112007-09-20 00:42:14 +0200126
Junio C Hamano83f50532006-02-20 21:50:01 -0800127 write_tree(sha1);
128 puts(sha1_to_hex(sha1));
129 exit(0);
130}