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