blob: ca5e655d2f8b4243284eacf640230349e0d11d88 [file] [log] [blame]
Linus Torvalds8bc9a0c2005-04-07 15:16:10 -07001/*
2 * GIT - The information manager from hell
3 *
4 * Copyright (C) Linus Torvalds, 2005
5 */
Junio C Hamano4d3fe0c2005-09-06 12:53:56 -07006
Nguyễn Thái Ngọc Duyf8adbec2019-01-24 15:29:12 +07007#define USE_THE_INDEX_COMPATIBILITY_MACROS
Linus Torvaldse83c5162005-04-07 15:13:13 -07008#include "cache.h"
Brandon Williamsb2141fc2017-06-14 11:07:36 -07009#include "config.h"
Michael Haggerty697cc8e2014-10-01 12:28:42 +020010#include "lockfile.h"
Daniel Barkalowee6566e2005-09-05 02:04:48 -040011#include "object.h"
12#include "tree.h"
Linus Torvalds1ccf5a32006-05-29 12:18:00 -070013#include "tree-walk.h"
Junio C Hamanobad68ec2006-04-24 21:18:58 -070014#include "cache-tree.h"
Johannes Schindelin16da1342006-07-30 20:25:18 +020015#include "unpack-trees.h"
Junio C Hamanof8a9d422006-12-04 16:00:46 -080016#include "dir.h"
Peter Eriksend147e502006-05-23 14:15:32 +020017#include "builtin.h"
Stephen Boyd5a56da52009-06-25 22:14:10 -070018#include "parse-options.h"
Junio C Hamanocfc57892009-12-25 00:30:51 -080019#include "resolve-undo.h"
Stefan Beller25804912017-03-14 14:46:42 -070020#include "submodule.h"
21#include "submodule-config.h"
Daniel Barkalowee6566e2005-09-05 02:04:48 -040022
Linus Torvalds933bf402007-08-09 22:21:29 -070023static int nr_trees;
Jan Krügerfb1bb962010-09-10 15:28:59 +020024static int read_empty;
Junio C Hamanoca885a42008-03-13 22:07:18 -070025static struct tree *trees[MAX_UNPACK_TREES];
Daniel Barkalowee6566e2005-09-05 02:04:48 -040026
brian m. carlson4939e2c2017-05-06 22:10:30 +000027static int list_tree(struct object_id *oid)
Daniel Barkalowee6566e2005-09-05 02:04:48 -040028{
Linus Torvalds933bf402007-08-09 22:21:29 -070029 struct tree *tree;
30
Junio C Hamanoca885a42008-03-13 22:07:18 -070031 if (nr_trees >= MAX_UNPACK_TREES)
32 die("I cannot read more than %d trees", MAX_UNPACK_TREES);
brian m. carlsona9dbc172017-05-06 22:10:37 +000033 tree = parse_tree_indirect(oid);
Daniel Barkalowee6566e2005-09-05 02:04:48 -040034 if (!tree)
35 return -1;
Linus Torvalds933bf402007-08-09 22:21:29 -070036 trees[nr_trees++] = tree;
Daniel Barkalowee6566e2005-09-05 02:04:48 -040037 return 0;
38}
39
Stephen Boyd5a56da52009-06-25 22:14:10 -070040static const char * const read_tree_usage[] = {
Alex Henrie9476c2c2015-08-26 22:27:06 -060041 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>]])"),
Stephen Boyd5a56da52009-06-25 22:14:10 -070042 NULL
43};
44
45static int index_output_cb(const struct option *opt, const char *arg,
46 int unset)
47{
Jeff King517fe802018-11-05 01:45:42 -050048 BUG_ON_OPT_NEG(unset);
Stephen Boyd5a56da52009-06-25 22:14:10 -070049 set_alternate_index_output(arg);
50 return 0;
51}
52
53static 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
Jeff King517fe802018-11-05 01:45:42 -050059 BUG_ON_OPT_NEG(unset);
60
Stephen Boyd5a56da52009-06-25 22:14:10 -070061 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}
Junio C Hamanoc5bac172005-04-20 19:49:16 -070076
René Scharfeeb9ae4b2013-06-02 17:46:55 +020077static void debug_stage(const char *label, const struct cache_entry *ce,
Junio C Hamanoba655da2009-09-14 02:22:00 -070078 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,
brian m. carlson99d1a982016-09-05 20:07:52 +000088 oid_to_hex(&ce->oid));
Junio C Hamanoba655da2009-09-14 02:22:00 -070089}
90
René Scharfe5828e832013-06-02 17:46:56 +020091static int debug_merge(const struct cache_entry * const *stages,
92 struct unpack_trees_options *o)
Junio C Hamanoba655da2009-09-14 02:22:00 -070093{
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];
Jeff King5096d492015-09-24 17:06:08 -0400100 xsnprintf(buf, sizeof(buf), "ent#%d", i);
Junio C Hamanoba655da2009-09-14 02:22:00 -0700101 debug_stage(buf, stages[i], o);
102 }
103 return 0;
104}
105
Stefan Beller046b4822017-05-31 17:30:47 -0700106static int git_read_tree_config(const char *var, const char *value, void *cb)
Stefan Beller25804912017-03-14 14:46:42 -0700107{
Stefan Beller046b4822017-05-31 17:30:47 -0700108 if (!strcmp(var, "submodule.recurse"))
109 return git_default_submodule_config(var, value, cb);
Stefan Beller25804912017-03-14 14:46:42 -0700110
Stefan Beller046b4822017-05-31 17:30:47 -0700111 return git_default_config(var, value, cb);
Stefan Beller25804912017-03-14 14:46:42 -0700112}
113
Jeff King76a7bc02019-05-09 17:27:24 -0400114int cmd_read_tree(int argc, const char **argv, const char *cmd_prefix)
Linus Torvaldse83c5162005-04-07 15:13:13 -0700115{
Nguyễn Thái Ngọc Duy03b86642014-06-13 19:19:23 +0700116 int i, stage = 0;
brian m. carlson4939e2c2017-05-06 22:10:30 +0000117 struct object_id oid;
Junio C Hamanoca885a42008-03-13 22:07:18 -0700118 struct tree_desc t[MAX_UNPACK_TREES];
Johannes Schindelin16da1342006-07-30 20:25:18 +0200119 struct unpack_trees_options opts;
Stephen Boyd5a56da52009-06-25 22:14:10 -0700120 int prefix_set = 0;
Martin Ågren0fa5a2e2018-05-09 22:55:39 +0200121 struct lock_file lock_file = LOCK_INIT;
Stephen Boyd5a56da52009-06-25 22:14:10 -0700122 const struct option read_tree_options[] = {
Nguyễn Thái Ngọc Duy230c6cd2012-08-20 19:32:34 +0700123 { OPTION_CALLBACK, 0, "index-output", NULL, N_("file"),
124 N_("write resulting index to <file>"),
Stephen Boyd5a56da52009-06-25 22:14:10 -0700125 PARSE_OPT_NONEG, index_output_cb },
Stefan Beller84a7f092017-01-09 17:45:39 -0800126 OPT_BOOL(0, "empty", &read_empty,
127 N_("only empty the index")),
Nguyễn Thái Ngọc Duy230c6cd2012-08-20 19:32:34 +0700128 OPT__VERBOSE(&opts.verbose_update, N_("be verbose")),
129 OPT_GROUP(N_("Merging")),
Stefan Beller84a7f092017-01-09 17:45:39 -0800130 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")),
Nguyễn Thái Ngọc Duy230c6cd2012-08-20 19:32:34 +0700138 { OPTION_STRING, 0, "prefix", &opts.prefix, N_("<subdirectory>/"),
139 N_("read the tree into the index under <subdirectory>/"),
René Scharfe5f0df442018-08-02 21:18:14 +0200140 PARSE_OPT_NONEG },
Stefan Beller84a7f092017-01-09 17:45:39 -0800141 OPT_BOOL('u', NULL, &opts.update,
142 N_("update working tree with merge result")),
Stephen Boyd5a56da52009-06-25 22:14:10 -0700143 { OPTION_CALLBACK, 0, "exclude-per-directory", &opts,
Nguyễn Thái Ngọc Duy230c6cd2012-08-20 19:32:34 +0700144 N_("gitignore"),
145 N_("allow explicitly ignored files to be overwritten"),
Stephen Boyd5a56da52009-06-25 22:14:10 -0700146 PARSE_OPT_NONEG, exclude_per_directory_cb },
Stefan Beller84a7f092017-01-09 17:45:39 -0800147 OPT_BOOL('i', NULL, &opts.index_only,
148 N_("don't check the working tree after merging")),
Nguyễn Thái Ngọc Duy230c6cd2012-08-20 19:32:34 +0700149 OPT__DRY_RUN(&opts.dry_run, N_("don't update the index or the work tree")),
Stefan Beller84a7f092017-01-09 17:45:39 -0800150 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")),
Stefan Beller58b75bd2017-05-26 12:10:10 -0700154 { OPTION_CALLBACK, 0, "recurse-submodules", NULL,
Stefan Beller25804912017-03-14 14:46:42 -0700155 "checkout", "control recursive updating of submodules",
Stefan Bellerd7a38032017-05-26 12:10:12 -0700156 PARSE_OPT_OPTARG, option_parse_recurse_submodules_worktree_updater },
Nguyễn Thái Ngọc Duy3e414852019-03-22 16:31:37 +0700157 OPT__QUIET(&opts.quiet, N_("suppress feedback messages")),
Stephen Boyd5a56da52009-06-25 22:14:10 -0700158 OPT_END()
159 };
Linus Torvaldse83c5162005-04-07 15:13:13 -0700160
Johannes Schindelin16da1342006-07-30 20:25:18 +0200161 memset(&opts, 0, sizeof(opts));
162 opts.head_idx = -1;
Linus Torvalds34110cd2008-03-06 18:12:28 -0800163 opts.src_index = &the_index;
164 opts.dst_index = &the_index;
Shawn Pearce344c52a2006-07-08 14:34:02 -0400165
Stefan Beller046b4822017-05-31 17:30:47 -0700166 git_config(git_read_tree_config, NULL);
Junio C Hamano53228a52005-11-26 00:50:02 -0800167
Jeff King76a7bc02019-05-09 17:27:24 -0400168 argc = parse_options(argc, argv, cmd_prefix, read_tree_options,
Stephen Boyd5a56da52009-06-25 22:14:10 -0700169 read_tree_usage, 0);
170
Stefan Bellerd7a38032017-05-26 12:10:12 -0700171 hold_locked_index(&lock_file, LOCK_DIE_ON_ERROR);
Stefan Beller25804912017-03-14 14:46:42 -0700172
Stephen Boyd5a56da52009-06-25 22:14:10 -0700173 prefix_set = opts.prefix ? 1 : 0;
174 if (1 < opts.merge + opts.reset + prefix_set)
175 die("Which one? -m, --reset, or --prefix?");
Alexandre Julliarddb137fe2009-08-17 17:35:44 +0200176
Nguyễn Thái Ngọc Duy5a092ce2014-06-13 19:19:48 +0700177 /*
178 * NEEDSWORK
179 *
180 * The old index should be read anyway even if we're going to
181 * destroy all index entries because we still need to preserve
182 * certain information such as index version or split-index
183 * mode.
184 */
185
Alexandre Julliarddb137fe2009-08-17 17:35:44 +0200186 if (opts.reset || opts.merge || opts.prefix) {
187 if (read_cache_unmerged() && (opts.prefix || opts.merge))
188 die("You need to resolve your current index first");
189 stage = opts.merge = 1;
190 }
Junio C Hamanocfc57892009-12-25 00:30:51 -0800191 resolve_undo_clear();
Stephen Boyd5a56da52009-06-25 22:14:10 -0700192
193 for (i = 0; i < argc; i++) {
Linus Torvalds83adac32005-04-09 12:11:25 -0700194 const char *arg = argv[i];
195
brian m. carlson4939e2c2017-05-06 22:10:30 +0000196 if (get_oid(arg, &oid))
Dmitry V. Levin31fff302006-05-09 01:43:38 +0400197 die("Not a valid object name %s", arg);
brian m. carlson4939e2c2017-05-06 22:10:30 +0000198 if (list_tree(&oid) < 0)
Petr Baudis2de381f2005-04-13 02:28:48 -0700199 die("failed to unpack tree object %s", arg);
Linus Torvaldsd99082e2005-04-15 22:53:45 -0700200 stage++;
Linus Torvalds83adac32005-04-09 12:11:25 -0700201 }
Junio C Hamanob9b10d32017-05-10 21:31:54 -0700202 if (!nr_trees && !read_empty && !opts.merge)
Jan Krügerfb1bb962010-09-10 15:28:59 +0200203 warning("read-tree: emptying the index with no arguments is deprecated; use --empty");
204 else if (nr_trees > 0 && read_empty)
205 die("passing trees as arguments contradicts --empty");
206
Stephen Boyd5a56da52009-06-25 22:14:10 -0700207 if (1 < opts.index_only + opts.update)
208 die("-u and -i at the same time makes no sense");
Junio C Hamanoc01499e2013-10-16 10:26:39 -0700209 if ((opts.update || opts.index_only) && !opts.merge)
Stephen Boyda429d2d2009-06-25 22:14:09 -0700210 die("%s is meaningless without -m, --reset, or --prefix",
211 opts.update ? "-u" : "-i");
Junio C Hamanof8a9d422006-12-04 16:00:46 -0800212 if ((opts.dir && !opts.update))
213 die("--exclude-per-directory is meaningless unless -u");
Nguyễn Thái Ngọc Duyb6469a82008-08-28 20:03:22 +0700214 if (opts.merge && !opts.index_only)
215 setup_work_tree();
Junio C Hamano7dd43572005-10-02 00:50:16 -0700216
Johannes Schindelin16da1342006-07-30 20:25:18 +0200217 if (opts.merge) {
Daniel Barkalowee6566e2005-09-05 02:04:48 -0400218 switch (stage - 1) {
Jean-Noel Avila99322422017-05-11 14:06:33 +0200219 case 0:
220 die("you must specify at least one tree to merge");
221 break;
Daniel Barkalowee6566e2005-09-05 02:04:48 -0400222 case 1:
Johannes Schindelin16da1342006-07-30 20:25:18 +0200223 opts.fn = opts.prefix ? bind_merge : oneway_merge;
Daniel Barkalowee6566e2005-09-05 02:04:48 -0400224 break;
225 case 2:
Johannes Schindelin16da1342006-07-30 20:25:18 +0200226 opts.fn = twoway_merge;
Junio C Hamanofa7b3c22008-11-12 11:52:35 -0800227 opts.initial_checkout = is_cache_unborn();
Daniel Barkalowee6566e2005-09-05 02:04:48 -0400228 break;
229 case 3:
Daniel Barkalowee6566e2005-09-05 02:04:48 -0400230 default:
Johannes Schindelin16da1342006-07-30 20:25:18 +0200231 opts.fn = threeway_merge;
Daniel Barkalowee6566e2005-09-05 02:04:48 -0400232 break;
Junio C Hamano03efa6d2005-06-10 18:36:08 -0700233 }
Daniel Barkalowee6566e2005-09-05 02:04:48 -0400234
Daniel Barkalowee6566e2005-09-05 02:04:48 -0400235 if (stage - 1 >= 3)
Johannes Schindelin16da1342006-07-30 20:25:18 +0200236 opts.head_idx = stage - 2;
Daniel Barkalowee6566e2005-09-05 02:04:48 -0400237 else
Johannes Schindelin16da1342006-07-30 20:25:18 +0200238 opts.head_idx = 1;
Daniel Barkalowee6566e2005-09-05 02:04:48 -0400239 }
240
Junio C Hamanoba655da2009-09-14 02:22:00 -0700241 if (opts.debug_unpack)
242 opts.fn = debug_merge;
243
Junio C Hamano8cc21ce2009-04-20 03:58:17 -0700244 cache_tree_free(&active_cache_tree);
Linus Torvalds933bf402007-08-09 22:21:29 -0700245 for (i = 0; i < nr_trees; i++) {
246 struct tree *tree = trees[i];
247 parse_tree(tree);
248 init_tree_desc(t+i, tree->buffer, tree->size);
249 }
Daniel Barkalow203a2fe2008-02-07 11:39:48 -0500250 if (unpack_trees(nr_trees, t, &opts))
251 return 128;
Junio C Hamano7927a552006-04-27 01:33:07 -0700252
Jens Lehmannea5070c2011-05-25 22:10:41 +0200253 if (opts.debug_unpack || opts.dry_run)
Junio C Hamanoba655da2009-09-14 02:22:00 -0700254 return 0; /* do not write the index out */
255
Junio C Hamano7927a552006-04-27 01:33:07 -0700256 /*
257 * When reading only one tree (either the most basic form,
258 * "-m ent" or "--reset ent" form), we can obtain a fully
259 * valid cache-tree because the index must match exactly
260 * what came from the tree.
261 */
Junio C Hamano8cc21ce2009-04-20 03:58:17 -0700262 if (nr_trees == 1 && !opts.prefix)
Nguyễn Thái Ngọc Duyc207e9e2018-11-10 06:49:02 +0100263 prime_cache_tree(the_repository,
264 the_repository->index,
265 trees[0]);
Junio C Hamano7927a552006-04-27 01:33:07 -0700266
Nguyễn Thái Ngọc Duy03b86642014-06-13 19:19:23 +0700267 if (write_locked_index(&the_index, &lock_file, COMMIT_LOCK))
Petr Baudis2de381f2005-04-13 02:28:48 -0700268 die("unable to write new index file");
Linus Torvalds9614b8d2005-04-11 15:39:26 -0700269 return 0;
Linus Torvaldse83c5162005-04-07 15:13:13 -0700270}