Make 'unpack_trees()' have a separate source and destination index
[git/git.git] / builtin-read-tree.c
CommitLineData
8bc9a0c7
LT
1/*
2 * GIT - The information manager from hell
3 *
4 * Copyright (C) Linus Torvalds, 2005
5 */
4d3fe0c5 6
e83c5163 7#include "cache.h"
ee6566e8
DB
8#include "object.h"
9#include "tree.h"
1ccf5a34 10#include "tree-walk.h"
bad68ec9 11#include "cache-tree.h"
16da134b 12#include "unpack-trees.h"
f8a9d428 13#include "dir.h"
d147e501 14#include "builtin.h"
ee6566e8 15
a9ab2009 16#define MAX_TREES 8
933bf40a 17static int nr_trees;
f34f2b0b 18static struct tree *trees[MAX_TREES];
ee6566e8 19
ee6566e8
DB
20static int list_tree(unsigned char *sha1)
21{
933bf40a
LT
22 struct tree *tree;
23
a9ab2009
JH
24 if (nr_trees >= MAX_TREES)
25 die("I cannot read more than %d trees", MAX_TREES);
933bf40a 26 tree = parse_tree_indirect(sha1);
ee6566e8
DB
27 if (!tree)
28 return -1;
933bf40a 29 trees[nr_trees++] = tree;
ee6566e8
DB
30 return 0;
31}
32
438195cc
LT
33static int read_cache_unmerged(void)
34{
b0d6e646 35 int i;
438195cc 36 struct cache_entry **dst;
b0d6e646 37 struct cache_entry *last = NULL;
438195cc
LT
38
39 read_cache();
40 dst = active_cache;
438195cc
LT
41 for (i = 0; i < active_nr; i++) {
42 struct cache_entry *ce = active_cache[i];
43 if (ce_stage(ce)) {
d070e3a3 44 remove_index_entry(ce);
b0d6e646
JH
45 if (last && !strcmp(ce->name, last->name))
46 continue;
076b0adc 47 cache_tree_invalidate_path(active_cache_tree, ce->name);
b0d6e646 48 last = ce;
f5ed3b30 49 continue;
438195cc 50 }
b0d6e646 51 *dst++ = ce;
438195cc 52 }
b0d6e646
JH
53 active_nr = dst - active_cache;
54 return !!last;
438195cc
LT
55}
56
7927a55d
JH
57static void prime_cache_tree_rec(struct cache_tree *it, struct tree *tree)
58{
1ccf5a34 59 struct tree_desc desc;
4c068a98 60 struct name_entry entry;
7927a55d 61 int cnt;
283c8eef 62
e702496e 63 hashcpy(it->sha1, tree->object.sha1);
6fda5e51 64 init_tree_desc(&desc, tree->buffer, tree->size);
1ccf5a34 65 cnt = 0;
4c068a98
LT
66 while (tree_entry(&desc, &entry)) {
67 if (!S_ISDIR(entry.mode))
7927a55d
JH
68 cnt++;
69 else {
70 struct cache_tree_sub *sub;
4c068a98 71 struct tree *subtree = lookup_tree(entry.sha1);
7927a55d
JH
72 if (!subtree->object.parsed)
73 parse_tree(subtree);
4c068a98 74 sub = cache_tree_sub(it, entry.path);
7927a55d
JH
75 sub->cache_tree = cache_tree();
76 prime_cache_tree_rec(sub->cache_tree, subtree);
77 cnt += sub->cache_tree->entry_count;
78 }
79 }
80 it->entry_count = cnt;
81}
82
83static void prime_cache_tree(void)
84{
933bf40a 85 if (!nr_trees)
7927a55d
JH
86 return;
87 active_cache_tree = cache_tree();
933bf40a 88 prime_cache_tree_rec(active_cache_tree, trees[0]);
7927a55d
JH
89
90}
91
6da08783 92static const char read_tree_usage[] = "git-read-tree (<sha> | [[-m [--trivial] [--aggressive] | --reset | --prefix=<prefix>] [-u | -i]] [--exclude-per-directory=<gitignore>] [--index-output=<file>] <sha1> [<sha2> [<sha3>]])";
c5bac17a 93
021b6e45 94static struct lock_file lock_file;
96cd5429 95
a91af794 96int cmd_read_tree(int argc, const char **argv, const char *unused_prefix)
e83c5163 97{
6d6776cb 98 int i, newfd, stage = 0;
e83c5163 99 unsigned char sha1[20];
f34f2b0b 100 struct tree_desc t[MAX_TREES];
16da134b 101 struct unpack_trees_options opts;
bb233d69 102
16da134b
JS
103 memset(&opts, 0, sizeof(opts));
104 opts.head_idx = -1;
34110cd4
LT
105 opts.src_index = &the_index;
106 opts.dst_index = &the_index;
344c52ae 107
84a9b58c 108 git_config(git_default_config);
53228a5f 109
30ca07a2 110 newfd = hold_locked_index(&lock_file, 1);
83adac3c 111
39b4ac99
AR
112 git_config(git_default_config);
113
83adac3c
LT
114 for (i = 1; i < argc; i++) {
115 const char *arg = argv[i];
116
720d150c
JH
117 /* "-u" means "update", meaning that a merge will update
118 * the working tree.
119 */
220a0b52 120 if (!strcmp(arg, "-u")) {
16da134b 121 opts.update = 1;
220a0b52
LT
122 continue;
123 }
124
744633cb 125 if (!strcmp(arg, "-v")) {
16da134b 126 opts.verbose_update = 1;
744633cb
JH
127 continue;
128 }
129
720d150c
JH
130 /* "-i" means "index only", meaning that a merge will
131 * not even look at the working tree.
132 */
133 if (!strcmp(arg, "-i")) {
16da134b 134 opts.index_only = 1;
720d150c
JH
135 continue;
136 }
137
5e7f56ac
JH
138 if (!prefixcmp(arg, "--index-output=")) {
139 set_alternate_index_output(arg + 15);
140 continue;
141 }
142
f4c6f2d3
JH
143 /* "--prefix=<subdirectory>/" means keep the current index
144 * entries and put the entries from the tree under the
145 * given subdirectory.
146 */
cc44c765 147 if (!prefixcmp(arg, "--prefix=")) {
16da134b 148 if (stage || opts.merge || opts.prefix)
f4c6f2d3 149 usage(read_tree_usage);
16da134b
JS
150 opts.prefix = arg + 9;
151 opts.merge = 1;
f4c6f2d3
JH
152 stage = 1;
153 if (read_cache_unmerged())
154 die("you need to resolve your current index first");
155 continue;
156 }
157
b0d6e646
JH
158 /* This differs from "-m" in that we'll silently ignore
159 * unmerged entries and overwrite working tree files that
160 * correspond to them.
161 */
438195cc 162 if (!strcmp(arg, "--reset")) {
16da134b 163 if (stage || opts.merge || opts.prefix)
438195cc 164 usage(read_tree_usage);
16da134b
JS
165 opts.reset = 1;
166 opts.merge = 1;
438195cc
LT
167 stage = 1;
168 read_cache_unmerged();
7875b50d 169 continue;
438195cc
LT
170 }
171
23822a35 172 if (!strcmp(arg, "--trivial")) {
16da134b 173 opts.trivial_merges_only = 1;
23822a35
LT
174 continue;
175 }
176
1b1fdf8c 177 if (!strcmp(arg, "--aggressive")) {
16da134b 178 opts.aggressive = 1;
1b1fdf8c
JH
179 continue;
180 }
181
d99082e0 182 /* "-m" stands for "merge", meaning we start in stage 1 */
83adac3c 183 if (!strcmp(arg, "-m")) {
16da134b 184 if (stage || opts.merge || opts.prefix)
438195cc
LT
185 usage(read_tree_usage);
186 if (read_cache_unmerged())
187 die("you need to resolve your current index first");
d99082e0 188 stage = 1;
16da134b 189 opts.merge = 1;
83adac3c
LT
190 continue;
191 }
03efa6d9 192
cc44c765 193 if (!prefixcmp(arg, "--exclude-per-directory=")) {
f8a9d428
JH
194 struct dir_struct *dir;
195
196 if (opts.dir)
197 die("more than one --exclude-per-directory are given.");
198
aa4cfa85 199 dir = xcalloc(1, sizeof(*opts.dir));
f8a9d428
JH
200 dir->show_ignored = 1;
201 dir->exclude_per_dir = arg + 24;
202 opts.dir = dir;
203 /* We do not need to nor want to do read-directory
204 * here; we are merely interested in reusing the
205 * per directory ignore stack mechanism.
206 */
207 continue;
208 }
209
720d150c 210 /* using -u and -i at the same time makes no sense */
16da134b 211 if (1 < opts.index_only + opts.update)
720d150c
JH
212 usage(read_tree_usage);
213
31fff305
DL
214 if (get_sha1(arg, sha1))
215 die("Not a valid object name %s", arg);
ee6566e8 216 if (list_tree(sha1) < 0)
2de381f9 217 die("failed to unpack tree object %s", arg);
d99082e0 218 stage++;
83adac3c 219 }
16da134b 220 if ((opts.update||opts.index_only) && !opts.merge)
a57f0b58 221 usage(read_tree_usage);
f8a9d428
JH
222 if ((opts.dir && !opts.update))
223 die("--exclude-per-directory is meaningless unless -u");
7dd43575 224
16da134b 225 if (opts.merge) {
ee6566e8 226 if (stage < 2)
a3a65234 227 die("just how do you expect me to merge %d trees?", stage-1);
ee6566e8
DB
228 switch (stage - 1) {
229 case 1:
16da134b 230 opts.fn = opts.prefix ? bind_merge : oneway_merge;
ee6566e8
DB
231 break;
232 case 2:
16da134b 233 opts.fn = twoway_merge;
ee6566e8
DB
234 break;
235 case 3:
ee6566e8 236 default:
16da134b 237 opts.fn = threeway_merge;
b34c39cf 238 cache_tree_free(&active_cache_tree);
ee6566e8 239 break;
03efa6d9 240 }
ee6566e8 241
ee6566e8 242 if (stage - 1 >= 3)
16da134b 243 opts.head_idx = stage - 2;
ee6566e8 244 else
16da134b 245 opts.head_idx = 1;
ee6566e8
DB
246 }
247
933bf40a
LT
248 for (i = 0; i < nr_trees; i++) {
249 struct tree *tree = trees[i];
250 parse_tree(tree);
251 init_tree_desc(t+i, tree->buffer, tree->size);
252 }
203a2fe1
DB
253 if (unpack_trees(nr_trees, t, &opts))
254 return 128;
7927a55d
JH
255
256 /*
257 * When reading only one tree (either the most basic form,
258 * "-m ent" or "--reset ent" form), we can obtain a fully
259 * valid cache-tree because the index must match exactly
260 * what came from the tree.
261 */
933bf40a 262 if (nr_trees && !opts.prefix && (!opts.merge || (stage == 2))) {
b6c4a480 263 cache_tree_free(&active_cache_tree);
7927a55d
JH
264 prime_cache_tree();
265 }
266
96cd5429 267 if (write_cache(newfd, active_cache, active_nr) ||
4ed7cd3a 268 commit_locked_index(&lock_file))
2de381f9 269 die("unable to write new index file");
9614b8dc 270 return 0;
e83c5163 271}