2 * GIT - the stupid content tracker
4 * Copyright (c) Junio C Hamano, 2006, 2009
11 #include "parse-options.h"
12 #include "object-store.h"
14 static struct treeent
{
18 char name
[FLEX_ARRAY
];
20 static int alloc
, used
;
22 static void append_to_tree(unsigned mode
, struct object_id
*oid
, char *path
)
25 size_t len
= strlen(path
);
26 if (strchr(path
, '/'))
27 die("path %s contains slash", path
);
29 FLEX_ALLOC_MEM(ent
, name
, path
, len
);
32 oidcpy(&ent
->oid
, oid
);
34 ALLOC_GROW(entries
, used
+ 1, alloc
);
35 entries
[used
++] = ent
;
38 static int ent_compare(const void *a_
, const void *b_
)
40 struct treeent
*a
= *(struct treeent
**)a_
;
41 struct treeent
*b
= *(struct treeent
**)b_
;
42 return base_name_compare(a
->name
, a
->len
, a
->mode
,
43 b
->name
, b
->len
, b
->mode
);
46 static void write_tree(struct object_id
*oid
)
52 QSORT(entries
, used
, ent_compare
);
53 for (size
= i
= 0; i
< used
; i
++)
54 size
+= 32 + entries
[i
]->len
;
56 strbuf_init(&buf
, size
);
57 for (i
= 0; i
< used
; i
++) {
58 struct treeent
*ent
= entries
[i
];
59 strbuf_addf(&buf
, "%o %s%c", ent
->mode
, ent
->name
, '\0');
60 strbuf_add(&buf
, ent
->oid
.hash
, the_hash_algo
->rawsz
);
63 write_object_file(buf
.buf
, buf
.len
, OBJ_TREE
, oid
);
67 static const char *mktree_usage
[] = {
68 "git mktree [-z] [--missing] [--batch]",
72 static void mktree_line(char *buf
, int nul_term_line
, int allow_missing
)
77 enum object_type mode_type
; /* object type derived from mode */
78 enum object_type obj_type
; /* object type derived from sha */
79 struct object_info oi
= OBJECT_INFO_INIT
;
80 char *path
, *to_free
= NULL
;
85 * Read non-recursive ls-tree output format:
86 * mode SP type SP sha1 TAB name
88 mode
= strtoul(ptr
, &ntr
, 8);
89 if (ptr
== ntr
|| !ntr
|| *ntr
!= ' ')
90 die("input format error: %s", buf
);
91 ptr
= ntr
+ 1; /* type */
92 ntr
= strchr(ptr
, ' ');
93 if (!ntr
|| parse_oid_hex(ntr
+ 1, &oid
, &p
) ||
95 die("input format error: %s", buf
);
97 /* It is perfectly normal if we do not have a commit from a submodule */
98 if (S_ISGITLINK(mode
))
102 *ntr
++ = 0; /* now at the beginning of SHA1 */
104 path
= (char *)p
+ 1; /* at the beginning of name */
105 if (!nul_term_line
&& path
[0] == '"') {
106 struct strbuf p_uq
= STRBUF_INIT
;
107 if (unquote_c_style(&p_uq
, path
, NULL
))
108 die("invalid quoting");
109 path
= to_free
= strbuf_detach(&p_uq
, NULL
);
113 * Object type is redundantly derivable three ways.
114 * These should all agree.
116 mode_type
= object_type(mode
);
117 if (mode_type
!= type_from_string(ptr
)) {
118 die("entry '%s' object type (%s) doesn't match mode type (%s)",
119 path
, ptr
, type_name(mode_type
));
122 /* Check the type of object identified by oid without fetching objects */
123 oi
.typep
= &obj_type
;
124 if (oid_object_info_extended(the_repository
, &oid
, &oi
,
125 OBJECT_INFO_LOOKUP_REPLACE
|
127 OBJECT_INFO_SKIP_FETCH_OBJECT
) < 0)
132 ; /* no problem - missing objects are presumed to be of the right type */
134 die("entry '%s' object %s is unavailable", path
, oid_to_hex(&oid
));
137 if (obj_type
!= mode_type
) {
139 * The object exists but is of the wrong type.
140 * This is a problem regardless of allow_missing
141 * because the new tree entry will never be correct.
143 die("entry '%s' object %s is a %s but specified type was (%s)",
144 path
, oid_to_hex(&oid
), type_name(obj_type
), type_name(mode_type
));
148 append_to_tree(mode
, &oid
, path
);
152 int cmd_mktree(int ac
, const char **av
, const char *prefix
)
154 struct strbuf sb
= STRBUF_INIT
;
155 struct object_id oid
;
156 int nul_term_line
= 0;
157 int allow_missing
= 0;
158 int is_batch_mode
= 0;
160 strbuf_getline_fn getline_fn
;
162 const struct option option
[] = {
163 OPT_BOOL('z', NULL
, &nul_term_line
, N_("input is NUL terminated")),
164 OPT_SET_INT( 0 , "missing", &allow_missing
, N_("allow missing objects"), 1),
165 OPT_SET_INT( 0 , "batch", &is_batch_mode
, N_("allow creation of more than one tree"), 1),
169 ac
= parse_options(ac
, av
, prefix
, option
, mktree_usage
, 0);
170 getline_fn
= nul_term_line
? strbuf_getline_nul
: strbuf_getline_lf
;
174 if (getline_fn(&sb
, stdin
) == EOF
) {
178 if (sb
.buf
[0] == '\0') {
179 /* empty lines denote tree boundaries in batch mode */
182 die("input format error: (blank line only valid in batch mode)");
184 mktree_line(sb
.buf
, nul_term_line
, allow_missing
);
186 if (is_batch_mode
&& got_eof
&& used
< 1) {
188 * Execution gets here if the last tree entry is terminated with a
189 * new-line. The final new-line has been made optional to be
190 * consistent with the original non-batch behaviour of mktree.
192 ; /* skip creating an empty tree */
195 puts(oid_to_hex(&oid
));
198 used
=0; /* reset tree entry buffer for re-use in batch mode */