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