Merge branch 'gj/push-more-verbose-advice'
[git/mingw.git] / builtin / read-tree.c
blob0d7ef847a70581214fb88c3a981581670f7decbe
1 /*
2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
5 */
7 #include "cache.h"
8 #include "object.h"
9 #include "tree.h"
10 #include "tree-walk.h"
11 #include "cache-tree.h"
12 #include "unpack-trees.h"
13 #include "dir.h"
14 #include "builtin.h"
15 #include "parse-options.h"
16 #include "resolve-undo.h"
18 static int nr_trees;
19 static int read_empty;
20 static struct tree *trees[MAX_UNPACK_TREES];
22 static int list_tree(unsigned char *sha1)
24 struct tree *tree;
26 if (nr_trees >= MAX_UNPACK_TREES)
27 die("I cannot read more than %d trees", MAX_UNPACK_TREES);
28 tree = parse_tree_indirect(sha1);
29 if (!tree)
30 return -1;
31 trees[nr_trees++] = tree;
32 return 0;
35 static const char * const read_tree_usage[] = {
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>]])"),
37 NULL
40 static int index_output_cb(const struct option *opt, const char *arg,
41 int unset)
43 set_alternate_index_output(arg);
44 return 0;
47 static int exclude_per_directory_cb(const struct option *opt, const char *arg,
48 int unset)
50 struct dir_struct *dir;
51 struct unpack_trees_options *opts;
53 opts = (struct unpack_trees_options *)opt->value;
55 if (opts->dir)
56 die("more than one --exclude-per-directory given.");
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.
66 return 0;
69 static void debug_stage(const char *label, const struct cache_entry *ce,
70 struct unpack_trees_options *o)
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));
83 static int debug_merge(const struct cache_entry * const *stages,
84 struct unpack_trees_options *o)
86 int i;
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);
95 return 0;
98 static struct lock_file lock_file;
100 int cmd_read_tree(int argc, const char **argv, const char *unused_prefix)
102 int i, newfd, stage = 0;
103 unsigned char sha1[20];
104 struct tree_desc t[MAX_UNPACK_TREES];
105 struct unpack_trees_options opts;
106 int prefix_set = 0;
107 const struct option read_tree_options[] = {
108 { OPTION_CALLBACK, 0, "index-output", NULL, N_("file"),
109 N_("write resulting index to <file>"),
110 PARSE_OPT_NONEG, index_output_cb },
111 OPT_SET_INT(0, "empty", &read_empty,
112 N_("only empty the index"), 1),
113 OPT__VERBOSE(&opts.verbose_update, N_("be verbose")),
114 OPT_GROUP(N_("Merging")),
115 OPT_SET_INT('m', NULL, &opts.merge,
116 N_("perform a merge in addition to a read"), 1),
117 OPT_SET_INT(0, "trivial", &opts.trivial_merges_only,
118 N_("3-way merge if no file level merging required"), 1),
119 OPT_SET_INT(0, "aggressive", &opts.aggressive,
120 N_("3-way merge in presence of adds and removes"), 1),
121 OPT_SET_INT(0, "reset", &opts.reset,
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>/"),
125 PARSE_OPT_NONEG | PARSE_OPT_LITERAL_ARGHELP },
126 OPT_SET_INT('u', NULL, &opts.update,
127 N_("update working tree with merge result"), 1),
128 { OPTION_CALLBACK, 0, "exclude-per-directory", &opts,
129 N_("gitignore"),
130 N_("allow explicitly ignored files to be overwritten"),
131 PARSE_OPT_NONEG, exclude_per_directory_cb },
132 OPT_SET_INT('i', NULL, &opts.index_only,
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")),
135 OPT_SET_INT(0, "no-sparse-checkout", &opts.skip_sparse_checkout,
136 N_("skip applying sparse checkout filter"), 1),
137 OPT_SET_INT(0, "debug-unpack", &opts.debug_unpack,
138 N_("debug unpack-trees"), 1),
139 OPT_END()
142 memset(&opts, 0, sizeof(opts));
143 opts.head_idx = -1;
144 opts.src_index = &the_index;
145 opts.dst_index = &the_index;
147 git_config(git_default_config, NULL);
149 argc = parse_options(argc, argv, unused_prefix, read_tree_options,
150 read_tree_usage, 0);
152 newfd = hold_locked_index(&lock_file, 1);
154 prefix_set = opts.prefix ? 1 : 0;
155 if (1 < opts.merge + opts.reset + prefix_set)
156 die("Which one? -m, --reset, or --prefix?");
158 if (opts.reset || opts.merge || opts.prefix) {
159 if (read_cache_unmerged() && (opts.prefix || opts.merge))
160 die("You need to resolve your current index first");
161 stage = opts.merge = 1;
163 resolve_undo_clear();
165 for (i = 0; i < argc; i++) {
166 const char *arg = argv[i];
168 if (get_sha1(arg, sha1))
169 die("Not a valid object name %s", arg);
170 if (list_tree(sha1) < 0)
171 die("failed to unpack tree object %s", arg);
172 stage++;
174 if (nr_trees == 0 && !read_empty)
175 warning("read-tree: emptying the index with no arguments is deprecated; use --empty");
176 else if (nr_trees > 0 && read_empty)
177 die("passing trees as arguments contradicts --empty");
179 if (1 < opts.index_only + opts.update)
180 die("-u and -i at the same time makes no sense");
181 if ((opts.update || opts.index_only) && !opts.merge)
182 die("%s is meaningless without -m, --reset, or --prefix",
183 opts.update ? "-u" : "-i");
184 if ((opts.dir && !opts.update))
185 die("--exclude-per-directory is meaningless unless -u");
186 if (opts.merge && !opts.index_only)
187 setup_work_tree();
189 if (opts.merge) {
190 if (stage < 2)
191 die("just how do you expect me to merge %d trees?", stage-1);
192 switch (stage - 1) {
193 case 1:
194 opts.fn = opts.prefix ? bind_merge : oneway_merge;
195 break;
196 case 2:
197 opts.fn = twoway_merge;
198 opts.initial_checkout = is_cache_unborn();
199 break;
200 case 3:
201 default:
202 opts.fn = threeway_merge;
203 break;
206 if (stage - 1 >= 3)
207 opts.head_idx = stage - 2;
208 else
209 opts.head_idx = 1;
212 if (opts.debug_unpack)
213 opts.fn = debug_merge;
215 cache_tree_free(&active_cache_tree);
216 for (i = 0; i < nr_trees; i++) {
217 struct tree *tree = trees[i];
218 parse_tree(tree);
219 init_tree_desc(t+i, tree->buffer, tree->size);
221 if (unpack_trees(nr_trees, t, &opts))
222 return 128;
224 if (opts.debug_unpack || opts.dry_run)
225 return 0; /* do not write the index out */
228 * When reading only one tree (either the most basic form,
229 * "-m ent" or "--reset ent" form), we can obtain a fully
230 * valid cache-tree because the index must match exactly
231 * what came from the tree.
233 if (nr_trees == 1 && !opts.prefix)
234 prime_cache_tree(&active_cache_tree, trees[0]);
236 if (write_cache(newfd, active_cache, active_nr) ||
237 commit_locked_index(&lock_file))
238 die("unable to write new index file");
239 return 0;