2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
11 #include "tree-walk.h"
12 #include "cache-tree.h"
13 #include "unpack-trees.h"
16 #include "parse-options.h"
17 #include "resolve-undo.h"
20 static int read_empty
;
21 static struct tree
*trees
[MAX_UNPACK_TREES
];
23 static int list_tree(unsigned char *sha1
)
27 if (nr_trees
>= MAX_UNPACK_TREES
)
28 die("I cannot read more than %d trees", MAX_UNPACK_TREES
);
29 tree
= parse_tree_indirect(sha1
);
32 trees
[nr_trees
++] = tree
;
36 static const char * const read_tree_usage
[] = {
37 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>]])"),
41 static int index_output_cb(const struct option
*opt
, const char *arg
,
44 set_alternate_index_output(arg
);
48 static int exclude_per_directory_cb(const struct option
*opt
, const char *arg
,
51 struct dir_struct
*dir
;
52 struct unpack_trees_options
*opts
;
54 opts
= (struct unpack_trees_options
*)opt
->value
;
57 die("more than one --exclude-per-directory given.");
59 dir
= xcalloc(1, sizeof(*opts
->dir
));
60 dir
->flags
|= DIR_SHOW_IGNORED
;
61 dir
->exclude_per_dir
= arg
;
63 /* We do not need to nor want to do read-directory
64 * here; we are merely interested in reusing the
65 * per directory ignore stack mechanism.
70 static void debug_stage(const char *label
, const struct cache_entry
*ce
,
71 struct unpack_trees_options
*o
)
75 printf("(missing)\n");
76 else if (ce
== o
->df_conflict_entry
)
77 printf("(conflict)\n");
79 printf("%06o #%d %s %.8s\n",
80 ce
->ce_mode
, ce_stage(ce
), ce
->name
,
81 oid_to_hex(&ce
->oid
));
84 static int debug_merge(const struct cache_entry
* const *stages
,
85 struct unpack_trees_options
*o
)
89 printf("* %d-way merge\n", o
->merge_size
);
90 debug_stage("index", stages
[0], o
);
91 for (i
= 1; i
<= o
->merge_size
; i
++) {
93 xsnprintf(buf
, sizeof(buf
), "ent#%d", i
);
94 debug_stage(buf
, stages
[i
], o
);
99 static struct lock_file lock_file
;
101 int cmd_read_tree(int argc
, const char **argv
, const char *unused_prefix
)
104 unsigned char sha1
[20];
105 struct tree_desc t
[MAX_UNPACK_TREES
];
106 struct unpack_trees_options opts
;
108 const struct option read_tree_options
[] = {
109 { OPTION_CALLBACK
, 0, "index-output", NULL
, N_("file"),
110 N_("write resulting index to <file>"),
111 PARSE_OPT_NONEG
, index_output_cb
},
112 OPT_SET_INT(0, "empty", &read_empty
,
113 N_("only empty the index"), 1),
114 OPT__VERBOSE(&opts
.verbose_update
, N_("be verbose")),
115 OPT_GROUP(N_("Merging")),
116 OPT_SET_INT('m', NULL
, &opts
.merge
,
117 N_("perform a merge in addition to a read"), 1),
118 OPT_SET_INT(0, "trivial", &opts
.trivial_merges_only
,
119 N_("3-way merge if no file level merging required"), 1),
120 OPT_SET_INT(0, "aggressive", &opts
.aggressive
,
121 N_("3-way merge in presence of adds and removes"), 1),
122 OPT_SET_INT(0, "reset", &opts
.reset
,
123 N_("same as -m, but discard unmerged entries"), 1),
124 { OPTION_STRING
, 0, "prefix", &opts
.prefix
, N_("<subdirectory>/"),
125 N_("read the tree into the index under <subdirectory>/"),
126 PARSE_OPT_NONEG
| PARSE_OPT_LITERAL_ARGHELP
},
127 OPT_SET_INT('u', NULL
, &opts
.update
,
128 N_("update working tree with merge result"), 1),
129 { OPTION_CALLBACK
, 0, "exclude-per-directory", &opts
,
131 N_("allow explicitly ignored files to be overwritten"),
132 PARSE_OPT_NONEG
, exclude_per_directory_cb
},
133 OPT_SET_INT('i', NULL
, &opts
.index_only
,
134 N_("don't check the working tree after merging"), 1),
135 OPT__DRY_RUN(&opts
.dry_run
, N_("don't update the index or the work tree")),
136 OPT_SET_INT(0, "no-sparse-checkout", &opts
.skip_sparse_checkout
,
137 N_("skip applying sparse checkout filter"), 1),
138 OPT_SET_INT(0, "debug-unpack", &opts
.debug_unpack
,
139 N_("debug unpack-trees"), 1),
143 memset(&opts
, 0, sizeof(opts
));
145 opts
.src_index
= &the_index
;
146 opts
.dst_index
= &the_index
;
148 git_config(git_default_config
, NULL
);
150 argc
= parse_options(argc
, argv
, unused_prefix
, read_tree_options
,
153 hold_locked_index(&lock_file
, LOCK_DIE_ON_ERROR
);
155 prefix_set
= opts
.prefix
? 1 : 0;
156 if (1 < opts
.merge
+ opts
.reset
+ prefix_set
)
157 die("Which one? -m, --reset, or --prefix?");
162 * The old index should be read anyway even if we're going to
163 * destroy all index entries because we still need to preserve
164 * certain information such as index version or split-index
168 if (opts
.reset
|| opts
.merge
|| opts
.prefix
) {
169 if (read_cache_unmerged() && (opts
.prefix
|| opts
.merge
))
170 die("You need to resolve your current index first");
171 stage
= opts
.merge
= 1;
173 resolve_undo_clear();
175 for (i
= 0; i
< argc
; i
++) {
176 const char *arg
= argv
[i
];
178 if (get_sha1(arg
, sha1
))
179 die("Not a valid object name %s", arg
);
180 if (list_tree(sha1
) < 0)
181 die("failed to unpack tree object %s", arg
);
184 if (nr_trees
== 0 && !read_empty
)
185 warning("read-tree: emptying the index with no arguments is deprecated; use --empty");
186 else if (nr_trees
> 0 && read_empty
)
187 die("passing trees as arguments contradicts --empty");
189 if (1 < opts
.index_only
+ opts
.update
)
190 die("-u and -i at the same time makes no sense");
191 if ((opts
.update
|| opts
.index_only
) && !opts
.merge
)
192 die("%s is meaningless without -m, --reset, or --prefix",
193 opts
.update
? "-u" : "-i");
194 if ((opts
.dir
&& !opts
.update
))
195 die("--exclude-per-directory is meaningless unless -u");
196 if (opts
.merge
&& !opts
.index_only
)
201 die("just how do you expect me to merge %d trees?", stage
-1);
204 opts
.fn
= opts
.prefix
? bind_merge
: oneway_merge
;
207 opts
.fn
= twoway_merge
;
208 opts
.initial_checkout
= is_cache_unborn();
212 opts
.fn
= threeway_merge
;
217 opts
.head_idx
= stage
- 2;
222 if (opts
.debug_unpack
)
223 opts
.fn
= debug_merge
;
225 cache_tree_free(&active_cache_tree
);
226 for (i
= 0; i
< nr_trees
; i
++) {
227 struct tree
*tree
= trees
[i
];
229 init_tree_desc(t
+i
, tree
->buffer
, tree
->size
);
231 if (unpack_trees(nr_trees
, t
, &opts
))
234 if (opts
.debug_unpack
|| opts
.dry_run
)
235 return 0; /* do not write the index out */
238 * When reading only one tree (either the most basic form,
239 * "-m ent" or "--reset ent" form), we can obtain a fully
240 * valid cache-tree because the index must match exactly
241 * what came from the tree.
243 if (nr_trees
== 1 && !opts
.prefix
)
244 prime_cache_tree(&the_index
, trees
[0]);
246 if (write_locked_index(&the_index
, &lock_file
, COMMIT_LOCK
))
247 die("unable to write new index file");