2 * GIT - the stupid content tracker
4 * Copyright (c) Junio C Hamano, 2006, 2009
10 #include "parse-options.h"
11 #include "object-store.h"
13 static struct treeent
{
17 char name
[FLEX_ARRAY
];
19 static int alloc
, used
;
21 static void append_to_tree(unsigned mode
, struct object_id
*oid
, char *path
)
24 size_t len
= strlen(path
);
25 if (strchr(path
, '/'))
26 die("path %s contains slash", path
);
28 FLEX_ALLOC_MEM(ent
, name
, path
, len
);
31 oidcpy(&ent
->oid
, oid
);
33 ALLOC_GROW(entries
, used
+ 1, alloc
);
34 entries
[used
++] = ent
;
37 static int ent_compare(const void *a_
, const void *b_
)
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
);
45 static void write_tree(struct object_id
*oid
)
51 QSORT(entries
, used
, ent_compare
);
52 for (size
= i
= 0; i
< used
; i
++)
53 size
+= 32 + entries
[i
]->len
;
55 strbuf_init(&buf
, size
);
56 for (i
= 0; i
< used
; i
++) {
57 struct treeent
*ent
= entries
[i
];
58 strbuf_addf(&buf
, "%o %s%c", ent
->mode
, ent
->name
, '\0');
59 strbuf_add(&buf
, ent
->oid
.hash
, the_hash_algo
->rawsz
);
62 write_object_file(buf
.buf
, buf
.len
, OBJ_TREE
, oid
);
66 static const char *mktree_usage
[] = {
67 "git mktree [-z] [--missing] [--batch]",
71 static void mktree_line(char *buf
, int nul_term_line
, int allow_missing
)
76 enum object_type mode_type
; /* object type derived from mode */
77 enum object_type obj_type
; /* object type derived from sha */
78 struct object_info oi
= OBJECT_INFO_INIT
;
79 char *path
, *to_free
= NULL
;
84 * Read non-recursive ls-tree output format:
85 * mode SP type SP sha1 TAB name
87 mode
= strtoul(ptr
, &ntr
, 8);
88 if (ptr
== ntr
|| !ntr
|| *ntr
!= ' ')
89 die("input format error: %s", buf
);
90 ptr
= ntr
+ 1; /* type */
91 ntr
= strchr(ptr
, ' ');
92 if (!ntr
|| parse_oid_hex(ntr
+ 1, &oid
, &p
) ||
94 die("input format error: %s", buf
);
96 /* It is perfectly normal if we do not have a commit from a submodule */
97 if (S_ISGITLINK(mode
))
101 *ntr
++ = 0; /* now at the beginning of SHA1 */
103 path
= (char *)p
+ 1; /* at the beginning of name */
104 if (!nul_term_line
&& path
[0] == '"') {
105 struct strbuf p_uq
= STRBUF_INIT
;
106 if (unquote_c_style(&p_uq
, path
, NULL
))
107 die("invalid quoting");
108 path
= to_free
= strbuf_detach(&p_uq
, NULL
);
112 * Object type is redundantly derivable three ways.
113 * These should all agree.
115 mode_type
= object_type(mode
);
116 if (mode_type
!= type_from_string(ptr
)) {
117 die("entry '%s' object type (%s) doesn't match mode type (%s)",
118 path
, ptr
, type_name(mode_type
));
121 /* Check the type of object identified by oid without fetching objects */
122 oi
.typep
= &obj_type
;
123 if (oid_object_info_extended(the_repository
, &oid
, &oi
,
124 OBJECT_INFO_LOOKUP_REPLACE
|
126 OBJECT_INFO_SKIP_FETCH_OBJECT
) < 0)
131 ; /* no problem - missing objects are presumed to be of the right type */
133 die("entry '%s' object %s is unavailable", path
, oid_to_hex(&oid
));
136 if (obj_type
!= mode_type
) {
138 * The object exists but is of the wrong type.
139 * This is a problem regardless of allow_missing
140 * because the new tree entry will never be correct.
142 die("entry '%s' object %s is a %s but specified type was (%s)",
143 path
, oid_to_hex(&oid
), type_name(obj_type
), type_name(mode_type
));
147 append_to_tree(mode
, &oid
, path
);
151 int cmd_mktree(int ac
, const char **av
, const char *prefix
)
153 struct strbuf sb
= STRBUF_INIT
;
154 struct object_id oid
;
155 int nul_term_line
= 0;
156 int allow_missing
= 0;
157 int is_batch_mode
= 0;
159 strbuf_getline_fn getline_fn
;
161 const struct option option
[] = {
162 OPT_BOOL('z', NULL
, &nul_term_line
, N_("input is NUL terminated")),
163 OPT_SET_INT( 0 , "missing", &allow_missing
, N_("allow missing objects"), 1),
164 OPT_SET_INT( 0 , "batch", &is_batch_mode
, N_("allow creation of more than one tree"), 1),
168 ac
= parse_options(ac
, av
, prefix
, option
, mktree_usage
, 0);
169 getline_fn
= nul_term_line
? strbuf_getline_nul
: strbuf_getline_lf
;
173 if (getline_fn(&sb
, stdin
) == EOF
) {
177 if (sb
.buf
[0] == '\0') {
178 /* empty lines denote tree boundaries in batch mode */
181 die("input format error: (blank line only valid in batch mode)");
183 mktree_line(sb
.buf
, nul_term_line
, allow_missing
);
185 if (is_batch_mode
&& got_eof
&& used
< 1) {
187 * Execution gets here if the last tree entry is terminated with a
188 * new-line. The final new-line has been made optional to be
189 * consistent with the original non-batch behaviour of mktree.
191 ; /* skip creating an empty tree */
194 puts(oid_to_hex(&oid
));
197 used
=0; /* reset tree entry buffer for re-use in batch mode */