2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
7 #define USE_THE_INDEX_VARIABLE
13 #include "tree-walk.h"
14 #include "cache-tree.h"
15 #include "unpack-trees.h"
18 #include "parse-options.h"
19 #include "resolve-undo.h"
20 #include "submodule.h"
21 #include "submodule-config.h"
24 static int read_empty
;
25 static struct tree
*trees
[MAX_UNPACK_TREES
];
27 static int list_tree(struct object_id
*oid
)
31 if (nr_trees
>= MAX_UNPACK_TREES
)
32 die("I cannot read more than %d trees", MAX_UNPACK_TREES
);
33 tree
= parse_tree_indirect(oid
);
36 trees
[nr_trees
++] = tree
;
40 static const char * const read_tree_usage
[] = {
41 N_("git read-tree [(-m [--trivial] [--aggressive] | --reset | --prefix=<prefix>)\n"
42 " [-u | -i]] [--index-output=<file>] [--no-sparse-checkout]\n"
43 " (--empty | <tree-ish1> [<tree-ish2> [<tree-ish3>]])"),
47 static int index_output_cb(const struct option
*opt
, const char *arg
,
50 BUG_ON_OPT_NEG(unset
);
51 set_alternate_index_output(arg
);
55 static int exclude_per_directory_cb(const struct option
*opt
, const char *arg
,
58 struct unpack_trees_options
*opts
;
60 BUG_ON_OPT_NEG(unset
);
62 opts
= (struct unpack_trees_options
*)opt
->value
;
65 die("--exclude-per-directory is meaningless unless -u");
66 if (strcmp(arg
, ".gitignore"))
67 die("--exclude-per-directory argument must be .gitignore");
71 static void debug_stage(const char *label
, const struct cache_entry
*ce
,
72 struct unpack_trees_options
*o
)
76 printf("(missing)\n");
77 else if (ce
== o
->df_conflict_entry
)
78 printf("(conflict)\n");
80 printf("%06o #%d %s %.8s\n",
81 ce
->ce_mode
, ce_stage(ce
), ce
->name
,
82 oid_to_hex(&ce
->oid
));
85 static int debug_merge(const struct cache_entry
* const *stages
,
86 struct unpack_trees_options
*o
)
90 printf("* %d-way merge\n", o
->merge_size
);
91 debug_stage("index", stages
[0], o
);
92 for (i
= 1; i
<= o
->merge_size
; i
++) {
94 xsnprintf(buf
, sizeof(buf
), "ent#%d", i
);
95 debug_stage(buf
, stages
[i
], o
);
100 static int git_read_tree_config(const char *var
, const char *value
, void *cb
)
102 if (!strcmp(var
, "submodule.recurse"))
103 return git_default_submodule_config(var
, value
, cb
);
105 return git_default_config(var
, value
, cb
);
108 int cmd_read_tree(int argc
, const char **argv
, const char *cmd_prefix
)
111 struct object_id oid
;
112 struct tree_desc t
[MAX_UNPACK_TREES
];
113 struct unpack_trees_options opts
;
115 struct lock_file lock_file
= LOCK_INIT
;
116 const struct option read_tree_options
[] = {
117 OPT__SUPER_PREFIX(&opts
.super_prefix
),
118 OPT_CALLBACK_F(0, "index-output", NULL
, N_("file"),
119 N_("write resulting index to <file>"),
120 PARSE_OPT_NONEG
, index_output_cb
),
121 OPT_BOOL(0, "empty", &read_empty
,
122 N_("only empty the index")),
123 OPT__VERBOSE(&opts
.verbose_update
, N_("be verbose")),
124 OPT_GROUP(N_("Merging")),
125 OPT_BOOL('m', NULL
, &opts
.merge
,
126 N_("perform a merge in addition to a read")),
127 OPT_BOOL(0, "trivial", &opts
.trivial_merges_only
,
128 N_("3-way merge if no file level merging required")),
129 OPT_BOOL(0, "aggressive", &opts
.aggressive
,
130 N_("3-way merge in presence of adds and removes")),
131 OPT_BOOL(0, "reset", &opts
.reset
,
132 N_("same as -m, but discard unmerged entries")),
133 { OPTION_STRING
, 0, "prefix", &opts
.prefix
, N_("<subdirectory>/"),
134 N_("read the tree into the index under <subdirectory>/"),
136 OPT_BOOL('u', NULL
, &opts
.update
,
137 N_("update working tree with merge result")),
138 OPT_CALLBACK_F(0, "exclude-per-directory", &opts
,
140 N_("allow explicitly ignored files to be overwritten"),
141 PARSE_OPT_NONEG
, exclude_per_directory_cb
),
142 OPT_BOOL('i', NULL
, &opts
.index_only
,
143 N_("don't check the working tree after merging")),
144 OPT__DRY_RUN(&opts
.dry_run
, N_("don't update the index or the work tree")),
145 OPT_BOOL(0, "no-sparse-checkout", &opts
.skip_sparse_checkout
,
146 N_("skip applying sparse checkout filter")),
147 OPT_BOOL(0, "debug-unpack", &opts
.debug_unpack
,
148 N_("debug unpack-trees")),
149 OPT_CALLBACK_F(0, "recurse-submodules", NULL
,
150 "checkout", "control recursive updating of submodules",
151 PARSE_OPT_OPTARG
, option_parse_recurse_submodules_worktree_updater
),
152 OPT__QUIET(&opts
.quiet
, N_("suppress feedback messages")),
156 memset(&opts
, 0, sizeof(opts
));
158 opts
.src_index
= &the_index
;
159 opts
.dst_index
= &the_index
;
161 git_config(git_read_tree_config
, NULL
);
163 argc
= parse_options(argc
, argv
, cmd_prefix
, read_tree_options
,
166 prefix_set
= opts
.prefix
? 1 : 0;
167 if (1 < opts
.merge
+ opts
.reset
+ prefix_set
)
168 die("Which one? -m, --reset, or --prefix?");
170 /* Prefix should not start with a directory separator */
171 if (opts
.prefix
&& opts
.prefix
[0] == '/')
172 die("Invalid prefix, prefix cannot start with '/'");
175 opts
.reset
= UNPACK_RESET_OVERWRITE_UNTRACKED
;
177 prepare_repo_settings(the_repository
);
178 the_repository
->settings
.command_requires_full_index
= 0;
180 repo_hold_locked_index(the_repository
, &lock_file
, LOCK_DIE_ON_ERROR
);
185 * The old index should be read anyway even if we're going to
186 * destroy all index entries because we still need to preserve
187 * certain information such as index version or split-index
191 if (opts
.reset
|| opts
.merge
|| opts
.prefix
) {
192 if (repo_read_index_unmerged(the_repository
) && (opts
.prefix
|| opts
.merge
))
193 die(_("You need to resolve your current index first"));
194 stage
= opts
.merge
= 1;
196 resolve_undo_clear_index(&the_index
);
198 for (i
= 0; i
< argc
; i
++) {
199 const char *arg
= argv
[i
];
201 if (get_oid(arg
, &oid
))
202 die("Not a valid object name %s", arg
);
203 if (list_tree(&oid
) < 0)
204 die("failed to unpack tree object %s", arg
);
207 if (!nr_trees
&& !read_empty
&& !opts
.merge
)
208 warning("read-tree: emptying the index with no arguments is deprecated; use --empty");
209 else if (nr_trees
> 0 && read_empty
)
210 die("passing trees as arguments contradicts --empty");
212 if (1 < opts
.index_only
+ opts
.update
)
213 die("-u and -i at the same time makes no sense");
214 if ((opts
.update
|| opts
.index_only
) && !opts
.merge
)
215 die("%s is meaningless without -m, --reset, or --prefix",
216 opts
.update
? "-u" : "-i");
217 if (opts
.update
&& !opts
.reset
)
218 opts
.preserve_ignored
= 0;
219 /* otherwise, opts.preserve_ignored is irrelevant */
220 if (opts
.merge
&& !opts
.index_only
)
223 if (opts
.skip_sparse_checkout
)
224 ensure_full_index(&the_index
);
229 die("you must specify at least one tree to merge");
232 opts
.fn
= opts
.prefix
? bind_merge
: oneway_merge
;
235 opts
.fn
= twoway_merge
;
236 opts
.initial_checkout
= is_index_unborn(&the_index
);
240 opts
.fn
= threeway_merge
;
245 opts
.head_idx
= stage
- 2;
250 if (opts
.debug_unpack
)
251 opts
.fn
= debug_merge
;
253 /* If we're going to prime_cache_tree later, skip cache tree update */
254 if (nr_trees
== 1 && !opts
.prefix
)
255 opts
.skip_cache_tree_update
= 1;
257 cache_tree_free(&the_index
.cache_tree
);
258 for (i
= 0; i
< nr_trees
; i
++) {
259 struct tree
*tree
= trees
[i
];
261 init_tree_desc(t
+i
, tree
->buffer
, tree
->size
);
263 if (unpack_trees(nr_trees
, t
, &opts
))
266 if (opts
.debug_unpack
|| opts
.dry_run
)
267 return 0; /* do not write the index out */
270 * When reading only one tree (either the most basic form,
271 * "-m ent" or "--reset ent" form), we can obtain a fully
272 * valid cache-tree because the index must match exactly
273 * what came from the tree.
275 if (nr_trees
== 1 && !opts
.prefix
)
276 prime_cache_tree(the_repository
,
277 the_repository
->index
,
280 if (write_locked_index(&the_index
, &lock_file
, COMMIT_LOCK
))
281 die("unable to write new index file");