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