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" |
Junio C Hamano | 83f5053 | 2006-02-20 21:50:01 -0800 | [diff] [blame] | 10 | |
| 11 | static struct treeent { |
| 12 | unsigned mode; |
| 13 | unsigned char sha1[20]; |
| 14 | int len; |
| 15 | char name[FLEX_ARRAY]; |
| 16 | } **entries; |
| 17 | static int alloc, used; |
| 18 | |
| 19 | static void append_to_tree(unsigned mode, unsigned char *sha1, char *path) |
| 20 | { |
| 21 | struct treeent *ent; |
| 22 | int len = strlen(path); |
| 23 | if (strchr(path, '/')) |
| 24 | die("path %s contains slash", path); |
| 25 | |
| 26 | if (alloc <= used) { |
| 27 | alloc = alloc_nr(used); |
| 28 | entries = xrealloc(entries, sizeof(*entries) * alloc); |
| 29 | } |
| 30 | ent = entries[used++] = xmalloc(sizeof(**entries) + len + 1); |
| 31 | ent->mode = mode; |
| 32 | ent->len = len; |
Shawn Pearce | e702496 | 2006-08-23 02:49:00 -0400 | [diff] [blame] | 33 | hashcpy(ent->sha1, sha1); |
Junio C Hamano | 83f5053 | 2006-02-20 21:50:01 -0800 | [diff] [blame] | 34 | memcpy(ent->name, path, len+1); |
| 35 | } |
| 36 | |
| 37 | static int ent_compare(const void *a_, const void *b_) |
| 38 | { |
| 39 | struct treeent *a = *(struct treeent **)a_; |
| 40 | struct treeent *b = *(struct treeent **)b_; |
| 41 | return base_name_compare(a->name, a->len, a->mode, |
| 42 | b->name, b->len, b->mode); |
| 43 | } |
| 44 | |
| 45 | static void write_tree(unsigned char *sha1) |
| 46 | { |
Pierre Habouzit | d52bc66 | 2007-09-06 13:20:08 +0200 | [diff] [blame] | 47 | struct strbuf buf; |
| 48 | size_t size; |
Junio C Hamano | 83f5053 | 2006-02-20 21:50:01 -0800 | [diff] [blame] | 49 | int i; |
| 50 | |
| 51 | qsort(entries, used, sizeof(*entries), ent_compare); |
Junio C Hamano | 83f5053 | 2006-02-20 21:50:01 -0800 | [diff] [blame] | 52 | for (size = i = 0; i < used; i++) |
| 53 | size += 32 + entries[i]->len; |
Junio C Hamano | 83f5053 | 2006-02-20 21:50:01 -0800 | [diff] [blame] | 54 | |
Pierre Habouzit | f1696ee | 2007-09-10 12:35:04 +0200 | [diff] [blame] | 55 | strbuf_init(&buf, size); |
Junio C Hamano | 83f5053 | 2006-02-20 21:50:01 -0800 | [diff] [blame] | 56 | for (i = 0; i < used; i++) { |
| 57 | struct treeent *ent = entries[i]; |
Pierre Habouzit | d52bc66 | 2007-09-06 13:20:08 +0200 | [diff] [blame] | 58 | strbuf_addf(&buf, "%o %s%c", ent->mode, ent->name, '\0'); |
| 59 | strbuf_add(&buf, ent->sha1, 20); |
Junio C Hamano | 83f5053 | 2006-02-20 21:50:01 -0800 | [diff] [blame] | 60 | } |
Pierre Habouzit | d52bc66 | 2007-09-06 13:20:08 +0200 | [diff] [blame] | 61 | |
| 62 | write_sha1_file(buf.buf, buf.len, tree_type, sha1); |
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[] = { |
Josh Micich | f1cf2d8 | 2009-05-14 12:51:15 -0700 | [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 | |
Junio C Hamano | 1c64e79 | 2009-05-10 11:30:03 -0700 | [diff] [blame] | 70 | static void mktree_line(char *buf, size_t len, int line_termination, int allow_missing) |
Junio C Hamano | fe0bb5f | 2009-05-10 10:41:22 -0700 | [diff] [blame] | 71 | { |
| 72 | char *ptr, *ntr; |
| 73 | unsigned mode; |
Josh Micich | 31c8221 | 2009-05-14 15:49:10 -0700 | [diff] [blame] | 74 | enum object_type mode_type; /* object type derived from mode */ |
| 75 | enum object_type obj_type; /* object type derived from sha */ |
Junio C Hamano | fe0bb5f | 2009-05-10 10:41:22 -0700 | [diff] [blame] | 76 | char *path; |
| 77 | unsigned char sha1[20]; |
| 78 | |
| 79 | ptr = buf; |
| 80 | /* |
| 81 | * Read non-recursive ls-tree output format: |
| 82 | * mode SP type SP sha1 TAB name |
| 83 | */ |
| 84 | mode = strtoul(ptr, &ntr, 8); |
| 85 | if (ptr == ntr || !ntr || *ntr != ' ') |
| 86 | die("input format error: %s", buf); |
| 87 | ptr = ntr + 1; /* type */ |
| 88 | ntr = strchr(ptr, ' '); |
| 89 | if (!ntr || buf + len <= ntr + 40 || |
| 90 | ntr[41] != '\t' || |
| 91 | get_sha1_hex(ntr + 1, sha1)) |
| 92 | die("input format error: %s", buf); |
Junio C Hamano | ad87b5d | 2009-05-10 10:45:52 -0700 | [diff] [blame] | 93 | |
| 94 | /* 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] | 95 | if (S_ISGITLINK(mode)) |
| 96 | allow_missing = 1; |
| 97 | |
Junio C Hamano | ad87b5d | 2009-05-10 10:45:52 -0700 | [diff] [blame] | 98 | |
Junio C Hamano | fe0bb5f | 2009-05-10 10:41:22 -0700 | [diff] [blame] | 99 | *ntr++ = 0; /* now at the beginning of SHA1 */ |
Junio C Hamano | fe0bb5f | 2009-05-10 10:41:22 -0700 | [diff] [blame] | 100 | |
| 101 | path = ntr + 41; /* at the beginning of name */ |
| 102 | if (line_termination && path[0] == '"') { |
| 103 | struct strbuf p_uq = STRBUF_INIT; |
| 104 | if (unquote_c_style(&p_uq, path, NULL)) |
| 105 | die("invalid quoting"); |
| 106 | path = strbuf_detach(&p_uq, NULL); |
| 107 | } |
Josh Micich | 31c8221 | 2009-05-14 15:49:10 -0700 | [diff] [blame] | 108 | |
| 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)", |
| 116 | path, ptr, typename(mode_type)); |
| 117 | } |
| 118 | |
| 119 | /* Check the type of object identified by sha1 */ |
| 120 | obj_type = sha1_object_info(sha1, NULL); |
| 121 | if (obj_type < 0) { |
| 122 | if (allow_missing) { |
| 123 | ; /* no problem - missing objects are presumed to be of the right type */ |
| 124 | } else { |
| 125 | die("entry '%s' object %s is unavailable", path, sha1_to_hex(sha1)); |
| 126 | } |
| 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)", |
| 135 | path, sha1_to_hex(sha1), typename(obj_type), typename(mode_type)); |
| 136 | } |
| 137 | } |
| 138 | |
Junio C Hamano | fe0bb5f | 2009-05-10 10:41:22 -0700 | [diff] [blame] | 139 | append_to_tree(mode, sha1, path); |
| 140 | } |
| 141 | |
Junio C Hamano | 633e355 | 2009-05-10 10:28:18 -0700 | [diff] [blame] | 142 | int cmd_mktree(int ac, const char **av, const char *prefix) |
Junio C Hamano | 83f5053 | 2006-02-20 21:50:01 -0800 | [diff] [blame] | 143 | { |
Brandon Casey | f285a2d | 2008-10-09 14:12:12 -0500 | [diff] [blame] | 144 | struct strbuf sb = STRBUF_INIT; |
Junio C Hamano | 83f5053 | 2006-02-20 21:50:01 -0800 | [diff] [blame] | 145 | unsigned char sha1[20]; |
| 146 | int line_termination = '\n'; |
Junio C Hamano | 1c64e79 | 2009-05-10 11:30:03 -0700 | [diff] [blame] | 147 | int allow_missing = 0; |
Josh Micich | f1cf2d8 | 2009-05-14 12:51:15 -0700 | [diff] [blame] | 148 | int is_batch_mode = 0; |
| 149 | int got_eof = 0; |
| 150 | |
Junio C Hamano | 1fdee85 | 2009-05-10 10:31:56 -0700 | [diff] [blame] | 151 | const struct option option[] = { |
| 152 | OPT_SET_INT('z', NULL, &line_termination, "input is NUL terminated", '\0'), |
Junio C Hamano | 1c64e79 | 2009-05-10 11:30:03 -0700 | [diff] [blame] | 153 | OPT_SET_INT( 0 , "missing", &allow_missing, "allow missing objects", 1), |
Josh Micich | f1cf2d8 | 2009-05-14 12:51:15 -0700 | [diff] [blame] | 154 | OPT_SET_INT( 0 , "batch", &is_batch_mode, "allow creation of more than one tree", 1), |
Junio C Hamano | 1fdee85 | 2009-05-10 10:31:56 -0700 | [diff] [blame] | 155 | OPT_END() |
| 156 | }; |
Junio C Hamano | 83f5053 | 2006-02-20 21:50:01 -0800 | [diff] [blame] | 157 | |
Stephen Boyd | 3778292 | 2009-05-23 11:53:12 -0700 | [diff] [blame] | 158 | ac = parse_options(ac, av, prefix, option, mktree_usage, 0); |
Junio C Hamano | 83f5053 | 2006-02-20 21:50:01 -0800 | [diff] [blame] | 159 | |
Josh Micich | f1cf2d8 | 2009-05-14 12:51:15 -0700 | [diff] [blame] | 160 | while (!got_eof) { |
| 161 | while (1) { |
| 162 | if (strbuf_getline(&sb, stdin, line_termination) == EOF) { |
| 163 | got_eof = 1; |
| 164 | break; |
| 165 | } |
| 166 | if (sb.buf[0] == '\0') { |
| 167 | /* empty lines denote tree boundaries in batch mode */ |
| 168 | if (is_batch_mode) |
| 169 | break; |
| 170 | die("input format error: (blank line only valid in batch mode)"); |
| 171 | } |
| 172 | mktree_line(sb.buf, sb.len, line_termination, allow_missing); |
| 173 | } |
| 174 | if (is_batch_mode && got_eof && used < 1) { |
| 175 | /* |
| 176 | * Execution gets here if the last tree entry is terminated with a |
| 177 | * new-line. The final new-line has been made optional to be |
| 178 | * consistent with the original non-batch behaviour of mktree. |
| 179 | */ |
| 180 | ; /* skip creating an empty tree */ |
| 181 | } else { |
| 182 | write_tree(sha1); |
| 183 | puts(sha1_to_hex(sha1)); |
| 184 | fflush(stdout); |
| 185 | } |
| 186 | used=0; /* reset tree entry buffer for re-use in batch mode */ |
| 187 | } |
Pierre Habouzit | e6c019d | 2007-09-17 11:19:04 +0200 | [diff] [blame] | 188 | strbuf_release(&sb); |
Junio C Hamano | 83f5053 | 2006-02-20 21:50:01 -0800 | [diff] [blame] | 189 | exit(0); |
| 190 | } |