blob: 891991b00d673457ecdc6d82d1aa400a9eff03e9 [file] [log] [blame]
Junio C Hamano83f50532006-02-20 21:50:01 -08001/*
2 * GIT - the stupid content tracker
3 *
Junio C Hamano633e3552009-05-10 10:28:18 -07004 * Copyright (c) Junio C Hamano, 2006, 2009
Junio C Hamano83f50532006-02-20 21:50:01 -08005 */
Junio C Hamano633e3552009-05-10 10:28:18 -07006#include "builtin.h"
Junio C Hamano83f50532006-02-20 21:50:01 -08007#include "quote.h"
Peter Eriksen8e440252006-04-02 14:44:09 +02008#include "tree.h"
Junio C Hamano1fdee852009-05-10 10:31:56 -07009#include "parse-options.h"
Stefan Bellercbd53a22018-05-15 16:42:15 -070010#include "object-store.h"
Junio C Hamano83f50532006-02-20 21:50:01 -080011
12static struct treeent {
13 unsigned mode;
brian m. carlson83eb0802018-03-12 02:27:41 +000014 struct object_id oid;
Junio C Hamano83f50532006-02-20 21:50:01 -080015 int len;
16 char name[FLEX_ARRAY];
17} **entries;
18static int alloc, used;
19
brian m. carlson83eb0802018-03-12 02:27:41 +000020static void append_to_tree(unsigned mode, struct object_id *oid, char *path)
Junio C Hamano83f50532006-02-20 21:50:01 -080021{
22 struct treeent *ent;
Jeff King96ffc062016-02-22 17:44:32 -050023 size_t len = strlen(path);
Junio C Hamano83f50532006-02-20 21:50:01 -080024 if (strchr(path, '/'))
25 die("path %s contains slash", path);
26
Jeff King96ffc062016-02-22 17:44:32 -050027 FLEX_ALLOC_MEM(ent, name, path, len);
Junio C Hamano83f50532006-02-20 21:50:01 -080028 ent->mode = mode;
29 ent->len = len;
brian m. carlson83eb0802018-03-12 02:27:41 +000030 oidcpy(&ent->oid, oid);
Jeff King96ffc062016-02-22 17:44:32 -050031
32 ALLOC_GROW(entries, used + 1, alloc);
33 entries[used++] = ent;
Junio C Hamano83f50532006-02-20 21:50:01 -080034}
35
36static 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
Patryk Obaraa09c9852018-01-28 01:13:19 +010044static void write_tree(struct object_id *oid)
Junio C Hamano83f50532006-02-20 21:50:01 -080045{
Pierre Habouzitd52bc662007-09-06 13:20:08 +020046 struct strbuf buf;
47 size_t size;
Junio C Hamano83f50532006-02-20 21:50:01 -080048 int i;
49
René Scharfe9ed0d8d2016-09-29 17:27:31 +020050 QSORT(entries, used, ent_compare);
Junio C Hamano83f50532006-02-20 21:50:01 -080051 for (size = i = 0; i < used; i++)
52 size += 32 + entries[i]->len;
Junio C Hamano83f50532006-02-20 21:50:01 -080053
Pierre Habouzitf1696ee2007-09-10 12:35:04 +020054 strbuf_init(&buf, size);
Junio C Hamano83f50532006-02-20 21:50:01 -080055 for (i = 0; i < used; i++) {
56 struct treeent *ent = entries[i];
Pierre Habouzitd52bc662007-09-06 13:20:08 +020057 strbuf_addf(&buf, "%o %s%c", ent->mode, ent->name, '\0');
brian m. carlson83eb0802018-03-12 02:27:41 +000058 strbuf_add(&buf, ent->oid.hash, the_hash_algo->rawsz);
Junio C Hamano83f50532006-02-20 21:50:01 -080059 }
Pierre Habouzitd52bc662007-09-06 13:20:08 +020060
Patryk Obaraa09c9852018-01-28 01:13:19 +010061 write_object_file(buf.buf, buf.len, tree_type, oid);
Junio C Hamanoc444c162011-11-10 09:10:51 -080062 strbuf_release(&buf);
Junio C Hamano83f50532006-02-20 21:50:01 -080063}
64
Junio C Hamano1fdee852009-05-10 10:31:56 -070065static const char *mktree_usage[] = {
Nguyễn Thái Ngọc Duya6312812012-08-20 19:32:25 +070066 N_("git mktree [-z] [--missing] [--batch]"),
Junio C Hamano1fdee852009-05-10 10:31:56 -070067 NULL
68};
Junio C Hamano83f50532006-02-20 21:50:01 -080069
Jeff Kingbe27fb72019-05-09 17:30:37 -040070static void mktree_line(char *buf, int nul_term_line, int allow_missing)
Junio C Hamanofe0bb5f2009-05-10 10:41:22 -070071{
72 char *ptr, *ntr;
brian m. carlson83eb0802018-03-12 02:27:41 +000073 const char *p;
Junio C Hamanofe0bb5f2009-05-10 10:41:22 -070074 unsigned mode;
Josh Micich31c82212009-05-14 15:49:10 -070075 enum object_type mode_type; /* object type derived from mode */
76 enum object_type obj_type; /* object type derived from sha */
Johannes Schindelin43e61e72017-05-04 15:57:28 +020077 char *path, *to_free = NULL;
brian m. carlson83eb0802018-03-12 02:27:41 +000078 struct object_id oid;
Junio C Hamanofe0bb5f2009-05-10 10:41:22 -070079
80 ptr = buf;
81 /*
82 * Read non-recursive ls-tree output format:
83 * mode SP type SP sha1 TAB name
84 */
85 mode = strtoul(ptr, &ntr, 8);
86 if (ptr == ntr || !ntr || *ntr != ' ')
87 die("input format error: %s", buf);
88 ptr = ntr + 1; /* type */
89 ntr = strchr(ptr, ' ');
brian m. carlson83eb0802018-03-12 02:27:41 +000090 if (!ntr || parse_oid_hex(ntr + 1, &oid, &p) ||
91 *p != '\t')
Junio C Hamanofe0bb5f2009-05-10 10:41:22 -070092 die("input format error: %s", buf);
Junio C Hamanoad87b5d2009-05-10 10:45:52 -070093
94 /* It is perfectly normal if we do not have a commit from a submodule */
Junio C Hamano1c64e792009-05-10 11:30:03 -070095 if (S_ISGITLINK(mode))
96 allow_missing = 1;
97
Junio C Hamanoad87b5d2009-05-10 10:45:52 -070098
Junio C Hamanofe0bb5f2009-05-10 10:41:22 -070099 *ntr++ = 0; /* now at the beginning of SHA1 */
Junio C Hamanofe0bb5f2009-05-10 10:41:22 -0700100
brian m. carlson58ce21b2018-10-15 00:01:51 +0000101 path = (char *)p + 1; /* at the beginning of name */
Junio C Hamanob4df87b2016-01-13 15:55:12 -0800102 if (!nul_term_line && path[0] == '"') {
Junio C Hamanofe0bb5f2009-05-10 10:41:22 -0700103 struct strbuf p_uq = STRBUF_INIT;
104 if (unquote_c_style(&p_uq, path, NULL))
105 die("invalid quoting");
Johannes Schindelin43e61e72017-05-04 15:57:28 +0200106 path = to_free = strbuf_detach(&p_uq, NULL);
Junio C Hamanofe0bb5f2009-05-10 10:41:22 -0700107 }
Josh Micich31c82212009-05-14 15:49:10 -0700108
109 /*
110 * Object type is redundantly derivable three ways.
111 * These should all agree.
112 */
113 mode_type = object_type(mode);
114 if (mode_type != type_from_string(ptr)) {
115 die("entry '%s' object type (%s) doesn't match mode type (%s)",
Brandon Williamsdebca9d2018-02-14 10:59:24 -0800116 path, ptr, type_name(mode_type));
Josh Micich31c82212009-05-14 15:49:10 -0700117 }
118
119 /* Check the type of object identified by sha1 */
Stefan Beller0df8e962018-04-25 11:20:59 -0700120 obj_type = oid_object_info(the_repository, &oid, NULL);
Josh Micich31c82212009-05-14 15:49:10 -0700121 if (obj_type < 0) {
122 if (allow_missing) {
123 ; /* no problem - missing objects are presumed to be of the right type */
124 } else {
brian m. carlson83eb0802018-03-12 02:27:41 +0000125 die("entry '%s' object %s is unavailable", path, oid_to_hex(&oid));
Josh Micich31c82212009-05-14 15:49:10 -0700126 }
127 } else {
128 if (obj_type != mode_type) {
129 /*
130 * The object exists but is of the wrong type.
131 * This is a problem regardless of allow_missing
132 * because the new tree entry will never be correct.
133 */
134 die("entry '%s' object %s is a %s but specified type was (%s)",
brian m. carlson83eb0802018-03-12 02:27:41 +0000135 path, oid_to_hex(&oid), type_name(obj_type), type_name(mode_type));
Josh Micich31c82212009-05-14 15:49:10 -0700136 }
137 }
138
brian m. carlson83eb0802018-03-12 02:27:41 +0000139 append_to_tree(mode, &oid, path);
Johannes Schindelin43e61e72017-05-04 15:57:28 +0200140 free(to_free);
Junio C Hamanofe0bb5f2009-05-10 10:41:22 -0700141}
142
Junio C Hamano633e3552009-05-10 10:28:18 -0700143int cmd_mktree(int ac, const char **av, const char *prefix)
Junio C Hamano83f50532006-02-20 21:50:01 -0800144{
Brandon Caseyf285a2d2008-10-09 14:12:12 -0500145 struct strbuf sb = STRBUF_INIT;
Patryk Obaraa09c9852018-01-28 01:13:19 +0100146 struct object_id oid;
Junio C Hamanob4df87b2016-01-13 15:55:12 -0800147 int nul_term_line = 0;
Junio C Hamano1c64e792009-05-10 11:30:03 -0700148 int allow_missing = 0;
Josh Micichf1cf2d82009-05-14 12:51:15 -0700149 int is_batch_mode = 0;
150 int got_eof = 0;
Junio C Hamanob4df87b2016-01-13 15:55:12 -0800151 strbuf_getline_fn getline_fn;
Josh Micichf1cf2d82009-05-14 12:51:15 -0700152
Junio C Hamano1fdee852009-05-10 10:31:56 -0700153 const struct option option[] = {
Junio C Hamanob4df87b2016-01-13 15:55:12 -0800154 OPT_BOOL('z', NULL, &nul_term_line, N_("input is NUL terminated")),
Nguyễn Thái Ngọc Duya6312812012-08-20 19:32:25 +0700155 OPT_SET_INT( 0 , "missing", &allow_missing, N_("allow missing objects"), 1),
156 OPT_SET_INT( 0 , "batch", &is_batch_mode, N_("allow creation of more than one tree"), 1),
Junio C Hamano1fdee852009-05-10 10:31:56 -0700157 OPT_END()
158 };
Junio C Hamano83f50532006-02-20 21:50:01 -0800159
Stephen Boyd37782922009-05-23 11:53:12 -0700160 ac = parse_options(ac, av, prefix, option, mktree_usage, 0);
Junio C Hamanob4df87b2016-01-13 15:55:12 -0800161 getline_fn = nul_term_line ? strbuf_getline_nul : strbuf_getline_lf;
Junio C Hamano83f50532006-02-20 21:50:01 -0800162
Josh Micichf1cf2d82009-05-14 12:51:15 -0700163 while (!got_eof) {
164 while (1) {
Junio C Hamanob4df87b2016-01-13 15:55:12 -0800165 if (getline_fn(&sb, stdin) == EOF) {
Josh Micichf1cf2d82009-05-14 12:51:15 -0700166 got_eof = 1;
167 break;
168 }
169 if (sb.buf[0] == '\0') {
170 /* empty lines denote tree boundaries in batch mode */
171 if (is_batch_mode)
172 break;
173 die("input format error: (blank line only valid in batch mode)");
174 }
Jeff Kingbe27fb72019-05-09 17:30:37 -0400175 mktree_line(sb.buf, nul_term_line, allow_missing);
Josh Micichf1cf2d82009-05-14 12:51:15 -0700176 }
177 if (is_batch_mode && got_eof && used < 1) {
178 /*
179 * Execution gets here if the last tree entry is terminated with a
180 * new-line. The final new-line has been made optional to be
181 * consistent with the original non-batch behaviour of mktree.
182 */
183 ; /* skip creating an empty tree */
184 } else {
Patryk Obaraa09c9852018-01-28 01:13:19 +0100185 write_tree(&oid);
186 puts(oid_to_hex(&oid));
Josh Micichf1cf2d82009-05-14 12:51:15 -0700187 fflush(stdout);
188 }
189 used=0; /* reset tree entry buffer for re-use in batch mode */
190 }
Pierre Habouzite6c019d2007-09-17 11:19:04 +0200191 strbuf_release(&sb);
Junio C Hamano83f50532006-02-20 21:50:01 -0800192 exit(0);
193}