Git fork
at reftables-rust 209 lines 5.5 kB view raw
1/* 2 * GIT - the stupid content tracker 3 * 4 * Copyright (c) Junio C Hamano, 2006, 2009 5 */ 6#define USE_THE_REPOSITORY_VARIABLE 7#include "builtin.h" 8#include "gettext.h" 9#include "hex.h" 10#include "quote.h" 11#include "strbuf.h" 12#include "tree.h" 13#include "parse-options.h" 14#include "object-file.h" 15#include "odb.h" 16 17static struct treeent { 18 unsigned mode; 19 struct object_id oid; 20 int len; 21 char name[FLEX_ARRAY]; 22} **entries; 23static int alloc, used; 24 25static void append_to_tree(unsigned mode, struct object_id *oid, char *path) 26{ 27 struct treeent *ent; 28 size_t len = strlen(path); 29 if (strchr(path, '/')) 30 die("path %s contains slash", path); 31 32 FLEX_ALLOC_MEM(ent, name, path, len); 33 ent->mode = mode; 34 ent->len = len; 35 oidcpy(&ent->oid, oid); 36 37 ALLOC_GROW(entries, used + 1, alloc); 38 entries[used++] = ent; 39} 40 41static int ent_compare(const void *a_, const void *b_) 42{ 43 struct treeent *a = *(struct treeent **)a_; 44 struct treeent *b = *(struct treeent **)b_; 45 return base_name_compare(a->name, a->len, a->mode, 46 b->name, b->len, b->mode); 47} 48 49static void write_tree(struct object_id *oid) 50{ 51 struct strbuf buf; 52 size_t size; 53 int i; 54 55 QSORT(entries, used, ent_compare); 56 for (size = i = 0; i < used; i++) 57 size += 32 + entries[i]->len; 58 59 strbuf_init(&buf, size); 60 for (i = 0; i < used; i++) { 61 struct treeent *ent = entries[i]; 62 strbuf_addf(&buf, "%o %s%c", ent->mode, ent->name, '\0'); 63 strbuf_add(&buf, ent->oid.hash, the_hash_algo->rawsz); 64 } 65 66 odb_write_object(the_repository->objects, buf.buf, buf.len, OBJ_TREE, oid); 67 strbuf_release(&buf); 68} 69 70static const char *const mktree_usage[] = { 71 "git mktree [-z] [--missing] [--batch]", 72 NULL 73}; 74 75static void mktree_line(char *buf, int nul_term_line, int allow_missing) 76{ 77 char *ptr, *ntr; 78 const char *p; 79 unsigned mode; 80 enum object_type mode_type; /* object type derived from mode */ 81 enum object_type obj_type; /* object type derived from sha */ 82 struct object_info oi = OBJECT_INFO_INIT; 83 char *path, *to_free = NULL; 84 struct object_id oid; 85 86 ptr = buf; 87 /* 88 * Read non-recursive ls-tree output format: 89 * mode SP type SP sha1 TAB name 90 */ 91 mode = strtoul(ptr, &ntr, 8); 92 if (ptr == ntr || !ntr || *ntr != ' ') 93 die("input format error: %s", buf); 94 ptr = ntr + 1; /* type */ 95 ntr = strchr(ptr, ' '); 96 if (!ntr || parse_oid_hex(ntr + 1, &oid, &p) || 97 *p != '\t') 98 die("input format error: %s", buf); 99 100 /* It is perfectly normal if we do not have a commit from a submodule */ 101 if (S_ISGITLINK(mode)) 102 allow_missing = 1; 103 104 105 *ntr++ = 0; /* now at the beginning of SHA1 */ 106 107 path = (char *)p + 1; /* at the beginning of name */ 108 if (!nul_term_line && path[0] == '"') { 109 struct strbuf p_uq = STRBUF_INIT; 110 if (unquote_c_style(&p_uq, path, NULL)) 111 die("invalid quoting"); 112 path = to_free = strbuf_detach(&p_uq, NULL); 113 } 114 115 /* 116 * Object type is redundantly derivable three ways. 117 * These should all agree. 118 */ 119 mode_type = object_type(mode); 120 if (mode_type != type_from_string(ptr)) { 121 die("entry '%s' object type (%s) doesn't match mode type (%s)", 122 path, ptr, type_name(mode_type)); 123 } 124 125 /* Check the type of object identified by oid without fetching objects */ 126 oi.typep = &obj_type; 127 if (odb_read_object_info_extended(the_repository->objects, &oid, &oi, 128 OBJECT_INFO_LOOKUP_REPLACE | 129 OBJECT_INFO_QUICK | 130 OBJECT_INFO_SKIP_FETCH_OBJECT) < 0) 131 obj_type = -1; 132 133 if (obj_type < 0) { 134 if (allow_missing) { 135 ; /* no problem - missing objects are presumed to be of the right type */ 136 } else { 137 die("entry '%s' object %s is unavailable", path, oid_to_hex(&oid)); 138 } 139 } else { 140 if (obj_type != mode_type) { 141 /* 142 * The object exists but is of the wrong type. 143 * This is a problem regardless of allow_missing 144 * because the new tree entry will never be correct. 145 */ 146 die("entry '%s' object %s is a %s but specified type was (%s)", 147 path, oid_to_hex(&oid), type_name(obj_type), type_name(mode_type)); 148 } 149 } 150 151 append_to_tree(mode, &oid, path); 152 free(to_free); 153} 154 155int cmd_mktree(int ac, 156 const char **av, 157 const char *prefix, 158 struct repository *repo UNUSED) 159{ 160 struct strbuf sb = STRBUF_INIT; 161 struct object_id oid; 162 int nul_term_line = 0; 163 int allow_missing = 0; 164 int is_batch_mode = 0; 165 int got_eof = 0; 166 strbuf_getline_fn getline_fn; 167 168 const struct option option[] = { 169 OPT_BOOL('z', NULL, &nul_term_line, N_("input is NUL terminated")), 170 OPT_SET_INT( 0 , "missing", &allow_missing, N_("allow missing objects"), 1), 171 OPT_SET_INT( 0 , "batch", &is_batch_mode, N_("allow creation of more than one tree"), 1), 172 OPT_END() 173 }; 174 175 ac = parse_options(ac, av, prefix, option, mktree_usage, 0); 176 getline_fn = nul_term_line ? strbuf_getline_nul : strbuf_getline_lf; 177 178 while (!got_eof) { 179 while (1) { 180 if (getline_fn(&sb, stdin) == EOF) { 181 got_eof = 1; 182 break; 183 } 184 if (sb.buf[0] == '\0') { 185 /* empty lines denote tree boundaries in batch mode */ 186 if (is_batch_mode) 187 break; 188 die("input format error: (blank line only valid in batch mode)"); 189 } 190 mktree_line(sb.buf, nul_term_line, allow_missing); 191 } 192 if (is_batch_mode && got_eof && used < 1) { 193 /* 194 * Execution gets here if the last tree entry is terminated with a 195 * new-line. The final new-line has been made optional to be 196 * consistent with the original non-batch behaviour of mktree. 197 */ 198 ; /* skip creating an empty tree */ 199 } else { 200 write_tree(&oid); 201 puts(oid_to_hex(&oid)); 202 fflush(stdout); 203 } 204 used=0; /* reset tree entry buffer for re-use in batch mode */ 205 } 206 strbuf_release(&sb); 207 208 return 0; 209}