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