1 #define NO_THE_INDEX_COMPATIBILITY_MACROS
3 #include "argv-array.h"
4 #include "repository.h"
9 #include "cache-tree.h"
10 #include "unpack-trees.h"
14 #include "split-index.h"
16 #include "submodule.h"
17 #include "submodule-config.h"
18 #include "fsmonitor.h"
19 #include "object-store.h"
20 #include "fetch-object.h"
23 * Error messages expected by scripts out of plumbing commands such as
24 * read-tree. Non-scripted Porcelain is not required to use these messages
25 * and in fact are encouraged to reword them to better suit their particular
26 * situation better. See how "git checkout" and "git merge" replaces
27 * them using setup_unpack_trees_porcelain(), for example.
29 static const char *unpack_plumbing_errors
[NB_UNPACK_TREES_ERROR_TYPES
] = {
30 /* ERROR_WOULD_OVERWRITE */
31 "Entry '%s' would be overwritten by merge. Cannot merge.",
33 /* ERROR_NOT_UPTODATE_FILE */
34 "Entry '%s' not uptodate. Cannot merge.",
36 /* ERROR_NOT_UPTODATE_DIR */
37 "Updating '%s' would lose untracked files in it",
39 /* ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN */
40 "Untracked working tree file '%s' would be overwritten by merge.",
42 /* ERROR_WOULD_LOSE_UNTRACKED_REMOVED */
43 "Untracked working tree file '%s' would be removed by merge.",
45 /* ERROR_BIND_OVERLAP */
46 "Entry '%s' overlaps with '%s'. Cannot bind.",
48 /* ERROR_SPARSE_NOT_UPTODATE_FILE */
49 "Entry '%s' not uptodate. Cannot update sparse checkout.",
51 /* ERROR_WOULD_LOSE_ORPHANED_OVERWRITTEN */
52 "Working tree file '%s' would be overwritten by sparse checkout update.",
54 /* ERROR_WOULD_LOSE_ORPHANED_REMOVED */
55 "Working tree file '%s' would be removed by sparse checkout update.",
57 /* ERROR_WOULD_LOSE_SUBMODULE */
58 "Submodule '%s' cannot checkout new HEAD.",
61 #define ERRORMSG(o,type) \
62 ( ((o) && (o)->msgs[(type)]) \
63 ? ((o)->msgs[(type)]) \
64 : (unpack_plumbing_errors[(type)]) )
66 static const char *super_prefixed(const char *path
)
69 * It is necessary and sufficient to have two static buffers
70 * here, as the return value of this function is fed to
71 * error() using the unpack_*_errors[] templates we see above.
73 static struct strbuf buf
[2] = {STRBUF_INIT
, STRBUF_INIT
};
74 static int super_prefix_len
= -1;
75 static unsigned idx
= ARRAY_SIZE(buf
) - 1;
77 if (super_prefix_len
< 0) {
78 const char *super_prefix
= get_super_prefix();
83 for (i
= 0; i
< ARRAY_SIZE(buf
); i
++)
84 strbuf_addstr(&buf
[i
], super_prefix
);
85 super_prefix_len
= buf
[0].len
;
89 if (!super_prefix_len
)
92 if (++idx
>= ARRAY_SIZE(buf
))
95 strbuf_setlen(&buf
[idx
], super_prefix_len
);
96 strbuf_addstr(&buf
[idx
], path
);
101 void setup_unpack_trees_porcelain(struct unpack_trees_options
*opts
,
105 const char **msgs
= opts
->msgs
;
108 argv_array_init(&opts
->msgs_to_free
);
110 if (!strcmp(cmd
, "checkout"))
111 msg
= advice_commit_before_merge
112 ? _("Your local changes to the following files would be overwritten by checkout:\n%%s"
113 "Please commit your changes or stash them before you switch branches.")
114 : _("Your local changes to the following files would be overwritten by checkout:\n%%s");
115 else if (!strcmp(cmd
, "merge"))
116 msg
= advice_commit_before_merge
117 ? _("Your local changes to the following files would be overwritten by merge:\n%%s"
118 "Please commit your changes or stash them before you merge.")
119 : _("Your local changes to the following files would be overwritten by merge:\n%%s");
121 msg
= advice_commit_before_merge
122 ? _("Your local changes to the following files would be overwritten by %s:\n%%s"
123 "Please commit your changes or stash them before you %s.")
124 : _("Your local changes to the following files would be overwritten by %s:\n%%s");
125 msgs
[ERROR_WOULD_OVERWRITE
] = msgs
[ERROR_NOT_UPTODATE_FILE
] =
126 argv_array_pushf(&opts
->msgs_to_free
, msg
, cmd
, cmd
);
128 msgs
[ERROR_NOT_UPTODATE_DIR
] =
129 _("Updating the following directories would lose untracked files in them:\n%s");
131 if (!strcmp(cmd
, "checkout"))
132 msg
= advice_commit_before_merge
133 ? _("The following untracked working tree files would be removed by checkout:\n%%s"
134 "Please move or remove them before you switch branches.")
135 : _("The following untracked working tree files would be removed by checkout:\n%%s");
136 else if (!strcmp(cmd
, "merge"))
137 msg
= advice_commit_before_merge
138 ? _("The following untracked working tree files would be removed by merge:\n%%s"
139 "Please move or remove them before you merge.")
140 : _("The following untracked working tree files would be removed by merge:\n%%s");
142 msg
= advice_commit_before_merge
143 ? _("The following untracked working tree files would be removed by %s:\n%%s"
144 "Please move or remove them before you %s.")
145 : _("The following untracked working tree files would be removed by %s:\n%%s");
146 msgs
[ERROR_WOULD_LOSE_UNTRACKED_REMOVED
] =
147 argv_array_pushf(&opts
->msgs_to_free
, msg
, cmd
, cmd
);
149 if (!strcmp(cmd
, "checkout"))
150 msg
= advice_commit_before_merge
151 ? _("The following untracked working tree files would be overwritten by checkout:\n%%s"
152 "Please move or remove them before you switch branches.")
153 : _("The following untracked working tree files would be overwritten by checkout:\n%%s");
154 else if (!strcmp(cmd
, "merge"))
155 msg
= advice_commit_before_merge
156 ? _("The following untracked working tree files would be overwritten by merge:\n%%s"
157 "Please move or remove them before you merge.")
158 : _("The following untracked working tree files would be overwritten by merge:\n%%s");
160 msg
= advice_commit_before_merge
161 ? _("The following untracked working tree files would be overwritten by %s:\n%%s"
162 "Please move or remove them before you %s.")
163 : _("The following untracked working tree files would be overwritten by %s:\n%%s");
164 msgs
[ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN
] =
165 argv_array_pushf(&opts
->msgs_to_free
, msg
, cmd
, cmd
);
168 * Special case: ERROR_BIND_OVERLAP refers to a pair of paths, we
169 * cannot easily display it as a list.
171 msgs
[ERROR_BIND_OVERLAP
] = _("Entry '%s' overlaps with '%s'. Cannot bind.");
173 msgs
[ERROR_SPARSE_NOT_UPTODATE_FILE
] =
174 _("Cannot update sparse checkout: the following entries are not up to date:\n%s");
175 msgs
[ERROR_WOULD_LOSE_ORPHANED_OVERWRITTEN
] =
176 _("The following working tree files would be overwritten by sparse checkout update:\n%s");
177 msgs
[ERROR_WOULD_LOSE_ORPHANED_REMOVED
] =
178 _("The following working tree files would be removed by sparse checkout update:\n%s");
179 msgs
[ERROR_WOULD_LOSE_SUBMODULE
] =
180 _("Cannot update submodule:\n%s");
182 opts
->show_all_errors
= 1;
183 /* rejected paths may not have a static buffer */
184 for (i
= 0; i
< ARRAY_SIZE(opts
->unpack_rejects
); i
++)
185 opts
->unpack_rejects
[i
].strdup_strings
= 1;
188 void clear_unpack_trees_porcelain(struct unpack_trees_options
*opts
)
190 argv_array_clear(&opts
->msgs_to_free
);
191 memset(opts
->msgs
, 0, sizeof(opts
->msgs
));
194 static int do_add_entry(struct unpack_trees_options
*o
, struct cache_entry
*ce
,
195 unsigned int set
, unsigned int clear
)
202 ce
->ce_flags
= (ce
->ce_flags
& ~clear
) | set
;
203 return add_index_entry(&o
->result
, ce
,
204 ADD_CACHE_OK_TO_ADD
| ADD_CACHE_OK_TO_REPLACE
);
207 static void add_entry(struct unpack_trees_options
*o
,
208 const struct cache_entry
*ce
,
209 unsigned int set
, unsigned int clear
)
211 do_add_entry(o
, dup_cache_entry(ce
, &o
->result
), set
, clear
);
215 * add error messages on path <path>
216 * corresponding to the type <e> with the message <msg>
217 * indicating if it should be display in porcelain or not
219 static int add_rejected_path(struct unpack_trees_options
*o
,
220 enum unpack_trees_error_types e
,
223 if (!o
->show_all_errors
)
224 return error(ERRORMSG(o
, e
), super_prefixed(path
));
227 * Otherwise, insert in a list for future display by
228 * display_error_msgs()
230 string_list_append(&o
->unpack_rejects
[e
], path
);
235 * display all the error messages stored in a nice way
237 static void display_error_msgs(struct unpack_trees_options
*o
)
240 int something_displayed
= 0;
241 for (e
= 0; e
< NB_UNPACK_TREES_ERROR_TYPES
; e
++) {
242 struct string_list
*rejects
= &o
->unpack_rejects
[e
];
243 if (rejects
->nr
> 0) {
244 struct strbuf path
= STRBUF_INIT
;
245 something_displayed
= 1;
246 for (i
= 0; i
< rejects
->nr
; i
++)
247 strbuf_addf(&path
, "\t%s\n", rejects
->items
[i
].string
);
248 error(ERRORMSG(o
, e
), super_prefixed(path
.buf
));
249 strbuf_release(&path
);
251 string_list_clear(rejects
, 0);
253 if (something_displayed
)
254 fprintf(stderr
, _("Aborting\n"));
257 static int check_submodule_move_head(const struct cache_entry
*ce
,
260 struct unpack_trees_options
*o
)
262 unsigned flags
= SUBMODULE_MOVE_HEAD_DRY_RUN
;
263 const struct submodule
*sub
= submodule_from_ce(ce
);
269 flags
|= SUBMODULE_MOVE_HEAD_FORCE
;
271 if (submodule_move_head(ce
->name
, old_id
, new_id
, flags
))
272 return o
->gently
? -1 :
273 add_rejected_path(o
, ERROR_WOULD_LOSE_SUBMODULE
, ce
->name
);
278 * Preform the loading of the repository's gitmodules file. This function is
279 * used by 'check_update()' to perform loading of the gitmodules file in two
280 * differnt situations:
281 * (1) before removing entries from the working tree if the gitmodules file has
282 * been marked for removal. This situation is specified by 'state' == NULL.
283 * (2) before checking out entries to the working tree if the gitmodules file
284 * has been marked for update. This situation is specified by 'state' != NULL.
286 static void load_gitmodules_file(struct index_state
*index
,
287 struct checkout
*state
)
289 int pos
= index_name_pos(index
, GITMODULES_FILE
, strlen(GITMODULES_FILE
));
292 struct cache_entry
*ce
= index
->cache
[pos
];
293 if (!state
&& ce
->ce_flags
& CE_WT_REMOVE
) {
294 repo_read_gitmodules(the_repository
);
295 } else if (state
&& (ce
->ce_flags
& CE_UPDATE
)) {
296 submodule_free(the_repository
);
297 checkout_entry(ce
, state
, NULL
);
298 repo_read_gitmodules(the_repository
);
303 static struct progress
*get_progress(struct unpack_trees_options
*o
)
305 unsigned cnt
= 0, total
= 0;
306 struct index_state
*index
= &o
->result
;
308 if (!o
->update
|| !o
->verbose_update
)
311 for (; cnt
< index
->cache_nr
; cnt
++) {
312 const struct cache_entry
*ce
= index
->cache
[cnt
];
313 if (ce
->ce_flags
& (CE_UPDATE
| CE_WT_REMOVE
))
317 return start_delayed_progress(_("Checking out files"), total
);
320 static void setup_collided_checkout_detection(struct checkout
*state
,
321 struct index_state
*index
)
326 for (i
= 0; i
< index
->cache_nr
; i
++)
327 index
->cache
[i
]->ce_flags
&= ~CE_MATCHED
;
330 static void report_collided_checkout(struct index_state
*index
)
332 struct string_list list
= STRING_LIST_INIT_NODUP
;
335 for (i
= 0; i
< index
->cache_nr
; i
++) {
336 struct cache_entry
*ce
= index
->cache
[i
];
338 if (!(ce
->ce_flags
& CE_MATCHED
))
341 string_list_append(&list
, ce
->name
);
342 ce
->ce_flags
&= ~CE_MATCHED
;
345 list
.cmp
= fspathcmp
;
346 string_list_sort(&list
);
349 warning(_("the following paths have collided (e.g. case-sensitive paths\n"
350 "on a case-insensitive filesystem) and only one from the same\n"
351 "colliding group is in the working tree:\n"));
353 for (i
= 0; i
< list
.nr
; i
++)
354 fprintf(stderr
, " '%s'\n", list
.items
[i
].string
);
357 string_list_clear(&list
, 0);
360 static int check_updates(struct unpack_trees_options
*o
)
364 struct progress
*progress
;
365 struct index_state
*index
= &o
->result
;
366 struct checkout state
= CHECKOUT_INIT
;
369 trace_performance_enter();
372 state
.refresh_cache
= 1;
373 state
.istate
= index
;
376 setup_collided_checkout_detection(&state
, index
);
378 progress
= get_progress(o
);
381 git_attr_set_direction(GIT_ATTR_CHECKOUT
);
383 if (should_update_submodules() && o
->update
&& !o
->dry_run
)
384 load_gitmodules_file(index
, NULL
);
386 for (i
= 0; i
< index
->cache_nr
; i
++) {
387 const struct cache_entry
*ce
= index
->cache
[i
];
389 if (ce
->ce_flags
& CE_WT_REMOVE
) {
390 display_progress(progress
, ++cnt
);
391 if (o
->update
&& !o
->dry_run
)
395 remove_marked_cache_entries(index
, 0);
396 remove_scheduled_dirs();
398 if (should_update_submodules() && o
->update
&& !o
->dry_run
)
399 load_gitmodules_file(index
, &state
);
401 enable_delayed_checkout(&state
);
402 if (repository_format_partial_clone
&& o
->update
&& !o
->dry_run
) {
404 * Prefetch the objects that are to be checked out in the loop
407 struct oid_array to_fetch
= OID_ARRAY_INIT
;
408 int fetch_if_missing_store
= fetch_if_missing
;
409 fetch_if_missing
= 0;
410 for (i
= 0; i
< index
->cache_nr
; i
++) {
411 struct cache_entry
*ce
= index
->cache
[i
];
412 if ((ce
->ce_flags
& CE_UPDATE
) &&
413 !S_ISGITLINK(ce
->ce_mode
)) {
414 if (!has_object_file(&ce
->oid
))
415 oid_array_append(&to_fetch
, &ce
->oid
);
419 fetch_objects(repository_format_partial_clone
,
420 to_fetch
.oid
, to_fetch
.nr
);
421 fetch_if_missing
= fetch_if_missing_store
;
422 oid_array_clear(&to_fetch
);
424 for (i
= 0; i
< index
->cache_nr
; i
++) {
425 struct cache_entry
*ce
= index
->cache
[i
];
427 if (ce
->ce_flags
& CE_UPDATE
) {
428 if (ce
->ce_flags
& CE_WT_REMOVE
)
429 BUG("both update and delete flags are set on %s",
431 display_progress(progress
, ++cnt
);
432 ce
->ce_flags
&= ~CE_UPDATE
;
433 if (o
->update
&& !o
->dry_run
) {
434 errs
|= checkout_entry(ce
, &state
, NULL
);
438 stop_progress(&progress
);
439 errs
|= finish_delayed_checkout(&state
);
441 git_attr_set_direction(GIT_ATTR_CHECKIN
);
444 report_collided_checkout(index
);
446 trace_performance_leave("check_updates");
450 static int verify_uptodate_sparse(const struct cache_entry
*ce
,
451 struct unpack_trees_options
*o
);
452 static int verify_absent_sparse(const struct cache_entry
*ce
,
453 enum unpack_trees_error_types
,
454 struct unpack_trees_options
*o
);
456 static int apply_sparse_checkout(struct index_state
*istate
,
457 struct cache_entry
*ce
,
458 struct unpack_trees_options
*o
)
460 int was_skip_worktree
= ce_skip_worktree(ce
);
462 if (ce
->ce_flags
& CE_NEW_SKIP_WORKTREE
)
463 ce
->ce_flags
|= CE_SKIP_WORKTREE
;
465 ce
->ce_flags
&= ~CE_SKIP_WORKTREE
;
466 if (was_skip_worktree
!= ce_skip_worktree(ce
)) {
467 ce
->ce_flags
|= CE_UPDATE_IN_BASE
;
468 mark_fsmonitor_invalid(istate
, ce
);
469 istate
->cache_changed
|= CE_ENTRY_CHANGED
;
473 * if (!was_skip_worktree && !ce_skip_worktree()) {
474 * This is perfectly normal. Move on;
479 * Merge strategies may set CE_UPDATE|CE_REMOVE outside checkout
480 * area as a result of ce_skip_worktree() shortcuts in
481 * verify_absent() and verify_uptodate().
482 * Make sure they don't modify worktree if they are already
483 * outside checkout area
485 if (was_skip_worktree
&& ce_skip_worktree(ce
)) {
486 ce
->ce_flags
&= ~CE_UPDATE
;
489 * By default, when CE_REMOVE is on, CE_WT_REMOVE is also
490 * on to get that file removed from both index and worktree.
491 * If that file is already outside worktree area, don't
494 if (ce
->ce_flags
& CE_REMOVE
)
495 ce
->ce_flags
&= ~CE_WT_REMOVE
;
498 if (!was_skip_worktree
&& ce_skip_worktree(ce
)) {
500 * If CE_UPDATE is set, verify_uptodate() must be called already
501 * also stat info may have lost after merged_entry() so calling
502 * verify_uptodate() again may fail
504 if (!(ce
->ce_flags
& CE_UPDATE
) && verify_uptodate_sparse(ce
, o
))
506 ce
->ce_flags
|= CE_WT_REMOVE
;
507 ce
->ce_flags
&= ~CE_UPDATE
;
509 if (was_skip_worktree
&& !ce_skip_worktree(ce
)) {
510 if (verify_absent_sparse(ce
, ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN
, o
))
512 ce
->ce_flags
|= CE_UPDATE
;
517 static inline int call_unpack_fn(const struct cache_entry
* const *src
,
518 struct unpack_trees_options
*o
)
520 int ret
= o
->fn(src
, o
);
526 static void mark_ce_used(struct cache_entry
*ce
, struct unpack_trees_options
*o
)
528 ce
->ce_flags
|= CE_UNPACKED
;
530 if (o
->cache_bottom
< o
->src_index
->cache_nr
&&
531 o
->src_index
->cache
[o
->cache_bottom
] == ce
) {
532 int bottom
= o
->cache_bottom
;
533 while (bottom
< o
->src_index
->cache_nr
&&
534 o
->src_index
->cache
[bottom
]->ce_flags
& CE_UNPACKED
)
536 o
->cache_bottom
= bottom
;
540 static void mark_all_ce_unused(struct index_state
*index
)
543 for (i
= 0; i
< index
->cache_nr
; i
++)
544 index
->cache
[i
]->ce_flags
&= ~(CE_UNPACKED
| CE_ADDED
| CE_NEW_SKIP_WORKTREE
);
547 static int locate_in_src_index(const struct cache_entry
*ce
,
548 struct unpack_trees_options
*o
)
550 struct index_state
*index
= o
->src_index
;
551 int len
= ce_namelen(ce
);
552 int pos
= index_name_pos(index
, ce
->name
, len
);
559 * We call unpack_index_entry() with an unmerged cache entry
560 * only in diff-index, and it wants a single callback. Skip
561 * the other unmerged entry with the same name.
563 static void mark_ce_used_same_name(struct cache_entry
*ce
,
564 struct unpack_trees_options
*o
)
566 struct index_state
*index
= o
->src_index
;
567 int len
= ce_namelen(ce
);
570 for (pos
= locate_in_src_index(ce
, o
); pos
< index
->cache_nr
; pos
++) {
571 struct cache_entry
*next
= index
->cache
[pos
];
572 if (len
!= ce_namelen(next
) ||
573 memcmp(ce
->name
, next
->name
, len
))
575 mark_ce_used(next
, o
);
579 static struct cache_entry
*next_cache_entry(struct unpack_trees_options
*o
)
581 const struct index_state
*index
= o
->src_index
;
582 int pos
= o
->cache_bottom
;
584 while (pos
< index
->cache_nr
) {
585 struct cache_entry
*ce
= index
->cache
[pos
];
586 if (!(ce
->ce_flags
& CE_UNPACKED
))
593 static void add_same_unmerged(const struct cache_entry
*ce
,
594 struct unpack_trees_options
*o
)
596 struct index_state
*index
= o
->src_index
;
597 int len
= ce_namelen(ce
);
598 int pos
= index_name_pos(index
, ce
->name
, len
);
601 die("programming error in a caller of mark_ce_used_same_name");
602 for (pos
= -pos
- 1; pos
< index
->cache_nr
; pos
++) {
603 struct cache_entry
*next
= index
->cache
[pos
];
604 if (len
!= ce_namelen(next
) ||
605 memcmp(ce
->name
, next
->name
, len
))
607 add_entry(o
, next
, 0, 0);
608 mark_ce_used(next
, o
);
612 static int unpack_index_entry(struct cache_entry
*ce
,
613 struct unpack_trees_options
*o
)
615 const struct cache_entry
*src
[MAX_UNPACK_TREES
+ 1] = { NULL
, };
622 if (o
->skip_unmerged
) {
623 add_entry(o
, ce
, 0, 0);
627 ret
= call_unpack_fn(src
, o
);
629 mark_ce_used_same_name(ce
, o
);
633 static int find_cache_pos(struct traverse_info
*, const struct name_entry
*);
635 static void restore_cache_bottom(struct traverse_info
*info
, int bottom
)
637 struct unpack_trees_options
*o
= info
->data
;
639 if (o
->diff_index_cached
)
641 o
->cache_bottom
= bottom
;
644 static int switch_cache_bottom(struct traverse_info
*info
)
646 struct unpack_trees_options
*o
= info
->data
;
649 if (o
->diff_index_cached
)
651 ret
= o
->cache_bottom
;
652 pos
= find_cache_pos(info
->prev
, &info
->name
);
655 o
->cache_bottom
= -2 - pos
;
657 o
->cache_bottom
= o
->src_index
->cache_nr
;
661 static inline int are_same_oid(struct name_entry
*name_j
, struct name_entry
*name_k
)
663 return name_j
->oid
&& name_k
->oid
&& oideq(name_j
->oid
, name_k
->oid
);
666 static int all_trees_same_as_cache_tree(int n
, unsigned long dirmask
,
667 struct name_entry
*names
,
668 struct traverse_info
*info
)
670 struct unpack_trees_options
*o
= info
->data
;
673 if (!o
->merge
|| dirmask
!= ((1 << n
) - 1))
676 for (i
= 1; i
< n
; i
++)
677 if (!are_same_oid(names
, names
+ i
))
680 return cache_tree_matches_traversal(o
->src_index
->cache_tree
, names
, info
);
683 static int index_pos_by_traverse_info(struct name_entry
*names
,
684 struct traverse_info
*info
)
686 struct unpack_trees_options
*o
= info
->data
;
687 int len
= traverse_path_len(info
, names
);
688 char *name
= xmalloc(len
+ 1 /* slash */ + 1 /* NUL */);
691 make_traverse_path(name
, info
, names
);
694 pos
= index_name_pos(o
->src_index
, name
, len
);
696 BUG("This is a directory and should not exist in index");
698 if (!starts_with(o
->src_index
->cache
[pos
]->name
, name
) ||
699 (pos
> 0 && starts_with(o
->src_index
->cache
[pos
-1]->name
, name
)))
700 BUG("pos must point at the first entry in this directory");
706 * Fast path if we detect that all trees are the same as cache-tree at this
707 * path. We'll walk these trees in an iterative loop using cache-tree/index
708 * instead of ODB since we already know what these trees contain.
710 static int traverse_by_cache_tree(int pos
, int nr_entries
, int nr_names
,
711 struct name_entry
*names
,
712 struct traverse_info
*info
)
714 struct cache_entry
*src
[MAX_UNPACK_TREES
+ 1] = { NULL
, };
715 struct unpack_trees_options
*o
= info
->data
;
716 struct cache_entry
*tree_ce
= NULL
;
721 BUG("We need cache-tree to do this optimization");
724 * Do what unpack_callback() and unpack_nondirectories() normally
725 * do. But we walk all paths in an iterative loop instead.
727 * D/F conflicts and higher stage entries are not a concern
728 * because cache-tree would be invalidated and we would never
729 * get here in the first place.
731 for (i
= 0; i
< nr_entries
; i
++) {
732 int new_ce_len
, len
, rc
;
734 src
[0] = o
->src_index
->cache
[pos
+ i
];
736 len
= ce_namelen(src
[0]);
737 new_ce_len
= cache_entry_size(len
);
739 if (new_ce_len
> ce_len
) {
741 tree_ce
= xrealloc(tree_ce
, new_ce_len
);
742 memset(tree_ce
, 0, new_ce_len
);
745 tree_ce
->ce_flags
= create_ce_flags(0);
747 for (d
= 1; d
<= nr_names
; d
++)
751 tree_ce
->ce_mode
= src
[0]->ce_mode
;
752 tree_ce
->ce_namelen
= len
;
753 oidcpy(&tree_ce
->oid
, &src
[0]->oid
);
754 memcpy(tree_ce
->name
, src
[0]->name
, len
+ 1);
756 rc
= call_unpack_fn((const struct cache_entry
* const *)src
, o
);
762 mark_ce_used(src
[0], o
);
766 printf("Unpacked %d entries from %s to %s using cache-tree\n",
768 o
->src_index
->cache
[pos
]->name
,
769 o
->src_index
->cache
[pos
+ nr_entries
- 1]->name
);
773 static int traverse_trees_recursive(int n
, unsigned long dirmask
,
774 unsigned long df_conflicts
,
775 struct name_entry
*names
,
776 struct traverse_info
*info
)
780 struct tree_desc t
[MAX_UNPACK_TREES
];
781 void *buf
[MAX_UNPACK_TREES
];
782 struct traverse_info newinfo
;
783 struct name_entry
*p
;
786 nr_entries
= all_trees_same_as_cache_tree(n
, dirmask
, names
, info
);
787 if (nr_entries
> 0) {
788 struct unpack_trees_options
*o
= info
->data
;
789 int pos
= index_pos_by_traverse_info(names
, info
);
791 if (!o
->merge
|| df_conflicts
)
792 BUG("Wrong condition to get here buddy");
795 * All entries up to 'pos' must have been processed
796 * (i.e. marked CE_UNPACKED) at this point. But to be safe,
797 * save and restore cache_bottom anyway to not miss
798 * unprocessed entries before 'pos'.
800 bottom
= o
->cache_bottom
;
801 ret
= traverse_by_cache_tree(pos
, nr_entries
, n
, names
, info
);
802 o
->cache_bottom
= bottom
;
812 newinfo
.pathspec
= info
->pathspec
;
814 newinfo
.pathlen
+= tree_entry_len(p
) + 1;
815 newinfo
.df_conflicts
|= df_conflicts
;
818 * Fetch the tree from the ODB for each peer directory in the
821 * For 2- and 3-way traversals, we try to avoid hitting the
822 * ODB twice for the same OID. This should yield a nice speed
823 * up in checkouts and merges when the commits are similar.
825 * We don't bother doing the full O(n^2) search for larger n,
826 * because wider traversals don't happen that often and we
827 * avoid the search setup.
829 * When 2 peer OIDs are the same, we just copy the tree
830 * descriptor data. This implicitly borrows the buffer
831 * data from the earlier cell.
833 for (i
= 0; i
< n
; i
++, dirmask
>>= 1) {
834 if (i
> 0 && are_same_oid(&names
[i
], &names
[i
- 1]))
836 else if (i
> 1 && are_same_oid(&names
[i
], &names
[i
- 2]))
839 const struct object_id
*oid
= NULL
;
842 buf
[nr_buf
++] = fill_tree_descriptor(t
+ i
, oid
);
846 bottom
= switch_cache_bottom(&newinfo
);
847 ret
= traverse_trees(n
, t
, &newinfo
);
848 restore_cache_bottom(&newinfo
, bottom
);
850 for (i
= 0; i
< nr_buf
; i
++)
857 * Compare the traverse-path to the cache entry without actually
858 * having to generate the textual representation of the traverse
861 * NOTE! This *only* compares up to the size of the traverse path
862 * itself - the caller needs to do the final check for the cache
863 * entry having more data at the end!
865 static int do_compare_entry_piecewise(const struct cache_entry
*ce
, const struct traverse_info
*info
, const struct name_entry
*n
)
867 int len
, pathlen
, ce_len
;
871 int cmp
= do_compare_entry_piecewise(ce
, info
->prev
,
876 pathlen
= info
->pathlen
;
877 ce_len
= ce_namelen(ce
);
879 /* If ce_len < pathlen then we must have previously hit "name == directory" entry */
880 if (ce_len
< pathlen
)
884 ce_name
= ce
->name
+ pathlen
;
886 len
= tree_entry_len(n
);
887 return df_name_compare(ce_name
, ce_len
, S_IFREG
, n
->path
, len
, n
->mode
);
890 static int do_compare_entry(const struct cache_entry
*ce
,
891 const struct traverse_info
*info
,
892 const struct name_entry
*n
)
894 int len
, pathlen
, ce_len
;
899 * If we have not precomputed the traverse path, it is quicker
900 * to avoid doing so. But if we have precomputed it,
901 * it is quicker to use the precomputed version.
903 if (!info
->traverse_path
)
904 return do_compare_entry_piecewise(ce
, info
, n
);
906 cmp
= strncmp(ce
->name
, info
->traverse_path
, info
->pathlen
);
910 pathlen
= info
->pathlen
;
911 ce_len
= ce_namelen(ce
);
913 if (ce_len
< pathlen
)
917 ce_name
= ce
->name
+ pathlen
;
919 len
= tree_entry_len(n
);
920 return df_name_compare(ce_name
, ce_len
, S_IFREG
, n
->path
, len
, n
->mode
);
923 static int compare_entry(const struct cache_entry
*ce
, const struct traverse_info
*info
, const struct name_entry
*n
)
925 int cmp
= do_compare_entry(ce
, info
, n
);
930 * Even if the beginning compared identically, the ce should
931 * compare as bigger than a directory leading up to it!
933 return ce_namelen(ce
) > traverse_path_len(info
, n
);
936 static int ce_in_traverse_path(const struct cache_entry
*ce
,
937 const struct traverse_info
*info
)
941 if (do_compare_entry(ce
, info
->prev
, &info
->name
))
944 * If ce (blob) is the same name as the path (which is a tree
945 * we will be descending into), it won't be inside it.
947 return (info
->pathlen
< ce_namelen(ce
));
950 static struct cache_entry
*create_ce_entry(const struct traverse_info
*info
,
951 const struct name_entry
*n
,
953 struct index_state
*istate
,
956 int len
= traverse_path_len(info
, n
);
957 struct cache_entry
*ce
=
959 make_empty_transient_cache_entry(len
) :
960 make_empty_cache_entry(istate
, len
);
962 ce
->ce_mode
= create_ce_mode(n
->mode
);
963 ce
->ce_flags
= create_ce_flags(stage
);
964 ce
->ce_namelen
= len
;
965 oidcpy(&ce
->oid
, n
->oid
);
966 make_traverse_path(ce
->name
, info
, n
);
972 * Note that traverse_by_cache_tree() duplicates some logic in this function
973 * without actually calling it. If you change the logic here you may need to
974 * check and change there as well.
976 static int unpack_nondirectories(int n
, unsigned long mask
,
977 unsigned long dirmask
,
978 struct cache_entry
**src
,
979 const struct name_entry
*names
,
980 const struct traverse_info
*info
)
983 struct unpack_trees_options
*o
= info
->data
;
984 unsigned long conflicts
= info
->df_conflicts
| dirmask
;
986 /* Do we have *only* directories? Nothing to do */
987 if (mask
== dirmask
&& !src
[0])
991 * Ok, we've filled in up to any potential index entry in src[0],
994 for (i
= 0; i
< n
; i
++) {
996 unsigned int bit
= 1ul << i
;
997 if (conflicts
& bit
) {
998 src
[i
+ o
->merge
] = o
->df_conflict_entry
;
1005 else if (i
+ 1 < o
->head_idx
)
1007 else if (i
+ 1 > o
->head_idx
)
1013 * If the merge bit is set, then the cache entries are
1014 * discarded in the following block. In this case,
1015 * construct "transient" cache_entries, as they are
1016 * not stored in the index. otherwise construct the
1017 * cache entry from the index aware logic.
1019 src
[i
+ o
->merge
] = create_ce_entry(info
, names
+ i
, stage
, &o
->result
, o
->merge
);
1023 int rc
= call_unpack_fn((const struct cache_entry
* const *)src
,
1025 for (i
= 0; i
< n
; i
++) {
1026 struct cache_entry
*ce
= src
[i
+ o
->merge
];
1027 if (ce
!= o
->df_conflict_entry
)
1028 discard_cache_entry(ce
);
1033 for (i
= 0; i
< n
; i
++)
1034 if (src
[i
] && src
[i
] != o
->df_conflict_entry
)
1035 if (do_add_entry(o
, src
[i
], 0, 0))
1041 static int unpack_failed(struct unpack_trees_options
*o
, const char *message
)
1043 discard_index(&o
->result
);
1044 if (!o
->gently
&& !o
->exiting_early
) {
1046 return error("%s", message
);
1053 * The tree traversal is looking at name p. If we have a matching entry,
1054 * return it. If name p is a directory in the index, do not return
1055 * anything, as we will want to match it when the traversal descends into
1058 static int find_cache_pos(struct traverse_info
*info
,
1059 const struct name_entry
*p
)
1062 struct unpack_trees_options
*o
= info
->data
;
1063 struct index_state
*index
= o
->src_index
;
1064 int pfxlen
= info
->pathlen
;
1065 int p_len
= tree_entry_len(p
);
1067 for (pos
= o
->cache_bottom
; pos
< index
->cache_nr
; pos
++) {
1068 const struct cache_entry
*ce
= index
->cache
[pos
];
1069 const char *ce_name
, *ce_slash
;
1072 if (ce
->ce_flags
& CE_UNPACKED
) {
1074 * cache_bottom entry is already unpacked, so
1075 * we can never match it; don't check it
1078 if (pos
== o
->cache_bottom
)
1082 if (!ce_in_traverse_path(ce
, info
)) {
1084 * Check if we can skip future cache checks
1085 * (because we're already past all possible
1086 * entries in the traverse path).
1088 if (info
->traverse_path
) {
1089 if (strncmp(ce
->name
, info
->traverse_path
,
1095 ce_name
= ce
->name
+ pfxlen
;
1096 ce_slash
= strchr(ce_name
, '/');
1098 ce_len
= ce_slash
- ce_name
;
1100 ce_len
= ce_namelen(ce
) - pfxlen
;
1101 cmp
= name_compare(p
->path
, p_len
, ce_name
, ce_len
);
1103 * Exact match; if we have a directory we need to
1104 * delay returning it.
1107 return ce_slash
? -2 - pos
: pos
;
1109 continue; /* keep looking */
1111 * ce_name sorts after p->path; could it be that we
1112 * have files under p->path directory in the index?
1113 * E.g. ce_name == "t-i", and p->path == "t"; we may
1114 * have "t/a" in the index.
1116 if (p_len
< ce_len
&& !memcmp(ce_name
, p
->path
, p_len
) &&
1117 ce_name
[p_len
] < '/')
1118 continue; /* keep looking */
1124 static struct cache_entry
*find_cache_entry(struct traverse_info
*info
,
1125 const struct name_entry
*p
)
1127 int pos
= find_cache_pos(info
, p
);
1128 struct unpack_trees_options
*o
= info
->data
;
1131 return o
->src_index
->cache
[pos
];
1136 static void debug_path(struct traverse_info
*info
)
1139 debug_path(info
->prev
);
1140 if (*info
->prev
->name
.path
)
1143 printf("%s", info
->name
.path
);
1146 static void debug_name_entry(int i
, struct name_entry
*n
)
1148 printf("ent#%d %06o %s\n", i
,
1149 n
->path
? n
->mode
: 0,
1150 n
->path
? n
->path
: "(missing)");
1153 static void debug_unpack_callback(int n
,
1155 unsigned long dirmask
,
1156 struct name_entry
*names
,
1157 struct traverse_info
*info
)
1160 printf("* unpack mask %lu, dirmask %lu, cnt %d ",
1164 for (i
= 0; i
< n
; i
++)
1165 debug_name_entry(i
, names
+ i
);
1169 * Note that traverse_by_cache_tree() duplicates some logic in this function
1170 * without actually calling it. If you change the logic here you may need to
1171 * check and change there as well.
1173 static int unpack_callback(int n
, unsigned long mask
, unsigned long dirmask
, struct name_entry
*names
, struct traverse_info
*info
)
1175 struct cache_entry
*src
[MAX_UNPACK_TREES
+ 1] = { NULL
, };
1176 struct unpack_trees_options
*o
= info
->data
;
1177 const struct name_entry
*p
= names
;
1179 /* Find first entry with a real name (we could use "mask" too) */
1183 if (o
->debug_unpack
)
1184 debug_unpack_callback(n
, mask
, dirmask
, names
, info
);
1186 /* Are we supposed to look at the index too? */
1190 struct cache_entry
*ce
;
1192 if (o
->diff_index_cached
)
1193 ce
= next_cache_entry(o
);
1195 ce
= find_cache_entry(info
, p
);
1199 cmp
= compare_entry(ce
, info
, p
);
1201 if (unpack_index_entry(ce
, o
) < 0)
1202 return unpack_failed(o
, NULL
);
1208 * If we skip unmerged index
1209 * entries, we'll skip this
1210 * entry *and* the tree
1211 * entries associated with it!
1213 if (o
->skip_unmerged
) {
1214 add_same_unmerged(ce
, o
);
1224 if (unpack_nondirectories(n
, mask
, dirmask
, src
, names
, info
) < 0)
1227 if (o
->merge
&& src
[0]) {
1228 if (ce_stage(src
[0]))
1229 mark_ce_used_same_name(src
[0], o
);
1231 mark_ce_used(src
[0], o
);
1234 /* Now handle any directories.. */
1236 /* special case: "diff-index --cached" looking at a tree */
1237 if (o
->diff_index_cached
&&
1238 n
== 1 && dirmask
== 1 && S_ISDIR(names
->mode
)) {
1240 matches
= cache_tree_matches_traversal(o
->src_index
->cache_tree
,
1243 * Everything under the name matches; skip the
1244 * entire hierarchy. diff_index_cached codepath
1245 * special cases D/F conflicts in such a way that
1246 * it does not do any look-ahead, so this is safe.
1249 o
->cache_bottom
+= matches
;
1254 if (traverse_trees_recursive(n
, dirmask
, mask
& ~dirmask
,
1263 static int clear_ce_flags_1(struct index_state
*istate
,
1264 struct cache_entry
**cache
, int nr
,
1265 struct strbuf
*prefix
,
1266 int select_mask
, int clear_mask
,
1267 struct exclude_list
*el
, int defval
);
1269 /* Whole directory matching */
1270 static int clear_ce_flags_dir(struct index_state
*istate
,
1271 struct cache_entry
**cache
, int nr
,
1272 struct strbuf
*prefix
,
1274 int select_mask
, int clear_mask
,
1275 struct exclude_list
*el
, int defval
)
1277 struct cache_entry
**cache_end
;
1279 int ret
= is_excluded_from_list(prefix
->buf
, prefix
->len
,
1280 basename
, &dtype
, el
, istate
);
1283 strbuf_addch(prefix
, '/');
1285 /* If undecided, use matching result of parent dir in defval */
1289 for (cache_end
= cache
; cache_end
!= cache
+ nr
; cache_end
++) {
1290 struct cache_entry
*ce
= *cache_end
;
1291 if (strncmp(ce
->name
, prefix
->buf
, prefix
->len
))
1296 * TODO: check el, if there are no patterns that may conflict
1297 * with ret (iow, we know in advance the incl/excl
1298 * decision for the entire directory), clear flag here without
1299 * calling clear_ce_flags_1(). That function will call
1300 * the expensive is_excluded_from_list() on every entry.
1302 rc
= clear_ce_flags_1(istate
, cache
, cache_end
- cache
,
1304 select_mask
, clear_mask
,
1306 strbuf_setlen(prefix
, prefix
->len
- 1);
1311 * Traverse the index, find every entry that matches according to
1312 * o->el. Do "ce_flags &= ~clear_mask" on those entries. Return the
1313 * number of traversed entries.
1315 * If select_mask is non-zero, only entries whose ce_flags has on of
1316 * those bits enabled are traversed.
1318 * cache : pointer to an index entry
1319 * prefix_len : an offset to its path
1321 * The current path ("prefix") including the trailing '/' is
1322 * cache[0]->name[0..(prefix_len-1)]
1323 * Top level path has prefix_len zero.
1325 static int clear_ce_flags_1(struct index_state
*istate
,
1326 struct cache_entry
**cache
, int nr
,
1327 struct strbuf
*prefix
,
1328 int select_mask
, int clear_mask
,
1329 struct exclude_list
*el
, int defval
)
1331 struct cache_entry
**cache_end
= cache
+ nr
;
1334 * Process all entries that have the given prefix and meet
1335 * select_mask condition
1337 while(cache
!= cache_end
) {
1338 struct cache_entry
*ce
= *cache
;
1339 const char *name
, *slash
;
1340 int len
, dtype
, ret
;
1342 if (select_mask
&& !(ce
->ce_flags
& select_mask
)) {
1347 if (prefix
->len
&& strncmp(ce
->name
, prefix
->buf
, prefix
->len
))
1350 name
= ce
->name
+ prefix
->len
;
1351 slash
= strchr(name
, '/');
1353 /* If it's a directory, try whole directory match first */
1358 strbuf_add(prefix
, name
, len
);
1360 processed
= clear_ce_flags_dir(istate
, cache
, cache_end
- cache
,
1362 prefix
->buf
+ prefix
->len
- len
,
1363 select_mask
, clear_mask
,
1366 /* clear_c_f_dir eats a whole dir already? */
1369 strbuf_setlen(prefix
, prefix
->len
- len
);
1373 strbuf_addch(prefix
, '/');
1374 cache
+= clear_ce_flags_1(istate
, cache
, cache_end
- cache
,
1376 select_mask
, clear_mask
, el
, defval
);
1377 strbuf_setlen(prefix
, prefix
->len
- len
- 1);
1382 dtype
= ce_to_dtype(ce
);
1383 ret
= is_excluded_from_list(ce
->name
, ce_namelen(ce
),
1384 name
, &dtype
, el
, istate
);
1388 ce
->ce_flags
&= ~clear_mask
;
1391 return nr
- (cache_end
- cache
);
1394 static int clear_ce_flags(struct index_state
*istate
,
1395 int select_mask
, int clear_mask
,
1396 struct exclude_list
*el
)
1398 static struct strbuf prefix
= STRBUF_INIT
;
1400 strbuf_reset(&prefix
);
1402 return clear_ce_flags_1(istate
,
1406 select_mask
, clear_mask
,
1411 * Set/Clear CE_NEW_SKIP_WORKTREE according to $GIT_DIR/info/sparse-checkout
1413 static void mark_new_skip_worktree(struct exclude_list
*el
,
1414 struct index_state
*istate
,
1415 int select_flag
, int skip_wt_flag
)
1420 * 1. Pretend the narrowest worktree: only unmerged entries
1423 for (i
= 0; i
< istate
->cache_nr
; i
++) {
1424 struct cache_entry
*ce
= istate
->cache
[i
];
1426 if (select_flag
&& !(ce
->ce_flags
& select_flag
))
1429 if (!ce_stage(ce
) && !(ce
->ce_flags
& CE_CONFLICTED
))
1430 ce
->ce_flags
|= skip_wt_flag
;
1432 ce
->ce_flags
&= ~skip_wt_flag
;
1436 * 2. Widen worktree according to sparse-checkout file.
1437 * Matched entries will have skip_wt_flag cleared (i.e. "in")
1439 clear_ce_flags(istate
, select_flag
, skip_wt_flag
, el
);
1442 static int verify_absent(const struct cache_entry
*,
1443 enum unpack_trees_error_types
,
1444 struct unpack_trees_options
*);
1446 * N-way merge "len" trees. Returns 0 on success, -1 on failure to manipulate the
1447 * resulting index, -2 on failure to reflect the changes to the work tree.
1449 * CE_ADDED, CE_UNPACKED and CE_NEW_SKIP_WORKTREE are used internally
1451 int unpack_trees(unsigned len
, struct tree_desc
*t
, struct unpack_trees_options
*o
)
1454 static struct cache_entry
*dfc
;
1455 struct exclude_list el
;
1457 if (len
> MAX_UNPACK_TREES
)
1458 die("unpack_trees takes at most %d trees", MAX_UNPACK_TREES
);
1460 trace_performance_enter();
1461 memset(&el
, 0, sizeof(el
));
1462 if (!core_apply_sparse_checkout
|| !o
->update
)
1463 o
->skip_sparse_checkout
= 1;
1464 if (!o
->skip_sparse_checkout
) {
1465 char *sparse
= git_pathdup("info/sparse-checkout");
1466 if (add_excludes_from_file_to_list(sparse
, "", 0, &el
, NULL
) < 0)
1467 o
->skip_sparse_checkout
= 1;
1473 memset(&o
->result
, 0, sizeof(o
->result
));
1474 o
->result
.initialized
= 1;
1475 o
->result
.timestamp
.sec
= o
->src_index
->timestamp
.sec
;
1476 o
->result
.timestamp
.nsec
= o
->src_index
->timestamp
.nsec
;
1477 o
->result
.version
= o
->src_index
->version
;
1478 if (!o
->src_index
->split_index
) {
1479 o
->result
.split_index
= NULL
;
1480 } else if (o
->src_index
== o
->dst_index
) {
1482 * o->dst_index (and thus o->src_index) will be discarded
1483 * and overwritten with o->result at the end of this function,
1484 * so just use src_index's split_index to avoid having to
1487 o
->result
.split_index
= o
->src_index
->split_index
;
1488 o
->result
.split_index
->refcount
++;
1490 o
->result
.split_index
= init_split_index(&o
->result
);
1492 oidcpy(&o
->result
.oid
, &o
->src_index
->oid
);
1493 o
->merge_size
= len
;
1494 mark_all_ce_unused(o
->src_index
);
1497 * Sparse checkout loop #1: set NEW_SKIP_WORKTREE on existing entries
1499 if (!o
->skip_sparse_checkout
)
1500 mark_new_skip_worktree(o
->el
, o
->src_index
, 0, CE_NEW_SKIP_WORKTREE
);
1503 dfc
= xcalloc(1, cache_entry_size(0));
1504 o
->df_conflict_entry
= dfc
;
1507 const char *prefix
= o
->prefix
? o
->prefix
: "";
1508 struct traverse_info info
;
1510 setup_traverse_info(&info
, prefix
);
1511 info
.fn
= unpack_callback
;
1513 info
.show_all_errors
= o
->show_all_errors
;
1514 info
.pathspec
= o
->pathspec
;
1518 * Unpack existing index entries that sort before the
1519 * prefix the tree is spliced into. Note that o->merge
1520 * is always true in this case.
1523 struct cache_entry
*ce
= next_cache_entry(o
);
1526 if (ce_in_traverse_path(ce
, &info
))
1528 if (unpack_index_entry(ce
, o
) < 0)
1533 trace_performance_enter();
1534 ret
= traverse_trees(len
, t
, &info
);
1535 trace_performance_leave("traverse_trees");
1540 /* Any left-over entries in the index? */
1543 struct cache_entry
*ce
= next_cache_entry(o
);
1546 if (unpack_index_entry(ce
, o
) < 0)
1550 mark_all_ce_unused(o
->src_index
);
1552 if (o
->trivial_merges_only
&& o
->nontrivial_merge
) {
1553 ret
= unpack_failed(o
, "Merge requires file-level merging");
1557 if (!o
->skip_sparse_checkout
) {
1558 int empty_worktree
= 1;
1561 * Sparse checkout loop #2: set NEW_SKIP_WORKTREE on entries not in loop #1
1562 * If the will have NEW_SKIP_WORKTREE, also set CE_SKIP_WORKTREE
1563 * so apply_sparse_checkout() won't attempt to remove it from worktree
1565 mark_new_skip_worktree(o
->el
, &o
->result
, CE_ADDED
, CE_SKIP_WORKTREE
| CE_NEW_SKIP_WORKTREE
);
1568 for (i
= 0; i
< o
->result
.cache_nr
; i
++) {
1569 struct cache_entry
*ce
= o
->result
.cache
[i
];
1572 * Entries marked with CE_ADDED in merged_entry() do not have
1573 * verify_absent() check (the check is effectively disabled
1574 * because CE_NEW_SKIP_WORKTREE is set unconditionally).
1576 * Do the real check now because we have had
1577 * correct CE_NEW_SKIP_WORKTREE
1579 if (ce
->ce_flags
& CE_ADDED
&&
1580 verify_absent(ce
, ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN
, o
)) {
1581 if (!o
->show_all_errors
)
1586 if (apply_sparse_checkout(&o
->result
, ce
, o
)) {
1587 if (!o
->show_all_errors
)
1591 if (!ce_skip_worktree(ce
))
1598 * Sparse checkout is meant to narrow down checkout area
1599 * but it does not make sense to narrow down to empty working
1600 * tree. This is usually a mistake in sparse checkout rules.
1601 * Do not allow users to do that.
1603 if (o
->result
.cache_nr
&& empty_worktree
) {
1604 ret
= unpack_failed(o
, "Sparse checkout leaves no entry on working directory");
1609 ret
= check_updates(o
) ? (-2) : 0;
1611 move_index_extensions(&o
->result
, o
->src_index
);
1613 if (git_env_bool("GIT_TEST_CHECK_CACHE_TREE", 0))
1614 cache_tree_verify(&o
->result
);
1615 if (!o
->result
.cache_tree
)
1616 o
->result
.cache_tree
= cache_tree();
1617 if (!cache_tree_fully_valid(o
->result
.cache_tree
))
1618 cache_tree_update(&o
->result
,
1622 discard_index(o
->dst_index
);
1623 *o
->dst_index
= o
->result
;
1625 discard_index(&o
->result
);
1627 o
->src_index
= NULL
;
1630 trace_performance_leave("unpack_trees");
1631 clear_exclude_list(&el
);
1635 if (o
->show_all_errors
)
1636 display_error_msgs(o
);
1637 mark_all_ce_unused(o
->src_index
);
1638 ret
= unpack_failed(o
, NULL
);
1639 if (o
->exiting_early
)
1644 /* Here come the merge functions */
1646 static int reject_merge(const struct cache_entry
*ce
,
1647 struct unpack_trees_options
*o
)
1649 return o
->gently
? -1 :
1650 add_rejected_path(o
, ERROR_WOULD_OVERWRITE
, ce
->name
);
1653 static int same(const struct cache_entry
*a
, const struct cache_entry
*b
)
1659 if ((a
->ce_flags
| b
->ce_flags
) & CE_CONFLICTED
)
1661 return a
->ce_mode
== b
->ce_mode
&&
1662 oideq(&a
->oid
, &b
->oid
);
1667 * When a CE gets turned into an unmerged entry, we
1668 * want it to be up-to-date
1670 static int verify_uptodate_1(const struct cache_entry
*ce
,
1671 struct unpack_trees_options
*o
,
1672 enum unpack_trees_error_types error_type
)
1680 * CE_VALID and CE_SKIP_WORKTREE cheat, we better check again
1681 * if this entry is truly up-to-date because this file may be
1684 if ((ce
->ce_flags
& CE_VALID
) || ce_skip_worktree(ce
))
1685 ; /* keep checking */
1686 else if (o
->reset
|| ce_uptodate(ce
))
1689 if (!lstat(ce
->name
, &st
)) {
1690 int flags
= CE_MATCH_IGNORE_VALID
|CE_MATCH_IGNORE_SKIP_WORKTREE
;
1691 unsigned changed
= ie_match_stat(o
->src_index
, ce
, &st
, flags
);
1693 if (submodule_from_ce(ce
)) {
1694 int r
= check_submodule_move_head(ce
,
1695 "HEAD", oid_to_hex(&ce
->oid
), o
);
1697 return o
->gently
? -1 :
1698 add_rejected_path(o
, error_type
, ce
->name
);
1705 * Historic default policy was to allow submodule to be out
1706 * of sync wrt the superproject index. If the submodule was
1707 * not considered interesting above, we don't care here.
1709 if (S_ISGITLINK(ce
->ce_mode
))
1714 if (errno
== ENOENT
)
1716 return o
->gently
? -1 :
1717 add_rejected_path(o
, error_type
, ce
->name
);
1720 int verify_uptodate(const struct cache_entry
*ce
,
1721 struct unpack_trees_options
*o
)
1723 if (!o
->skip_sparse_checkout
&& (ce
->ce_flags
& CE_NEW_SKIP_WORKTREE
))
1725 return verify_uptodate_1(ce
, o
, ERROR_NOT_UPTODATE_FILE
);
1728 static int verify_uptodate_sparse(const struct cache_entry
*ce
,
1729 struct unpack_trees_options
*o
)
1731 return verify_uptodate_1(ce
, o
, ERROR_SPARSE_NOT_UPTODATE_FILE
);
1735 * TODO: We should actually invalidate o->result, not src_index [1].
1736 * But since cache tree and untracked cache both are not copied to
1737 * o->result until unpacking is complete, we invalidate them on
1738 * src_index instead with the assumption that they will be copied to
1739 * dst_index at the end.
1741 * [1] src_index->cache_tree is also used in unpack_callback() so if
1742 * we invalidate o->result, we need to update it to use
1743 * o->result.cache_tree as well.
1745 static void invalidate_ce_path(const struct cache_entry
*ce
,
1746 struct unpack_trees_options
*o
)
1750 cache_tree_invalidate_path(o
->src_index
, ce
->name
);
1751 untracked_cache_invalidate_path(o
->src_index
, ce
->name
, 1);
1755 * Check that checking out ce->sha1 in subdir ce->name is not
1756 * going to overwrite any working files.
1758 * Currently, git does not checkout subprojects during a superproject
1759 * checkout, so it is not going to overwrite anything.
1761 static int verify_clean_submodule(const char *old_sha1
,
1762 const struct cache_entry
*ce
,
1763 enum unpack_trees_error_types error_type
,
1764 struct unpack_trees_options
*o
)
1766 if (!submodule_from_ce(ce
))
1769 return check_submodule_move_head(ce
, old_sha1
,
1770 oid_to_hex(&ce
->oid
), o
);
1773 static int verify_clean_subdirectory(const struct cache_entry
*ce
,
1774 enum unpack_trees_error_types error_type
,
1775 struct unpack_trees_options
*o
)
1778 * we are about to extract "ce->name"; we would not want to lose
1779 * anything in the existing directory there.
1783 struct dir_struct d
;
1787 if (S_ISGITLINK(ce
->ce_mode
)) {
1788 struct object_id oid
;
1789 int sub_head
= resolve_gitlink_ref(ce
->name
, "HEAD", &oid
);
1791 * If we are not going to update the submodule, then
1794 if (!sub_head
&& oideq(&oid
, &ce
->oid
))
1796 return verify_clean_submodule(sub_head
? NULL
: oid_to_hex(&oid
),
1801 * First let's make sure we do not have a local modification
1802 * in that directory.
1804 namelen
= ce_namelen(ce
);
1805 for (i
= locate_in_src_index(ce
, o
);
1806 i
< o
->src_index
->cache_nr
;
1808 struct cache_entry
*ce2
= o
->src_index
->cache
[i
];
1809 int len
= ce_namelen(ce2
);
1810 if (len
< namelen
||
1811 strncmp(ce
->name
, ce2
->name
, namelen
) ||
1812 ce2
->name
[namelen
] != '/')
1815 * ce2->name is an entry in the subdirectory to be
1818 if (!ce_stage(ce2
)) {
1819 if (verify_uptodate(ce2
, o
))
1821 add_entry(o
, ce2
, CE_REMOVE
, 0);
1822 invalidate_ce_path(ce
, o
);
1823 mark_ce_used(ce2
, o
);
1829 * Then we need to make sure that we do not lose a locally
1830 * present file that is not ignored.
1832 pathbuf
= xstrfmt("%.*s/", namelen
, ce
->name
);
1834 memset(&d
, 0, sizeof(d
));
1836 d
.exclude_per_dir
= o
->dir
->exclude_per_dir
;
1837 i
= read_directory(&d
, o
->src_index
, pathbuf
, namelen
+1, NULL
);
1839 return o
->gently
? -1 :
1840 add_rejected_path(o
, ERROR_NOT_UPTODATE_DIR
, ce
->name
);
1846 * This gets called when there was no index entry for the tree entry 'dst',
1847 * but we found a file in the working tree that 'lstat()' said was fine,
1848 * and we're on a case-insensitive filesystem.
1850 * See if we can find a case-insensitive match in the index that also
1851 * matches the stat information, and assume it's that other file!
1853 static int icase_exists(struct unpack_trees_options
*o
, const char *name
, int len
, struct stat
*st
)
1855 const struct cache_entry
*src
;
1857 src
= index_file_exists(o
->src_index
, name
, len
, 1);
1858 return src
&& !ie_match_stat(o
->src_index
, src
, st
, CE_MATCH_IGNORE_VALID
|CE_MATCH_IGNORE_SKIP_WORKTREE
);
1861 static int check_ok_to_remove(const char *name
, int len
, int dtype
,
1862 const struct cache_entry
*ce
, struct stat
*st
,
1863 enum unpack_trees_error_types error_type
,
1864 struct unpack_trees_options
*o
)
1866 const struct cache_entry
*result
;
1869 * It may be that the 'lstat()' succeeded even though
1870 * target 'ce' was absent, because there is an old
1871 * entry that is different only in case..
1873 * Ignore that lstat() if it matches.
1875 if (ignore_case
&& icase_exists(o
, name
, len
, st
))
1879 is_excluded(o
->dir
, o
->src_index
, name
, &dtype
))
1881 * ce->name is explicitly excluded, so it is Ok to
1885 if (S_ISDIR(st
->st_mode
)) {
1887 * We are checking out path "foo" and
1888 * found "foo/." in the working tree.
1889 * This is tricky -- if we have modified
1890 * files that are in "foo/" we would lose
1893 if (verify_clean_subdirectory(ce
, error_type
, o
) < 0)
1899 * The previous round may already have decided to
1900 * delete this path, which is in a subdirectory that
1901 * is being replaced with a blob.
1903 result
= index_file_exists(&o
->result
, name
, len
, 0);
1905 if (result
->ce_flags
& CE_REMOVE
)
1909 return o
->gently
? -1 :
1910 add_rejected_path(o
, error_type
, name
);
1914 * We do not want to remove or overwrite a working tree file that
1915 * is not tracked, unless it is ignored.
1917 static int verify_absent_1(const struct cache_entry
*ce
,
1918 enum unpack_trees_error_types error_type
,
1919 struct unpack_trees_options
*o
)
1924 if (o
->index_only
|| o
->reset
|| !o
->update
)
1927 len
= check_leading_path(ce
->name
, ce_namelen(ce
));
1934 path
= xmemdupz(ce
->name
, len
);
1935 if (lstat(path
, &st
))
1936 ret
= error_errno("cannot stat '%s'", path
);
1938 if (submodule_from_ce(ce
))
1939 ret
= check_submodule_move_head(ce
,
1940 oid_to_hex(&ce
->oid
),
1943 ret
= check_ok_to_remove(path
, len
, DT_UNKNOWN
, NULL
,
1944 &st
, error_type
, o
);
1948 } else if (lstat(ce
->name
, &st
)) {
1949 if (errno
!= ENOENT
)
1950 return error_errno("cannot stat '%s'", ce
->name
);
1953 if (submodule_from_ce(ce
))
1954 return check_submodule_move_head(ce
, oid_to_hex(&ce
->oid
),
1957 return check_ok_to_remove(ce
->name
, ce_namelen(ce
),
1958 ce_to_dtype(ce
), ce
, &st
,
1963 static int verify_absent(const struct cache_entry
*ce
,
1964 enum unpack_trees_error_types error_type
,
1965 struct unpack_trees_options
*o
)
1967 if (!o
->skip_sparse_checkout
&& (ce
->ce_flags
& CE_NEW_SKIP_WORKTREE
))
1969 return verify_absent_1(ce
, error_type
, o
);
1972 static int verify_absent_sparse(const struct cache_entry
*ce
,
1973 enum unpack_trees_error_types error_type
,
1974 struct unpack_trees_options
*o
)
1976 enum unpack_trees_error_types orphaned_error
= error_type
;
1977 if (orphaned_error
== ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN
)
1978 orphaned_error
= ERROR_WOULD_LOSE_ORPHANED_OVERWRITTEN
;
1980 return verify_absent_1(ce
, orphaned_error
, o
);
1983 static int merged_entry(const struct cache_entry
*ce
,
1984 const struct cache_entry
*old
,
1985 struct unpack_trees_options
*o
)
1987 int update
= CE_UPDATE
;
1988 struct cache_entry
*merge
= dup_cache_entry(ce
, &o
->result
);
1992 * New index entries. In sparse checkout, the following
1993 * verify_absent() will be delayed until after
1994 * traverse_trees() finishes in unpack_trees(), then:
1996 * - CE_NEW_SKIP_WORKTREE will be computed correctly
1997 * - verify_absent() be called again, this time with
1998 * correct CE_NEW_SKIP_WORKTREE
2000 * verify_absent() call here does nothing in sparse
2001 * checkout (i.e. o->skip_sparse_checkout == 0)
2004 merge
->ce_flags
|= CE_NEW_SKIP_WORKTREE
;
2006 if (verify_absent(merge
,
2007 ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN
, o
)) {
2008 discard_cache_entry(merge
);
2011 invalidate_ce_path(merge
, o
);
2013 if (submodule_from_ce(ce
)) {
2014 int ret
= check_submodule_move_head(ce
, NULL
,
2015 oid_to_hex(&ce
->oid
),
2021 } else if (!(old
->ce_flags
& CE_CONFLICTED
)) {
2023 * See if we can re-use the old CE directly?
2024 * That way we get the uptodate stat info.
2026 * This also removes the UPDATE flag on a match; otherwise
2027 * we will end up overwriting local changes in the work tree.
2029 if (same(old
, merge
)) {
2030 copy_cache_entry(merge
, old
);
2033 if (verify_uptodate(old
, o
)) {
2034 discard_cache_entry(merge
);
2037 /* Migrate old flags over */
2038 update
|= old
->ce_flags
& (CE_SKIP_WORKTREE
| CE_NEW_SKIP_WORKTREE
);
2039 invalidate_ce_path(old
, o
);
2042 if (submodule_from_ce(ce
)) {
2043 int ret
= check_submodule_move_head(ce
, oid_to_hex(&old
->oid
),
2044 oid_to_hex(&ce
->oid
),
2051 * Previously unmerged entry left as an existence
2052 * marker by read_index_unmerged();
2054 invalidate_ce_path(old
, o
);
2057 do_add_entry(o
, merge
, update
, CE_STAGEMASK
);
2061 static int deleted_entry(const struct cache_entry
*ce
,
2062 const struct cache_entry
*old
,
2063 struct unpack_trees_options
*o
)
2065 /* Did it exist in the index? */
2067 if (verify_absent(ce
, ERROR_WOULD_LOSE_UNTRACKED_REMOVED
, o
))
2071 if (!(old
->ce_flags
& CE_CONFLICTED
) && verify_uptodate(old
, o
))
2073 add_entry(o
, ce
, CE_REMOVE
, 0);
2074 invalidate_ce_path(ce
, o
);
2078 static int keep_entry(const struct cache_entry
*ce
,
2079 struct unpack_trees_options
*o
)
2081 add_entry(o
, ce
, 0, 0);
2083 invalidate_ce_path(ce
, o
);
2088 static void show_stage_entry(FILE *o
,
2089 const char *label
, const struct cache_entry
*ce
)
2092 fprintf(o
, "%s (missing)\n", label
);
2094 fprintf(o
, "%s%06o %s %d\t%s\n",
2097 oid_to_hex(&ce
->oid
),
2103 int threeway_merge(const struct cache_entry
* const *stages
,
2104 struct unpack_trees_options
*o
)
2106 const struct cache_entry
*index
;
2107 const struct cache_entry
*head
;
2108 const struct cache_entry
*remote
= stages
[o
->head_idx
+ 1];
2111 int remote_match
= 0;
2113 int df_conflict_head
= 0;
2114 int df_conflict_remote
= 0;
2116 int any_anc_missing
= 0;
2117 int no_anc_exists
= 1;
2120 for (i
= 1; i
< o
->head_idx
; i
++) {
2121 if (!stages
[i
] || stages
[i
] == o
->df_conflict_entry
)
2122 any_anc_missing
= 1;
2128 head
= stages
[o
->head_idx
];
2130 if (head
== o
->df_conflict_entry
) {
2131 df_conflict_head
= 1;
2135 if (remote
== o
->df_conflict_entry
) {
2136 df_conflict_remote
= 1;
2141 * First, if there's a #16 situation, note that to prevent #13
2144 if (!same(remote
, head
)) {
2145 for (i
= 1; i
< o
->head_idx
; i
++) {
2146 if (same(stages
[i
], head
)) {
2149 if (same(stages
[i
], remote
)) {
2156 * We start with cases where the index is allowed to match
2157 * something other than the head: #14(ALT) and #2ALT, where it
2158 * is permitted to match the result instead.
2160 /* #14, #14ALT, #2ALT */
2161 if (remote
&& !df_conflict_head
&& head_match
&& !remote_match
) {
2162 if (index
&& !same(index
, remote
) && !same(index
, head
))
2163 return reject_merge(index
, o
);
2164 return merged_entry(remote
, index
, o
);
2167 * If we have an entry in the index cache, then we want to
2168 * make sure that it matches head.
2170 if (index
&& !same(index
, head
))
2171 return reject_merge(index
, o
);
2175 if (same(head
, remote
))
2176 return merged_entry(head
, index
, o
);
2178 if (!df_conflict_remote
&& remote_match
&& !head_match
)
2179 return merged_entry(head
, index
, o
);
2183 if (!head
&& !remote
&& any_anc_missing
)
2187 * Under the "aggressive" rule, we resolve mostly trivial
2188 * cases that we historically had git-merge-one-file resolve.
2190 if (o
->aggressive
) {
2191 int head_deleted
= !head
;
2192 int remote_deleted
= !remote
;
2193 const struct cache_entry
*ce
= NULL
;
2202 for (i
= 1; i
< o
->head_idx
; i
++) {
2203 if (stages
[i
] && stages
[i
] != o
->df_conflict_entry
) {
2212 * Deleted in one and unchanged in the other.
2214 if ((head_deleted
&& remote_deleted
) ||
2215 (head_deleted
&& remote
&& remote_match
) ||
2216 (remote_deleted
&& head
&& head_match
)) {
2218 return deleted_entry(index
, index
, o
);
2219 if (ce
&& !head_deleted
) {
2220 if (verify_absent(ce
, ERROR_WOULD_LOSE_UNTRACKED_REMOVED
, o
))
2226 * Added in both, identically.
2228 if (no_anc_exists
&& head
&& remote
&& same(head
, remote
))
2229 return merged_entry(head
, index
, o
);
2233 /* Below are "no merge" cases, which require that the index be
2234 * up-to-date to avoid the files getting overwritten with
2235 * conflict resolution files.
2238 if (verify_uptodate(index
, o
))
2242 o
->nontrivial_merge
= 1;
2244 /* #2, #3, #4, #6, #7, #9, #10, #11. */
2246 if (!head_match
|| !remote_match
) {
2247 for (i
= 1; i
< o
->head_idx
; i
++) {
2248 if (stages
[i
] && stages
[i
] != o
->df_conflict_entry
) {
2249 keep_entry(stages
[i
], o
);
2257 fprintf(stderr
, "read-tree: warning #16 detected\n");
2258 show_stage_entry(stderr
, "head ", stages
[head_match
]);
2259 show_stage_entry(stderr
, "remote ", stages
[remote_match
]);
2262 if (head
) { count
+= keep_entry(head
, o
); }
2263 if (remote
) { count
+= keep_entry(remote
, o
); }
2270 * The rule is to "carry forward" what is in the index without losing
2271 * information across a "fast-forward", favoring a successful merge
2272 * over a merge failure when it makes sense. For details of the
2273 * "carry forward" rule, please see <Documentation/git-read-tree.txt>.
2276 int twoway_merge(const struct cache_entry
* const *src
,
2277 struct unpack_trees_options
*o
)
2279 const struct cache_entry
*current
= src
[0];
2280 const struct cache_entry
*oldtree
= src
[1];
2281 const struct cache_entry
*newtree
= src
[2];
2283 if (o
->merge_size
!= 2)
2284 return error("Cannot do a twoway merge of %d trees",
2287 if (oldtree
== o
->df_conflict_entry
)
2289 if (newtree
== o
->df_conflict_entry
)
2293 if (current
->ce_flags
& CE_CONFLICTED
) {
2294 if (same(oldtree
, newtree
) || o
->reset
) {
2296 return deleted_entry(current
, current
, o
);
2298 return merged_entry(newtree
, current
, o
);
2300 return reject_merge(current
, o
);
2301 } else if ((!oldtree
&& !newtree
) || /* 4 and 5 */
2302 (!oldtree
&& newtree
&&
2303 same(current
, newtree
)) || /* 6 and 7 */
2304 (oldtree
&& newtree
&&
2305 same(oldtree
, newtree
)) || /* 14 and 15 */
2306 (oldtree
&& newtree
&&
2307 !same(oldtree
, newtree
) && /* 18 and 19 */
2308 same(current
, newtree
))) {
2309 return keep_entry(current
, o
);
2310 } else if (oldtree
&& !newtree
&& same(current
, oldtree
)) {
2312 return deleted_entry(oldtree
, current
, o
);
2313 } else if (oldtree
&& newtree
&&
2314 same(current
, oldtree
) && !same(current
, newtree
)) {
2316 return merged_entry(newtree
, current
, o
);
2318 return reject_merge(current
, o
);
2321 if (oldtree
&& !o
->initial_checkout
) {
2323 * deletion of the path was staged;
2325 if (same(oldtree
, newtree
))
2327 return reject_merge(oldtree
, o
);
2329 return merged_entry(newtree
, current
, o
);
2331 return deleted_entry(oldtree
, current
, o
);
2337 * Keep the index entries at stage0, collapse stage1 but make sure
2338 * stage0 does not have anything there.
2340 int bind_merge(const struct cache_entry
* const *src
,
2341 struct unpack_trees_options
*o
)
2343 const struct cache_entry
*old
= src
[0];
2344 const struct cache_entry
*a
= src
[1];
2346 if (o
->merge_size
!= 1)
2347 return error("Cannot do a bind merge of %d trees",
2350 return o
->gently
? -1 :
2351 error(ERRORMSG(o
, ERROR_BIND_OVERLAP
),
2352 super_prefixed(a
->name
),
2353 super_prefixed(old
->name
));
2355 return keep_entry(old
, o
);
2357 return merged_entry(a
, NULL
, o
);
2364 * - take the stat information from stage0, take the data from stage1
2366 int oneway_merge(const struct cache_entry
* const *src
,
2367 struct unpack_trees_options
*o
)
2369 const struct cache_entry
*old
= src
[0];
2370 const struct cache_entry
*a
= src
[1];
2372 if (o
->merge_size
!= 1)
2373 return error("Cannot do a oneway merge of %d trees",
2376 if (!a
|| a
== o
->df_conflict_entry
)
2377 return deleted_entry(old
, old
, o
);
2379 if (old
&& same(old
, a
)) {
2381 if (o
->reset
&& o
->update
&& !ce_uptodate(old
) && !ce_skip_worktree(old
)) {
2383 if (lstat(old
->name
, &st
) ||
2384 ie_match_stat(o
->src_index
, old
, &st
, CE_MATCH_IGNORE_VALID
|CE_MATCH_IGNORE_SKIP_WORKTREE
))
2385 update
|= CE_UPDATE
;
2387 if (o
->update
&& S_ISGITLINK(old
->ce_mode
) &&
2388 should_update_submodules() && !verify_uptodate(old
, o
))
2389 update
|= CE_UPDATE
;
2390 add_entry(o
, old
, update
, 0);
2393 return merged_entry(a
, old
, o
);