blob: e9eb795d64b9cd6a6940995d41dd9c3f3239df02 [file] [log] [blame]
Johannes Schindelin16da1342006-07-30 20:25:18 +02001#include "cache.h"
Junio C Hamanof8a9d422006-12-04 16:00:46 -08002#include "dir.h"
Johannes Schindelin16da1342006-07-30 20:25:18 +02003#include "tree.h"
4#include "tree-walk.h"
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02005#include "cache-tree.h"
Johannes Schindelin16da1342006-07-30 20:25:18 +02006#include "unpack-trees.h"
Nicolas Pitre96a02f82007-04-18 14:27:45 -04007#include "progress.h"
Sven Verdoolaege0cf73752007-07-17 20:28:28 +02008#include "refs.h"
Johannes Schindelin16da1342006-07-30 20:25:18 +02009
Johannes Schindelin076b0ad2006-07-30 20:26:15 +020010#define DBRT_DEBUG 1
11
Johannes Schindelin16da1342006-07-30 20:25:18 +020012struct tree_entry_list {
13 struct tree_entry_list *next;
Johannes Schindelin16da1342006-07-30 20:25:18 +020014 unsigned int mode;
15 const char *name;
16 const unsigned char *sha1;
17};
18
Linus Torvalds933bf402007-08-09 22:21:29 -070019static struct tree_entry_list *create_tree_entry_list(struct tree_desc *desc)
Johannes Schindelin16da1342006-07-30 20:25:18 +020020{
Johannes Schindelin16da1342006-07-30 20:25:18 +020021 struct name_entry one;
22 struct tree_entry_list *ret = NULL;
23 struct tree_entry_list **list_p = &ret;
24
Linus Torvalds933bf402007-08-09 22:21:29 -070025 while (tree_entry(desc, &one)) {
Johannes Schindelin16da1342006-07-30 20:25:18 +020026 struct tree_entry_list *entry;
27
28 entry = xmalloc(sizeof(struct tree_entry_list));
29 entry->name = one.path;
30 entry->sha1 = one.sha1;
31 entry->mode = one.mode;
Johannes Schindelin16da1342006-07-30 20:25:18 +020032 entry->next = NULL;
33
34 *list_p = entry;
35 list_p = &entry->next;
36 }
37 return ret;
38}
39
40static int entcmp(const char *name1, int dir1, const char *name2, int dir2)
41{
42 int len1 = strlen(name1);
43 int len2 = strlen(name2);
44 int len = len1 < len2 ? len1 : len2;
45 int ret = memcmp(name1, name2, len);
46 unsigned char c1, c2;
47 if (ret)
48 return ret;
49 c1 = name1[len];
50 c2 = name2[len];
51 if (!c1 && dir1)
52 c1 = '/';
53 if (!c2 && dir2)
54 c2 = '/';
55 ret = (c1 < c2) ? -1 : (c1 > c2) ? 1 : 0;
56 if (c1 && c2 && !ret)
57 ret = len1 - len2;
58 return ret;
59}
60
Linus Torvaldsb48d5a02007-08-10 12:15:54 -070061static inline void remove_entry(int remove)
62{
63 if (remove >= 0)
64 remove_cache_entry_at(remove);
65}
66
Johannes Schindelin16da1342006-07-30 20:25:18 +020067static int unpack_trees_rec(struct tree_entry_list **posns, int len,
68 const char *base, struct unpack_trees_options *o,
Johannes Schindelin16da1342006-07-30 20:25:18 +020069 struct tree_entry_list *df_conflict_list)
70{
Linus Torvaldsb48d5a02007-08-10 12:15:54 -070071 int remove;
Johannes Schindelin16da1342006-07-30 20:25:18 +020072 int baselen = strlen(base);
73 int src_size = len + 1;
Junio C Hamanof8a9d422006-12-04 16:00:46 -080074 int retval = 0;
75
Johannes Schindelin16da1342006-07-30 20:25:18 +020076 do {
77 int i;
78 const char *first;
79 int firstdir = 0;
80 int pathlen;
81 unsigned ce_size;
82 struct tree_entry_list **subposns;
83 struct cache_entry **src;
84 int any_files = 0;
85 int any_dirs = 0;
86 char *cache_name;
87 int ce_stage;
88
89 /* Find the first name in the input. */
90
91 first = NULL;
92 cache_name = NULL;
93
94 /* Check the cache */
Junio C Hamano9a4d8fd2007-04-02 15:06:59 -070095 if (o->merge && o->pos < active_nr) {
Johannes Schindelin16da1342006-07-30 20:25:18 +020096 /* This is a bit tricky: */
97 /* If the index has a subdirectory (with
98 * contents) as the first name, it'll get a
99 * filename like "foo/bar". But that's after
100 * "foo", so the entry in trees will get
101 * handled first, at which point we'll go into
102 * "foo", and deal with "bar" from the index,
103 * because the base will be "foo/". The only
104 * way we can actually have "foo/bar" first of
105 * all the things is if the trees don't
106 * contain "foo" at all, in which case we'll
107 * handle "foo/bar" without going into the
108 * directory, but that's fine (and will return
109 * an error anyway, with the added unknown
110 * file case.
111 */
112
Junio C Hamano9a4d8fd2007-04-02 15:06:59 -0700113 cache_name = active_cache[o->pos]->name;
Johannes Schindelin16da1342006-07-30 20:25:18 +0200114 if (strlen(cache_name) > baselen &&
115 !memcmp(cache_name, base, baselen)) {
116 cache_name += baselen;
117 first = cache_name;
118 } else {
119 cache_name = NULL;
120 }
121 }
122
123#if DBRT_DEBUG > 1
124 if (first)
125 printf("index %s\n", first);
126#endif
127 for (i = 0; i < len; i++) {
128 if (!posns[i] || posns[i] == df_conflict_list)
129 continue;
130#if DBRT_DEBUG > 1
131 printf("%d %s\n", i + 1, posns[i]->name);
132#endif
133 if (!first || entcmp(first, firstdir,
134 posns[i]->name,
René Scharfe1843d8d2007-07-24 23:54:25 +0200135 S_ISDIR(posns[i]->mode)) > 0) {
Johannes Schindelin16da1342006-07-30 20:25:18 +0200136 first = posns[i]->name;
René Scharfe1843d8d2007-07-24 23:54:25 +0200137 firstdir = S_ISDIR(posns[i]->mode);
Johannes Schindelin16da1342006-07-30 20:25:18 +0200138 }
139 }
140 /* No name means we're done */
141 if (!first)
Junio C Hamanof8a9d422006-12-04 16:00:46 -0800142 goto leave_directory;
Johannes Schindelin16da1342006-07-30 20:25:18 +0200143
144 pathlen = strlen(first);
145 ce_size = cache_entry_size(baselen + pathlen);
146
147 src = xcalloc(src_size, sizeof(struct cache_entry *));
148
149 subposns = xcalloc(len, sizeof(struct tree_list_entry *));
150
Linus Torvaldsb48d5a02007-08-10 12:15:54 -0700151 remove = -1;
Johannes Schindelin16da1342006-07-30 20:25:18 +0200152 if (cache_name && !strcmp(cache_name, first)) {
153 any_files = 1;
Junio C Hamano9a4d8fd2007-04-02 15:06:59 -0700154 src[0] = active_cache[o->pos];
Linus Torvaldsb48d5a02007-08-10 12:15:54 -0700155 remove = o->pos;
Johannes Schindelin16da1342006-07-30 20:25:18 +0200156 }
157
158 for (i = 0; i < len; i++) {
159 struct cache_entry *ce;
160
161 if (!posns[i] ||
162 (posns[i] != df_conflict_list &&
163 strcmp(first, posns[i]->name))) {
164 continue;
165 }
166
167 if (posns[i] == df_conflict_list) {
168 src[i + o->merge] = o->df_conflict_entry;
169 continue;
170 }
171
René Scharfe1843d8d2007-07-24 23:54:25 +0200172 if (S_ISDIR(posns[i]->mode)) {
Johannes Schindelin16da1342006-07-30 20:25:18 +0200173 struct tree *tree = lookup_tree(posns[i]->sha1);
Linus Torvalds933bf402007-08-09 22:21:29 -0700174 struct tree_desc t;
Johannes Schindelin16da1342006-07-30 20:25:18 +0200175 any_dirs = 1;
176 parse_tree(tree);
Linus Torvalds933bf402007-08-09 22:21:29 -0700177 init_tree_desc(&t, tree->buffer, tree->size);
178 subposns[i] = create_tree_entry_list(&t);
Johannes Schindelin16da1342006-07-30 20:25:18 +0200179 posns[i] = posns[i]->next;
180 src[i + o->merge] = o->df_conflict_entry;
181 continue;
182 }
183
184 if (!o->merge)
185 ce_stage = 0;
186 else if (i + 1 < o->head_idx)
187 ce_stage = 1;
188 else if (i + 1 > o->head_idx)
189 ce_stage = 3;
190 else
191 ce_stage = 2;
192
193 ce = xcalloc(1, ce_size);
194 ce->ce_mode = create_ce_mode(posns[i]->mode);
195 ce->ce_flags = create_ce_flags(baselen + pathlen,
196 ce_stage);
197 memcpy(ce->name, base, baselen);
198 memcpy(ce->name + baselen, first, pathlen + 1);
199
200 any_files = 1;
201
Shawn Pearcee7024962006-08-23 02:49:00 -0400202 hashcpy(ce->sha1, posns[i]->sha1);
Johannes Schindelin16da1342006-07-30 20:25:18 +0200203 src[i + o->merge] = ce;
204 subposns[i] = df_conflict_list;
205 posns[i] = posns[i]->next;
206 }
207 if (any_files) {
208 if (o->merge) {
209 int ret;
210
211#if DBRT_DEBUG > 1
212 printf("%s:\n", first);
213 for (i = 0; i < src_size; i++) {
214 printf(" %d ", i);
215 if (src[i])
216 printf("%s\n", sha1_to_hex(src[i]->sha1));
217 else
218 printf("\n");
219 }
220#endif
Linus Torvaldsb48d5a02007-08-10 12:15:54 -0700221 ret = o->fn(src, o, remove);
Johannes Schindelin16da1342006-07-30 20:25:18 +0200222
223#if DBRT_DEBUG > 1
224 printf("Added %d entries\n", ret);
225#endif
Junio C Hamano9a4d8fd2007-04-02 15:06:59 -0700226 o->pos += ret;
Johannes Schindelin16da1342006-07-30 20:25:18 +0200227 } else {
Linus Torvaldsb48d5a02007-08-10 12:15:54 -0700228 remove_entry(remove);
Johannes Schindelin16da1342006-07-30 20:25:18 +0200229 for (i = 0; i < src_size; i++) {
230 if (src[i]) {
231 add_cache_entry(src[i], ADD_CACHE_OK_TO_ADD|ADD_CACHE_SKIP_DFCHECK);
232 }
233 }
234 }
235 }
236 if (any_dirs) {
237 char *newbase = xmalloc(baselen + 2 + pathlen);
238 memcpy(newbase, base, baselen);
239 memcpy(newbase + baselen, first, pathlen);
240 newbase[baselen + pathlen] = '/';
241 newbase[baselen + pathlen + 1] = '\0';
242 if (unpack_trees_rec(subposns, len, newbase, o,
Junio C Hamano9a4d8fd2007-04-02 15:06:59 -0700243 df_conflict_list)) {
Junio C Hamanof8a9d422006-12-04 16:00:46 -0800244 retval = -1;
245 goto leave_directory;
246 }
Johannes Schindelin16da1342006-07-30 20:25:18 +0200247 free(newbase);
248 }
249 free(subposns);
250 free(src);
251 } while (1);
Junio C Hamanof8a9d422006-12-04 16:00:46 -0800252
253 leave_directory:
Junio C Hamanof8a9d422006-12-04 16:00:46 -0800254 return retval;
Johannes Schindelin16da1342006-07-30 20:25:18 +0200255}
256
257/* Unlink the last component and attempt to remove leading
258 * directories, in case this unlink is the removal of the
259 * last entry in the directory -- empty directories are removed.
260 */
Junio C Hamano16a4c612007-05-10 23:44:53 -0700261static void unlink_entry(char *name, char *last_symlink)
Johannes Schindelin16da1342006-07-30 20:25:18 +0200262{
263 char *cp, *prev;
264
Junio C Hamano16a4c612007-05-10 23:44:53 -0700265 if (has_symlink_leading_path(name, last_symlink))
266 return;
Johannes Schindelin16da1342006-07-30 20:25:18 +0200267 if (unlink(name))
268 return;
269 prev = NULL;
270 while (1) {
271 int status;
272 cp = strrchr(name, '/');
273 if (prev)
274 *prev = '/';
275 if (!cp)
276 break;
277
278 *cp = 0;
279 status = rmdir(name);
280 if (status) {
281 *cp = '/';
282 break;
283 }
284 prev = cp;
285 }
286}
287
Johannes Schindelin16da1342006-07-30 20:25:18 +0200288static struct checkout state;
289static void check_updates(struct cache_entry **src, int nr,
Junio C Hamano16a4c612007-05-10 23:44:53 -0700290 struct unpack_trees_options *o)
Johannes Schindelin16da1342006-07-30 20:25:18 +0200291{
292 unsigned short mask = htons(CE_UPDATE);
Nicolas Pitre96a02f82007-04-18 14:27:45 -0400293 unsigned cnt = 0, total = 0;
Nicolas Pitredc6a0752007-10-30 14:57:32 -0400294 struct progress *progress = NULL;
Junio C Hamano16a4c612007-05-10 23:44:53 -0700295 char last_symlink[PATH_MAX];
Johannes Schindelin16da1342006-07-30 20:25:18 +0200296
297 if (o->update && o->verbose_update) {
298 for (total = cnt = 0; cnt < nr; cnt++) {
299 struct cache_entry *ce = src[cnt];
300 if (!ce->ce_mode || ce->ce_flags & mask)
301 total++;
302 }
303
Nicolas Pitredc6a0752007-10-30 14:57:32 -0400304 progress = start_progress_delay("Checking out files",
305 total, 50, 2);
Johannes Schindelin16da1342006-07-30 20:25:18 +0200306 cnt = 0;
307 }
308
Junio C Hamano16a4c612007-05-10 23:44:53 -0700309 *last_symlink = '\0';
Johannes Schindelin16da1342006-07-30 20:25:18 +0200310 while (nr--) {
311 struct cache_entry *ce = *src++;
312
Nicolas Pitre4d4fcc52007-10-30 14:57:33 -0400313 if (!ce->ce_mode || ce->ce_flags & mask)
314 display_progress(progress, ++cnt);
Johannes Schindelin16da1342006-07-30 20:25:18 +0200315 if (!ce->ce_mode) {
316 if (o->update)
Junio C Hamano16a4c612007-05-10 23:44:53 -0700317 unlink_entry(ce->name, last_symlink);
Johannes Schindelin16da1342006-07-30 20:25:18 +0200318 continue;
319 }
320 if (ce->ce_flags & mask) {
321 ce->ce_flags &= ~mask;
Junio C Hamano16a4c612007-05-10 23:44:53 -0700322 if (o->update) {
Johannes Schindelin16da1342006-07-30 20:25:18 +0200323 checkout_entry(ce, &state, NULL);
Junio C Hamano16a4c612007-05-10 23:44:53 -0700324 *last_symlink = '\0';
325 }
Johannes Schindelin16da1342006-07-30 20:25:18 +0200326 }
327 }
Nicolas Pitre4d4fcc52007-10-30 14:57:33 -0400328 stop_progress(&progress);
Johannes Schindelin16da1342006-07-30 20:25:18 +0200329}
330
Linus Torvalds933bf402007-08-09 22:21:29 -0700331int unpack_trees(unsigned len, struct tree_desc *t, struct unpack_trees_options *o)
Johannes Schindelin16da1342006-07-30 20:25:18 +0200332{
Johannes Schindelin16da1342006-07-30 20:25:18 +0200333 struct tree_entry_list **posns;
334 int i;
Johannes Schindelin16da1342006-07-30 20:25:18 +0200335 struct tree_entry_list df_conflict_list;
Junio C Hamano0fb1eaa2006-12-04 02:11:39 -0800336 static struct cache_entry *dfc;
Johannes Schindelin16da1342006-07-30 20:25:18 +0200337
338 memset(&df_conflict_list, 0, sizeof(df_conflict_list));
339 df_conflict_list.next = &df_conflict_list;
Johannes Schindelin076b0ad2006-07-30 20:26:15 +0200340 memset(&state, 0, sizeof(state));
Johannes Schindelin16da1342006-07-30 20:25:18 +0200341 state.base_dir = "";
342 state.force = 1;
343 state.quiet = 1;
344 state.refresh_cache = 1;
345
346 o->merge_size = len;
Junio C Hamano0fb1eaa2006-12-04 02:11:39 -0800347
348 if (!dfc)
349 dfc = xcalloc(1, sizeof(struct cache_entry) + 1);
350 o->df_conflict_entry = dfc;
Johannes Schindelin16da1342006-07-30 20:25:18 +0200351
352 if (len) {
353 posns = xmalloc(len * sizeof(struct tree_entry_list *));
Linus Torvalds933bf402007-08-09 22:21:29 -0700354 for (i = 0; i < len; i++)
355 posns[i] = create_tree_entry_list(t+i);
356
Johannes Schindelin16da1342006-07-30 20:25:18 +0200357 if (unpack_trees_rec(posns, len, o->prefix ? o->prefix : "",
Junio C Hamano9a4d8fd2007-04-02 15:06:59 -0700358 o, &df_conflict_list))
Johannes Schindelin16da1342006-07-30 20:25:18 +0200359 return -1;
360 }
361
362 if (o->trivial_merges_only && o->nontrivial_merge)
363 die("Merge requires file-level merging");
364
365 check_updates(active_cache, active_nr, o);
366 return 0;
367}
Johannes Schindelin076b0ad2006-07-30 20:26:15 +0200368
369/* Here come the merge functions */
370
371static void reject_merge(struct cache_entry *ce)
372{
373 die("Entry '%s' would be overwritten by merge. Cannot merge.",
374 ce->name);
375}
376
377static int same(struct cache_entry *a, struct cache_entry *b)
378{
379 if (!!a != !!b)
380 return 0;
381 if (!a && !b)
382 return 1;
383 return a->ce_mode == b->ce_mode &&
David Rientjesa89fccd2006-08-17 11:54:57 -0700384 !hashcmp(a->sha1, b->sha1);
Johannes Schindelin076b0ad2006-07-30 20:26:15 +0200385}
386
387
388/*
389 * When a CE gets turned into an unmerged entry, we
390 * want it to be up-to-date
391 */
392static void verify_uptodate(struct cache_entry *ce,
393 struct unpack_trees_options *o)
394{
395 struct stat st;
396
397 if (o->index_only || o->reset)
398 return;
399
400 if (!lstat(ce->name, &st)) {
Junio C Hamano4bd5b7d2007-11-10 00:15:03 -0800401 unsigned changed = ce_match_stat(ce, &st, CE_MATCH_IGNORE_VALID);
Johannes Schindelin076b0ad2006-07-30 20:26:15 +0200402 if (!changed)
403 return;
Junio C Hamano936492d2007-08-03 22:13:09 -0700404 /*
405 * NEEDSWORK: the current default policy is to allow
406 * submodule to be out of sync wrt the supermodule
407 * index. This needs to be tightened later for
408 * submodules that are marked to be automatically
409 * checked out.
410 */
411 if (S_ISGITLINK(ntohl(ce->ce_mode)))
412 return;
Johannes Schindelin076b0ad2006-07-30 20:26:15 +0200413 errno = 0;
414 }
Johannes Schindelin076b0ad2006-07-30 20:26:15 +0200415 if (errno == ENOENT)
416 return;
417 die("Entry '%s' not uptodate. Cannot merge.", ce->name);
418}
419
420static void invalidate_ce_path(struct cache_entry *ce)
421{
422 if (ce)
423 cache_tree_invalidate_path(active_cache_tree, ce->name);
424}
425
Sven Verdoolaege0cf73752007-07-17 20:28:28 +0200426/*
427 * Check that checking out ce->sha1 in subdir ce->name is not
428 * going to overwrite any working files.
429 *
430 * Currently, git does not checkout subprojects during a superproject
431 * checkout, so it is not going to overwrite anything.
432 */
433static int verify_clean_submodule(struct cache_entry *ce, const char *action,
434 struct unpack_trees_options *o)
435{
436 return 0;
437}
438
439static int verify_clean_subdirectory(struct cache_entry *ce, const char *action,
Junio C Hamanoc8193532007-03-15 23:25:22 -0700440 struct unpack_trees_options *o)
441{
442 /*
Sven Verdoolaege0cf73752007-07-17 20:28:28 +0200443 * we are about to extract "ce->name"; we would not want to lose
Junio C Hamanoc8193532007-03-15 23:25:22 -0700444 * anything in the existing directory there.
445 */
446 int namelen;
447 int pos, i;
448 struct dir_struct d;
449 char *pathbuf;
450 int cnt = 0;
Sven Verdoolaege0cf73752007-07-17 20:28:28 +0200451 unsigned char sha1[20];
452
453 if (S_ISGITLINK(ntohl(ce->ce_mode)) &&
454 resolve_gitlink_ref(ce->name, "HEAD", sha1) == 0) {
455 /* If we are not going to update the submodule, then
456 * we don't care.
457 */
458 if (!hashcmp(sha1, ce->sha1))
459 return 0;
460 return verify_clean_submodule(ce, action, o);
461 }
Junio C Hamanoc8193532007-03-15 23:25:22 -0700462
463 /*
464 * First let's make sure we do not have a local modification
465 * in that directory.
466 */
Sven Verdoolaege0cf73752007-07-17 20:28:28 +0200467 namelen = strlen(ce->name);
468 pos = cache_name_pos(ce->name, namelen);
Junio C Hamanoc8193532007-03-15 23:25:22 -0700469 if (0 <= pos)
470 return cnt; /* we have it as nondirectory */
471 pos = -pos - 1;
472 for (i = pos; i < active_nr; i++) {
473 struct cache_entry *ce = active_cache[i];
474 int len = ce_namelen(ce);
475 if (len < namelen ||
Sven Verdoolaege0cf73752007-07-17 20:28:28 +0200476 strncmp(ce->name, ce->name, namelen) ||
Junio C Hamanoc8193532007-03-15 23:25:22 -0700477 ce->name[namelen] != '/')
478 break;
479 /*
480 * ce->name is an entry in the subdirectory.
481 */
482 if (!ce_stage(ce)) {
483 verify_uptodate(ce, o);
484 ce->ce_mode = 0;
485 }
486 cnt++;
487 }
488
489 /*
490 * Then we need to make sure that we do not lose a locally
491 * present file that is not ignored.
492 */
493 pathbuf = xmalloc(namelen + 2);
Sven Verdoolaege0cf73752007-07-17 20:28:28 +0200494 memcpy(pathbuf, ce->name, namelen);
Junio C Hamanoc8193532007-03-15 23:25:22 -0700495 strcpy(pathbuf+namelen, "/");
496
497 memset(&d, 0, sizeof(d));
498 if (o->dir)
499 d.exclude_per_dir = o->dir->exclude_per_dir;
Sven Verdoolaege0cf73752007-07-17 20:28:28 +0200500 i = read_directory(&d, ce->name, pathbuf, namelen+1, NULL);
Junio C Hamanoc8193532007-03-15 23:25:22 -0700501 if (i)
502 die("Updating '%s' would lose untracked files in it",
Sven Verdoolaege0cf73752007-07-17 20:28:28 +0200503 ce->name);
Junio C Hamanoc8193532007-03-15 23:25:22 -0700504 free(pathbuf);
505 return cnt;
506}
507
Johannes Schindelin076b0ad2006-07-30 20:26:15 +0200508/*
509 * We do not want to remove or overwrite a working tree file that
Junio C Hamanof8a9d422006-12-04 16:00:46 -0800510 * is not tracked, unless it is ignored.
Johannes Schindelin076b0ad2006-07-30 20:26:15 +0200511 */
Sven Verdoolaege0cf73752007-07-17 20:28:28 +0200512static void verify_absent(struct cache_entry *ce, const char *action,
Johannes Schindelin076b0ad2006-07-30 20:26:15 +0200513 struct unpack_trees_options *o)
514{
515 struct stat st;
516
517 if (o->index_only || o->reset || !o->update)
518 return;
Junio C Hamanoc8193532007-03-15 23:25:22 -0700519
Sven Verdoolaege0cf73752007-07-17 20:28:28 +0200520 if (has_symlink_leading_path(ce->name, NULL))
Junio C Hamanoec0603e2007-07-12 01:04:16 -0700521 return;
522
Sven Verdoolaege0cf73752007-07-17 20:28:28 +0200523 if (!lstat(ce->name, &st)) {
Junio C Hamanoc8193532007-03-15 23:25:22 -0700524 int cnt;
525
Sven Verdoolaege0cf73752007-07-17 20:28:28 +0200526 if (o->dir && excluded(o->dir, ce->name))
Junio C Hamanoc8193532007-03-15 23:25:22 -0700527 /*
Sven Verdoolaege0cf73752007-07-17 20:28:28 +0200528 * ce->name is explicitly excluded, so it is Ok to
Junio C Hamanoc8193532007-03-15 23:25:22 -0700529 * overwrite it.
530 */
531 return;
532 if (S_ISDIR(st.st_mode)) {
533 /*
534 * We are checking out path "foo" and
535 * found "foo/." in the working tree.
536 * This is tricky -- if we have modified
537 * files that are in "foo/" we would lose
538 * it.
539 */
Sven Verdoolaege0cf73752007-07-17 20:28:28 +0200540 cnt = verify_clean_subdirectory(ce, action, o);
Junio C Hamanoc8193532007-03-15 23:25:22 -0700541
542 /*
543 * If this removed entries from the index,
544 * what that means is:
545 *
546 * (1) the caller unpack_trees_rec() saw path/foo
547 * in the index, and it has not removed it because
548 * it thinks it is handling 'path' as blob with
549 * D/F conflict;
550 * (2) we will return "ok, we placed a merged entry
551 * in the index" which would cause o->pos to be
552 * incremented by one;
553 * (3) however, original o->pos now has 'path/foo'
554 * marked with "to be removed".
555 *
556 * We need to increment it by the number of
557 * deleted entries here.
558 */
559 o->pos += cnt;
560 return;
561 }
562
563 /*
564 * The previous round may already have decided to
565 * delete this path, which is in a subdirectory that
566 * is being replaced with a blob.
567 */
Sven Verdoolaege0cf73752007-07-17 20:28:28 +0200568 cnt = cache_name_pos(ce->name, strlen(ce->name));
Junio C Hamanoc8193532007-03-15 23:25:22 -0700569 if (0 <= cnt) {
570 struct cache_entry *ce = active_cache[cnt];
571 if (!ce_stage(ce) && !ce->ce_mode)
572 return;
573 }
574
Johannes Schindelin076b0ad2006-07-30 20:26:15 +0200575 die("Untracked working tree file '%s' "
Sven Verdoolaege0cf73752007-07-17 20:28:28 +0200576 "would be %s by merge.", ce->name, action);
Junio C Hamanoc8193532007-03-15 23:25:22 -0700577 }
Johannes Schindelin076b0ad2006-07-30 20:26:15 +0200578}
579
580static int merged_entry(struct cache_entry *merge, struct cache_entry *old,
581 struct unpack_trees_options *o)
582{
583 merge->ce_flags |= htons(CE_UPDATE);
584 if (old) {
585 /*
586 * See if we can re-use the old CE directly?
587 * That way we get the uptodate stat info.
588 *
589 * This also removes the UPDATE flag on
590 * a match.
591 */
592 if (same(old, merge)) {
593 *merge = *old;
594 } else {
595 verify_uptodate(old, o);
596 invalidate_ce_path(old);
597 }
598 }
599 else {
Sven Verdoolaege0cf73752007-07-17 20:28:28 +0200600 verify_absent(merge, "overwritten", o);
Johannes Schindelin076b0ad2006-07-30 20:26:15 +0200601 invalidate_ce_path(merge);
602 }
603
604 merge->ce_flags &= ~htons(CE_STAGEMASK);
605 add_cache_entry(merge, ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE);
606 return 1;
607}
608
609static int deleted_entry(struct cache_entry *ce, struct cache_entry *old,
610 struct unpack_trees_options *o)
611{
612 if (old)
613 verify_uptodate(old, o);
614 else
Sven Verdoolaege0cf73752007-07-17 20:28:28 +0200615 verify_absent(ce, "removed", o);
Johannes Schindelin076b0ad2006-07-30 20:26:15 +0200616 ce->ce_mode = 0;
617 add_cache_entry(ce, ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE);
618 invalidate_ce_path(ce);
619 return 1;
620}
621
Junio C Hamano7f7932a2007-04-02 00:06:12 -0700622static int keep_entry(struct cache_entry *ce, struct unpack_trees_options *o)
Johannes Schindelin076b0ad2006-07-30 20:26:15 +0200623{
624 add_cache_entry(ce, ADD_CACHE_OK_TO_ADD);
625 return 1;
626}
627
628#if DBRT_DEBUG
629static void show_stage_entry(FILE *o,
630 const char *label, const struct cache_entry *ce)
631{
632 if (!ce)
633 fprintf(o, "%s (missing)\n", label);
634 else
635 fprintf(o, "%s%06o %s %d\t%s\n",
636 label,
637 ntohl(ce->ce_mode),
638 sha1_to_hex(ce->sha1),
639 ce_stage(ce),
640 ce->name);
641}
642#endif
643
644int threeway_merge(struct cache_entry **stages,
Linus Torvaldsb48d5a02007-08-10 12:15:54 -0700645 struct unpack_trees_options *o,
646 int remove)
Johannes Schindelin076b0ad2006-07-30 20:26:15 +0200647{
648 struct cache_entry *index;
649 struct cache_entry *head;
650 struct cache_entry *remote = stages[o->head_idx + 1];
651 int count;
652 int head_match = 0;
653 int remote_match = 0;
Johannes Schindelin076b0ad2006-07-30 20:26:15 +0200654
655 int df_conflict_head = 0;
656 int df_conflict_remote = 0;
657
658 int any_anc_missing = 0;
659 int no_anc_exists = 1;
660 int i;
661
662 for (i = 1; i < o->head_idx; i++) {
Junio C Hamano4c4caaf2007-04-07 05:49:19 -0700663 if (!stages[i] || stages[i] == o->df_conflict_entry)
Johannes Schindelin076b0ad2006-07-30 20:26:15 +0200664 any_anc_missing = 1;
Junio C Hamano4c4caaf2007-04-07 05:49:19 -0700665 else
Johannes Schindelin076b0ad2006-07-30 20:26:15 +0200666 no_anc_exists = 0;
Johannes Schindelin076b0ad2006-07-30 20:26:15 +0200667 }
668
669 index = stages[0];
670 head = stages[o->head_idx];
671
672 if (head == o->df_conflict_entry) {
673 df_conflict_head = 1;
674 head = NULL;
675 }
676
677 if (remote == o->df_conflict_entry) {
678 df_conflict_remote = 1;
679 remote = NULL;
680 }
681
Johannes Schindelin076b0ad2006-07-30 20:26:15 +0200682 /* First, if there's a #16 situation, note that to prevent #13
683 * and #14.
684 */
685 if (!same(remote, head)) {
686 for (i = 1; i < o->head_idx; i++) {
687 if (same(stages[i], head)) {
688 head_match = i;
689 }
690 if (same(stages[i], remote)) {
691 remote_match = i;
692 }
693 }
694 }
695
696 /* We start with cases where the index is allowed to match
697 * something other than the head: #14(ALT) and #2ALT, where it
698 * is permitted to match the result instead.
699 */
700 /* #14, #14ALT, #2ALT */
701 if (remote && !df_conflict_head && head_match && !remote_match) {
702 if (index && !same(index, remote) && !same(index, head))
703 reject_merge(index);
704 return merged_entry(remote, index, o);
705 }
706 /*
707 * If we have an entry in the index cache, then we want to
708 * make sure that it matches head.
709 */
710 if (index && !same(index, head)) {
711 reject_merge(index);
712 }
713
714 if (head) {
715 /* #5ALT, #15 */
716 if (same(head, remote))
717 return merged_entry(head, index, o);
718 /* #13, #3ALT */
719 if (!df_conflict_remote && remote_match && !head_match)
720 return merged_entry(head, index, o);
721 }
722
723 /* #1 */
Linus Torvalds566b5c02007-08-10 12:53:51 -0700724 if (!head && !remote && any_anc_missing) {
725 remove_entry(remove);
Johannes Schindelin076b0ad2006-07-30 20:26:15 +0200726 return 0;
Linus Torvalds566b5c02007-08-10 12:53:51 -0700727 }
Johannes Schindelin076b0ad2006-07-30 20:26:15 +0200728
729 /* Under the new "aggressive" rule, we resolve mostly trivial
730 * cases that we historically had git-merge-one-file resolve.
731 */
732 if (o->aggressive) {
733 int head_deleted = !head && !df_conflict_head;
734 int remote_deleted = !remote && !df_conflict_remote;
Sven Verdoolaege0cf73752007-07-17 20:28:28 +0200735 struct cache_entry *ce = NULL;
Junio C Hamano4c4caaf2007-04-07 05:49:19 -0700736
737 if (index)
Sven Verdoolaege0cf73752007-07-17 20:28:28 +0200738 ce = index;
Junio C Hamano4c4caaf2007-04-07 05:49:19 -0700739 else if (head)
Sven Verdoolaege0cf73752007-07-17 20:28:28 +0200740 ce = head;
Junio C Hamano4c4caaf2007-04-07 05:49:19 -0700741 else if (remote)
Sven Verdoolaege0cf73752007-07-17 20:28:28 +0200742 ce = remote;
Junio C Hamano4c4caaf2007-04-07 05:49:19 -0700743 else {
744 for (i = 1; i < o->head_idx; i++) {
745 if (stages[i] && stages[i] != o->df_conflict_entry) {
Sven Verdoolaege0cf73752007-07-17 20:28:28 +0200746 ce = stages[i];
Junio C Hamano4c4caaf2007-04-07 05:49:19 -0700747 break;
748 }
749 }
750 }
751
Johannes Schindelin076b0ad2006-07-30 20:26:15 +0200752 /*
753 * Deleted in both.
754 * Deleted in one and unchanged in the other.
755 */
756 if ((head_deleted && remote_deleted) ||
757 (head_deleted && remote && remote_match) ||
758 (remote_deleted && head && head_match)) {
Linus Torvalds566b5c02007-08-10 12:53:51 -0700759 remove_entry(remove);
Johannes Schindelin076b0ad2006-07-30 20:26:15 +0200760 if (index)
761 return deleted_entry(index, index, o);
Sven Verdoolaege0cf73752007-07-17 20:28:28 +0200762 else if (ce && !head_deleted)
763 verify_absent(ce, "removed", o);
Johannes Schindelin076b0ad2006-07-30 20:26:15 +0200764 return 0;
765 }
766 /*
767 * Added in both, identically.
768 */
769 if (no_anc_exists && head && remote && same(head, remote))
770 return merged_entry(head, index, o);
771
772 }
773
774 /* Below are "no merge" cases, which require that the index be
775 * up-to-date to avoid the files getting overwritten with
776 * conflict resolution files.
777 */
778 if (index) {
779 verify_uptodate(index, o);
780 }
Johannes Schindelin076b0ad2006-07-30 20:26:15 +0200781
Linus Torvalds566b5c02007-08-10 12:53:51 -0700782 remove_entry(remove);
Johannes Schindelin076b0ad2006-07-30 20:26:15 +0200783 o->nontrivial_merge = 1;
784
Junio C Hamanoea4b52a2007-04-07 05:42:01 -0700785 /* #2, #3, #4, #6, #7, #9, #10, #11. */
Johannes Schindelin076b0ad2006-07-30 20:26:15 +0200786 count = 0;
787 if (!head_match || !remote_match) {
788 for (i = 1; i < o->head_idx; i++) {
Junio C Hamano4c4caaf2007-04-07 05:49:19 -0700789 if (stages[i] && stages[i] != o->df_conflict_entry) {
Junio C Hamano7f7932a2007-04-02 00:06:12 -0700790 keep_entry(stages[i], o);
Johannes Schindelin076b0ad2006-07-30 20:26:15 +0200791 count++;
792 break;
793 }
794 }
795 }
796#if DBRT_DEBUG
797 else {
798 fprintf(stderr, "read-tree: warning #16 detected\n");
799 show_stage_entry(stderr, "head ", stages[head_match]);
800 show_stage_entry(stderr, "remote ", stages[remote_match]);
801 }
802#endif
Junio C Hamano7f7932a2007-04-02 00:06:12 -0700803 if (head) { count += keep_entry(head, o); }
804 if (remote) { count += keep_entry(remote, o); }
Johannes Schindelin076b0ad2006-07-30 20:26:15 +0200805 return count;
806}
807
808/*
809 * Two-way merge.
810 *
811 * The rule is to "carry forward" what is in the index without losing
812 * information across a "fast forward", favoring a successful merge
813 * over a merge failure when it makes sense. For details of the
814 * "carry forward" rule, please see <Documentation/git-read-tree.txt>.
815 *
816 */
817int twoway_merge(struct cache_entry **src,
Linus Torvaldsb48d5a02007-08-10 12:15:54 -0700818 struct unpack_trees_options *o,
819 int remove)
Johannes Schindelin076b0ad2006-07-30 20:26:15 +0200820{
821 struct cache_entry *current = src[0];
Junio C Hamanob8ba1532007-04-02 16:29:56 -0700822 struct cache_entry *oldtree = src[1];
823 struct cache_entry *newtree = src[2];
Johannes Schindelin076b0ad2006-07-30 20:26:15 +0200824
825 if (o->merge_size != 2)
826 return error("Cannot do a twoway merge of %d trees",
827 o->merge_size);
828
Junio C Hamanob8ba1532007-04-02 16:29:56 -0700829 if (oldtree == o->df_conflict_entry)
830 oldtree = NULL;
831 if (newtree == o->df_conflict_entry)
832 newtree = NULL;
833
Johannes Schindelin076b0ad2006-07-30 20:26:15 +0200834 if (current) {
835 if ((!oldtree && !newtree) || /* 4 and 5 */
836 (!oldtree && newtree &&
837 same(current, newtree)) || /* 6 and 7 */
838 (oldtree && newtree &&
839 same(oldtree, newtree)) || /* 14 and 15 */
840 (oldtree && newtree &&
Junio C Hamanob8ba1532007-04-02 16:29:56 -0700841 !same(oldtree, newtree) && /* 18 and 19 */
Johannes Schindelin076b0ad2006-07-30 20:26:15 +0200842 same(current, newtree))) {
Junio C Hamano7f7932a2007-04-02 00:06:12 -0700843 return keep_entry(current, o);
Johannes Schindelin076b0ad2006-07-30 20:26:15 +0200844 }
845 else if (oldtree && !newtree && same(current, oldtree)) {
846 /* 10 or 11 */
Linus Torvaldsd6996762007-08-10 12:31:20 -0700847 remove_entry(remove);
Johannes Schindelin076b0ad2006-07-30 20:26:15 +0200848 return deleted_entry(oldtree, current, o);
849 }
850 else if (oldtree && newtree &&
851 same(current, oldtree) && !same(current, newtree)) {
852 /* 20 or 21 */
853 return merged_entry(newtree, current, o);
854 }
855 else {
856 /* all other failures */
Linus Torvaldsd6996762007-08-10 12:31:20 -0700857 remove_entry(remove);
Johannes Schindelin076b0ad2006-07-30 20:26:15 +0200858 if (oldtree)
859 reject_merge(oldtree);
860 if (current)
861 reject_merge(current);
862 if (newtree)
863 reject_merge(newtree);
864 return -1;
865 }
866 }
867 else if (newtree)
868 return merged_entry(newtree, current, o);
Linus Torvaldsd6996762007-08-10 12:31:20 -0700869 remove_entry(remove);
870 return deleted_entry(oldtree, current, o);
Johannes Schindelin076b0ad2006-07-30 20:26:15 +0200871}
872
873/*
874 * Bind merge.
875 *
876 * Keep the index entries at stage0, collapse stage1 but make sure
877 * stage0 does not have anything there.
878 */
879int bind_merge(struct cache_entry **src,
Linus Torvaldsb48d5a02007-08-10 12:15:54 -0700880 struct unpack_trees_options *o,
881 int remove)
Johannes Schindelin076b0ad2006-07-30 20:26:15 +0200882{
883 struct cache_entry *old = src[0];
884 struct cache_entry *a = src[1];
885
886 if (o->merge_size != 1)
887 return error("Cannot do a bind merge of %d trees\n",
888 o->merge_size);
889 if (a && old)
890 die("Entry '%s' overlaps. Cannot bind.", a->name);
891 if (!a)
Junio C Hamano7f7932a2007-04-02 00:06:12 -0700892 return keep_entry(old, o);
Johannes Schindelin076b0ad2006-07-30 20:26:15 +0200893 else
894 return merged_entry(a, NULL, o);
895}
896
897/*
898 * One-way merge.
899 *
900 * The rule is:
901 * - take the stat information from stage0, take the data from stage1
902 */
903int oneway_merge(struct cache_entry **src,
Linus Torvaldsb48d5a02007-08-10 12:15:54 -0700904 struct unpack_trees_options *o,
905 int remove)
Johannes Schindelin076b0ad2006-07-30 20:26:15 +0200906{
907 struct cache_entry *old = src[0];
908 struct cache_entry *a = src[1];
909
910 if (o->merge_size != 1)
911 return error("Cannot do a oneway merge of %d trees",
912 o->merge_size);
913
Linus Torvalds288f0722007-08-10 12:21:20 -0700914 if (!a) {
915 remove_entry(remove);
Johannes Schindelin076b0ad2006-07-30 20:26:15 +0200916 return deleted_entry(old, old, o);
Linus Torvalds288f0722007-08-10 12:21:20 -0700917 }
Johannes Schindelin076b0ad2006-07-30 20:26:15 +0200918 if (old && same(old, a)) {
919 if (o->reset) {
920 struct stat st;
921 if (lstat(old->name, &st) ||
Junio C Hamano4bd5b7d2007-11-10 00:15:03 -0800922 ce_match_stat(old, &st, CE_MATCH_IGNORE_VALID))
Johannes Schindelin076b0ad2006-07-30 20:26:15 +0200923 old->ce_flags |= htons(CE_UPDATE);
924 }
Junio C Hamano7f7932a2007-04-02 00:06:12 -0700925 return keep_entry(old, o);
Johannes Schindelin076b0ad2006-07-30 20:26:15 +0200926 }
927 return merged_entry(a, old, o);
928}