2 * GIT - the stupid content tracker
4 * Copyright (c) Junio C Hamano, 2006, 2009
9 #include "parse-options.h"
10 #include "object-store.h"
12 static struct treeent
{
16 char name
[FLEX_ARRAY
];
18 static int alloc
, used
;
20 static void append_to_tree(unsigned mode
, struct object_id
*oid
, char *path
)
23 size_t len
= strlen(path
);
24 if (strchr(path
, '/'))
25 die("path %s contains slash", path
);
27 FLEX_ALLOC_MEM(ent
, name
, path
, len
);
30 oidcpy(&ent
->oid
, oid
);
32 ALLOC_GROW(entries
, used
+ 1, alloc
);
33 entries
[used
++] = ent
;
36 static int ent_compare(const void *a_
, const void *b_
)
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
);
44 static void write_tree(struct object_id
*oid
)
50 QSORT(entries
, used
, ent_compare
);
51 for (size
= i
= 0; i
< used
; i
++)
52 size
+= 32 + entries
[i
]->len
;
54 strbuf_init(&buf
, size
);
55 for (i
= 0; i
< used
; i
++) {
56 struct treeent
*ent
= entries
[i
];
57 strbuf_addf(&buf
, "%o %s%c", ent
->mode
, ent
->name
, '\0');
58 strbuf_add(&buf
, ent
->oid
.hash
, the_hash_algo
->rawsz
);
61 write_object_file(buf
.buf
, buf
.len
, OBJ_TREE
, oid
);
65 static const char *mktree_usage
[] = {
66 "git mktree [-z] [--missing] [--batch]",
70 static void mktree_line(char *buf
, int nul_term_line
, int allow_missing
)
75 enum object_type mode_type
; /* object type derived from mode */
76 enum object_type obj_type
; /* object type derived from sha */
77 struct object_info oi
= OBJECT_INFO_INIT
;
78 char *path
, *to_free
= NULL
;
83 * Read non-recursive ls-tree output format:
84 * mode SP type SP sha1 TAB name
86 mode
= strtoul(ptr
, &ntr
, 8);
87 if (ptr
== ntr
|| !ntr
|| *ntr
!= ' ')
88 die("input format error: %s", buf
);
89 ptr
= ntr
+ 1; /* type */
90 ntr
= strchr(ptr
, ' ');
91 if (!ntr
|| parse_oid_hex(ntr
+ 1, &oid
, &p
) ||
93 die("input format error: %s", buf
);
95 /* It is perfectly normal if we do not have a commit from a submodule */
96 if (S_ISGITLINK(mode
))
100 *ntr
++ = 0; /* now at the beginning of SHA1 */
102 path
= (char *)p
+ 1; /* at the beginning of name */
103 if (!nul_term_line
&& path
[0] == '"') {
104 struct strbuf p_uq
= STRBUF_INIT
;
105 if (unquote_c_style(&p_uq
, path
, NULL
))
106 die("invalid quoting");
107 path
= to_free
= strbuf_detach(&p_uq
, NULL
);
111 * Object type is redundantly derivable three ways.
112 * These should all agree.
114 mode_type
= object_type(mode
);
115 if (mode_type
!= type_from_string(ptr
)) {
116 die("entry '%s' object type (%s) doesn't match mode type (%s)",
117 path
, ptr
, type_name(mode_type
));
120 /* Check the type of object identified by oid without fetching objects */
121 oi
.typep
= &obj_type
;
122 if (oid_object_info_extended(the_repository
, &oid
, &oi
,
123 OBJECT_INFO_LOOKUP_REPLACE
|
125 OBJECT_INFO_SKIP_FETCH_OBJECT
) < 0)
130 ; /* no problem - missing objects are presumed to be of the right type */
132 die("entry '%s' object %s is unavailable", path
, oid_to_hex(&oid
));
135 if (obj_type
!= mode_type
) {
137 * The object exists but is of the wrong type.
138 * This is a problem regardless of allow_missing
139 * because the new tree entry will never be correct.
141 die("entry '%s' object %s is a %s but specified type was (%s)",
142 path
, oid_to_hex(&oid
), type_name(obj_type
), type_name(mode_type
));
146 append_to_tree(mode
, &oid
, path
);
150 int cmd_mktree(int ac
, const char **av
, const char *prefix
)
152 struct strbuf sb
= STRBUF_INIT
;
153 struct object_id oid
;
154 int nul_term_line
= 0;
155 int allow_missing
= 0;
156 int is_batch_mode
= 0;
158 strbuf_getline_fn getline_fn
;
160 const struct option option
[] = {
161 OPT_BOOL('z', NULL
, &nul_term_line
, N_("input is NUL terminated")),
162 OPT_SET_INT( 0 , "missing", &allow_missing
, N_("allow missing objects"), 1),
163 OPT_SET_INT( 0 , "batch", &is_batch_mode
, N_("allow creation of more than one tree"), 1),
167 ac
= parse_options(ac
, av
, prefix
, option
, mktree_usage
, 0);
168 getline_fn
= nul_term_line
? strbuf_getline_nul
: strbuf_getline_lf
;
172 if (getline_fn(&sb
, stdin
) == EOF
) {
176 if (sb
.buf
[0] == '\0') {
177 /* empty lines denote tree boundaries in batch mode */
180 die("input format error: (blank line only valid in batch mode)");
182 mktree_line(sb
.buf
, nul_term_line
, allow_missing
);
184 if (is_batch_mode
&& got_eof
&& used
< 1) {
186 * Execution gets here if the last tree entry is terminated with a
187 * new-line. The final new-line has been made optional to be
188 * consistent with the original non-batch behaviour of mktree.
190 ; /* skip creating an empty tree */
193 puts(oid_to_hex(&oid
));
196 used
=0; /* reset tree entry buffer for re-use in batch mode */