git-completion: add "git stash"
[git] / builtin-read-tree.c
1 /*
2  * GIT - The information manager from hell
3  *
4  * Copyright (C) Linus Torvalds, 2005
5  */
6
7 #include "cache.h"
8 #include "object.h"
9 #include "tree.h"
10 #include "tree-walk.h"
11 #include "cache-tree.h"
12 #include "unpack-trees.h"
13 #include "dir.h"
14 #include "builtin.h"
15
16 static struct object_list *trees;
17
18 static int list_tree(unsigned char *sha1)
19 {
20         struct tree *tree = parse_tree_indirect(sha1);
21         if (!tree)
22                 return -1;
23         object_list_append(&tree->object, &trees);
24         return 0;
25 }
26
27 static int read_cache_unmerged(void)
28 {
29         int i;
30         struct cache_entry **dst;
31         struct cache_entry *last = NULL;
32
33         read_cache();
34         dst = active_cache;
35         for (i = 0; i < active_nr; i++) {
36                 struct cache_entry *ce = active_cache[i];
37                 if (ce_stage(ce)) {
38                         if (last && !strcmp(ce->name, last->name))
39                                 continue;
40                         cache_tree_invalidate_path(active_cache_tree, ce->name);
41                         last = ce;
42                         ce->ce_mode = 0;
43                         ce->ce_flags &= ~htons(CE_STAGEMASK);
44                 }
45                 *dst++ = ce;
46         }
47         active_nr = dst - active_cache;
48         return !!last;
49 }
50
51 static void prime_cache_tree_rec(struct cache_tree *it, struct tree *tree)
52 {
53         struct tree_desc desc;
54         struct name_entry entry;
55         int cnt;
56
57         hashcpy(it->sha1, tree->object.sha1);
58         init_tree_desc(&desc, tree->buffer, tree->size);
59         cnt = 0;
60         while (tree_entry(&desc, &entry)) {
61                 if (!S_ISDIR(entry.mode))
62                         cnt++;
63                 else {
64                         struct cache_tree_sub *sub;
65                         struct tree *subtree = lookup_tree(entry.sha1);
66                         if (!subtree->object.parsed)
67                                 parse_tree(subtree);
68                         sub = cache_tree_sub(it, entry.path);
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
77 static 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
87 static 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>]])";
88
89 static struct lock_file lock_file;
90
91 int cmd_read_tree(int argc, const char **argv, const char *unused_prefix)
92 {
93         int i, newfd, stage = 0;
94         unsigned char sha1[20];
95         struct unpack_trees_options opts;
96
97         memset(&opts, 0, sizeof(opts));
98         opts.head_idx = -1;
99
100         git_config(git_default_config);
101
102         newfd = hold_locked_index(&lock_file, 1);
103
104         git_config(git_default_config);
105
106         for (i = 1; i < argc; i++) {
107                 const char *arg = argv[i];
108
109                 /* "-u" means "update", meaning that a merge will update
110                  * the working tree.
111                  */
112                 if (!strcmp(arg, "-u")) {
113                         opts.update = 1;
114                         continue;
115                 }
116
117                 if (!strcmp(arg, "-v")) {
118                         opts.verbose_update = 1;
119                         continue;
120                 }
121
122                 /* "-i" means "index only", meaning that a merge will
123                  * not even look at the working tree.
124                  */
125                 if (!strcmp(arg, "-i")) {
126                         opts.index_only = 1;
127                         continue;
128                 }
129
130                 if (!prefixcmp(arg, "--index-output=")) {
131                         set_alternate_index_output(arg + 15);
132                         continue;
133                 }
134
135                 /* "--prefix=<subdirectory>/" means keep the current index
136                  *  entries and put the entries from the tree under the
137                  * given subdirectory.
138                  */
139                 if (!prefixcmp(arg, "--prefix=")) {
140                         if (stage || opts.merge || opts.prefix)
141                                 usage(read_tree_usage);
142                         opts.prefix = arg + 9;
143                         opts.merge = 1;
144                         stage = 1;
145                         if (read_cache_unmerged())
146                                 die("you need to resolve your current index first");
147                         continue;
148                 }
149
150                 /* This differs from "-m" in that we'll silently ignore
151                  * unmerged entries and overwrite working tree files that
152                  * correspond to them.
153                  */
154                 if (!strcmp(arg, "--reset")) {
155                         if (stage || opts.merge || opts.prefix)
156                                 usage(read_tree_usage);
157                         opts.reset = 1;
158                         opts.merge = 1;
159                         stage = 1;
160                         read_cache_unmerged();
161                         continue;
162                 }
163
164                 if (!strcmp(arg, "--trivial")) {
165                         opts.trivial_merges_only = 1;
166                         continue;
167                 }
168
169                 if (!strcmp(arg, "--aggressive")) {
170                         opts.aggressive = 1;
171                         continue;
172                 }
173
174                 /* "-m" stands for "merge", meaning we start in stage 1 */
175                 if (!strcmp(arg, "-m")) {
176                         if (stage || opts.merge || opts.prefix)
177                                 usage(read_tree_usage);
178                         if (read_cache_unmerged())
179                                 die("you need to resolve your current index first");
180                         stage = 1;
181                         opts.merge = 1;
182                         continue;
183                 }
184
185                 if (!prefixcmp(arg, "--exclude-per-directory=")) {
186                         struct dir_struct *dir;
187
188                         if (opts.dir)
189                                 die("more than one --exclude-per-directory are given.");
190
191                         dir = xcalloc(1, sizeof(*opts.dir));
192                         dir->show_ignored = 1;
193                         dir->exclude_per_dir = arg + 24;
194                         opts.dir = dir;
195                         /* We do not need to nor want to do read-directory
196                          * here; we are merely interested in reusing the
197                          * per directory ignore stack mechanism.
198                          */
199                         continue;
200                 }
201
202                 /* using -u and -i at the same time makes no sense */
203                 if (1 < opts.index_only + opts.update)
204                         usage(read_tree_usage);
205
206                 if (get_sha1(arg, sha1))
207                         die("Not a valid object name %s", arg);
208                 if (list_tree(sha1) < 0)
209                         die("failed to unpack tree object %s", arg);
210                 stage++;
211         }
212         if ((opts.update||opts.index_only) && !opts.merge)
213                 usage(read_tree_usage);
214         if ((opts.dir && !opts.update))
215                 die("--exclude-per-directory is meaningless unless -u");
216
217         if (opts.prefix) {
218                 int pfxlen = strlen(opts.prefix);
219                 int pos;
220                 if (opts.prefix[pfxlen-1] != '/')
221                         die("prefix must end with /");
222                 if (stage != 2)
223                         die("binding merge takes only one tree");
224                 pos = cache_name_pos(opts.prefix, pfxlen);
225                 if (0 <= pos)
226                         die("corrupt index file");
227                 pos = -pos-1;
228                 if (pos < active_nr &&
229                     !strncmp(active_cache[pos]->name, opts.prefix, pfxlen))
230                         die("subdirectory '%s' already exists.", opts.prefix);
231                 pos = cache_name_pos(opts.prefix, pfxlen-1);
232                 if (0 <= pos)
233                         die("file '%.*s' already exists.",
234                                         pfxlen-1, opts.prefix);
235                 opts.pos = -1 - pos;
236         }
237
238         if (opts.merge) {
239                 if (stage < 2)
240                         die("just how do you expect me to merge %d trees?", stage-1);
241                 switch (stage - 1) {
242                 case 1:
243                         opts.fn = opts.prefix ? bind_merge : oneway_merge;
244                         break;
245                 case 2:
246                         opts.fn = twoway_merge;
247                         break;
248                 case 3:
249                 default:
250                         opts.fn = threeway_merge;
251                         cache_tree_free(&active_cache_tree);
252                         break;
253                 }
254
255                 if (stage - 1 >= 3)
256                         opts.head_idx = stage - 2;
257                 else
258                         opts.head_idx = 1;
259         }
260
261         unpack_trees(trees, &opts);
262
263         /*
264          * When reading only one tree (either the most basic form,
265          * "-m ent" or "--reset ent" form), we can obtain a fully
266          * valid cache-tree because the index must match exactly
267          * what came from the tree.
268          */
269         if (trees && trees->item && !opts.prefix && (!opts.merge || (stage == 2))) {
270                 cache_tree_free(&active_cache_tree);
271                 prime_cache_tree();
272         }
273
274         if (write_cache(newfd, active_cache, active_nr) ||
275             close(newfd) || commit_locked_index(&lock_file))
276                 die("unable to write new index file");
277         return 0;
278 }