@matheusbsbs / matheus-bagliano / commits / 83f50539a9
Fork 0

git-mktree: reverse of git-ls-tree.

This reads data in the format a (non recursive) ls-tree outputs and writes a tree object to the object database. The created tree object name is output to the standard output. For convenience, the input data does not need to be sorted; the command sorts the input lines internally. By request from Tommi Virtanen. Signed-off-by: Junio C Hamano <junkio@cox.net>

Junio C Hamano committed Feb 20, 2006 at 21:50 UTC 83f50539a9b1f18ccf0a4e1b84169eb4a24b3ea4
2 files changed +138 -1
Makefile
+1 -1
@@ -143,7 +143,7 @@ PROGRAMS = \
143 git-diff-tree$X git-fetch-pack$X git-fsck-objects$X \
144 git-hash-object$X git-index-pack$X git-init-db$X \
145 git-local-fetch$X git-ls-files$X git-ls-tree$X git-merge-base$X \
146 - git-merge-index$X git-mktag$X git-pack-objects$X git-patch-id$X \
146 + git-merge-index$X git-mktag$X git-mktree$X git-pack-objects$X git-patch-id$X \
147 git-peek-remote$X git-prune-packed$X git-read-tree$X \
148 git-receive-pack$X git-rev-list$X git-rev-parse$X \
149 git-send-pack$X git-show-branch$X git-shell$X \
mktree.c new
+137
@@ -0,0 +1,137 @@
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 +
10 +static struct treeent {
11 + unsigned mode;
12 + unsigned char sha1[20];
13 + int len;
14 + char name[FLEX_ARRAY];
15 +} **entries;
16 +static int alloc, used;
17 +
18 +static 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;
32 + memcpy(ent->sha1, sha1, 20);
33 + memcpy(ent->name, path, len+1);
34 +}
35 +
36 +static 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 +
44 +static void write_tree(unsigned char *sha1)
45 +{
46 + char *buffer;
47 + unsigned long size, offset;
48 + int i;
49 +
50 + qsort(entries, used, sizeof(*entries), ent_compare);
51 + size = 100;
52 + for (size = i = 0; i < used; i++)
53 + size += 32 + entries[i]->len;
54 + buffer = xmalloc(size);
55 + offset = 0;
56 +
57 + for (i = 0; i < used; i++) {
58 + struct treeent *ent = entries[i];
59 +
60 + if (offset + ent->len + 100 < size) {
61 + size = alloc_nr(offset + ent->len + 100);
62 + buffer = xrealloc(buffer, size);
63 + }
64 + offset += sprintf(buffer + offset, "%o ", ent->mode);
65 + offset += sprintf(buffer + offset, "%s", ent->name);
66 + buffer[offset++] = 0;
67 + memcpy(buffer + offset, ent->sha1, 20);
68 + offset += 20;
69 + }
70 + write_sha1_file(buffer, offset, "tree", sha1);
71 +}
72 +
73 +static const char mktree_usage[] = "mktree [-z]";
74 +
75 +int main(int ac, char **av)
76 +{
77 + struct strbuf sb;
78 + unsigned char sha1[20];
79 + int line_termination = '\n';
80 +
81 + setup_git_directory();
82 +
83 + while ((1 < ac) && av[1][0] == '-') {
84 + char *arg = av[1];
85 + if (!strcmp("-z", arg))
86 + line_termination = 0;
87 + else
88 + usage(mktree_usage);
89 + ac--;
90 + av++;
91 + }
92 +
93 + strbuf_init(&sb);
94 + while (1) {
95 + int len;
96 + char *ptr, *ntr;
97 + unsigned mode;
98 + char type[20];
99 + char *path;
100 +
101 + read_line(&sb, stdin, line_termination);
102 + if (sb.eof)
103 + break;
104 + len = sb.len;
105 + ptr = sb.buf;
106 + /* Input is non-recursive ls-tree output format
107 + * mode SP type SP sha1 TAB name
108 + */
109 + mode = strtoul(ptr, &ntr, 8);
110 + if (ptr == ntr || !ntr || *ntr != ' ')
111 + die("input format error: %s", sb.buf);
112 + ptr = ntr + 1; /* type */
113 + ntr = strchr(ptr, ' ');
114 + if (!ntr || sb.buf + len <= ntr + 41 ||
115 + ntr[41] != '\t' ||
116 + get_sha1_hex(ntr + 1, sha1))
117 + die("input format error: %s", sb.buf);
118 + if (sha1_object_info(sha1, type, NULL))
119 + die("object %s unavailable", sha1_to_hex(sha1));
120 + *ntr++ = 0; /* now at the beginning of SHA1 */
121 + if (strcmp(ptr, type))
122 + die("object type %s mismatch (%s)", ptr, type);
123 + ntr += 41; /* at the beginning of name */
124 + if (line_termination && ntr[0] == '"')
125 + path = unquote_c_style(ntr, NULL);
126 + else
127 + path = ntr;
128 +
129 + append_to_tree(mode, sha1, path);
130 +
131 + if (path != ntr)
132 + free(path);
133 + }
134 + write_tree(sha1);
135 + puts(sha1_to_hex(sha1));
136 + exit(0);
137 +}