@matheusbsbs / matheus-bagliano / commits / 8cc21ce78c
Fork 0

read-tree A B: do not corrupt cache-tree

An earlier commit aab3b9a (read-tree A B C: do not create a bogus index and do not segfault, 2009-03-12) resurrected the support for an obscure (but useful) feature to read and overlay more than one tree into the index without the -m (merge) option. But the fix was not enough. Exercising this feature exposes a longstanding bug in the code that primes the cache-tree in the index from the tree that was read. The intention was that when we know that the index must exactly match the tree we just read, we prime the entire cache-tree with it. However, the logic to detect that case incorrectly triggered if you read two trees without -m. This resulted in a corrupted cache-tree, and write-tree would have produced an incorrect tree object out of such an index. Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed Apr 20, 2009 at 03:58 UTC 8cc21ce78c2f3781024117047c0650861f890213
1 file changed +2 -4
builtin-read-tree.c
+2 -4
@@ -211,7 +211,6 @@ int cmd_read_tree(int argc, const char **argv, const char *unused_prefix)
211 case 3:
212 default:
213 opts.fn = threeway_merge;
214 - cache_tree_free(&active_cache_tree);
214 break;
215 }
216
@@ -221,6 +220,7 @@ int cmd_read_tree(int argc, const char **argv, const char *unused_prefix)
220 opts.head_idx = 1;
221 }
222
223 + cache_tree_free(&active_cache_tree);
224 for (i = 0; i < nr_trees; i++) {
225 struct tree *tree = trees[i];
226 parse_tree(tree);
@@ -235,10 +235,8 @@ int cmd_read_tree(int argc, const char **argv, const char *unused_prefix)
235 * valid cache-tree because the index must match exactly
236 * what came from the tree.
237 */
238 - if (nr_trees && !opts.prefix && (!opts.merge || (stage == 2))) {
239 - cache_tree_free(&active_cache_tree);
238 + if (nr_trees == 1 && !opts.prefix)
239 prime_cache_tree();
241 - }
240
241 if (write_cache(newfd, active_cache, active_nr) ||
242 commit_locked_index(&lock_file))