Merge branch 'jt/test-protocol-version' into jt/fetch-no-update-shallow-in-proto-v2
[git] / builtin / read-tree.c
1 /*
2  * GIT - The information manager from hell
3  *
4  * Copyright (C) Linus Torvalds, 2005
5  */
6
7 #define USE_THE_INDEX_COMPATIBILITY_MACROS
8 #include "cache.h"
9 #include "config.h"
10 #include "lockfile.h"
11 #include "object.h"
12 #include "tree.h"
13 #include "tree-walk.h"
14 #include "cache-tree.h"
15 #include "unpack-trees.h"
16 #include "dir.h"
17 #include "builtin.h"
18 #include "parse-options.h"
19 #include "resolve-undo.h"
20 #include "submodule.h"
21 #include "submodule-config.h"
22
23 static int nr_trees;
24 static int read_empty;
25 static struct tree *trees[MAX_UNPACK_TREES];
26
27 static int list_tree(struct object_id *oid)
28 {
29         struct tree *tree;
30
31         if (nr_trees >= MAX_UNPACK_TREES)
32                 die("I cannot read more than %d trees", MAX_UNPACK_TREES);
33         tree = parse_tree_indirect(oid);
34         if (!tree)
35                 return -1;
36         trees[nr_trees++] = tree;
37         return 0;
38 }
39
40 static const char * const read_tree_usage[] = {
41         N_("git read-tree [(-m [--trivial] [--aggressive] | --reset | --prefix=<prefix>) [-u [--exclude-per-directory=<gitignore>] | -i]] [--no-sparse-checkout] [--index-output=<file>] (--empty | <tree-ish1> [<tree-ish2> [<tree-ish3>]])"),
42         NULL
43 };
44
45 static int index_output_cb(const struct option *opt, const char *arg,
46                                  int unset)
47 {
48         BUG_ON_OPT_NEG(unset);
49         set_alternate_index_output(arg);
50         return 0;
51 }
52
53 static int exclude_per_directory_cb(const struct option *opt, const char *arg,
54                                     int unset)
55 {
56         struct dir_struct *dir;
57         struct unpack_trees_options *opts;
58
59         BUG_ON_OPT_NEG(unset);
60
61         opts = (struct unpack_trees_options *)opt->value;
62
63         if (opts->dir)
64                 die("more than one --exclude-per-directory given.");
65
66         dir = xcalloc(1, sizeof(*opts->dir));
67         dir->flags |= DIR_SHOW_IGNORED;
68         dir->exclude_per_dir = arg;
69         opts->dir = dir;
70         /* We do not need to nor want to do read-directory
71          * here; we are merely interested in reusing the
72          * per directory ignore stack mechanism.
73          */
74         return 0;
75 }
76
77 static void debug_stage(const char *label, const struct cache_entry *ce,
78                         struct unpack_trees_options *o)
79 {
80         printf("%s ", label);
81         if (!ce)
82                 printf("(missing)\n");
83         else if (ce == o->df_conflict_entry)
84                 printf("(conflict)\n");
85         else
86                 printf("%06o #%d %s %.8s\n",
87                        ce->ce_mode, ce_stage(ce), ce->name,
88                        oid_to_hex(&ce->oid));
89 }
90
91 static int debug_merge(const struct cache_entry * const *stages,
92                        struct unpack_trees_options *o)
93 {
94         int i;
95
96         printf("* %d-way merge\n", o->merge_size);
97         debug_stage("index", stages[0], o);
98         for (i = 1; i <= o->merge_size; i++) {
99                 char buf[24];
100                 xsnprintf(buf, sizeof(buf), "ent#%d", i);
101                 debug_stage(buf, stages[i], o);
102         }
103         return 0;
104 }
105
106 static int git_read_tree_config(const char *var, const char *value, void *cb)
107 {
108         if (!strcmp(var, "submodule.recurse"))
109                 return git_default_submodule_config(var, value, cb);
110
111         return git_default_config(var, value, cb);
112 }
113
114 int cmd_read_tree(int argc, const char **argv, const char *unused_prefix)
115 {
116         int i, stage = 0;
117         struct object_id oid;
118         struct tree_desc t[MAX_UNPACK_TREES];
119         struct unpack_trees_options opts;
120         int prefix_set = 0;
121         struct lock_file lock_file = LOCK_INIT;
122         const struct option read_tree_options[] = {
123                 { OPTION_CALLBACK, 0, "index-output", NULL, N_("file"),
124                   N_("write resulting index to <file>"),
125                   PARSE_OPT_NONEG, index_output_cb },
126                 OPT_BOOL(0, "empty", &read_empty,
127                             N_("only empty the index")),
128                 OPT__VERBOSE(&opts.verbose_update, N_("be verbose")),
129                 OPT_GROUP(N_("Merging")),
130                 OPT_BOOL('m', NULL, &opts.merge,
131                          N_("perform a merge in addition to a read")),
132                 OPT_BOOL(0, "trivial", &opts.trivial_merges_only,
133                          N_("3-way merge if no file level merging required")),
134                 OPT_BOOL(0, "aggressive", &opts.aggressive,
135                          N_("3-way merge in presence of adds and removes")),
136                 OPT_BOOL(0, "reset", &opts.reset,
137                          N_("same as -m, but discard unmerged entries")),
138                 { OPTION_STRING, 0, "prefix", &opts.prefix, N_("<subdirectory>/"),
139                   N_("read the tree into the index under <subdirectory>/"),
140                   PARSE_OPT_NONEG },
141                 OPT_BOOL('u', NULL, &opts.update,
142                          N_("update working tree with merge result")),
143                 { OPTION_CALLBACK, 0, "exclude-per-directory", &opts,
144                   N_("gitignore"),
145                   N_("allow explicitly ignored files to be overwritten"),
146                   PARSE_OPT_NONEG, exclude_per_directory_cb },
147                 OPT_BOOL('i', NULL, &opts.index_only,
148                          N_("don't check the working tree after merging")),
149                 OPT__DRY_RUN(&opts.dry_run, N_("don't update the index or the work tree")),
150                 OPT_BOOL(0, "no-sparse-checkout", &opts.skip_sparse_checkout,
151                          N_("skip applying sparse checkout filter")),
152                 OPT_BOOL(0, "debug-unpack", &opts.debug_unpack,
153                          N_("debug unpack-trees")),
154                 { OPTION_CALLBACK, 0, "recurse-submodules", NULL,
155                             "checkout", "control recursive updating of submodules",
156                             PARSE_OPT_OPTARG, option_parse_recurse_submodules_worktree_updater },
157                 OPT_END()
158         };
159
160         memset(&opts, 0, sizeof(opts));
161         opts.head_idx = -1;
162         opts.src_index = &the_index;
163         opts.dst_index = &the_index;
164
165         git_config(git_read_tree_config, NULL);
166
167         argc = parse_options(argc, argv, unused_prefix, read_tree_options,
168                              read_tree_usage, 0);
169
170         hold_locked_index(&lock_file, LOCK_DIE_ON_ERROR);
171
172         prefix_set = opts.prefix ? 1 : 0;
173         if (1 < opts.merge + opts.reset + prefix_set)
174                 die("Which one? -m, --reset, or --prefix?");
175
176         /*
177          * NEEDSWORK
178          *
179          * The old index should be read anyway even if we're going to
180          * destroy all index entries because we still need to preserve
181          * certain information such as index version or split-index
182          * mode.
183          */
184
185         if (opts.reset || opts.merge || opts.prefix) {
186                 if (read_cache_unmerged() && (opts.prefix || opts.merge))
187                         die("You need to resolve your current index first");
188                 stage = opts.merge = 1;
189         }
190         resolve_undo_clear();
191
192         for (i = 0; i < argc; i++) {
193                 const char *arg = argv[i];
194
195                 if (get_oid(arg, &oid))
196                         die("Not a valid object name %s", arg);
197                 if (list_tree(&oid) < 0)
198                         die("failed to unpack tree object %s", arg);
199                 stage++;
200         }
201         if (!nr_trees && !read_empty && !opts.merge)
202                 warning("read-tree: emptying the index with no arguments is deprecated; use --empty");
203         else if (nr_trees > 0 && read_empty)
204                 die("passing trees as arguments contradicts --empty");
205
206         if (1 < opts.index_only + opts.update)
207                 die("-u and -i at the same time makes no sense");
208         if ((opts.update || opts.index_only) && !opts.merge)
209                 die("%s is meaningless without -m, --reset, or --prefix",
210                     opts.update ? "-u" : "-i");
211         if ((opts.dir && !opts.update))
212                 die("--exclude-per-directory is meaningless unless -u");
213         if (opts.merge && !opts.index_only)
214                 setup_work_tree();
215
216         if (opts.merge) {
217                 switch (stage - 1) {
218                 case 0:
219                         die("you must specify at least one tree to merge");
220                         break;
221                 case 1:
222                         opts.fn = opts.prefix ? bind_merge : oneway_merge;
223                         break;
224                 case 2:
225                         opts.fn = twoway_merge;
226                         opts.initial_checkout = is_cache_unborn();
227                         break;
228                 case 3:
229                 default:
230                         opts.fn = threeway_merge;
231                         break;
232                 }
233
234                 if (stage - 1 >= 3)
235                         opts.head_idx = stage - 2;
236                 else
237                         opts.head_idx = 1;
238         }
239
240         if (opts.debug_unpack)
241                 opts.fn = debug_merge;
242
243         cache_tree_free(&active_cache_tree);
244         for (i = 0; i < nr_trees; i++) {
245                 struct tree *tree = trees[i];
246                 parse_tree(tree);
247                 init_tree_desc(t+i, tree->buffer, tree->size);
248         }
249         if (unpack_trees(nr_trees, t, &opts))
250                 return 128;
251
252         if (opts.debug_unpack || opts.dry_run)
253                 return 0; /* do not write the index out */
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          */
261         if (nr_trees == 1 && !opts.prefix)
262                 prime_cache_tree(the_repository,
263                                  the_repository->index,
264                                  trees[0]);
265
266         if (write_locked_index(&the_index, &lock_file, COMMIT_LOCK))
267                 die("unable to write new index file");
268         return 0;
269 }