The second batch
[alt-git.git] / builtin / read-tree.c
bloba8cf8504b8a368083d670ccd57bbdf51f84c6884
1 /*
2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
5 */
7 #include "builtin.h"
8 #include "config.h"
9 #include "gettext.h"
10 #include "hex.h"
11 #include "lockfile.h"
12 #include "object.h"
13 #include "object-name.h"
14 #include "tree.h"
15 #include "tree-walk.h"
16 #include "cache-tree.h"
17 #include "unpack-trees.h"
18 #include "parse-options.h"
19 #include "repository.h"
20 #include "resolve-undo.h"
21 #include "setup.h"
22 #include "sparse-index.h"
23 #include "submodule.h"
25 static int nr_trees;
26 static int read_empty;
27 static struct tree *trees[MAX_UNPACK_TREES];
29 static int list_tree(struct object_id *oid)
31 struct tree *tree;
33 if (nr_trees >= MAX_UNPACK_TREES)
34 die("I cannot read more than %d trees", MAX_UNPACK_TREES);
35 tree = parse_tree_indirect(oid);
36 if (!tree)
37 return -1;
38 trees[nr_trees++] = tree;
39 return 0;
42 static const char * const read_tree_usage[] = {
43 N_("git read-tree [(-m [--trivial] [--aggressive] | --reset | --prefix=<prefix>)\n"
44 " [-u | -i]] [--index-output=<file>] [--no-sparse-checkout]\n"
45 " (--empty | <tree-ish1> [<tree-ish2> [<tree-ish3>]])"),
46 NULL
49 static int index_output_cb(const struct option *opt UNUSED, const char *arg,
50 int unset)
52 BUG_ON_OPT_NEG(unset);
53 set_alternate_index_output(arg);
54 return 0;
57 static int exclude_per_directory_cb(const struct option *opt, const char *arg,
58 int unset)
60 struct unpack_trees_options *opts;
62 BUG_ON_OPT_NEG(unset);
64 opts = (struct unpack_trees_options *)opt->value;
66 if (!opts->update)
67 die("--exclude-per-directory is meaningless unless -u");
68 if (strcmp(arg, ".gitignore"))
69 die("--exclude-per-directory argument must be .gitignore");
70 return 0;
73 static void debug_stage(const char *label, const struct cache_entry *ce,
74 struct unpack_trees_options *o)
76 printf("%s ", label);
77 if (!ce)
78 printf("(missing)\n");
79 else if (ce == o->df_conflict_entry)
80 printf("(conflict)\n");
81 else
82 printf("%06o #%d %s %.8s\n",
83 ce->ce_mode, ce_stage(ce), ce->name,
84 oid_to_hex(&ce->oid));
87 static int debug_merge(const struct cache_entry * const *stages,
88 struct unpack_trees_options *o)
90 int i;
92 printf("* %d-way merge\n", o->internal.merge_size);
93 debug_stage("index", stages[0], o);
94 for (i = 1; i <= o->internal.merge_size; i++) {
95 char buf[24];
96 xsnprintf(buf, sizeof(buf), "ent#%d", i);
97 debug_stage(buf, stages[i], o);
99 return 0;
102 static int git_read_tree_config(const char *var, const char *value,
103 const struct config_context *ctx, void *cb)
105 if (!strcmp(var, "submodule.recurse"))
106 return git_default_submodule_config(var, value, cb);
108 return git_default_config(var, value, ctx, cb);
111 int cmd_read_tree(int argc, const char **argv, const char *cmd_prefix)
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 struct lock_file lock_file = LOCK_INIT;
119 const struct option read_tree_options[] = {
120 OPT__SUPER_PREFIX(&opts.super_prefix),
121 OPT_CALLBACK_F(0, "index-output", NULL, N_("file"),
122 N_("write resulting index to <file>"),
123 PARSE_OPT_NONEG, index_output_cb),
124 OPT_BOOL(0, "empty", &read_empty,
125 N_("only empty the index")),
126 OPT__VERBOSE(&opts.verbose_update, N_("be verbose")),
127 OPT_GROUP(N_("Merging")),
128 OPT_BOOL('m', NULL, &opts.merge,
129 N_("perform a merge in addition to a read")),
130 OPT_BOOL(0, "trivial", &opts.trivial_merges_only,
131 N_("3-way merge if no file level merging required")),
132 OPT_BOOL(0, "aggressive", &opts.aggressive,
133 N_("3-way merge in presence of adds and removes")),
134 OPT_BOOL(0, "reset", &opts.reset,
135 N_("same as -m, but discard unmerged entries")),
136 { OPTION_STRING, 0, "prefix", &opts.prefix, N_("<subdirectory>/"),
137 N_("read the tree into the index under <subdirectory>/"),
138 PARSE_OPT_NONEG },
139 OPT_BOOL('u', NULL, &opts.update,
140 N_("update working tree with merge result")),
141 OPT_CALLBACK_F(0, "exclude-per-directory", &opts,
142 N_("gitignore"),
143 N_("allow explicitly ignored files to be overwritten"),
144 PARSE_OPT_NONEG, exclude_per_directory_cb),
145 OPT_BOOL('i', NULL, &opts.index_only,
146 N_("don't check the working tree after merging")),
147 OPT__DRY_RUN(&opts.dry_run, N_("don't update the index or the work tree")),
148 OPT_BOOL(0, "no-sparse-checkout", &opts.skip_sparse_checkout,
149 N_("skip applying sparse checkout filter")),
150 OPT_BOOL(0, "debug-unpack", &opts.internal.debug_unpack,
151 N_("debug unpack-trees")),
152 OPT_CALLBACK_F(0, "recurse-submodules", NULL,
153 "checkout", "control recursive updating of submodules",
154 PARSE_OPT_OPTARG, option_parse_recurse_submodules_worktree_updater),
155 OPT__QUIET(&opts.quiet, N_("suppress feedback messages")),
156 OPT_END()
159 memset(&opts, 0, sizeof(opts));
160 opts.head_idx = -1;
161 opts.src_index = the_repository->index;
162 opts.dst_index = the_repository->index;
164 git_config(git_read_tree_config, NULL);
166 argc = parse_options(argc, argv, cmd_prefix, read_tree_options,
167 read_tree_usage, 0);
169 prefix_set = opts.prefix ? 1 : 0;
170 if (1 < opts.merge + opts.reset + prefix_set)
171 die("Which one? -m, --reset, or --prefix?");
173 /* Prefix should not start with a directory separator */
174 if (opts.prefix && opts.prefix[0] == '/')
175 die("Invalid prefix, prefix cannot start with '/'");
177 if (opts.reset)
178 opts.reset = UNPACK_RESET_OVERWRITE_UNTRACKED;
180 prepare_repo_settings(the_repository);
181 the_repository->settings.command_requires_full_index = 0;
183 repo_hold_locked_index(the_repository, &lock_file, LOCK_DIE_ON_ERROR);
186 * NEEDSWORK
188 * The old index should be read anyway even if we're going to
189 * destroy all index entries because we still need to preserve
190 * certain information such as index version or split-index
191 * mode.
194 if (opts.reset || opts.merge || opts.prefix) {
195 if (repo_read_index_unmerged(the_repository) && (opts.prefix || opts.merge))
196 die(_("You need to resolve your current index first"));
197 stage = opts.merge = 1;
199 resolve_undo_clear_index(the_repository->index);
201 for (i = 0; i < argc; i++) {
202 const char *arg = argv[i];
204 if (repo_get_oid(the_repository, arg, &oid))
205 die("Not a valid object name %s", arg);
206 if (list_tree(&oid) < 0)
207 die("failed to unpack tree object %s", arg);
208 stage++;
210 if (!nr_trees && !read_empty && !opts.merge)
211 warning("read-tree: emptying the index with no arguments is deprecated; use --empty");
212 else if (nr_trees > 0 && read_empty)
213 die("passing trees as arguments contradicts --empty");
215 if (1 < opts.index_only + opts.update)
216 die("-u and -i at the same time makes no sense");
217 if ((opts.update || opts.index_only) && !opts.merge)
218 die("%s is meaningless without -m, --reset, or --prefix",
219 opts.update ? "-u" : "-i");
220 if (opts.update && !opts.reset)
221 opts.preserve_ignored = 0;
222 /* otherwise, opts.preserve_ignored is irrelevant */
223 if (opts.merge && !opts.index_only)
224 setup_work_tree();
226 if (opts.skip_sparse_checkout)
227 ensure_full_index(the_repository->index);
229 if (opts.merge) {
230 switch (stage - 1) {
231 case 0:
232 die("you must specify at least one tree to merge");
233 break;
234 case 1:
235 opts.fn = opts.prefix ? bind_merge : oneway_merge;
236 break;
237 case 2:
238 opts.fn = twoway_merge;
239 opts.initial_checkout = is_index_unborn(the_repository->index);
240 break;
241 case 3:
242 default:
243 opts.fn = threeway_merge;
244 break;
247 if (stage - 1 >= 3)
248 opts.head_idx = stage - 2;
249 else
250 opts.head_idx = 1;
253 if (opts.internal.debug_unpack)
254 opts.fn = debug_merge;
256 /* If we're going to prime_cache_tree later, skip cache tree update */
257 if (nr_trees == 1 && !opts.prefix)
258 opts.skip_cache_tree_update = 1;
260 cache_tree_free(&the_repository->index->cache_tree);
261 for (i = 0; i < nr_trees; i++) {
262 struct tree *tree = trees[i];
263 if (parse_tree(tree) < 0)
264 return 128;
265 init_tree_desc(t+i, &tree->object.oid, tree->buffer, tree->size);
267 if (unpack_trees(nr_trees, t, &opts))
268 return 128;
270 if (opts.internal.debug_unpack || opts.dry_run)
271 return 0; /* do not write the index out */
274 * When reading only one tree (either the most basic form,
275 * "-m ent" or "--reset ent" form), we can obtain a fully
276 * valid cache-tree because the index must match exactly
277 * what came from the tree.
279 if (nr_trees == 1 && !opts.prefix)
280 prime_cache_tree(the_repository,
281 the_repository->index,
282 trees[0]);
284 if (write_locked_index(the_repository->index, &lock_file, COMMIT_LOCK))
285 die("unable to write new index file");
286 return 0;