| 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_QUICK | |
| 129 | OBJECT_INFO_SKIP_FETCH_OBJECT) < 0) |
| 130 | obj_type = -1; |
| 131 | |
| 132 | if (obj_type < 0) { |
| 133 | if (allow_missing) { |
| 134 | ; /* no problem - missing objects are presumed to be of the right type */ |
| 135 | } else { |
| 136 | die("entry '%s' object %s is unavailable", path, oid_to_hex(&oid)); |
| 137 | } |
| 138 | } else { |
| 139 | if (obj_type != mode_type) { |
| 140 | /* |
| 141 | * The object exists but is of the wrong type. |
| 142 | * This is a problem regardless of allow_missing |
| 143 | * because the new tree entry will never be correct. |
| 144 | */ |
| 145 | die("entry '%s' object %s is a %s but specified type was (%s)", |
| 146 | path, oid_to_hex(&oid), type_name(obj_type), type_name(mode_type)); |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | append_to_tree(mode, &oid, path); |
| 151 | free(to_free); |
| 152 | } |
| 153 | |
| 154 | int cmd_mktree(int ac, |
| 155 | const char **av, |
| 156 | const char *prefix, |
| 157 | struct repository *repo) |
| 158 | { |
| 159 | struct strbuf sb = STRBUF_INIT; |
| 160 | struct object_id oid; |
| 161 | int nul_term_line = 0; |
| 162 | int allow_missing = 0; |
| 163 | int is_batch_mode = 0; |
| 164 | int got_eof = 0; |
| 165 | strbuf_getline_fn getline_fn; |
| 166 | |
| 167 | const struct option option[] = { |
| 168 | OPT_BOOL('z', NULL, &nul_term_line, N_("input is NUL terminated")), |
| 169 | OPT_SET_INT( 0 , "missing", &allow_missing, N_("allow missing objects"), 1), |
| 170 | OPT_SET_INT( 0 , "batch", &is_batch_mode, N_("allow creation of more than one tree"), 1), |
| 171 | OPT_END() |
| 172 | }; |
| 173 | |
| 174 | ac = parse_options(ac, av, prefix, option, mktree_usage, 0); |
| 175 | getline_fn = nul_term_line ? strbuf_getline_nul : strbuf_getline_lf; |
| 176 | |
| 177 | while (!got_eof) { |
| 178 | while (1) { |
| 179 | if (getline_fn(&sb, stdin) == EOF) { |
| 180 | got_eof = 1; |
| 181 | break; |
| 182 | } |
| 183 | if (sb.buf[0] == '\0') { |
| 184 | /* empty lines denote tree boundaries in batch mode */ |
| 185 | if (is_batch_mode) |
| 186 | break; |
| 187 | die("input format error: (blank line only valid in batch mode)"); |
| 188 | } |
| 189 | mktree_line(repo, sb.buf, nul_term_line, allow_missing); |
| 190 | } |
| 191 | if (is_batch_mode && got_eof && used < 1) { |
| 192 | /* |
| 193 | * Execution gets here if the last tree entry is terminated with a |
| 194 | * new-line. The final new-line has been made optional to be |
| 195 | * consistent with the original non-batch behaviour of mktree. |
| 196 | */ |
| 197 | ; /* skip creating an empty tree */ |
| 198 | } else { |
| 199 | write_tree(repo, &oid); |
| 200 | puts(oid_to_hex(&oid)); |
| 201 | fflush(stdout); |
| 202 | } |
| 203 | used=0; /* reset tree entry buffer for re-use in batch mode */ |
| 204 | } |
| 205 | strbuf_release(&sb); |
| 206 | |
| 207 | return 0; |
| 208 | } |