]> git.ipfire.org Git - thirdparty/git.git/blame - mktree.c
use test number as port number
[thirdparty/git.git] / mktree.c
CommitLineData
83f50539
JH
1/*
2 * GIT - the stupid content tracker
3 *
4 * Copyright (c) Junio C Hamano, 2006
5 */
6#include "cache.h"
83f50539 7#include "quote.h"
8e440259 8#include "tree.h"
2fb3f6db 9#include "exec_cmd.h"
83f50539
JH
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;
e702496e 33 hashcpy(ent->sha1, sha1);
83f50539
JH
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{
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
34263de0 65static const char mktree_usage[] = "git mktree [-z]";
83f50539
JH
66
67int main(int ac, char **av)
68{
f285a2d7
BC
69 struct strbuf sb = STRBUF_INIT;
70 struct strbuf p_uq = STRBUF_INIT;
83f50539
JH
71 unsigned char sha1[20];
72 int line_termination = '\n';
73
2fb3f6db
SP
74 git_extract_argv0_path(av[0]);
75
83f50539
JH
76 setup_git_directory();
77
78 while ((1 < ac) && av[1][0] == '-') {
79 char *arg = av[1];
80 if (!strcmp("-z", arg))
81 line_termination = 0;
82 else
83 usage(mktree_usage);
84 ac--;
85 av++;
86 }
87
7fb1011e 88 while (strbuf_getline(&sb, stdin, line_termination) != EOF) {
83f50539
JH
89 char *ptr, *ntr;
90 unsigned mode;
21666f1a 91 enum object_type type;
83f50539
JH
92 char *path;
93
83f50539
JH
94 ptr = sb.buf;
95 /* Input is non-recursive ls-tree output format
96 * mode SP type SP sha1 TAB name
97 */
98 mode = strtoul(ptr, &ntr, 8);
99 if (ptr == ntr || !ntr || *ntr != ' ')
100 die("input format error: %s", sb.buf);
101 ptr = ntr + 1; /* type */
102 ntr = strchr(ptr, ' ');
b449f4cf 103 if (!ntr || sb.buf + sb.len <= ntr + 40 ||
83f50539
JH
104 ntr[41] != '\t' ||
105 get_sha1_hex(ntr + 1, sha1))
106 die("input format error: %s", sb.buf);
21666f1a
NP
107 type = sha1_object_info(sha1, NULL);
108 if (type < 0)
83f50539
JH
109 die("object %s unavailable", sha1_to_hex(sha1));
110 *ntr++ = 0; /* now at the beginning of SHA1 */
21666f1a
NP
111 if (type != type_from_string(ptr))
112 die("object type %s mismatch (%s)", ptr, typename(type));
83f50539 113
7fb1011e
PH
114 path = ntr + 41; /* at the beginning of name */
115 if (line_termination && path[0] == '"') {
116 strbuf_reset(&p_uq);
117 if (unquote_c_style(&p_uq, path, NULL)) {
118 die("invalid quoting");
119 }
120 path = p_uq.buf;
121 }
83f50539 122
7fb1011e 123 append_to_tree(mode, sha1, path);
83f50539 124 }
7fb1011e 125 strbuf_release(&p_uq);
e6c019d0 126 strbuf_release(&sb);
7fb1011e 127
83f50539
JH
128 write_tree(sha1);
129 puts(sha1_to_hex(sha1));
130 exit(0);
131}