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