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