http.c::http_fetch_ref(): use the new http API
[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 int nr_trees;
17 static struct tree *trees[MAX_UNPACK_TREES];
18
19 static int list_tree(unsigned char *sha1)
20 {
21         struct tree *tree;
22
23         if (nr_trees >= MAX_UNPACK_TREES)
24                 die("I cannot read more than %d trees", MAX_UNPACK_TREES);
25         tree = parse_tree_indirect(sha1);
26         if (!tree)
27                 return -1;
28         trees[nr_trees++] = tree;
29         return 0;
30 }
31
32 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>]])";
33
34 static struct lock_file lock_file;
35
36 int cmd_read_tree(int argc, const char **argv, const char *unused_prefix)
37 {
38         int i, newfd, stage = 0;
39         unsigned char sha1[20];
40         struct tree_desc t[MAX_UNPACK_TREES];
41         struct unpack_trees_options opts;
42
43         memset(&opts, 0, sizeof(opts));
44         opts.head_idx = -1;
45         opts.src_index = &the_index;
46         opts.dst_index = &the_index;
47
48         git_config(git_default_config, NULL);
49
50         newfd = hold_locked_index(&lock_file, 1);
51
52         for (i = 1; i < argc; i++) {
53                 const char *arg = argv[i];
54
55                 /* "-u" means "update", meaning that a merge will update
56                  * the working tree.
57                  */
58                 if (!strcmp(arg, "-u")) {
59                         opts.update = 1;
60                         continue;
61                 }
62
63                 if (!strcmp(arg, "-v")) {
64                         opts.verbose_update = 1;
65                         continue;
66                 }
67
68                 /* "-i" means "index only", meaning that a merge will
69                  * not even look at the working tree.
70                  */
71                 if (!strcmp(arg, "-i")) {
72                         opts.index_only = 1;
73                         continue;
74                 }
75
76                 if (!prefixcmp(arg, "--index-output=")) {
77                         set_alternate_index_output(arg + 15);
78                         continue;
79                 }
80
81                 /* "--prefix=<subdirectory>/" means keep the current index
82                  *  entries and put the entries from the tree under the
83                  * given subdirectory.
84                  */
85                 if (!prefixcmp(arg, "--prefix=")) {
86                         if (stage || opts.merge || opts.prefix)
87                                 usage(read_tree_usage);
88                         opts.prefix = arg + 9;
89                         opts.merge = 1;
90                         stage = 1;
91                         if (read_cache_unmerged())
92                                 die("you need to resolve your current index first");
93                         continue;
94                 }
95
96                 /* This differs from "-m" in that we'll silently ignore
97                  * unmerged entries and overwrite working tree files that
98                  * correspond to them.
99                  */
100                 if (!strcmp(arg, "--reset")) {
101                         if (stage || opts.merge || opts.prefix)
102                                 usage(read_tree_usage);
103                         opts.reset = 1;
104                         opts.merge = 1;
105                         stage = 1;
106                         read_cache_unmerged();
107                         continue;
108                 }
109
110                 if (!strcmp(arg, "--trivial")) {
111                         opts.trivial_merges_only = 1;
112                         continue;
113                 }
114
115                 if (!strcmp(arg, "--aggressive")) {
116                         opts.aggressive = 1;
117                         continue;
118                 }
119
120                 /* "-m" stands for "merge", meaning we start in stage 1 */
121                 if (!strcmp(arg, "-m")) {
122                         if (stage || opts.merge || opts.prefix)
123                                 usage(read_tree_usage);
124                         if (read_cache_unmerged())
125                                 die("you need to resolve your current index first");
126                         stage = 1;
127                         opts.merge = 1;
128                         continue;
129                 }
130
131                 if (!prefixcmp(arg, "--exclude-per-directory=")) {
132                         struct dir_struct *dir;
133
134                         if (opts.dir)
135                                 die("more than one --exclude-per-directory are given.");
136
137                         dir = xcalloc(1, sizeof(*opts.dir));
138                         dir->flags |= DIR_SHOW_IGNORED;
139                         dir->exclude_per_dir = arg + 24;
140                         opts.dir = dir;
141                         /* We do not need to nor want to do read-directory
142                          * here; we are merely interested in reusing the
143                          * per directory ignore stack mechanism.
144                          */
145                         continue;
146                 }
147
148                 /* using -u and -i at the same time makes no sense */
149                 if (1 < opts.index_only + opts.update)
150                         usage(read_tree_usage);
151
152                 if (get_sha1(arg, sha1))
153                         die("Not a valid object name %s", arg);
154                 if (list_tree(sha1) < 0)
155                         die("failed to unpack tree object %s", arg);
156                 stage++;
157         }
158         if ((opts.update||opts.index_only) && !opts.merge)
159                 usage(read_tree_usage);
160         if ((opts.dir && !opts.update))
161                 die("--exclude-per-directory is meaningless unless -u");
162         if (opts.merge && !opts.index_only)
163                 setup_work_tree();
164
165         if (opts.merge) {
166                 if (stage < 2)
167                         die("just how do you expect me to merge %d trees?", stage-1);
168                 switch (stage - 1) {
169                 case 1:
170                         opts.fn = opts.prefix ? bind_merge : oneway_merge;
171                         break;
172                 case 2:
173                         opts.fn = twoway_merge;
174                         opts.initial_checkout = is_cache_unborn();
175                         break;
176                 case 3:
177                 default:
178                         opts.fn = threeway_merge;
179                         break;
180                 }
181
182                 if (stage - 1 >= 3)
183                         opts.head_idx = stage - 2;
184                 else
185                         opts.head_idx = 1;
186         }
187
188         cache_tree_free(&active_cache_tree);
189         for (i = 0; i < nr_trees; i++) {
190                 struct tree *tree = trees[i];
191                 parse_tree(tree);
192                 init_tree_desc(t+i, tree->buffer, tree->size);
193         }
194         if (unpack_trees(nr_trees, t, &opts))
195                 return 128;
196
197         /*
198          * When reading only one tree (either the most basic form,
199          * "-m ent" or "--reset ent" form), we can obtain a fully
200          * valid cache-tree because the index must match exactly
201          * what came from the tree.
202          *
203          * The same holds true if we are switching between two trees
204          * using read-tree -m A B.  The index must match B after that.
205          */
206         if (nr_trees == 1 && !opts.prefix)
207                 prime_cache_tree(&active_cache_tree, trees[0]);
208         else if (nr_trees == 2 && opts.merge)
209                 prime_cache_tree(&active_cache_tree, trees[1]);
210
211         if (write_cache(newfd, active_cache, active_nr) ||
212             commit_locked_index(&lock_file))
213                 die("unable to write new index file");
214         return 0;
215 }