Junio C Hamano | 83f5053 | 2006-02-20 21:50:01 -0800 | [diff] [blame] | 1 | /* |
| 2 | * GIT - the stupid content tracker |
| 3 | * |
Junio C Hamano | 633e355 | 2009-05-10 10:28:18 -0700 | [diff] [blame] | 4 | * Copyright (c) Junio C Hamano, 2006, 2009 |
Junio C Hamano | 83f5053 | 2006-02-20 21:50:01 -0800 | [diff] [blame] | 5 | */ |
Junio C Hamano | 633e355 | 2009-05-10 10:28:18 -0700 | [diff] [blame] | 6 | #include "builtin.h" |
Junio C Hamano | 83f5053 | 2006-02-20 21:50:01 -0800 | [diff] [blame] | 7 | #include "quote.h" |
Peter Eriksen | 8e44025 | 2006-04-02 14:44:09 +0200 | [diff] [blame] | 8 | #include "tree.h" |
Junio C Hamano | 1fdee85 | 2009-05-10 10:31:56 -0700 | [diff] [blame] | 9 | #include "parse-options.h" |
Stefan Beller | cbd53a2 | 2018-05-15 16:42:15 -0700 | [diff] [blame] | 10 | #include "object-store.h" |
Junio C Hamano | 83f5053 | 2006-02-20 21:50:01 -0800 | [diff] [blame] | 11 | |
| 12 | static struct treeent { |
| 13 | unsigned mode; |
brian m. carlson | 83eb080 | 2018-03-12 02:27:41 +0000 | [diff] [blame] | 14 | struct object_id oid; |
Junio C Hamano | 83f5053 | 2006-02-20 21:50:01 -0800 | [diff] [blame] | 15 | int len; |
| 16 | char name[FLEX_ARRAY]; |
| 17 | } **entries; |
| 18 | static int alloc, used; |
| 19 | |
brian m. carlson | 83eb080 | 2018-03-12 02:27:41 +0000 | [diff] [blame] | 20 | static void append_to_tree(unsigned mode, struct object_id *oid, char *path) |
Junio C Hamano | 83f5053 | 2006-02-20 21:50:01 -0800 | [diff] [blame] | 21 | { |
| 22 | struct treeent *ent; |
Jeff King | 96ffc06 | 2016-02-22 17:44:32 -0500 | [diff] [blame] | 23 | size_t len = strlen(path); |
Junio C Hamano | 83f5053 | 2006-02-20 21:50:01 -0800 | [diff] [blame] | 24 | if (strchr(path, '/')) |
| 25 | die("path %s contains slash", path); |
| 26 | |
Jeff King | 96ffc06 | 2016-02-22 17:44:32 -0500 | [diff] [blame] | 27 | FLEX_ALLOC_MEM(ent, name, path, len); |
Junio C Hamano | 83f5053 | 2006-02-20 21:50:01 -0800 | [diff] [blame] | 28 | ent->mode = mode; |
| 29 | ent->len = len; |
brian m. carlson | 83eb080 | 2018-03-12 02:27:41 +0000 | [diff] [blame] | 30 | oidcpy(&ent->oid, oid); |
Jeff King | 96ffc06 | 2016-02-22 17:44:32 -0500 | [diff] [blame] | 31 | |
| 32 | ALLOC_GROW(entries, used + 1, alloc); |
| 33 | entries[used++] = ent; |
Junio C Hamano | 83f5053 | 2006-02-20 21:50:01 -0800 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | static 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 Obara | a09c985 | 2018-01-28 01:13:19 +0100 | [diff] [blame] | 44 | static void write_tree(struct object_id *oid) |
Junio C Hamano | 83f5053 | 2006-02-20 21:50:01 -0800 | [diff] [blame] | 45 | { |
Pierre Habouzit | d52bc66 | 2007-09-06 13:20:08 +0200 | [diff] [blame] | 46 | struct strbuf buf; |
| 47 | size_t size; |
Junio C Hamano | 83f5053 | 2006-02-20 21:50:01 -0800 | [diff] [blame] | 48 | int i; |
| 49 | |
René Scharfe | 9ed0d8d | 2016-09-29 17:27:31 +0200 | [diff] [blame] | 50 | QSORT(entries, used, ent_compare); |
Junio C Hamano | 83f5053 | 2006-02-20 21:50:01 -0800 | [diff] [blame] | 51 | for (size = i = 0; i < used; i++) |
| 52 | size += 32 + entries[i]->len; |
Junio C Hamano | 83f5053 | 2006-02-20 21:50:01 -0800 | [diff] [blame] | 53 | |
Pierre Habouzit | f1696ee | 2007-09-10 12:35:04 +0200 | [diff] [blame] | 54 | strbuf_init(&buf, size); |
Junio C Hamano | 83f5053 | 2006-02-20 21:50:01 -0800 | [diff] [blame] | 55 | for (i = 0; i < used; i++) { |
| 56 | struct treeent *ent = entries[i]; |
Pierre Habouzit | d52bc66 | 2007-09-06 13:20:08 +0200 | [diff] [blame] | 57 | strbuf_addf(&buf, "%o %s%c", ent->mode, ent->name, '\0'); |
brian m. carlson | 83eb080 | 2018-03-12 02:27:41 +0000 | [diff] [blame] | 58 | strbuf_add(&buf, ent->oid.hash, the_hash_algo->rawsz); |
Junio C Hamano | 83f5053 | 2006-02-20 21:50:01 -0800 | [diff] [blame] | 59 | } |
Pierre Habouzit | d52bc66 | 2007-09-06 13:20:08 +0200 | [diff] [blame] | 60 | |
Ævar Arnfjörð Bjarmason | c80d226 | 2022-02-05 00:48:26 +0100 | [diff] [blame] | 61 | write_object_file(buf.buf, buf.len, OBJ_TREE, oid); |
Junio C Hamano | c444c16 | 2011-11-10 09:10:51 -0800 | [diff] [blame] | 62 | strbuf_release(&buf); |
Junio C Hamano | 83f5053 | 2006-02-20 21:50:01 -0800 | [diff] [blame] | 63 | } |
| 64 | |
Junio C Hamano | 1fdee85 | 2009-05-10 10:31:56 -0700 | [diff] [blame] | 65 | static const char *mktree_usage[] = { |
Jean-Noël Avila | 959d670 | 2022-01-31 22:07:48 +0000 | [diff] [blame] | 66 | "git mktree [-z] [--missing] [--batch]", |
Junio C Hamano | 1fdee85 | 2009-05-10 10:31:56 -0700 | [diff] [blame] | 67 | NULL |
| 68 | }; |
Junio C Hamano | 83f5053 | 2006-02-20 21:50:01 -0800 | [diff] [blame] | 69 | |
Jeff King | be27fb7 | 2019-05-09 17:30:37 -0400 | [diff] [blame] | 70 | static void mktree_line(char *buf, int nul_term_line, int allow_missing) |
Junio C Hamano | fe0bb5f | 2009-05-10 10:41:22 -0700 | [diff] [blame] | 71 | { |
| 72 | char *ptr, *ntr; |
brian m. carlson | 83eb080 | 2018-03-12 02:27:41 +0000 | [diff] [blame] | 73 | const char *p; |
Junio C Hamano | fe0bb5f | 2009-05-10 10:41:22 -0700 | [diff] [blame] | 74 | unsigned mode; |
Josh Micich | 31c8221 | 2009-05-14 15:49:10 -0700 | [diff] [blame] | 75 | enum object_type mode_type; /* object type derived from mode */ |
| 76 | enum object_type obj_type; /* object type derived from sha */ |
Richard Oliver | 817b0f6 | 2022-06-21 14:59:39 +0100 | [diff] [blame] | 77 | struct object_info oi = OBJECT_INFO_INIT; |
Johannes Schindelin | 43e61e7 | 2017-05-04 15:57:28 +0200 | [diff] [blame] | 78 | char *path, *to_free = NULL; |
brian m. carlson | 83eb080 | 2018-03-12 02:27:41 +0000 | [diff] [blame] | 79 | struct object_id oid; |
Junio C Hamano | fe0bb5f | 2009-05-10 10:41:22 -0700 | [diff] [blame] | 80 | |
| 81 | ptr = buf; |
| 82 | /* |
| 83 | * Read non-recursive ls-tree output format: |
| 84 | * mode SP type SP sha1 TAB name |
| 85 | */ |
| 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, ' '); |
brian m. carlson | 83eb080 | 2018-03-12 02:27:41 +0000 | [diff] [blame] | 91 | if (!ntr || parse_oid_hex(ntr + 1, &oid, &p) || |
| 92 | *p != '\t') |
Junio C Hamano | fe0bb5f | 2009-05-10 10:41:22 -0700 | [diff] [blame] | 93 | die("input format error: %s", buf); |
Junio C Hamano | ad87b5d | 2009-05-10 10:45:52 -0700 | [diff] [blame] | 94 | |
| 95 | /* It is perfectly normal if we do not have a commit from a submodule */ |
Junio C Hamano | 1c64e79 | 2009-05-10 11:30:03 -0700 | [diff] [blame] | 96 | if (S_ISGITLINK(mode)) |
| 97 | allow_missing = 1; |
| 98 | |
Junio C Hamano | ad87b5d | 2009-05-10 10:45:52 -0700 | [diff] [blame] | 99 | |
Junio C Hamano | fe0bb5f | 2009-05-10 10:41:22 -0700 | [diff] [blame] | 100 | *ntr++ = 0; /* now at the beginning of SHA1 */ |
Junio C Hamano | fe0bb5f | 2009-05-10 10:41:22 -0700 | [diff] [blame] | 101 | |
brian m. carlson | 58ce21b | 2018-10-15 00:01:51 +0000 | [diff] [blame] | 102 | path = (char *)p + 1; /* at the beginning of name */ |
Junio C Hamano | b4df87b | 2016-01-13 15:55:12 -0800 | [diff] [blame] | 103 | if (!nul_term_line && path[0] == '"') { |
Junio C Hamano | fe0bb5f | 2009-05-10 10:41:22 -0700 | [diff] [blame] | 104 | struct strbuf p_uq = STRBUF_INIT; |
| 105 | if (unquote_c_style(&p_uq, path, NULL)) |
| 106 | die("invalid quoting"); |
Johannes Schindelin | 43e61e7 | 2017-05-04 15:57:28 +0200 | [diff] [blame] | 107 | path = to_free = strbuf_detach(&p_uq, NULL); |
Junio C Hamano | fe0bb5f | 2009-05-10 10:41:22 -0700 | [diff] [blame] | 108 | } |
Josh Micich | 31c8221 | 2009-05-14 15:49:10 -0700 | [diff] [blame] | 109 | |
| 110 | /* |
| 111 | * Object type is redundantly derivable three ways. |
| 112 | * These should all agree. |
| 113 | */ |
| 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)", |
Brandon Williams | debca9d | 2018-02-14 10:59:24 -0800 | [diff] [blame] | 117 | path, ptr, type_name(mode_type)); |
Josh Micich | 31c8221 | 2009-05-14 15:49:10 -0700 | [diff] [blame] | 118 | } |
| 119 | |
Richard Oliver | 817b0f6 | 2022-06-21 14:59:39 +0100 | [diff] [blame] | 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 | |
| 124 | OBJECT_INFO_QUICK | |
| 125 | OBJECT_INFO_SKIP_FETCH_OBJECT) < 0) |
| 126 | obj_type = -1; |
| 127 | |
Josh Micich | 31c8221 | 2009-05-14 15:49:10 -0700 | [diff] [blame] | 128 | if (obj_type < 0) { |
| 129 | if (allow_missing) { |
| 130 | ; /* no problem - missing objects are presumed to be of the right type */ |
| 131 | } else { |
brian m. carlson | 83eb080 | 2018-03-12 02:27:41 +0000 | [diff] [blame] | 132 | die("entry '%s' object %s is unavailable", path, oid_to_hex(&oid)); |
Josh Micich | 31c8221 | 2009-05-14 15:49:10 -0700 | [diff] [blame] | 133 | } |
| 134 | } else { |
| 135 | if (obj_type != mode_type) { |
| 136 | /* |
| 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. |
| 140 | */ |
| 141 | die("entry '%s' object %s is a %s but specified type was (%s)", |
brian m. carlson | 83eb080 | 2018-03-12 02:27:41 +0000 | [diff] [blame] | 142 | path, oid_to_hex(&oid), type_name(obj_type), type_name(mode_type)); |
Josh Micich | 31c8221 | 2009-05-14 15:49:10 -0700 | [diff] [blame] | 143 | } |
| 144 | } |
| 145 | |
brian m. carlson | 83eb080 | 2018-03-12 02:27:41 +0000 | [diff] [blame] | 146 | append_to_tree(mode, &oid, path); |
Johannes Schindelin | 43e61e7 | 2017-05-04 15:57:28 +0200 | [diff] [blame] | 147 | free(to_free); |
Junio C Hamano | fe0bb5f | 2009-05-10 10:41:22 -0700 | [diff] [blame] | 148 | } |
| 149 | |
Junio C Hamano | 633e355 | 2009-05-10 10:28:18 -0700 | [diff] [blame] | 150 | int cmd_mktree(int ac, const char **av, const char *prefix) |
Junio C Hamano | 83f5053 | 2006-02-20 21:50:01 -0800 | [diff] [blame] | 151 | { |
Brandon Casey | f285a2d | 2008-10-09 14:12:12 -0500 | [diff] [blame] | 152 | struct strbuf sb = STRBUF_INIT; |
Patryk Obara | a09c985 | 2018-01-28 01:13:19 +0100 | [diff] [blame] | 153 | struct object_id oid; |
Junio C Hamano | b4df87b | 2016-01-13 15:55:12 -0800 | [diff] [blame] | 154 | int nul_term_line = 0; |
Junio C Hamano | 1c64e79 | 2009-05-10 11:30:03 -0700 | [diff] [blame] | 155 | int allow_missing = 0; |
Josh Micich | f1cf2d8 | 2009-05-14 12:51:15 -0700 | [diff] [blame] | 156 | int is_batch_mode = 0; |
| 157 | int got_eof = 0; |
Junio C Hamano | b4df87b | 2016-01-13 15:55:12 -0800 | [diff] [blame] | 158 | strbuf_getline_fn getline_fn; |
Josh Micich | f1cf2d8 | 2009-05-14 12:51:15 -0700 | [diff] [blame] | 159 | |
Junio C Hamano | 1fdee85 | 2009-05-10 10:31:56 -0700 | [diff] [blame] | 160 | const struct option option[] = { |
Junio C Hamano | b4df87b | 2016-01-13 15:55:12 -0800 | [diff] [blame] | 161 | OPT_BOOL('z', NULL, &nul_term_line, N_("input is NUL terminated")), |
Nguyễn Thái Ngọc Duy | a631281 | 2012-08-20 19:32:25 +0700 | [diff] [blame] | 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), |
Junio C Hamano | 1fdee85 | 2009-05-10 10:31:56 -0700 | [diff] [blame] | 164 | OPT_END() |
| 165 | }; |
Junio C Hamano | 83f5053 | 2006-02-20 21:50:01 -0800 | [diff] [blame] | 166 | |
Stephen Boyd | 3778292 | 2009-05-23 11:53:12 -0700 | [diff] [blame] | 167 | ac = parse_options(ac, av, prefix, option, mktree_usage, 0); |
Junio C Hamano | b4df87b | 2016-01-13 15:55:12 -0800 | [diff] [blame] | 168 | getline_fn = nul_term_line ? strbuf_getline_nul : strbuf_getline_lf; |
Junio C Hamano | 83f5053 | 2006-02-20 21:50:01 -0800 | [diff] [blame] | 169 | |
Josh Micich | f1cf2d8 | 2009-05-14 12:51:15 -0700 | [diff] [blame] | 170 | while (!got_eof) { |
| 171 | while (1) { |
Junio C Hamano | b4df87b | 2016-01-13 15:55:12 -0800 | [diff] [blame] | 172 | if (getline_fn(&sb, stdin) == EOF) { |
Josh Micich | f1cf2d8 | 2009-05-14 12:51:15 -0700 | [diff] [blame] | 173 | got_eof = 1; |
| 174 | break; |
| 175 | } |
| 176 | if (sb.buf[0] == '\0') { |
| 177 | /* empty lines denote tree boundaries in batch mode */ |
| 178 | if (is_batch_mode) |
| 179 | break; |
| 180 | die("input format error: (blank line only valid in batch mode)"); |
| 181 | } |
Jeff King | be27fb7 | 2019-05-09 17:30:37 -0400 | [diff] [blame] | 182 | mktree_line(sb.buf, nul_term_line, allow_missing); |
Josh Micich | f1cf2d8 | 2009-05-14 12:51:15 -0700 | [diff] [blame] | 183 | } |
| 184 | if (is_batch_mode && got_eof && used < 1) { |
| 185 | /* |
| 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. |
| 189 | */ |
| 190 | ; /* skip creating an empty tree */ |
| 191 | } else { |
Patryk Obara | a09c985 | 2018-01-28 01:13:19 +0100 | [diff] [blame] | 192 | write_tree(&oid); |
| 193 | puts(oid_to_hex(&oid)); |
Josh Micich | f1cf2d8 | 2009-05-14 12:51:15 -0700 | [diff] [blame] | 194 | fflush(stdout); |
| 195 | } |
| 196 | used=0; /* reset tree entry buffer for re-use in batch mode */ |
| 197 | } |
Pierre Habouzit | e6c019d | 2007-09-17 11:19:04 +0200 | [diff] [blame] | 198 | strbuf_release(&sb); |
Ævar Arnfjörð Bjarmason | 338abb0 | 2021-06-08 12:48:03 +0200 | [diff] [blame] | 199 | return 0; |
Junio C Hamano | 83f5053 | 2006-02-20 21:50:01 -0800 | [diff] [blame] | 200 | } |