]>
Commit | Line | Data |
---|---|---|
83f50539 JH |
1 | /* |
2 | * GIT - the stupid content tracker | |
3 | * | |
633e3556 | 4 | * Copyright (c) Junio C Hamano, 2006, 2009 |
83f50539 | 5 | */ |
633e3556 | 6 | #include "builtin.h" |
83f50539 | 7 | #include "quote.h" |
8e440259 | 8 | #include "tree.h" |
1fdee85c | 9 | #include "parse-options.h" |
83f50539 JH |
10 | |
11 | static struct treeent { | |
12 | unsigned mode; | |
13 | unsigned char sha1[20]; | |
14 | int len; | |
15 | char name[FLEX_ARRAY]; | |
16 | } **entries; | |
17 | static int alloc, used; | |
18 | ||
19 | static 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; | |
e702496e | 33 | hashcpy(ent->sha1, sha1); |
83f50539 JH |
34 | memcpy(ent->name, path, len+1); |
35 | } | |
36 | ||
37 | static 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 | ||
45 | static void write_tree(unsigned char *sha1) | |
46 | { | |
d52bc661 PH |
47 | struct strbuf buf; |
48 | size_t size; | |
83f50539 JH |
49 | int i; |
50 | ||
51 | qsort(entries, used, sizeof(*entries), ent_compare); | |
83f50539 JH |
52 | for (size = i = 0; i < used; i++) |
53 | size += 32 + entries[i]->len; | |
83f50539 | 54 | |
f1696ee3 | 55 | strbuf_init(&buf, size); |
83f50539 JH |
56 | for (i = 0; i < used; i++) { |
57 | struct treeent *ent = entries[i]; | |
d52bc661 PH |
58 | strbuf_addf(&buf, "%o %s%c", ent->mode, ent->name, '\0'); |
59 | strbuf_add(&buf, ent->sha1, 20); | |
83f50539 | 60 | } |
d52bc661 PH |
61 | |
62 | write_sha1_file(buf.buf, buf.len, tree_type, sha1); | |
83f50539 JH |
63 | } |
64 | ||
1fdee85c | 65 | static const char *mktree_usage[] = { |
e01662bb | 66 | "git mktree [-z] [--missing]", |
1fdee85c JH |
67 | NULL |
68 | }; | |
83f50539 | 69 | |
1c64e79a | 70 | static void mktree_line(char *buf, size_t len, int line_termination, int allow_missing) |
fe0bb5f7 JH |
71 | { |
72 | char *ptr, *ntr; | |
73 | unsigned mode; | |
74 | enum object_type type; | |
75 | char *path; | |
76 | unsigned char sha1[20]; | |
77 | ||
78 | ptr = buf; | |
79 | /* | |
80 | * Read non-recursive ls-tree output format: | |
81 | * mode SP type SP sha1 TAB name | |
82 | */ | |
83 | mode = strtoul(ptr, &ntr, 8); | |
84 | if (ptr == ntr || !ntr || *ntr != ' ') | |
85 | die("input format error: %s", buf); | |
86 | ptr = ntr + 1; /* type */ | |
87 | ntr = strchr(ptr, ' '); | |
88 | if (!ntr || buf + len <= ntr + 40 || | |
89 | ntr[41] != '\t' || | |
90 | get_sha1_hex(ntr + 1, sha1)) | |
91 | die("input format error: %s", buf); | |
ad87b5dd JH |
92 | |
93 | /* It is perfectly normal if we do not have a commit from a submodule */ | |
1c64e79a JH |
94 | if (S_ISGITLINK(mode)) |
95 | allow_missing = 1; | |
96 | ||
97 | if (!allow_missing) | |
ad87b5dd JH |
98 | type = sha1_object_info(sha1, NULL); |
99 | else | |
1c64e79a | 100 | type = object_type(mode); |
ad87b5dd | 101 | |
fe0bb5f7 JH |
102 | if (type < 0) |
103 | die("object %s unavailable", sha1_to_hex(sha1)); | |
ad87b5dd | 104 | |
fe0bb5f7 JH |
105 | *ntr++ = 0; /* now at the beginning of SHA1 */ |
106 | if (type != type_from_string(ptr)) | |
107 | die("object type %s mismatch (%s)", ptr, typename(type)); | |
108 | ||
109 | path = ntr + 41; /* at the beginning of name */ | |
110 | if (line_termination && path[0] == '"') { | |
111 | struct strbuf p_uq = STRBUF_INIT; | |
112 | if (unquote_c_style(&p_uq, path, NULL)) | |
113 | die("invalid quoting"); | |
114 | path = strbuf_detach(&p_uq, NULL); | |
115 | } | |
116 | append_to_tree(mode, sha1, path); | |
117 | } | |
118 | ||
633e3556 | 119 | int cmd_mktree(int ac, const char **av, const char *prefix) |
83f50539 | 120 | { |
f285a2d7 | 121 | struct strbuf sb = STRBUF_INIT; |
83f50539 JH |
122 | unsigned char sha1[20]; |
123 | int line_termination = '\n'; | |
1c64e79a | 124 | int allow_missing = 0; |
1fdee85c JH |
125 | const struct option option[] = { |
126 | OPT_SET_INT('z', NULL, &line_termination, "input is NUL terminated", '\0'), | |
1c64e79a | 127 | OPT_SET_INT( 0 , "missing", &allow_missing, "allow missing objects", 1), |
1fdee85c JH |
128 | OPT_END() |
129 | }; | |
83f50539 | 130 | |
1fdee85c | 131 | ac = parse_options(ac, av, option, mktree_usage, 0); |
83f50539 | 132 | |
fe0bb5f7 | 133 | while (strbuf_getline(&sb, stdin, line_termination) != EOF) |
1c64e79a | 134 | mktree_line(sb.buf, sb.len, line_termination, allow_missing); |
fe0bb5f7 | 135 | |
e6c019d0 | 136 | strbuf_release(&sb); |
7fb1011e | 137 | |
83f50539 JH |
138 | write_tree(sha1); |
139 | puts(sha1_to_hex(sha1)); | |
140 | exit(0); | |
141 | } |