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