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