Junio C Hamano | 8f1d2e6 | 2006-01-07 01:33:54 -0800 | [diff] [blame] | 1 | #include "cache.h" |
Junio C Hamano | af3785d | 2007-08-09 13:42:50 -0700 | [diff] [blame] | 2 | #include "cache-tree.h" |
Daniel Barkalow | 175785e | 2005-04-18 11:39:48 -0700 | [diff] [blame] | 3 | #include "tree.h" |
| 4 | #include "blob.h" |
Daniel Barkalow | 77675e2 | 2005-09-05 02:03:51 -0400 | [diff] [blame] | 5 | #include "commit.h" |
| 6 | #include "tag.h" |
Linus Torvalds | 136f2e5 | 2006-05-29 12:16:12 -0700 | [diff] [blame] | 7 | #include "tree-walk.h" |
Daniel Barkalow | 175785e | 2005-04-18 11:39:48 -0700 | [diff] [blame] | 8 | |
| 9 | const char *tree_type = "tree"; |
| 10 | |
Junio C Hamano | af3785d | 2007-08-09 13:42:50 -0700 | [diff] [blame] | 11 | static int read_one_entry_opt(const unsigned char *sha1, const char *base, int baselen, const char *pathname, unsigned mode, int stage, int opt) |
Linus Torvalds | 94537c7 | 2005-04-22 16:42:37 -0700 | [diff] [blame] | 12 | { |
Linus Torvalds | 3c5e846 | 2005-11-26 09:38:20 -0800 | [diff] [blame] | 13 | int len; |
| 14 | unsigned int size; |
| 15 | struct cache_entry *ce; |
| 16 | |
| 17 | if (S_ISDIR(mode)) |
| 18 | return READ_TREE_RECURSIVE; |
| 19 | |
| 20 | len = strlen(pathname); |
| 21 | size = cache_entry_size(baselen + len); |
Peter Eriksen | 90321c1 | 2006-04-03 19:30:46 +0100 | [diff] [blame] | 22 | ce = xcalloc(1, size); |
Linus Torvalds | 94537c7 | 2005-04-22 16:42:37 -0700 | [diff] [blame] | 23 | |
| 24 | ce->ce_mode = create_ce_mode(mode); |
Thomas Gummerer | b60e188 | 2012-07-11 11:22:37 +0200 | [diff] [blame] | 25 | ce->ce_flags = create_ce_flags(stage); |
| 26 | ce->ce_namelen = baselen + len; |
Linus Torvalds | 94537c7 | 2005-04-22 16:42:37 -0700 | [diff] [blame] | 27 | memcpy(ce->name, base, baselen); |
| 28 | memcpy(ce->name + baselen, pathname, len+1); |
brian m. carlson | 99d1a98 | 2016-09-05 20:07:52 +0000 | [diff] [blame] | 29 | hashcpy(ce->oid.hash, sha1); |
Junio C Hamano | af3785d | 2007-08-09 13:42:50 -0700 | [diff] [blame] | 30 | return add_cache_entry(ce, opt); |
| 31 | } |
| 32 | |
Nguyễn Thái Ngọc Duy | 6a0b0b6 | 2014-11-30 16:05:00 +0700 | [diff] [blame] | 33 | static int read_one_entry(const unsigned char *sha1, struct strbuf *base, |
| 34 | const char *pathname, unsigned mode, int stage, |
| 35 | void *context) |
Junio C Hamano | af3785d | 2007-08-09 13:42:50 -0700 | [diff] [blame] | 36 | { |
Nguyễn Thái Ngọc Duy | 6a0b0b6 | 2014-11-30 16:05:00 +0700 | [diff] [blame] | 37 | return read_one_entry_opt(sha1, base->buf, base->len, pathname, |
| 38 | mode, stage, |
Junio C Hamano | af3785d | 2007-08-09 13:42:50 -0700 | [diff] [blame] | 39 | ADD_CACHE_OK_TO_ADD|ADD_CACHE_SKIP_DFCHECK); |
| 40 | } |
| 41 | |
| 42 | /* |
| 43 | * This is used when the caller knows there is no existing entries at |
| 44 | * the stage that will conflict with the entry being added. |
| 45 | */ |
Nguyễn Thái Ngọc Duy | 6a0b0b6 | 2014-11-30 16:05:00 +0700 | [diff] [blame] | 46 | static int read_one_entry_quick(const unsigned char *sha1, struct strbuf *base, |
| 47 | const char *pathname, unsigned mode, int stage, |
| 48 | void *context) |
Junio C Hamano | af3785d | 2007-08-09 13:42:50 -0700 | [diff] [blame] | 49 | { |
Nguyễn Thái Ngọc Duy | 6a0b0b6 | 2014-11-30 16:05:00 +0700 | [diff] [blame] | 50 | return read_one_entry_opt(sha1, base->buf, base->len, pathname, |
| 51 | mode, stage, |
Junio C Hamano | af3785d | 2007-08-09 13:42:50 -0700 | [diff] [blame] | 52 | ADD_CACHE_JUST_APPEND); |
Linus Torvalds | 94537c7 | 2005-04-22 16:42:37 -0700 | [diff] [blame] | 53 | } |
| 54 | |
Nguyễn Thái Ngọc Duy | ffd31f6 | 2011-03-25 16:34:18 +0700 | [diff] [blame] | 55 | static int read_tree_1(struct tree *tree, struct strbuf *base, |
Nguyễn Thái Ngọc Duy | 18e4f40 | 2013-07-14 15:35:52 +0700 | [diff] [blame] | 56 | int stage, const struct pathspec *pathspec, |
Nguyễn Thái Ngọc Duy | ffd31f6 | 2011-03-25 16:34:18 +0700 | [diff] [blame] | 57 | read_tree_fn_t fn, void *context) |
Linus Torvalds | 0ca14a5 | 2005-07-14 11:26:31 -0700 | [diff] [blame] | 58 | { |
Nguyễn Thái Ngọc Duy | ffd31f6 | 2011-03-25 16:34:18 +0700 | [diff] [blame] | 59 | struct tree_desc desc; |
| 60 | struct name_entry entry; |
| 61 | unsigned char sha1[20]; |
Nguyễn Thái Ngọc Duy | d688cf0 | 2011-10-24 17:36:10 +1100 | [diff] [blame] | 62 | int len, oldlen = base->len; |
| 63 | enum interesting retval = entry_not_interesting; |
Linus Torvalds | 0ca14a5 | 2005-07-14 11:26:31 -0700 | [diff] [blame] | 64 | |
Nguyễn Thái Ngọc Duy | ffd31f6 | 2011-03-25 16:34:18 +0700 | [diff] [blame] | 65 | if (parse_tree(tree)) |
| 66 | return -1; |
Linus Torvalds | 0ca14a5 | 2005-07-14 11:26:31 -0700 | [diff] [blame] | 67 | |
Nguyễn Thái Ngọc Duy | ffd31f6 | 2011-03-25 16:34:18 +0700 | [diff] [blame] | 68 | init_tree_desc(&desc, tree->buffer, tree->size); |
Linus Torvalds | 0ca14a5 | 2005-07-14 11:26:31 -0700 | [diff] [blame] | 69 | |
Nguyễn Thái Ngọc Duy | ffd31f6 | 2011-03-25 16:34:18 +0700 | [diff] [blame] | 70 | while (tree_entry(&desc, &entry)) { |
Nguyễn Thái Ngọc Duy | d688cf0 | 2011-10-24 17:36:10 +1100 | [diff] [blame] | 71 | if (retval != all_entries_interesting) { |
Nguyễn Thái Ngọc Duy | ffd31f6 | 2011-03-25 16:34:18 +0700 | [diff] [blame] | 72 | retval = tree_entry_interesting(&entry, base, 0, pathspec); |
Nguyễn Thái Ngọc Duy | d688cf0 | 2011-10-24 17:36:10 +1100 | [diff] [blame] | 73 | if (retval == all_entries_not_interesting) |
Nguyễn Thái Ngọc Duy | ffd31f6 | 2011-03-25 16:34:18 +0700 | [diff] [blame] | 74 | break; |
Nguyễn Thái Ngọc Duy | d688cf0 | 2011-10-24 17:36:10 +1100 | [diff] [blame] | 75 | if (retval == entry_not_interesting) |
Linus Torvalds | 0ca14a5 | 2005-07-14 11:26:31 -0700 | [diff] [blame] | 76 | continue; |
| 77 | } |
| 78 | |
brian m. carlson | 7d924c9 | 2016-04-17 23:10:39 +0000 | [diff] [blame] | 79 | switch (fn(entry.oid->hash, base, |
Nguyễn Thái Ngọc Duy | ffd31f6 | 2011-03-25 16:34:18 +0700 | [diff] [blame] | 80 | entry.path, entry.mode, stage, context)) { |
| 81 | case 0: |
| 82 | continue; |
| 83 | case READ_TREE_RECURSIVE: |
| 84 | break; |
| 85 | default: |
| 86 | return -1; |
| 87 | } |
| 88 | |
| 89 | if (S_ISDIR(entry.mode)) |
brian m. carlson | 7d924c9 | 2016-04-17 23:10:39 +0000 | [diff] [blame] | 90 | hashcpy(sha1, entry.oid->hash); |
Nguyễn Thái Ngọc Duy | ffd31f6 | 2011-03-25 16:34:18 +0700 | [diff] [blame] | 91 | else if (S_ISGITLINK(entry.mode)) { |
| 92 | struct commit *commit; |
| 93 | |
brian m. carlson | 7d924c9 | 2016-04-17 23:10:39 +0000 | [diff] [blame] | 94 | commit = lookup_commit(entry.oid->hash); |
Nguyễn Thái Ngọc Duy | ffd31f6 | 2011-03-25 16:34:18 +0700 | [diff] [blame] | 95 | if (!commit) |
| 96 | die("Commit %s in submodule path %s%s not found", |
brian m. carlson | 7d924c9 | 2016-04-17 23:10:39 +0000 | [diff] [blame] | 97 | oid_to_hex(entry.oid), |
Nguyễn Thái Ngọc Duy | ffd31f6 | 2011-03-25 16:34:18 +0700 | [diff] [blame] | 98 | base->buf, entry.path); |
| 99 | |
| 100 | if (parse_commit(commit)) |
| 101 | die("Invalid commit %s in submodule path %s%s", |
brian m. carlson | 7d924c9 | 2016-04-17 23:10:39 +0000 | [diff] [blame] | 102 | oid_to_hex(entry.oid), |
Nguyễn Thái Ngọc Duy | ffd31f6 | 2011-03-25 16:34:18 +0700 | [diff] [blame] | 103 | base->buf, entry.path); |
| 104 | |
brian m. carlson | ed1c997 | 2015-11-10 02:22:29 +0000 | [diff] [blame] | 105 | hashcpy(sha1, commit->tree->object.oid.hash); |
Nguyễn Thái Ngọc Duy | ffd31f6 | 2011-03-25 16:34:18 +0700 | [diff] [blame] | 106 | } |
| 107 | else |
Linus Torvalds | 0ca14a5 | 2005-07-14 11:26:31 -0700 | [diff] [blame] | 108 | continue; |
Linus Torvalds | 3e58763 | 2005-07-14 11:39:27 -0700 | [diff] [blame] | 109 | |
Nguyễn Thái Ngọc Duy | 0de1633 | 2011-10-24 17:36:09 +1100 | [diff] [blame] | 110 | len = tree_entry_len(&entry); |
Nguyễn Thái Ngọc Duy | ffd31f6 | 2011-03-25 16:34:18 +0700 | [diff] [blame] | 111 | strbuf_add(base, entry.path, len); |
| 112 | strbuf_addch(base, '/'); |
| 113 | retval = read_tree_1(lookup_tree(sha1), |
| 114 | base, stage, pathspec, |
| 115 | fn, context); |
| 116 | strbuf_setlen(base, oldlen); |
| 117 | if (retval) |
| 118 | return -1; |
Linus Torvalds | 0ca14a5 | 2005-07-14 11:26:31 -0700 | [diff] [blame] | 119 | } |
| 120 | return 0; |
| 121 | } |
| 122 | |
Daniel Barkalow | 521698b | 2006-01-26 01:13:36 -0500 | [diff] [blame] | 123 | int read_tree_recursive(struct tree *tree, |
Linus Torvalds | 3c5e846 | 2005-11-26 09:38:20 -0800 | [diff] [blame] | 124 | const char *base, int baselen, |
Nguyễn Thái Ngọc Duy | 18e4f40 | 2013-07-14 15:35:52 +0700 | [diff] [blame] | 125 | int stage, const struct pathspec *pathspec, |
René Scharfe | 671f070 | 2008-07-14 21:22:12 +0200 | [diff] [blame] | 126 | read_tree_fn_t fn, void *context) |
Linus Torvalds | 94537c7 | 2005-04-22 16:42:37 -0700 | [diff] [blame] | 127 | { |
Nguyễn Thái Ngọc Duy | ffd31f6 | 2011-03-25 16:34:18 +0700 | [diff] [blame] | 128 | struct strbuf sb = STRBUF_INIT; |
Nguyễn Thái Ngọc Duy | f0096c0 | 2011-03-25 16:34:19 +0700 | [diff] [blame] | 129 | int ret; |
Linus Torvalds | 0790a42 | 2006-05-29 12:17:28 -0700 | [diff] [blame] | 130 | |
Nguyễn Thái Ngọc Duy | ffd31f6 | 2011-03-25 16:34:18 +0700 | [diff] [blame] | 131 | strbuf_add(&sb, base, baselen); |
Nguyễn Thái Ngọc Duy | f0096c0 | 2011-03-25 16:34:19 +0700 | [diff] [blame] | 132 | ret = read_tree_1(tree, &sb, stage, pathspec, fn, context); |
Nguyễn Thái Ngọc Duy | ffd31f6 | 2011-03-25 16:34:18 +0700 | [diff] [blame] | 133 | strbuf_release(&sb); |
Nguyễn Thái Ngọc Duy | ffd31f6 | 2011-03-25 16:34:18 +0700 | [diff] [blame] | 134 | return ret; |
Linus Torvalds | 94537c7 | 2005-04-22 16:42:37 -0700 | [diff] [blame] | 135 | } |
| 136 | |
Junio C Hamano | af3785d | 2007-08-09 13:42:50 -0700 | [diff] [blame] | 137 | static int cmp_cache_name_compare(const void *a_, const void *b_) |
| 138 | { |
| 139 | const struct cache_entry *ce1, *ce2; |
| 140 | |
| 141 | ce1 = *((const struct cache_entry **)a_); |
| 142 | ce2 = *((const struct cache_entry **)b_); |
Thomas Gummerer | b60e188 | 2012-07-11 11:22:37 +0200 | [diff] [blame] | 143 | return cache_name_stage_compare(ce1->name, ce1->ce_namelen, ce_stage(ce1), |
| 144 | ce2->name, ce2->ce_namelen, ce_stage(ce2)); |
Junio C Hamano | af3785d | 2007-08-09 13:42:50 -0700 | [diff] [blame] | 145 | } |
| 146 | |
Nguyễn Thái Ngọc Duy | f0096c0 | 2011-03-25 16:34:19 +0700 | [diff] [blame] | 147 | int read_tree(struct tree *tree, int stage, struct pathspec *match) |
Linus Torvalds | 94537c7 | 2005-04-22 16:42:37 -0700 | [diff] [blame] | 148 | { |
Junio C Hamano | af3785d | 2007-08-09 13:42:50 -0700 | [diff] [blame] | 149 | read_tree_fn_t fn = NULL; |
| 150 | int i, err; |
| 151 | |
| 152 | /* |
| 153 | * Currently the only existing callers of this function all |
| 154 | * call it with stage=1 and after making sure there is nothing |
| 155 | * at that stage; we could always use read_one_entry_quick(). |
| 156 | * |
| 157 | * But when we decide to straighten out git-read-tree not to |
| 158 | * use unpack_trees() in some cases, this will probably start |
| 159 | * to matter. |
| 160 | */ |
| 161 | |
| 162 | /* |
| 163 | * See if we have cache entry at the stage. If so, |
| 164 | * do it the original slow way, otherwise, append and then |
| 165 | * sort at the end. |
| 166 | */ |
| 167 | for (i = 0; !fn && i < active_nr; i++) { |
Nguyễn Thái Ngọc Duy | 9c5e6c8 | 2013-07-09 22:29:00 +0700 | [diff] [blame] | 168 | const struct cache_entry *ce = active_cache[i]; |
Junio C Hamano | af3785d | 2007-08-09 13:42:50 -0700 | [diff] [blame] | 169 | if (ce_stage(ce) == stage) |
| 170 | fn = read_one_entry; |
| 171 | } |
| 172 | |
| 173 | if (!fn) |
| 174 | fn = read_one_entry_quick; |
René Scharfe | 671f070 | 2008-07-14 21:22:12 +0200 | [diff] [blame] | 175 | err = read_tree_recursive(tree, "", 0, stage, match, fn, NULL); |
Junio C Hamano | af3785d | 2007-08-09 13:42:50 -0700 | [diff] [blame] | 176 | if (fn == read_one_entry || err) |
| 177 | return err; |
| 178 | |
| 179 | /* |
| 180 | * Sort the cache entry -- we need to nuke the cache tree, though. |
| 181 | */ |
| 182 | cache_tree_free(&active_cache_tree); |
René Scharfe | 9ed0d8d | 2016-09-29 17:27:31 +0200 | [diff] [blame] | 183 | QSORT(active_cache, active_nr, cmp_cache_name_compare); |
Junio C Hamano | af3785d | 2007-08-09 13:42:50 -0700 | [diff] [blame] | 184 | return 0; |
Linus Torvalds | 94537c7 | 2005-04-22 16:42:37 -0700 | [diff] [blame] | 185 | } |
| 186 | |
Jason McMullan | 5d6ccf5 | 2005-06-03 11:05:39 -0400 | [diff] [blame] | 187 | struct tree *lookup_tree(const unsigned char *sha1) |
Daniel Barkalow | 175785e | 2005-04-18 11:39:48 -0700 | [diff] [blame] | 188 | { |
| 189 | struct object *obj = lookup_object(sha1); |
Linus Torvalds | 100c5f3 | 2007-04-16 22:11:43 -0700 | [diff] [blame] | 190 | if (!obj) |
Jeff King | d36f51c | 2014-07-13 02:41:55 -0400 | [diff] [blame] | 191 | return create_object(sha1, alloc_tree_node()); |
Jeff King | 8ff226a | 2014-07-13 02:42:03 -0400 | [diff] [blame] | 192 | return object_as_type(obj, OBJ_TREE, 0); |
Daniel Barkalow | 175785e | 2005-04-18 11:39:48 -0700 | [diff] [blame] | 193 | } |
| 194 | |
Nicolas Pitre | bd2c39f | 2005-05-06 13:48:34 -0400 | [diff] [blame] | 195 | int parse_tree_buffer(struct tree *item, void *buffer, unsigned long size) |
Daniel Barkalow | 175785e | 2005-04-18 11:39:48 -0700 | [diff] [blame] | 196 | { |
Daniel Barkalow | 175785e | 2005-04-18 11:39:48 -0700 | [diff] [blame] | 197 | if (item->object.parsed) |
| 198 | return 0; |
| 199 | item->object.parsed = 1; |
Linus Torvalds | 136f2e5 | 2006-05-29 12:16:12 -0700 | [diff] [blame] | 200 | item->buffer = buffer; |
| 201 | item->size = size; |
| 202 | |
Linus Torvalds | 2d9c58c | 2006-05-29 12:18:33 -0700 | [diff] [blame] | 203 | return 0; |
| 204 | } |
Linus Torvalds | 136f2e5 | 2006-05-29 12:16:12 -0700 | [diff] [blame] | 205 | |
Jeff King | 9cc2b07 | 2015-06-01 05:56:26 -0400 | [diff] [blame] | 206 | int parse_tree_gently(struct tree *item, int quiet_on_missing) |
Nicolas Pitre | bd2c39f | 2005-05-06 13:48:34 -0400 | [diff] [blame] | 207 | { |
Nicolas Pitre | 21666f1 | 2007-02-26 14:55:59 -0500 | [diff] [blame] | 208 | enum object_type type; |
Nicolas Pitre | bd2c39f | 2005-05-06 13:48:34 -0400 | [diff] [blame] | 209 | void *buffer; |
| 210 | unsigned long size; |
Nicolas Pitre | bd2c39f | 2005-05-06 13:48:34 -0400 | [diff] [blame] | 211 | |
| 212 | if (item->object.parsed) |
| 213 | return 0; |
brian m. carlson | ed1c997 | 2015-11-10 02:22:29 +0000 | [diff] [blame] | 214 | buffer = read_sha1_file(item->object.oid.hash, &type, &size); |
Nicolas Pitre | bd2c39f | 2005-05-06 13:48:34 -0400 | [diff] [blame] | 215 | if (!buffer) |
Jeff King | 9cc2b07 | 2015-06-01 05:56:26 -0400 | [diff] [blame] | 216 | return quiet_on_missing ? -1 : |
| 217 | error("Could not read %s", |
brian m. carlson | f2fd076 | 2015-11-10 02:22:28 +0000 | [diff] [blame] | 218 | oid_to_hex(&item->object.oid)); |
Nicolas Pitre | 21666f1 | 2007-02-26 14:55:59 -0500 | [diff] [blame] | 219 | if (type != OBJ_TREE) { |
Nicolas Pitre | bd2c39f | 2005-05-06 13:48:34 -0400 | [diff] [blame] | 220 | free(buffer); |
| 221 | return error("Object %s not a tree", |
brian m. carlson | f2fd076 | 2015-11-10 02:22:28 +0000 | [diff] [blame] | 222 | oid_to_hex(&item->object.oid)); |
Nicolas Pitre | bd2c39f | 2005-05-06 13:48:34 -0400 | [diff] [blame] | 223 | } |
Linus Torvalds | 136f2e5 | 2006-05-29 12:16:12 -0700 | [diff] [blame] | 224 | return parse_tree_buffer(item, buffer, size); |
Nicolas Pitre | bd2c39f | 2005-05-06 13:48:34 -0400 | [diff] [blame] | 225 | } |
Daniel Barkalow | 77675e2 | 2005-09-05 02:03:51 -0400 | [diff] [blame] | 226 | |
Jeff King | 6e454b9 | 2013-06-05 18:37:39 -0400 | [diff] [blame] | 227 | void free_tree_buffer(struct tree *tree) |
| 228 | { |
| 229 | free(tree->buffer); |
| 230 | tree->buffer = NULL; |
| 231 | tree->size = 0; |
| 232 | tree->object.parsed = 0; |
| 233 | } |
| 234 | |
Daniel Barkalow | 77675e2 | 2005-09-05 02:03:51 -0400 | [diff] [blame] | 235 | struct tree *parse_tree_indirect(const unsigned char *sha1) |
| 236 | { |
| 237 | struct object *obj = parse_object(sha1); |
| 238 | do { |
| 239 | if (!obj) |
| 240 | return NULL; |
Linus Torvalds | 1974632 | 2006-07-11 20:45:31 -0700 | [diff] [blame] | 241 | if (obj->type == OBJ_TREE) |
Daniel Barkalow | 77675e2 | 2005-09-05 02:03:51 -0400 | [diff] [blame] | 242 | return (struct tree *) obj; |
Linus Torvalds | 1974632 | 2006-07-11 20:45:31 -0700 | [diff] [blame] | 243 | else if (obj->type == OBJ_COMMIT) |
Daniel Barkalow | 77675e2 | 2005-09-05 02:03:51 -0400 | [diff] [blame] | 244 | obj = &(((struct commit *) obj)->tree->object); |
Linus Torvalds | 1974632 | 2006-07-11 20:45:31 -0700 | [diff] [blame] | 245 | else if (obj->type == OBJ_TAG) |
Daniel Barkalow | 77675e2 | 2005-09-05 02:03:51 -0400 | [diff] [blame] | 246 | obj = ((struct tag *) obj)->tagged; |
| 247 | else |
| 248 | return NULL; |
| 249 | if (!obj->parsed) |
brian m. carlson | f2fd076 | 2015-11-10 02:22:28 +0000 | [diff] [blame] | 250 | parse_object(obj->oid.hash); |
Daniel Barkalow | 77675e2 | 2005-09-05 02:03:51 -0400 | [diff] [blame] | 251 | } while (1); |
| 252 | } |