remove unnecessary initializations
[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"
d147e501 13#include "builtin.h"
ee6566e8 14
96f1e58f 15static struct object_list *trees;
ee6566e8 16
ee6566e8
DB
17static int list_tree(unsigned char *sha1)
18{
19 struct tree *tree = parse_tree_indirect(sha1);
20 if (!tree)
21 return -1;
22 object_list_append(&tree->object, &trees);
23 return 0;
24}
25
438195cc
LT
26static int read_cache_unmerged(void)
27{
b0d6e646 28 int i;
438195cc 29 struct cache_entry **dst;
b0d6e646 30 struct cache_entry *last = NULL;
438195cc
LT
31
32 read_cache();
33 dst = active_cache;
438195cc
LT
34 for (i = 0; i < active_nr; i++) {
35 struct cache_entry *ce = active_cache[i];
36 if (ce_stage(ce)) {
b0d6e646
JH
37 if (last && !strcmp(ce->name, last->name))
38 continue;
076b0adc 39 cache_tree_invalidate_path(active_cache_tree, ce->name);
b0d6e646
JH
40 last = ce;
41 ce->ce_mode = 0;
42 ce->ce_flags &= ~htons(CE_STAGEMASK);
438195cc 43 }
b0d6e646 44 *dst++ = ce;
438195cc 45 }
b0d6e646
JH
46 active_nr = dst - active_cache;
47 return !!last;
438195cc
LT
48}
49
7927a55d
JH
50static void prime_cache_tree_rec(struct cache_tree *it, struct tree *tree)
51{
1ccf5a34 52 struct tree_desc desc;
4c068a98 53 struct name_entry entry;
7927a55d 54 int cnt;
283c8eef 55
7927a55d 56 memcpy(it->sha1, tree->object.sha1, 20);
1ccf5a34
LT
57 desc.buf = tree->buffer;
58 desc.size = tree->size;
59 cnt = 0;
4c068a98
LT
60 while (tree_entry(&desc, &entry)) {
61 if (!S_ISDIR(entry.mode))
7927a55d
JH
62 cnt++;
63 else {
64 struct cache_tree_sub *sub;
4c068a98 65 struct tree *subtree = lookup_tree(entry.sha1);
7927a55d
JH
66 if (!subtree->object.parsed)
67 parse_tree(subtree);
4c068a98 68 sub = cache_tree_sub(it, entry.path);
7927a55d
JH
69 sub->cache_tree = cache_tree();
70 prime_cache_tree_rec(sub->cache_tree, subtree);
71 cnt += sub->cache_tree->entry_count;
72 }
73 }
74 it->entry_count = cnt;
75}
76
77static void prime_cache_tree(void)
78{
79 struct tree *tree = (struct tree *)trees->item;
80 if (!tree)
81 return;
82 active_cache_tree = cache_tree();
83 prime_cache_tree_rec(active_cache_tree, tree);
84
85}
86
f4c6f2d3 87static const char read_tree_usage[] = "git-read-tree (<sha> | [[-m [--aggressive] | --reset | --prefix=<prefix>] [-u | -i]] <sha1> [<sha2> [<sha3>]])";
c5bac17a 88
021b6e45 89static struct lock_file lock_file;
96cd5429 90
a91af794 91int cmd_read_tree(int argc, const char **argv, const char *unused_prefix)
e83c5163 92{
6d6776cb 93 int i, newfd, stage = 0;
e83c5163 94 unsigned char sha1[20];
16da134b 95 struct unpack_trees_options opts;
bb233d69 96
16da134b
JS
97 memset(&opts, 0, sizeof(opts));
98 opts.head_idx = -1;
344c52ae 99
16da134b 100 setup_git_directory();
84a9b58c 101 git_config(git_default_config);
53228a5f 102
40aaae88 103 newfd = hold_lock_file_for_update(&lock_file, get_index_file(), 1);
83adac3c 104
39b4ac99
AR
105 git_config(git_default_config);
106
83adac3c
LT
107 for (i = 1; i < argc; i++) {
108 const char *arg = argv[i];
109
720d150c
JH
110 /* "-u" means "update", meaning that a merge will update
111 * the working tree.
112 */
220a0b52 113 if (!strcmp(arg, "-u")) {
16da134b 114 opts.update = 1;
220a0b52
LT
115 continue;
116 }
117
744633cb 118 if (!strcmp(arg, "-v")) {
16da134b 119 opts.verbose_update = 1;
744633cb
JH
120 continue;
121 }
122
720d150c
JH
123 /* "-i" means "index only", meaning that a merge will
124 * not even look at the working tree.
125 */
126 if (!strcmp(arg, "-i")) {
16da134b 127 opts.index_only = 1;
720d150c
JH
128 continue;
129 }
130
f4c6f2d3
JH
131 /* "--prefix=<subdirectory>/" means keep the current index
132 * entries and put the entries from the tree under the
133 * given subdirectory.
134 */
135 if (!strncmp(arg, "--prefix=", 9)) {
16da134b 136 if (stage || opts.merge || opts.prefix)
f4c6f2d3 137 usage(read_tree_usage);
16da134b
JS
138 opts.prefix = arg + 9;
139 opts.merge = 1;
f4c6f2d3
JH
140 stage = 1;
141 if (read_cache_unmerged())
142 die("you need to resolve your current index first");
143 continue;
144 }
145
b0d6e646
JH
146 /* This differs from "-m" in that we'll silently ignore
147 * unmerged entries and overwrite working tree files that
148 * correspond to them.
149 */
438195cc 150 if (!strcmp(arg, "--reset")) {
16da134b 151 if (stage || opts.merge || opts.prefix)
438195cc 152 usage(read_tree_usage);
16da134b
JS
153 opts.reset = 1;
154 opts.merge = 1;
438195cc
LT
155 stage = 1;
156 read_cache_unmerged();
7875b50d 157 continue;
438195cc
LT
158 }
159
23822a35 160 if (!strcmp(arg, "--trivial")) {
16da134b 161 opts.trivial_merges_only = 1;
23822a35
LT
162 continue;
163 }
164
1b1fdf8c 165 if (!strcmp(arg, "--aggressive")) {
16da134b 166 opts.aggressive = 1;
1b1fdf8c
JH
167 continue;
168 }
169
d99082e0 170 /* "-m" stands for "merge", meaning we start in stage 1 */
83adac3c 171 if (!strcmp(arg, "-m")) {
16da134b 172 if (stage || opts.merge || opts.prefix)
438195cc
LT
173 usage(read_tree_usage);
174 if (read_cache_unmerged())
175 die("you need to resolve your current index first");
d99082e0 176 stage = 1;
16da134b 177 opts.merge = 1;
83adac3c
LT
178 continue;
179 }
03efa6d9 180
720d150c 181 /* using -u and -i at the same time makes no sense */
16da134b 182 if (1 < opts.index_only + opts.update)
720d150c
JH
183 usage(read_tree_usage);
184
31fff305
DL
185 if (get_sha1(arg, sha1))
186 die("Not a valid object name %s", arg);
ee6566e8 187 if (list_tree(sha1) < 0)
2de381f9 188 die("failed to unpack tree object %s", arg);
d99082e0 189 stage++;
83adac3c 190 }
16da134b 191 if ((opts.update||opts.index_only) && !opts.merge)
a57f0b58 192 usage(read_tree_usage);
7dd43575 193
16da134b
JS
194 if (opts.prefix) {
195 int pfxlen = strlen(opts.prefix);
f4c6f2d3 196 int pos;
16da134b 197 if (opts.prefix[pfxlen-1] != '/')
f4c6f2d3
JH
198 die("prefix must end with /");
199 if (stage != 2)
200 die("binding merge takes only one tree");
16da134b 201 pos = cache_name_pos(opts.prefix, pfxlen);
f4c6f2d3
JH
202 if (0 <= pos)
203 die("corrupt index file");
204 pos = -pos-1;
205 if (pos < active_nr &&
16da134b
JS
206 !strncmp(active_cache[pos]->name, opts.prefix, pfxlen))
207 die("subdirectory '%s' already exists.", opts.prefix);
208 pos = cache_name_pos(opts.prefix, pfxlen-1);
f4c6f2d3 209 if (0 <= pos)
16da134b
JS
210 die("file '%.*s' already exists.",
211 pfxlen-1, opts.prefix);
f4c6f2d3
JH
212 }
213
16da134b 214 if (opts.merge) {
ee6566e8 215 if (stage < 2)
a3a65234 216 die("just how do you expect me to merge %d trees?", stage-1);
ee6566e8
DB
217 switch (stage - 1) {
218 case 1:
16da134b 219 opts.fn = opts.prefix ? bind_merge : oneway_merge;
ee6566e8
DB
220 break;
221 case 2:
16da134b 222 opts.fn = twoway_merge;
ee6566e8
DB
223 break;
224 case 3:
ee6566e8 225 default:
16da134b 226 opts.fn = threeway_merge;
b34c39cf 227 cache_tree_free(&active_cache_tree);
ee6566e8 228 break;
03efa6d9 229 }
ee6566e8 230
ee6566e8 231 if (stage - 1 >= 3)
16da134b 232 opts.head_idx = stage - 2;
ee6566e8 233 else
16da134b 234 opts.head_idx = 1;
ee6566e8
DB
235 }
236
16da134b 237 unpack_trees(trees, &opts);
7927a55d
JH
238
239 /*
240 * When reading only one tree (either the most basic form,
241 * "-m ent" or "--reset ent" form), we can obtain a fully
242 * valid cache-tree because the index must match exactly
243 * what came from the tree.
244 */
16da134b 245 if (trees && trees->item && !opts.prefix && (!opts.merge || (stage == 2))) {
b6c4a480 246 cache_tree_free(&active_cache_tree);
7927a55d
JH
247 prime_cache_tree();
248 }
249
96cd5429 250 if (write_cache(newfd, active_cache, active_nr) ||
6244b249 251 close(newfd) || commit_lock_file(&lock_file))
2de381f9 252 die("unable to write new index file");
9614b8dc 253 return 0;
e83c5163 254}