2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
7 #define USE_THE_INDEX_VARIABLE
14 #include "object-name.h"
16 #include "tree-walk.h"
17 #include "cache-tree.h"
18 #include "unpack-trees.h"
19 #include "parse-options.h"
20 #include "repository.h"
21 #include "resolve-undo.h"
23 #include "sparse-index.h"
24 #include "submodule.h"
27 static int read_empty
;
28 static struct tree
*trees
[MAX_UNPACK_TREES
];
30 static int list_tree(struct object_id
*oid
)
34 if (nr_trees
>= MAX_UNPACK_TREES
)
35 die("I cannot read more than %d trees", MAX_UNPACK_TREES
);
36 tree
= parse_tree_indirect(oid
);
39 trees
[nr_trees
++] = tree
;
43 static const char * const read_tree_usage
[] = {
44 N_("git read-tree [(-m [--trivial] [--aggressive] | --reset | --prefix=<prefix>)\n"
45 " [-u | -i]] [--index-output=<file>] [--no-sparse-checkout]\n"
46 " (--empty | <tree-ish1> [<tree-ish2> [<tree-ish3>]])"),
50 static int index_output_cb(const struct option
*opt UNUSED
, const char *arg
,
53 BUG_ON_OPT_NEG(unset
);
54 set_alternate_index_output(arg
);
58 static int exclude_per_directory_cb(const struct option
*opt
, const char *arg
,
61 struct unpack_trees_options
*opts
;
63 BUG_ON_OPT_NEG(unset
);
65 opts
= (struct unpack_trees_options
*)opt
->value
;
68 die("--exclude-per-directory is meaningless unless -u");
69 if (strcmp(arg
, ".gitignore"))
70 die("--exclude-per-directory argument must be .gitignore");
74 static void debug_stage(const char *label
, const struct cache_entry
*ce
,
75 struct unpack_trees_options
*o
)
79 printf("(missing)\n");
80 else if (ce
== o
->df_conflict_entry
)
81 printf("(conflict)\n");
83 printf("%06o #%d %s %.8s\n",
84 ce
->ce_mode
, ce_stage(ce
), ce
->name
,
85 oid_to_hex(&ce
->oid
));
88 static int debug_merge(const struct cache_entry
* const *stages
,
89 struct unpack_trees_options
*o
)
93 printf("* %d-way merge\n", o
->internal
.merge_size
);
94 debug_stage("index", stages
[0], o
);
95 for (i
= 1; i
<= o
->internal
.merge_size
; i
++) {
97 xsnprintf(buf
, sizeof(buf
), "ent#%d", i
);
98 debug_stage(buf
, stages
[i
], o
);
103 static int git_read_tree_config(const char *var
, const char *value
,
104 const struct config_context
*ctx
, void *cb
)
106 if (!strcmp(var
, "submodule.recurse"))
107 return git_default_submodule_config(var
, value
, cb
);
109 return git_default_config(var
, value
, ctx
, cb
);
112 int cmd_read_tree(int argc
, const char **argv
, const char *cmd_prefix
)
115 struct object_id oid
;
116 struct tree_desc t
[MAX_UNPACK_TREES
];
117 struct unpack_trees_options opts
;
119 struct lock_file lock_file
= LOCK_INIT
;
120 const struct option read_tree_options
[] = {
121 OPT__SUPER_PREFIX(&opts
.super_prefix
),
122 OPT_CALLBACK_F(0, "index-output", NULL
, N_("file"),
123 N_("write resulting index to <file>"),
124 PARSE_OPT_NONEG
, index_output_cb
),
125 OPT_BOOL(0, "empty", &read_empty
,
126 N_("only empty the index")),
127 OPT__VERBOSE(&opts
.verbose_update
, N_("be verbose")),
128 OPT_GROUP(N_("Merging")),
129 OPT_BOOL('m', NULL
, &opts
.merge
,
130 N_("perform a merge in addition to a read")),
131 OPT_BOOL(0, "trivial", &opts
.trivial_merges_only
,
132 N_("3-way merge if no file level merging required")),
133 OPT_BOOL(0, "aggressive", &opts
.aggressive
,
134 N_("3-way merge in presence of adds and removes")),
135 OPT_BOOL(0, "reset", &opts
.reset
,
136 N_("same as -m, but discard unmerged entries")),
137 { OPTION_STRING
, 0, "prefix", &opts
.prefix
, N_("<subdirectory>/"),
138 N_("read the tree into the index under <subdirectory>/"),
140 OPT_BOOL('u', NULL
, &opts
.update
,
141 N_("update working tree with merge result")),
142 OPT_CALLBACK_F(0, "exclude-per-directory", &opts
,
144 N_("allow explicitly ignored files to be overwritten"),
145 PARSE_OPT_NONEG
, exclude_per_directory_cb
),
146 OPT_BOOL('i', NULL
, &opts
.index_only
,
147 N_("don't check the working tree after merging")),
148 OPT__DRY_RUN(&opts
.dry_run
, N_("don't update the index or the work tree")),
149 OPT_BOOL(0, "no-sparse-checkout", &opts
.skip_sparse_checkout
,
150 N_("skip applying sparse checkout filter")),
151 OPT_BOOL(0, "debug-unpack", &opts
.internal
.debug_unpack
,
152 N_("debug unpack-trees")),
153 OPT_CALLBACK_F(0, "recurse-submodules", NULL
,
154 "checkout", "control recursive updating of submodules",
155 PARSE_OPT_OPTARG
, option_parse_recurse_submodules_worktree_updater
),
156 OPT__QUIET(&opts
.quiet
, N_("suppress feedback messages")),
160 memset(&opts
, 0, sizeof(opts
));
162 opts
.src_index
= &the_index
;
163 opts
.dst_index
= &the_index
;
165 git_config(git_read_tree_config
, NULL
);
167 argc
= parse_options(argc
, argv
, cmd_prefix
, read_tree_options
,
170 prefix_set
= opts
.prefix
? 1 : 0;
171 if (1 < opts
.merge
+ opts
.reset
+ prefix_set
)
172 die("Which one? -m, --reset, or --prefix?");
174 /* Prefix should not start with a directory separator */
175 if (opts
.prefix
&& opts
.prefix
[0] == '/')
176 die("Invalid prefix, prefix cannot start with '/'");
179 opts
.reset
= UNPACK_RESET_OVERWRITE_UNTRACKED
;
181 prepare_repo_settings(the_repository
);
182 the_repository
->settings
.command_requires_full_index
= 0;
184 repo_hold_locked_index(the_repository
, &lock_file
, LOCK_DIE_ON_ERROR
);
189 * The old index should be read anyway even if we're going to
190 * destroy all index entries because we still need to preserve
191 * certain information such as index version or split-index
195 if (opts
.reset
|| opts
.merge
|| opts
.prefix
) {
196 if (repo_read_index_unmerged(the_repository
) && (opts
.prefix
|| opts
.merge
))
197 die(_("You need to resolve your current index first"));
198 stage
= opts
.merge
= 1;
200 resolve_undo_clear_index(&the_index
);
202 for (i
= 0; i
< argc
; i
++) {
203 const char *arg
= argv
[i
];
205 if (repo_get_oid(the_repository
, arg
, &oid
))
206 die("Not a valid object name %s", arg
);
207 if (list_tree(&oid
) < 0)
208 die("failed to unpack tree object %s", arg
);
211 if (!nr_trees
&& !read_empty
&& !opts
.merge
)
212 warning("read-tree: emptying the index with no arguments is deprecated; use --empty");
213 else if (nr_trees
> 0 && read_empty
)
214 die("passing trees as arguments contradicts --empty");
216 if (1 < opts
.index_only
+ opts
.update
)
217 die("-u and -i at the same time makes no sense");
218 if ((opts
.update
|| opts
.index_only
) && !opts
.merge
)
219 die("%s is meaningless without -m, --reset, or --prefix",
220 opts
.update
? "-u" : "-i");
221 if (opts
.update
&& !opts
.reset
)
222 opts
.preserve_ignored
= 0;
223 /* otherwise, opts.preserve_ignored is irrelevant */
224 if (opts
.merge
&& !opts
.index_only
)
227 if (opts
.skip_sparse_checkout
)
228 ensure_full_index(&the_index
);
233 die("you must specify at least one tree to merge");
236 opts
.fn
= opts
.prefix
? bind_merge
: oneway_merge
;
239 opts
.fn
= twoway_merge
;
240 opts
.initial_checkout
= is_index_unborn(&the_index
);
244 opts
.fn
= threeway_merge
;
249 opts
.head_idx
= stage
- 2;
254 if (opts
.internal
.debug_unpack
)
255 opts
.fn
= debug_merge
;
257 /* If we're going to prime_cache_tree later, skip cache tree update */
258 if (nr_trees
== 1 && !opts
.prefix
)
259 opts
.skip_cache_tree_update
= 1;
261 cache_tree_free(&the_index
.cache_tree
);
262 for (i
= 0; i
< nr_trees
; i
++) {
263 struct tree
*tree
= trees
[i
];
265 init_tree_desc(t
+i
, tree
->buffer
, tree
->size
);
267 if (unpack_trees(nr_trees
, t
, &opts
))
270 if (opts
.internal
.debug_unpack
|| opts
.dry_run
)
271 return 0; /* do not write the index out */
274 * When reading only one tree (either the most basic form,
275 * "-m ent" or "--reset ent" form), we can obtain a fully
276 * valid cache-tree because the index must match exactly
277 * what came from the tree.
279 if (nr_trees
== 1 && !opts
.prefix
)
280 prime_cache_tree(the_repository
,
281 the_repository
->index
,
284 if (write_locked_index(&the_index
, &lock_file
, COMMIT_LOCK
))
285 die("unable to write new index file");