1 #define USE_THE_REPOSITORY_VARIABLE
3 #include "git-compat-util.h"
6 #include "repository.h"
9 #include "environment.h"
12 #include "name-hash.h"
14 #include "tree-walk.h"
15 #include "cache-tree.h"
16 #include "unpack-trees.h"
20 #include "read-cache.h"
21 #include "split-index.h"
22 #include "sparse-index.h"
23 #include "submodule.h"
24 #include "submodule-config.h"
27 #include "fsmonitor.h"
28 #include "object-store-ll.h"
29 #include "promisor-remote.h"
31 #include "parallel-checkout.h"
35 * Error messages expected by scripts out of plumbing commands such as
36 * read-tree. Non-scripted Porcelain is not required to use these messages
37 * and in fact are encouraged to reword them to better suit their particular
38 * situation better. See how "git checkout" and "git merge" replaces
39 * them using setup_unpack_trees_porcelain(), for example.
41 static const char *unpack_plumbing_errors
[NB_UNPACK_TREES_WARNING_TYPES
] = {
42 /* ERROR_WOULD_OVERWRITE */
43 "Entry '%s' would be overwritten by merge. Cannot merge.",
45 /* ERROR_NOT_UPTODATE_FILE */
46 "Entry '%s' not uptodate. Cannot merge.",
48 /* ERROR_NOT_UPTODATE_DIR */
49 "Updating '%s' would lose untracked files in it",
51 /* ERROR_CWD_IN_THE_WAY */
52 "Refusing to remove '%s' since it is the current working directory.",
54 /* ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN */
55 "Untracked working tree file '%s' would be overwritten by merge.",
57 /* ERROR_WOULD_LOSE_UNTRACKED_REMOVED */
58 "Untracked working tree file '%s' would be removed by merge.",
60 /* ERROR_BIND_OVERLAP */
61 "Entry '%s' overlaps with '%s'. Cannot bind.",
63 /* ERROR_WOULD_LOSE_SUBMODULE */
64 "Submodule '%s' cannot checkout new HEAD.",
66 /* NB_UNPACK_TREES_ERROR_TYPES; just a meta value */
69 /* WARNING_SPARSE_NOT_UPTODATE_FILE */
70 "Path '%s' not uptodate; will not remove from working tree.",
72 /* WARNING_SPARSE_UNMERGED_FILE */
73 "Path '%s' unmerged; will not remove from working tree.",
75 /* WARNING_SPARSE_ORPHANED_NOT_OVERWRITTEN */
76 "Path '%s' already present; will not overwrite with sparse update.",
79 #define ERRORMSG(o,type) \
80 ( ((o) && (o)->internal.msgs[(type)]) \
81 ? ((o)->internal.msgs[(type)]) \
82 : (unpack_plumbing_errors[(type)]) )
84 static const char *super_prefixed(const char *path
, const char *super_prefix
)
87 * It is necessary and sufficient to have two static buffers
88 * here, as the return value of this function is fed to
89 * error() using the unpack_*_errors[] templates we see above.
91 static struct strbuf buf
[2] = {STRBUF_INIT
, STRBUF_INIT
};
92 static int super_prefix_len
= -1;
93 static unsigned idx
= ARRAY_SIZE(buf
) - 1;
95 if (super_prefix_len
< 0) {
100 for (i
= 0; i
< ARRAY_SIZE(buf
); i
++)
101 strbuf_addstr(&buf
[i
], super_prefix
);
102 super_prefix_len
= buf
[0].len
;
106 if (!super_prefix_len
)
109 if (++idx
>= ARRAY_SIZE(buf
))
112 strbuf_setlen(&buf
[idx
], super_prefix_len
);
113 strbuf_addstr(&buf
[idx
], path
);
118 void setup_unpack_trees_porcelain(struct unpack_trees_options
*opts
,
122 const char **msgs
= opts
->internal
.msgs
;
125 strvec_init(&opts
->internal
.msgs_to_free
);
127 if (!strcmp(cmd
, "checkout"))
128 msg
= advice_enabled(ADVICE_COMMIT_BEFORE_MERGE
)
129 ? _("Your local changes to the following files would be overwritten by checkout:\n%%s"
130 "Please commit your changes or stash them before you switch branches.")
131 : _("Your local changes to the following files would be overwritten by checkout:\n%%s");
132 else if (!strcmp(cmd
, "merge"))
133 msg
= advice_enabled(ADVICE_COMMIT_BEFORE_MERGE
)
134 ? _("Your local changes to the following files would be overwritten by merge:\n%%s"
135 "Please commit your changes or stash them before you merge.")
136 : _("Your local changes to the following files would be overwritten by merge:\n%%s");
138 msg
= advice_enabled(ADVICE_COMMIT_BEFORE_MERGE
)
139 ? _("Your local changes to the following files would be overwritten by %s:\n%%s"
140 "Please commit your changes or stash them before you %s.")
141 : _("Your local changes to the following files would be overwritten by %s:\n%%s");
142 msgs
[ERROR_WOULD_OVERWRITE
] = msgs
[ERROR_NOT_UPTODATE_FILE
] =
143 strvec_pushf(&opts
->internal
.msgs_to_free
, msg
, cmd
, cmd
);
145 msgs
[ERROR_NOT_UPTODATE_DIR
] =
146 _("Updating the following directories would lose untracked files in them:\n%s");
148 msgs
[ERROR_CWD_IN_THE_WAY
] =
149 _("Refusing to remove the current working directory:\n%s");
151 if (!strcmp(cmd
, "checkout"))
152 msg
= advice_enabled(ADVICE_COMMIT_BEFORE_MERGE
)
153 ? _("The following untracked working tree files would be removed by checkout:\n%%s"
154 "Please move or remove them before you switch branches.")
155 : _("The following untracked working tree files would be removed by checkout:\n%%s");
156 else if (!strcmp(cmd
, "merge"))
157 msg
= advice_enabled(ADVICE_COMMIT_BEFORE_MERGE
)
158 ? _("The following untracked working tree files would be removed by merge:\n%%s"
159 "Please move or remove them before you merge.")
160 : _("The following untracked working tree files would be removed by merge:\n%%s");
162 msg
= advice_enabled(ADVICE_COMMIT_BEFORE_MERGE
)
163 ? _("The following untracked working tree files would be removed by %s:\n%%s"
164 "Please move or remove them before you %s.")
165 : _("The following untracked working tree files would be removed by %s:\n%%s");
166 msgs
[ERROR_WOULD_LOSE_UNTRACKED_REMOVED
] =
167 strvec_pushf(&opts
->internal
.msgs_to_free
, msg
, cmd
, cmd
);
169 if (!strcmp(cmd
, "checkout"))
170 msg
= advice_enabled(ADVICE_COMMIT_BEFORE_MERGE
)
171 ? _("The following untracked working tree files would be overwritten by checkout:\n%%s"
172 "Please move or remove them before you switch branches.")
173 : _("The following untracked working tree files would be overwritten by checkout:\n%%s");
174 else if (!strcmp(cmd
, "merge"))
175 msg
= advice_enabled(ADVICE_COMMIT_BEFORE_MERGE
)
176 ? _("The following untracked working tree files would be overwritten by merge:\n%%s"
177 "Please move or remove them before you merge.")
178 : _("The following untracked working tree files would be overwritten by merge:\n%%s");
180 msg
= advice_enabled(ADVICE_COMMIT_BEFORE_MERGE
)
181 ? _("The following untracked working tree files would be overwritten by %s:\n%%s"
182 "Please move or remove them before you %s.")
183 : _("The following untracked working tree files would be overwritten by %s:\n%%s");
184 msgs
[ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN
] =
185 strvec_pushf(&opts
->internal
.msgs_to_free
, msg
, cmd
, cmd
);
188 * Special case: ERROR_BIND_OVERLAP refers to a pair of paths, we
189 * cannot easily display it as a list.
191 msgs
[ERROR_BIND_OVERLAP
] = _("Entry '%s' overlaps with '%s'. Cannot bind.");
193 msgs
[ERROR_WOULD_LOSE_SUBMODULE
] =
194 _("Cannot update submodule:\n%s");
196 msgs
[WARNING_SPARSE_NOT_UPTODATE_FILE
] =
197 _("The following paths are not up to date and were left despite sparse patterns:\n%s");
198 msgs
[WARNING_SPARSE_UNMERGED_FILE
] =
199 _("The following paths are unmerged and were left despite sparse patterns:\n%s");
200 msgs
[WARNING_SPARSE_ORPHANED_NOT_OVERWRITTEN
] =
201 _("The following paths were already present and thus not updated despite sparse patterns:\n%s");
203 opts
->internal
.show_all_errors
= 1;
204 /* rejected paths may not have a static buffer */
205 for (i
= 0; i
< ARRAY_SIZE(opts
->internal
.unpack_rejects
); i
++)
206 opts
->internal
.unpack_rejects
[i
].strdup_strings
= 1;
209 void clear_unpack_trees_porcelain(struct unpack_trees_options
*opts
)
211 strvec_clear(&opts
->internal
.msgs_to_free
);
212 memset(opts
->internal
.msgs
, 0, sizeof(opts
->internal
.msgs
));
213 discard_index(&opts
->internal
.result
);
216 static int do_add_entry(struct unpack_trees_options
*o
, struct cache_entry
*ce
,
217 unsigned int set
, unsigned int clear
)
224 ce
->ce_flags
= (ce
->ce_flags
& ~clear
) | set
;
225 return add_index_entry(&o
->internal
.result
, ce
,
226 ADD_CACHE_OK_TO_ADD
| ADD_CACHE_OK_TO_REPLACE
);
229 static void add_entry(struct unpack_trees_options
*o
,
230 const struct cache_entry
*ce
,
231 unsigned int set
, unsigned int clear
)
233 do_add_entry(o
, dup_cache_entry(ce
, &o
->internal
.result
), set
, clear
);
237 * add error messages on path <path>
238 * corresponding to the type <e> with the message <msg>
239 * indicating if it should be display in porcelain or not
241 static int add_rejected_path(struct unpack_trees_options
*o
,
242 enum unpack_trees_error_types e
,
248 if (!o
->internal
.show_all_errors
)
249 return error(ERRORMSG(o
, e
), super_prefixed(path
,
253 * Otherwise, insert in a list for future display by
254 * display_(error|warning)_msgs()
256 string_list_append(&o
->internal
.unpack_rejects
[e
], path
);
261 * display all the error messages stored in a nice way
263 static void display_error_msgs(struct unpack_trees_options
*o
)
266 unsigned error_displayed
= 0;
267 for (e
= 0; e
< NB_UNPACK_TREES_ERROR_TYPES
; e
++) {
268 struct string_list
*rejects
= &o
->internal
.unpack_rejects
[e
];
270 if (rejects
->nr
> 0) {
272 struct strbuf path
= STRBUF_INIT
;
275 for (i
= 0; i
< rejects
->nr
; i
++)
276 strbuf_addf(&path
, "\t%s\n", rejects
->items
[i
].string
);
277 error(ERRORMSG(o
, e
), super_prefixed(path
.buf
,
279 strbuf_release(&path
);
281 string_list_clear(rejects
, 0);
284 fprintf(stderr
, _("Aborting\n"));
288 * display all the warning messages stored in a nice way
290 static void display_warning_msgs(struct unpack_trees_options
*o
)
293 unsigned warning_displayed
= 0;
294 for (e
= NB_UNPACK_TREES_ERROR_TYPES
+ 1;
295 e
< NB_UNPACK_TREES_WARNING_TYPES
; e
++) {
296 struct string_list
*rejects
= &o
->internal
.unpack_rejects
[e
];
298 if (rejects
->nr
> 0) {
300 struct strbuf path
= STRBUF_INIT
;
302 warning_displayed
= 1;
303 for (i
= 0; i
< rejects
->nr
; i
++)
304 strbuf_addf(&path
, "\t%s\n", rejects
->items
[i
].string
);
305 warning(ERRORMSG(o
, e
), super_prefixed(path
.buf
,
307 strbuf_release(&path
);
309 string_list_clear(rejects
, 0);
311 if (warning_displayed
)
312 fprintf(stderr
, _("After fixing the above paths, you may want to run `git sparse-checkout reapply`.\n"));
314 static int check_submodule_move_head(const struct cache_entry
*ce
,
317 struct unpack_trees_options
*o
)
319 unsigned flags
= SUBMODULE_MOVE_HEAD_DRY_RUN
;
320 const struct submodule
*sub
= submodule_from_ce(ce
);
326 flags
|= SUBMODULE_MOVE_HEAD_FORCE
;
328 if (submodule_move_head(ce
->name
, o
->super_prefix
, old_id
, new_id
,
330 return add_rejected_path(o
, ERROR_WOULD_LOSE_SUBMODULE
, ce
->name
);
335 * Perform the loading of the repository's gitmodules file. This function is
336 * used by 'check_update()' to perform loading of the gitmodules file in two
337 * different situations:
338 * (1) before removing entries from the working tree if the gitmodules file has
339 * been marked for removal. This situation is specified by 'state' == NULL.
340 * (2) before checking out entries to the working tree if the gitmodules file
341 * has been marked for update. This situation is specified by 'state' != NULL.
343 static void load_gitmodules_file(struct index_state
*index
,
344 struct checkout
*state
)
346 int pos
= index_name_pos(index
, GITMODULES_FILE
, strlen(GITMODULES_FILE
));
349 struct cache_entry
*ce
= index
->cache
[pos
];
350 if (!state
&& ce
->ce_flags
& CE_WT_REMOVE
) {
351 repo_read_gitmodules(the_repository
, 0);
352 } else if (state
&& (ce
->ce_flags
& CE_UPDATE
)) {
353 submodule_free(the_repository
);
354 checkout_entry(ce
, state
, NULL
, NULL
);
355 repo_read_gitmodules(the_repository
, 0);
360 static struct progress
*get_progress(struct unpack_trees_options
*o
,
361 struct index_state
*index
)
363 unsigned cnt
= 0, total
= 0;
365 if (!o
->update
|| !o
->verbose_update
)
368 for (; cnt
< index
->cache_nr
; cnt
++) {
369 const struct cache_entry
*ce
= index
->cache
[cnt
];
370 if (ce
->ce_flags
& (CE_UPDATE
| CE_WT_REMOVE
))
374 return start_delayed_progress(_("Updating files"), total
);
377 static void setup_collided_checkout_detection(struct checkout
*state
,
378 struct index_state
*index
)
383 for (i
= 0; i
< index
->cache_nr
; i
++)
384 index
->cache
[i
]->ce_flags
&= ~CE_MATCHED
;
387 static void report_collided_checkout(struct index_state
*index
)
389 struct string_list list
= STRING_LIST_INIT_NODUP
;
392 for (i
= 0; i
< index
->cache_nr
; i
++) {
393 struct cache_entry
*ce
= index
->cache
[i
];
395 if (!(ce
->ce_flags
& CE_MATCHED
))
398 string_list_append(&list
, ce
->name
);
399 ce
->ce_flags
&= ~CE_MATCHED
;
402 list
.cmp
= fspathcmp
;
403 string_list_sort(&list
);
406 warning(_("the following paths have collided (e.g. case-sensitive paths\n"
407 "on a case-insensitive filesystem) and only one from the same\n"
408 "colliding group is in the working tree:\n"));
410 for (i
= 0; i
< list
.nr
; i
++)
411 fprintf(stderr
, " '%s'\n", list
.items
[i
].string
);
414 string_list_clear(&list
, 0);
417 static int must_checkout(const struct cache_entry
*ce
)
419 return ce
->ce_flags
& CE_UPDATE
;
422 static int check_updates(struct unpack_trees_options
*o
,
423 struct index_state
*index
)
427 struct progress
*progress
;
428 struct checkout state
= CHECKOUT_INIT
;
429 int i
, pc_workers
, pc_threshold
;
431 trace_performance_enter();
432 state
.super_prefix
= o
->super_prefix
;
435 state
.refresh_cache
= 1;
436 state
.istate
= index
;
437 clone_checkout_metadata(&state
.meta
, &o
->meta
, NULL
);
439 if (!o
->update
|| o
->dry_run
) {
440 remove_marked_cache_entries(index
, 0);
441 trace_performance_leave("check_updates");
446 setup_collided_checkout_detection(&state
, index
);
448 progress
= get_progress(o
, index
);
450 /* Start with clean cache to avoid using any possibly outdated info. */
451 invalidate_lstat_cache();
453 git_attr_set_direction(GIT_ATTR_CHECKOUT
);
455 if (should_update_submodules())
456 load_gitmodules_file(index
, NULL
);
458 for (i
= 0; i
< index
->cache_nr
; i
++) {
459 const struct cache_entry
*ce
= index
->cache
[i
];
461 if (ce
->ce_flags
& CE_WT_REMOVE
) {
462 display_progress(progress
, ++cnt
);
463 unlink_entry(ce
, o
->super_prefix
);
467 remove_marked_cache_entries(index
, 0);
468 remove_scheduled_dirs();
470 if (should_update_submodules())
471 load_gitmodules_file(index
, &state
);
473 if (repo_has_promisor_remote(the_repository
))
475 * Prefetch the objects that are to be checked out in the loop
478 prefetch_cache_entries(index
, must_checkout
);
480 get_parallel_checkout_configs(&pc_workers
, &pc_threshold
);
482 enable_delayed_checkout(&state
);
484 init_parallel_checkout();
485 for (i
= 0; i
< index
->cache_nr
; i
++) {
486 struct cache_entry
*ce
= index
->cache
[i
];
488 if (must_checkout(ce
)) {
489 size_t last_pc_queue_size
= pc_queue_size();
491 if (ce
->ce_flags
& CE_WT_REMOVE
)
492 BUG("both update and delete flags are set on %s",
494 ce
->ce_flags
&= ~CE_UPDATE
;
495 errs
|= checkout_entry(ce
, &state
, NULL
, NULL
);
497 if (last_pc_queue_size
== pc_queue_size())
498 display_progress(progress
, ++cnt
);
502 errs
|= run_parallel_checkout(&state
, pc_workers
, pc_threshold
,
504 stop_progress(&progress
);
505 errs
|= finish_delayed_checkout(&state
, o
->verbose_update
);
506 git_attr_set_direction(GIT_ATTR_CHECKIN
);
509 report_collided_checkout(index
);
511 trace_performance_leave("check_updates");
515 static int verify_uptodate_sparse(const struct cache_entry
*ce
,
516 struct unpack_trees_options
*o
);
517 static int verify_absent_sparse(const struct cache_entry
*ce
,
518 enum unpack_trees_error_types
,
519 struct unpack_trees_options
*o
);
521 static int apply_sparse_checkout(struct index_state
*istate
,
522 struct cache_entry
*ce
,
523 struct unpack_trees_options
*o
)
525 int was_skip_worktree
= ce_skip_worktree(ce
);
527 if (ce
->ce_flags
& CE_NEW_SKIP_WORKTREE
)
528 ce
->ce_flags
|= CE_SKIP_WORKTREE
;
530 ce
->ce_flags
&= ~CE_SKIP_WORKTREE
;
531 if (was_skip_worktree
!= ce_skip_worktree(ce
)) {
532 ce
->ce_flags
|= CE_UPDATE_IN_BASE
;
533 mark_fsmonitor_invalid(istate
, ce
);
534 istate
->cache_changed
|= CE_ENTRY_CHANGED
;
538 * if (!was_skip_worktree && !ce_skip_worktree()) {
539 * This is perfectly normal. Move on;
544 * Merge strategies may set CE_UPDATE|CE_REMOVE outside checkout
545 * area as a result of ce_skip_worktree() shortcuts in
546 * verify_absent() and verify_uptodate().
547 * Make sure they don't modify worktree if they are already
548 * outside checkout area
550 if (was_skip_worktree
&& ce_skip_worktree(ce
)) {
551 ce
->ce_flags
&= ~CE_UPDATE
;
554 * By default, when CE_REMOVE is on, CE_WT_REMOVE is also
555 * on to get that file removed from both index and worktree.
556 * If that file is already outside worktree area, don't
559 if (ce
->ce_flags
& CE_REMOVE
)
560 ce
->ce_flags
&= ~CE_WT_REMOVE
;
563 if (!was_skip_worktree
&& ce_skip_worktree(ce
)) {
565 * If CE_UPDATE is set, verify_uptodate() must be called already
566 * also stat info may have lost after merged_entry() so calling
567 * verify_uptodate() again may fail
569 if (!(ce
->ce_flags
& CE_UPDATE
) &&
570 verify_uptodate_sparse(ce
, o
)) {
571 ce
->ce_flags
&= ~CE_SKIP_WORKTREE
;
574 ce
->ce_flags
|= CE_WT_REMOVE
;
575 ce
->ce_flags
&= ~CE_UPDATE
;
577 if (was_skip_worktree
&& !ce_skip_worktree(ce
)) {
578 if (verify_absent_sparse(ce
, WARNING_SPARSE_ORPHANED_NOT_OVERWRITTEN
, o
))
580 ce
->ce_flags
|= CE_UPDATE
;
585 static int warn_conflicted_path(struct index_state
*istate
,
587 struct unpack_trees_options
*o
)
589 char *conflicting_path
= istate
->cache
[i
]->name
;
592 add_rejected_path(o
, WARNING_SPARSE_UNMERGED_FILE
, conflicting_path
);
594 /* Find out how many higher stage entries are at same path */
595 while ((++count
) + i
< istate
->cache_nr
&&
596 !strcmp(conflicting_path
, istate
->cache
[count
+ i
]->name
))
602 static inline int call_unpack_fn(const struct cache_entry
* const *src
,
603 struct unpack_trees_options
*o
)
605 int ret
= o
->fn(src
, o
);
611 static void mark_ce_used(struct cache_entry
*ce
, struct unpack_trees_options
*o
)
613 ce
->ce_flags
|= CE_UNPACKED
;
615 if (o
->internal
.cache_bottom
< o
->src_index
->cache_nr
&&
616 o
->src_index
->cache
[o
->internal
.cache_bottom
] == ce
) {
617 int bottom
= o
->internal
.cache_bottom
;
619 while (bottom
< o
->src_index
->cache_nr
&&
620 o
->src_index
->cache
[bottom
]->ce_flags
& CE_UNPACKED
)
622 o
->internal
.cache_bottom
= bottom
;
626 static void mark_all_ce_unused(struct index_state
*index
)
629 for (i
= 0; i
< index
->cache_nr
; i
++)
630 index
->cache
[i
]->ce_flags
&= ~(CE_UNPACKED
| CE_ADDED
| CE_NEW_SKIP_WORKTREE
);
633 static int locate_in_src_index(const struct cache_entry
*ce
,
634 struct unpack_trees_options
*o
)
636 struct index_state
*index
= o
->src_index
;
637 int len
= ce_namelen(ce
);
638 int pos
= index_name_pos(index
, ce
->name
, len
);
645 * We call unpack_index_entry() with an unmerged cache entry
646 * only in diff-index, and it wants a single callback. Skip
647 * the other unmerged entry with the same name.
649 static void mark_ce_used_same_name(struct cache_entry
*ce
,
650 struct unpack_trees_options
*o
)
652 struct index_state
*index
= o
->src_index
;
653 int len
= ce_namelen(ce
);
656 for (pos
= locate_in_src_index(ce
, o
); pos
< index
->cache_nr
; pos
++) {
657 struct cache_entry
*next
= index
->cache
[pos
];
658 if (len
!= ce_namelen(next
) ||
659 memcmp(ce
->name
, next
->name
, len
))
661 mark_ce_used(next
, o
);
665 static struct cache_entry
*next_cache_entry(struct unpack_trees_options
*o
)
667 const struct index_state
*index
= o
->src_index
;
668 int pos
= o
->internal
.cache_bottom
;
670 while (pos
< index
->cache_nr
) {
671 struct cache_entry
*ce
= index
->cache
[pos
];
672 if (!(ce
->ce_flags
& CE_UNPACKED
))
679 static void add_same_unmerged(const struct cache_entry
*ce
,
680 struct unpack_trees_options
*o
)
682 struct index_state
*index
= o
->src_index
;
683 int len
= ce_namelen(ce
);
684 int pos
= index_name_pos(index
, ce
->name
, len
);
687 die("programming error in a caller of mark_ce_used_same_name");
688 for (pos
= -pos
- 1; pos
< index
->cache_nr
; pos
++) {
689 struct cache_entry
*next
= index
->cache
[pos
];
690 if (len
!= ce_namelen(next
) ||
691 memcmp(ce
->name
, next
->name
, len
))
693 add_entry(o
, next
, 0, 0);
694 mark_ce_used(next
, o
);
698 static int unpack_index_entry(struct cache_entry
*ce
,
699 struct unpack_trees_options
*o
)
701 const struct cache_entry
*src
[MAX_UNPACK_TREES
+ 1] = { NULL
, };
708 if (o
->skip_unmerged
) {
709 add_entry(o
, ce
, 0, 0);
713 ret
= call_unpack_fn(src
, o
);
715 mark_ce_used_same_name(ce
, o
);
719 static int find_cache_pos(struct traverse_info
*, const char *p
, size_t len
);
721 static void restore_cache_bottom(struct traverse_info
*info
, int bottom
)
723 struct unpack_trees_options
*o
= info
->data
;
725 if (o
->diff_index_cached
)
727 o
->internal
.cache_bottom
= bottom
;
730 static int switch_cache_bottom(struct traverse_info
*info
)
732 struct unpack_trees_options
*o
= info
->data
;
735 if (o
->diff_index_cached
)
737 ret
= o
->internal
.cache_bottom
;
738 pos
= find_cache_pos(info
->prev
, info
->name
, info
->namelen
);
741 o
->internal
.cache_bottom
= -2 - pos
;
743 o
->internal
.cache_bottom
= o
->src_index
->cache_nr
;
747 static inline int are_same_oid(struct name_entry
*name_j
, struct name_entry
*name_k
)
749 return !is_null_oid(&name_j
->oid
) && !is_null_oid(&name_k
->oid
) && oideq(&name_j
->oid
, &name_k
->oid
);
752 static int all_trees_same_as_cache_tree(int n
, unsigned long dirmask
,
753 struct name_entry
*names
,
754 struct traverse_info
*info
)
756 struct unpack_trees_options
*o
= info
->data
;
759 if (!o
->merge
|| dirmask
!= ((1 << n
) - 1))
762 for (i
= 1; i
< n
; i
++)
763 if (!are_same_oid(names
, names
+ i
))
766 return cache_tree_matches_traversal(o
->src_index
->cache_tree
, names
, info
);
769 static int index_pos_by_traverse_info(struct name_entry
*names
,
770 struct traverse_info
*info
)
772 struct unpack_trees_options
*o
= info
->data
;
773 struct strbuf name
= STRBUF_INIT
;
776 strbuf_make_traverse_path(&name
, info
, names
->path
, names
->pathlen
);
777 strbuf_addch(&name
, '/');
778 pos
= index_name_pos(o
->src_index
, name
.buf
, name
.len
);
780 if (!o
->src_index
->sparse_index
||
781 !(o
->src_index
->cache
[pos
]->ce_flags
& CE_SKIP_WORKTREE
))
782 BUG("This is a directory and should not exist in index");
786 if (pos
>= o
->src_index
->cache_nr
||
787 !starts_with(o
->src_index
->cache
[pos
]->name
, name
.buf
) ||
788 (pos
> 0 && starts_with(o
->src_index
->cache
[pos
-1]->name
, name
.buf
)))
789 BUG("pos %d doesn't point to the first entry of %s in index",
791 strbuf_release(&name
);
796 * Fast path if we detect that all trees are the same as cache-tree at this
797 * path. We'll walk these trees in an iterative loop using cache-tree/index
798 * instead of ODB since we already know what these trees contain.
800 static int traverse_by_cache_tree(int pos
, int nr_entries
, int nr_names
,
801 struct traverse_info
*info
)
803 struct cache_entry
*src
[MAX_UNPACK_TREES
+ 1] = { NULL
, };
804 struct unpack_trees_options
*o
= info
->data
;
805 struct cache_entry
*tree_ce
= NULL
;
810 BUG("We need cache-tree to do this optimization");
811 if (nr_entries
+ pos
> o
->src_index
->cache_nr
)
812 return error(_("corrupted cache-tree has entries not present in index"));
815 * Do what unpack_callback() and unpack_single_entry() normally
816 * do. But we walk all paths in an iterative loop instead.
818 * D/F conflicts and higher stage entries are not a concern
819 * because cache-tree would be invalidated and we would never
820 * get here in the first place.
822 for (i
= 0; i
< nr_entries
; i
++) {
823 int new_ce_len
, len
, rc
;
825 src
[0] = o
->src_index
->cache
[pos
+ i
];
827 len
= ce_namelen(src
[0]);
828 new_ce_len
= cache_entry_size(len
);
830 if (new_ce_len
> ce_len
) {
832 tree_ce
= xrealloc(tree_ce
, new_ce_len
);
833 memset(tree_ce
, 0, new_ce_len
);
836 tree_ce
->ce_flags
= create_ce_flags(0);
838 for (d
= 1; d
<= nr_names
; d
++)
842 tree_ce
->ce_mode
= src
[0]->ce_mode
;
843 tree_ce
->ce_namelen
= len
;
844 oidcpy(&tree_ce
->oid
, &src
[0]->oid
);
845 memcpy(tree_ce
->name
, src
[0]->name
, len
+ 1);
847 rc
= call_unpack_fn((const struct cache_entry
* const *)src
, o
);
853 mark_ce_used(src
[0], o
);
856 if (o
->internal
.debug_unpack
)
857 printf("Unpacked %d entries from %s to %s using cache-tree\n",
859 o
->src_index
->cache
[pos
]->name
,
860 o
->src_index
->cache
[pos
+ nr_entries
- 1]->name
);
864 static int traverse_trees_recursive(int n
, unsigned long dirmask
,
865 unsigned long df_conflicts
,
866 struct name_entry
*names
,
867 struct traverse_info
*info
)
869 struct unpack_trees_options
*o
= info
->data
;
874 struct traverse_info newinfo
;
875 struct name_entry
*p
;
878 nr_entries
= all_trees_same_as_cache_tree(n
, dirmask
, names
, info
);
879 if (nr_entries
> 0) {
880 int pos
= index_pos_by_traverse_info(names
, info
);
882 if (!o
->merge
|| df_conflicts
)
883 BUG("Wrong condition to get here buddy");
886 * All entries up to 'pos' must have been processed
887 * (i.e. marked CE_UNPACKED) at this point. But to be safe,
888 * save and restore cache_bottom anyway to not miss
889 * unprocessed entries before 'pos'.
891 bottom
= o
->internal
.cache_bottom
;
892 ret
= traverse_by_cache_tree(pos
, nr_entries
, n
, info
);
893 o
->internal
.cache_bottom
= bottom
;
903 newinfo
.pathspec
= info
->pathspec
;
904 newinfo
.name
= p
->path
;
905 newinfo
.namelen
= p
->pathlen
;
906 newinfo
.mode
= p
->mode
;
907 newinfo
.pathlen
= st_add3(newinfo
.pathlen
, tree_entry_len(p
), 1);
908 newinfo
.df_conflicts
|= df_conflicts
;
914 * Fetch the tree from the ODB for each peer directory in the
917 * For 2- and 3-way traversals, we try to avoid hitting the
918 * ODB twice for the same OID. This should yield a nice speed
919 * up in checkouts and merges when the commits are similar.
921 * We don't bother doing the full O(n^2) search for larger n,
922 * because wider traversals don't happen that often and we
923 * avoid the search setup.
925 * When 2 peer OIDs are the same, we just copy the tree
926 * descriptor data. This implicitly borrows the buffer
927 * data from the earlier cell.
929 for (i
= 0; i
< n
; i
++, dirmask
>>= 1) {
930 if (i
> 0 && are_same_oid(&names
[i
], &names
[i
- 1]))
932 else if (i
> 1 && are_same_oid(&names
[i
], &names
[i
- 2]))
935 const struct object_id
*oid
= NULL
;
938 buf
[nr_buf
++] = fill_tree_descriptor(the_repository
, t
+ i
, oid
);
942 bottom
= switch_cache_bottom(&newinfo
);
943 ret
= traverse_trees(o
->src_index
, n
, t
, &newinfo
);
944 restore_cache_bottom(&newinfo
, bottom
);
946 for (i
= 0; i
< nr_buf
; i
++)
955 * Compare the traverse-path to the cache entry without actually
956 * having to generate the textual representation of the traverse
959 * NOTE! This *only* compares up to the size of the traverse path
960 * itself - the caller needs to do the final check for the cache
961 * entry having more data at the end!
963 static int do_compare_entry_piecewise(const struct cache_entry
*ce
,
964 const struct traverse_info
*info
,
965 const char *name
, size_t namelen
,
972 int cmp
= do_compare_entry_piecewise(ce
, info
->prev
,
973 info
->name
, info
->namelen
,
978 pathlen
= info
->pathlen
;
979 ce_len
= ce_namelen(ce
);
981 /* If ce_len < pathlen then we must have previously hit "name == directory" entry */
982 if (ce_len
< pathlen
)
986 ce_name
= ce
->name
+ pathlen
;
988 return df_name_compare(ce_name
, ce_len
, S_IFREG
, name
, namelen
, mode
);
991 static int do_compare_entry(const struct cache_entry
*ce
,
992 const struct traverse_info
*info
,
993 const char *name
, size_t namelen
,
1002 * If we have not precomputed the traverse path, it is quicker
1003 * to avoid doing so. But if we have precomputed it,
1004 * it is quicker to use the precomputed version.
1006 if (!info
->traverse_path
)
1007 return do_compare_entry_piecewise(ce
, info
, name
, namelen
, mode
);
1009 cmp
= strncmp(ce
->name
, info
->traverse_path
, info
->pathlen
);
1013 pathlen
= info
->pathlen
;
1014 ce_len
= ce_namelen(ce
);
1016 if (ce_len
< pathlen
)
1020 ce_name
= ce
->name
+ pathlen
;
1022 ce_mode
= S_ISSPARSEDIR(ce
->ce_mode
) ? S_IFDIR
: S_IFREG
;
1023 return df_name_compare(ce_name
, ce_len
, ce_mode
, name
, namelen
, mode
);
1026 static int compare_entry(const struct cache_entry
*ce
, const struct traverse_info
*info
, const struct name_entry
*n
)
1028 int cmp
= do_compare_entry(ce
, info
, n
->path
, n
->pathlen
, n
->mode
);
1033 * At this point, we know that we have a prefix match. If ce
1034 * is a sparse directory, then allow an exact match. This only
1035 * works when the input name is a directory, since ce->name
1036 * ends in a directory separator.
1038 if (S_ISSPARSEDIR(ce
->ce_mode
) &&
1039 ce
->ce_namelen
== traverse_path_len(info
, tree_entry_len(n
)) + 1)
1043 * Even if the beginning compared identically, the ce should
1044 * compare as bigger than a directory leading up to it!
1046 return ce_namelen(ce
) > traverse_path_len(info
, tree_entry_len(n
));
1049 static int ce_in_traverse_path(const struct cache_entry
*ce
,
1050 const struct traverse_info
*info
)
1054 if (do_compare_entry(ce
, info
->prev
,
1055 info
->name
, info
->namelen
, info
->mode
))
1058 * If ce (blob) is the same name as the path (which is a tree
1059 * we will be descending into), it won't be inside it.
1061 return (info
->pathlen
< ce_namelen(ce
));
1064 static struct cache_entry
*create_ce_entry(const struct traverse_info
*info
,
1065 const struct name_entry
*n
,
1067 struct index_state
*istate
,
1069 int is_sparse_directory
)
1071 size_t len
= traverse_path_len(info
, tree_entry_len(n
));
1072 size_t alloc_len
= is_sparse_directory
? len
+ 1 : len
;
1073 struct cache_entry
*ce
=
1075 make_empty_transient_cache_entry(alloc_len
, NULL
) :
1076 make_empty_cache_entry(istate
, alloc_len
);
1078 ce
->ce_mode
= create_ce_mode(n
->mode
);
1079 ce
->ce_flags
= create_ce_flags(stage
);
1080 ce
->ce_namelen
= len
;
1081 oidcpy(&ce
->oid
, &n
->oid
);
1082 /* len+1 because the cache_entry allocates space for NUL */
1083 make_traverse_path(ce
->name
, len
+ 1, info
, n
->path
, n
->pathlen
);
1085 if (is_sparse_directory
) {
1086 ce
->name
[len
] = '/';
1087 ce
->name
[len
+ 1] = '\0';
1089 ce
->ce_flags
|= CE_SKIP_WORKTREE
;
1096 * Determine whether the path specified by 'p' should be unpacked as a new
1097 * sparse directory in a sparse index. A new sparse directory 'A/':
1098 * - must be outside the sparse cone.
1099 * - must not already be in the index (i.e., no index entry with name 'A/'
1101 * - must not have any child entries in the index (i.e., no index entry
1102 * 'A/<something>' exists).
1103 * If 'p' meets the above requirements, return 1; otherwise, return 0.
1105 static int entry_is_new_sparse_dir(const struct traverse_info
*info
,
1106 const struct name_entry
*p
)
1109 struct strbuf dirpath
= STRBUF_INIT
;
1110 struct unpack_trees_options
*o
= info
->data
;
1112 if (!S_ISDIR(p
->mode
))
1116 * If the path is inside the sparse cone, it can't be a sparse directory.
1118 strbuf_add(&dirpath
, info
->traverse_path
, info
->pathlen
);
1119 strbuf_add(&dirpath
, p
->path
, p
->pathlen
);
1120 strbuf_addch(&dirpath
, '/');
1121 if (path_in_cone_mode_sparse_checkout(dirpath
.buf
, o
->src_index
)) {
1126 pos
= index_name_pos_sparse(o
->src_index
, dirpath
.buf
, dirpath
.len
);
1128 /* Path is already in the index, not a new sparse dir */
1133 /* Where would this sparse dir be inserted into the index? */
1135 if (pos
>= o
->src_index
->cache_nr
) {
1137 * Sparse dir would be inserted at the end of the index, so we
1138 * know it has no child entries.
1145 * If the dir has child entries in the index, the first would be at the
1146 * position the sparse directory would be inserted. If the entry at this
1147 * position is inside the dir, not a new sparse dir.
1149 res
= strncmp(o
->src_index
->cache
[pos
]->name
, dirpath
.buf
, dirpath
.len
);
1152 strbuf_release(&dirpath
);
1157 * Note that traverse_by_cache_tree() duplicates some logic in this function
1158 * without actually calling it. If you change the logic here you may need to
1159 * check and change there as well.
1161 static int unpack_single_entry(int n
, unsigned long mask
,
1162 unsigned long dirmask
,
1163 struct cache_entry
**src
,
1164 const struct name_entry
*names
,
1165 const struct traverse_info
*info
,
1166 int *is_new_sparse_dir
)
1169 struct unpack_trees_options
*o
= info
->data
;
1170 unsigned long conflicts
= info
->df_conflicts
| dirmask
;
1171 const struct name_entry
*p
= names
;
1173 *is_new_sparse_dir
= 0;
1174 if (mask
== dirmask
&& !src
[0]) {
1176 * If we're not in a sparse index, we can't unpack a directory
1177 * without recursing into it, so we return.
1179 if (!o
->src_index
->sparse_index
)
1182 /* Find first entry with a real name (we could use "mask" too) */
1187 * If the directory is completely missing from the index but
1188 * would otherwise be a sparse directory, we should unpack it.
1189 * If not, we'll return and continue recursively traversing the
1192 *is_new_sparse_dir
= entry_is_new_sparse_dir(info
, p
);
1193 if (!*is_new_sparse_dir
)
1198 * When we are unpacking a sparse directory, then this isn't necessarily
1199 * a directory-file conflict.
1201 if (mask
== dirmask
&&
1202 (*is_new_sparse_dir
|| (src
[0] && S_ISSPARSEDIR(src
[0]->ce_mode
))))
1206 * Ok, we've filled in up to any potential index entry in src[0],
1209 for (i
= 0; i
< n
; i
++) {
1211 unsigned int bit
= 1ul << i
;
1212 if (conflicts
& bit
) {
1213 src
[i
+ o
->merge
] = o
->df_conflict_entry
;
1220 else if (i
+ 1 < o
->head_idx
)
1222 else if (i
+ 1 > o
->head_idx
)
1228 * If the merge bit is set, then the cache entries are
1229 * discarded in the following block. In this case,
1230 * construct "transient" cache_entries, as they are
1231 * not stored in the index. otherwise construct the
1232 * cache entry from the index aware logic.
1234 src
[i
+ o
->merge
] = create_ce_entry(info
, names
+ i
, stage
,
1235 &o
->internal
.result
,
1236 o
->merge
, bit
& dirmask
);
1240 int rc
= call_unpack_fn((const struct cache_entry
* const *)src
,
1242 for (i
= 0; i
< n
; i
++) {
1243 struct cache_entry
*ce
= src
[i
+ o
->merge
];
1244 if (ce
!= o
->df_conflict_entry
)
1245 discard_cache_entry(ce
);
1250 for (i
= 0; i
< n
; i
++)
1251 if (src
[i
] && src
[i
] != o
->df_conflict_entry
)
1252 if (do_add_entry(o
, src
[i
], 0, 0))
1258 static int unpack_failed(struct unpack_trees_options
*o
, const char *message
)
1260 discard_index(&o
->internal
.result
);
1261 if (!o
->quiet
&& !o
->exiting_early
) {
1263 return error("%s", message
);
1270 * The tree traversal is looking at name p. If we have a matching entry,
1271 * return it. If name p is a directory in the index, do not return
1272 * anything, as we will want to match it when the traversal descends into
1275 static int find_cache_pos(struct traverse_info
*info
,
1276 const char *p
, size_t p_len
)
1279 struct unpack_trees_options
*o
= info
->data
;
1280 struct index_state
*index
= o
->src_index
;
1281 int pfxlen
= info
->pathlen
;
1283 for (pos
= o
->internal
.cache_bottom
; pos
< index
->cache_nr
; pos
++) {
1284 const struct cache_entry
*ce
= index
->cache
[pos
];
1285 const char *ce_name
, *ce_slash
;
1288 if (ce
->ce_flags
& CE_UNPACKED
) {
1290 * cache_bottom entry is already unpacked, so
1291 * we can never match it; don't check it
1294 if (pos
== o
->internal
.cache_bottom
)
1295 ++o
->internal
.cache_bottom
;
1298 if (!ce_in_traverse_path(ce
, info
)) {
1300 * Check if we can skip future cache checks
1301 * (because we're already past all possible
1302 * entries in the traverse path).
1304 if (info
->traverse_path
) {
1305 if (strncmp(ce
->name
, info
->traverse_path
,
1311 ce_name
= ce
->name
+ pfxlen
;
1312 ce_slash
= strchr(ce_name
, '/');
1314 ce_len
= ce_slash
- ce_name
;
1316 ce_len
= ce_namelen(ce
) - pfxlen
;
1317 cmp
= name_compare(p
, p_len
, ce_name
, ce_len
);
1319 * Exact match; if we have a directory we need to
1320 * delay returning it.
1323 return ce_slash
? -2 - pos
: pos
;
1325 continue; /* keep looking */
1327 * ce_name sorts after p->path; could it be that we
1328 * have files under p->path directory in the index?
1329 * E.g. ce_name == "t-i", and p->path == "t"; we may
1330 * have "t/a" in the index.
1332 if (p_len
< ce_len
&& !memcmp(ce_name
, p
, p_len
) &&
1333 ce_name
[p_len
] < '/')
1334 continue; /* keep looking */
1341 * Given a sparse directory entry 'ce', compare ce->name to
1342 * info->traverse_path + p->path + '/' if info->traverse_path
1345 * Compare ce->name to p->path + '/' otherwise. Note that
1346 * ce->name must end in a trailing '/' because it is a sparse
1349 static int sparse_dir_matches_path(const struct cache_entry
*ce
,
1350 struct traverse_info
*info
,
1351 const struct name_entry
*p
)
1353 assert(S_ISSPARSEDIR(ce
->ce_mode
));
1354 assert(ce
->name
[ce
->ce_namelen
- 1] == '/');
1357 return ce
->ce_namelen
== info
->pathlen
+ p
->pathlen
+ 1 &&
1358 ce
->name
[info
->pathlen
- 1] == '/' &&
1359 !strncmp(ce
->name
, info
->traverse_path
, info
->pathlen
) &&
1360 !strncmp(ce
->name
+ info
->pathlen
, p
->path
, p
->pathlen
);
1361 return ce
->ce_namelen
== p
->pathlen
+ 1 &&
1362 !strncmp(ce
->name
, p
->path
, p
->pathlen
);
1365 static struct cache_entry
*find_cache_entry(struct traverse_info
*info
,
1366 const struct name_entry
*p
)
1369 int pos
= find_cache_pos(info
, p
->path
, p
->pathlen
);
1370 struct unpack_trees_options
*o
= info
->data
;
1373 return o
->src_index
->cache
[pos
];
1376 * Check for a sparse-directory entry named "path/".
1377 * Due to the input p->path not having a trailing
1378 * slash, the negative 'pos' value overshoots the
1379 * expected position, hence "-2" instead of "-1".
1383 if (pos
< 0 || pos
>= o
->src_index
->cache_nr
)
1387 * Due to lexicographic sorting and sparse directory
1388 * entries ending with a trailing slash, our path as a
1389 * sparse directory (e.g "subdir/") and our path as a
1390 * file (e.g. "subdir") might be separated by other
1391 * paths (e.g. "subdir-").
1394 struct cache_entry
*ce
= o
->src_index
->cache
[pos
];
1396 if (!skip_prefix(ce
->name
, info
->traverse_path
, &path
) ||
1397 strncmp(path
, p
->path
, p
->pathlen
) ||
1398 path
[p
->pathlen
] != '/')
1401 if (S_ISSPARSEDIR(ce
->ce_mode
) &&
1402 sparse_dir_matches_path(ce
, info
, p
))
1411 static void debug_path(struct traverse_info
*info
)
1414 debug_path(info
->prev
);
1415 if (*info
->prev
->name
)
1418 printf("%s", info
->name
);
1421 static void debug_name_entry(int i
, struct name_entry
*n
)
1423 printf("ent#%d %06o %s\n", i
,
1424 n
->path
? n
->mode
: 0,
1425 n
->path
? n
->path
: "(missing)");
1428 static void debug_unpack_callback(int n
,
1430 unsigned long dirmask
,
1431 struct name_entry
*names
,
1432 struct traverse_info
*info
)
1435 printf("* unpack mask %lu, dirmask %lu, cnt %d ",
1439 for (i
= 0; i
< n
; i
++)
1440 debug_name_entry(i
, names
+ i
);
1444 * Returns true if and only if the given cache_entry is a
1445 * sparse-directory entry that matches the given name_entry
1446 * from the tree walk at the given traverse_info.
1448 static int is_sparse_directory_entry(struct cache_entry
*ce
,
1449 const struct name_entry
*name
,
1450 struct traverse_info
*info
)
1452 if (!ce
|| !name
|| !S_ISSPARSEDIR(ce
->ce_mode
))
1455 return sparse_dir_matches_path(ce
, info
, name
);
1458 static int unpack_sparse_callback(int n
, unsigned long mask
, unsigned long dirmask
, struct name_entry
*names
, struct traverse_info
*info
)
1460 struct cache_entry
*src
[MAX_UNPACK_TREES
+ 1] = { NULL
, };
1461 struct unpack_trees_options
*o
= info
->data
;
1462 int ret
, is_new_sparse_dir
;
1467 * Unlike in 'unpack_callback', where src[0] is derived from the index when
1468 * merging, src[0] is a transient cache entry derived from the first tree
1469 * provided. Create the temporary entry as if it came from a non-sparse index.
1471 if (!is_null_oid(&names
[0].oid
)) {
1472 src
[0] = create_ce_entry(info
, &names
[0], 0,
1473 &o
->internal
.result
, 1,
1474 dirmask
& (1ul << 0));
1475 src
[0]->ce_flags
|= (CE_SKIP_WORKTREE
| CE_NEW_SKIP_WORKTREE
);
1479 * 'unpack_single_entry' assumes that src[0] is derived directly from
1480 * the index, rather than from an entry in 'names'. This is *not* true when
1481 * merging a sparse directory, in which case names[0] is the "index" source
1482 * entry. To match the expectations of 'unpack_single_entry', shift past the
1483 * "index" tree (i.e., names[0]) and adjust 'names', 'n', 'mask', and
1484 * 'dirmask' accordingly.
1486 ret
= unpack_single_entry(n
- 1, mask
>> 1, dirmask
>> 1, src
, names
+ 1, info
, &is_new_sparse_dir
);
1489 discard_cache_entry(src
[0]);
1491 return ret
>= 0 ? mask
: -1;
1495 * Note that traverse_by_cache_tree() duplicates some logic in this function
1496 * without actually calling it. If you change the logic here you may need to
1497 * check and change there as well.
1499 static int unpack_callback(int n
, unsigned long mask
, unsigned long dirmask
, struct name_entry
*names
, struct traverse_info
*info
)
1501 struct cache_entry
*src
[MAX_UNPACK_TREES
+ 1] = { NULL
, };
1502 struct unpack_trees_options
*o
= info
->data
;
1503 const struct name_entry
*p
= names
;
1504 int is_new_sparse_dir
;
1506 /* Find first entry with a real name (we could use "mask" too) */
1510 if (o
->internal
.debug_unpack
)
1511 debug_unpack_callback(n
, mask
, dirmask
, names
, info
);
1513 /* Are we supposed to look at the index too? */
1517 struct cache_entry
*ce
;
1519 if (o
->diff_index_cached
)
1520 ce
= next_cache_entry(o
);
1522 ce
= find_cache_entry(info
, p
);
1526 cmp
= compare_entry(ce
, info
, p
);
1528 if (unpack_index_entry(ce
, o
) < 0)
1529 return unpack_failed(o
, NULL
);
1535 * If we skip unmerged index
1536 * entries, we'll skip this
1537 * entry *and* the tree
1538 * entries associated with it!
1540 if (o
->skip_unmerged
) {
1541 add_same_unmerged(ce
, o
);
1551 if (unpack_single_entry(n
, mask
, dirmask
, src
, names
, info
, &is_new_sparse_dir
))
1554 if (o
->merge
&& src
[0]) {
1555 if (ce_stage(src
[0]))
1556 mark_ce_used_same_name(src
[0], o
);
1558 mark_ce_used(src
[0], o
);
1561 /* Now handle any directories.. */
1563 /* special case: "diff-index --cached" looking at a tree */
1564 if (o
->diff_index_cached
&&
1565 n
== 1 && dirmask
== 1 && S_ISDIR(names
->mode
)) {
1567 matches
= cache_tree_matches_traversal(o
->src_index
->cache_tree
,
1570 * Everything under the name matches; skip the
1571 * entire hierarchy. diff_index_cached codepath
1572 * special cases D/F conflicts in such a way that
1573 * it does not do any look-ahead, so this is safe.
1577 * Only increment the cache_bottom if the
1578 * directory isn't a sparse directory index
1579 * entry (if it is, it was already incremented)
1580 * in 'mark_ce_used()'
1582 if (!src
[0] || !S_ISSPARSEDIR(src
[0]->ce_mode
))
1583 o
->internal
.cache_bottom
+= matches
;
1588 if (!is_sparse_directory_entry(src
[0], p
, info
) &&
1589 !is_new_sparse_dir
&&
1590 traverse_trees_recursive(n
, dirmask
, mask
& ~dirmask
,
1601 static int clear_ce_flags_1(struct index_state
*istate
,
1602 struct cache_entry
**cache
, int nr
,
1603 struct strbuf
*prefix
,
1604 int select_mask
, int clear_mask
,
1605 struct pattern_list
*pl
,
1606 enum pattern_match_result default_match
,
1609 /* Whole directory matching */
1610 static int clear_ce_flags_dir(struct index_state
*istate
,
1611 struct cache_entry
**cache
, int nr
,
1612 struct strbuf
*prefix
,
1614 int select_mask
, int clear_mask
,
1615 struct pattern_list
*pl
,
1616 enum pattern_match_result default_match
,
1619 struct cache_entry
**cache_end
;
1622 enum pattern_match_result ret
, orig_ret
;
1623 orig_ret
= path_matches_pattern_list(prefix
->buf
, prefix
->len
,
1624 basename
, &dtype
, pl
, istate
);
1626 strbuf_addch(prefix
, '/');
1628 /* If undecided, use matching result of parent dir in defval */
1629 if (orig_ret
== UNDECIDED
)
1630 ret
= default_match
;
1634 for (cache_end
= cache
; cache_end
!= cache
+ nr
; cache_end
++) {
1635 struct cache_entry
*ce
= *cache_end
;
1636 if (strncmp(ce
->name
, prefix
->buf
, prefix
->len
))
1640 if (pl
->use_cone_patterns
&& orig_ret
== MATCHED_RECURSIVE
) {
1641 struct cache_entry
**ce
= cache
;
1642 rc
= cache_end
- cache
;
1644 while (ce
< cache_end
) {
1645 (*ce
)->ce_flags
&= ~clear_mask
;
1648 } else if (pl
->use_cone_patterns
&& orig_ret
== NOT_MATCHED
) {
1649 rc
= cache_end
- cache
;
1651 rc
= clear_ce_flags_1(istate
, cache
, cache_end
- cache
,
1653 select_mask
, clear_mask
,
1658 strbuf_setlen(prefix
, prefix
->len
- 1);
1663 * Traverse the index, find every entry that matches according to
1664 * o->pl. Do "ce_flags &= ~clear_mask" on those entries. Return the
1665 * number of traversed entries.
1667 * If select_mask is non-zero, only entries whose ce_flags has on of
1668 * those bits enabled are traversed.
1670 * cache : pointer to an index entry
1671 * prefix_len : an offset to its path
1673 * The current path ("prefix") including the trailing '/' is
1674 * cache[0]->name[0..(prefix_len-1)]
1675 * Top level path has prefix_len zero.
1677 static int clear_ce_flags_1(struct index_state
*istate
,
1678 struct cache_entry
**cache
, int nr
,
1679 struct strbuf
*prefix
,
1680 int select_mask
, int clear_mask
,
1681 struct pattern_list
*pl
,
1682 enum pattern_match_result default_match
,
1685 struct cache_entry
**cache_end
= nr
? cache
+ nr
: cache
;
1688 * Process all entries that have the given prefix and meet
1689 * select_mask condition
1691 while(cache
!= cache_end
) {
1692 struct cache_entry
*ce
= *cache
;
1693 const char *name
, *slash
;
1695 enum pattern_match_result ret
;
1697 display_progress(istate
->progress
, progress_nr
);
1699 if (select_mask
&& !(ce
->ce_flags
& select_mask
)) {
1705 if (prefix
->len
&& strncmp(ce
->name
, prefix
->buf
, prefix
->len
))
1708 name
= ce
->name
+ prefix
->len
;
1709 slash
= strchr(name
, '/');
1711 /* If it's a directory, try whole directory match first */
1716 strbuf_add(prefix
, name
, len
);
1718 processed
= clear_ce_flags_dir(istate
, cache
, cache_end
- cache
,
1720 prefix
->buf
+ prefix
->len
- len
,
1721 select_mask
, clear_mask
,
1725 /* clear_c_f_dir eats a whole dir already? */
1728 progress_nr
+= processed
;
1729 strbuf_setlen(prefix
, prefix
->len
- len
);
1733 strbuf_addch(prefix
, '/');
1734 processed
= clear_ce_flags_1(istate
, cache
, cache_end
- cache
,
1736 select_mask
, clear_mask
, pl
,
1737 default_match
, progress_nr
);
1740 progress_nr
+= processed
;
1742 strbuf_setlen(prefix
, prefix
->len
- len
- 1);
1747 dtype
= ce_to_dtype(ce
);
1748 ret
= path_matches_pattern_list(ce
->name
,
1750 name
, &dtype
, pl
, istate
);
1751 if (ret
== UNDECIDED
)
1752 ret
= default_match
;
1753 if (ret
== MATCHED
|| ret
== MATCHED_RECURSIVE
)
1754 ce
->ce_flags
&= ~clear_mask
;
1759 display_progress(istate
->progress
, progress_nr
);
1760 return nr
- (cache_end
- cache
);
1763 static int clear_ce_flags(struct index_state
*istate
,
1764 int select_mask
, int clear_mask
,
1765 struct pattern_list
*pl
,
1768 static struct strbuf prefix
= STRBUF_INIT
;
1772 strbuf_reset(&prefix
);
1774 istate
->progress
= start_delayed_progress(
1775 _("Updating index flags"),
1778 xsnprintf(label
, sizeof(label
), "clear_ce_flags(0x%08lx,0x%08lx)",
1779 (unsigned long)select_mask
, (unsigned long)clear_mask
);
1780 trace2_region_enter("unpack_trees", label
, the_repository
);
1781 rval
= clear_ce_flags_1(istate
,
1785 select_mask
, clear_mask
,
1787 trace2_region_leave("unpack_trees", label
, the_repository
);
1789 stop_progress(&istate
->progress
);
1794 * Set/Clear CE_NEW_SKIP_WORKTREE according to $GIT_DIR/info/sparse-checkout
1796 static void mark_new_skip_worktree(struct pattern_list
*pl
,
1797 struct index_state
*istate
,
1798 int select_flag
, int skip_wt_flag
,
1804 * 1. Pretend the narrowest worktree: only unmerged entries
1807 for (i
= 0; i
< istate
->cache_nr
; i
++) {
1808 struct cache_entry
*ce
= istate
->cache
[i
];
1810 if (select_flag
&& !(ce
->ce_flags
& select_flag
))
1813 if (!ce_stage(ce
) && !(ce
->ce_flags
& CE_CONFLICTED
))
1814 ce
->ce_flags
|= skip_wt_flag
;
1816 ce
->ce_flags
&= ~skip_wt_flag
;
1820 * 2. Widen worktree according to sparse-checkout file.
1821 * Matched entries will have skip_wt_flag cleared (i.e. "in")
1823 clear_ce_flags(istate
, select_flag
, skip_wt_flag
, pl
, show_progress
);
1826 static void populate_from_existing_patterns(struct unpack_trees_options
*o
,
1827 struct pattern_list
*pl
)
1829 if (get_sparse_checkout_patterns(pl
) < 0)
1830 o
->skip_sparse_checkout
= 1;
1832 o
->internal
.pl
= pl
;
1835 static void update_sparsity_for_prefix(const char *prefix
,
1836 struct index_state
*istate
)
1838 int prefix_len
= strlen(prefix
);
1839 struct strbuf ce_prefix
= STRBUF_INIT
;
1841 if (!istate
->sparse_index
)
1844 while (prefix_len
> 0 && prefix
[prefix_len
- 1] == '/')
1847 if (prefix_len
<= 0)
1848 BUG("Invalid prefix passed to update_sparsity_for_prefix");
1850 strbuf_grow(&ce_prefix
, prefix_len
+ 1);
1851 strbuf_add(&ce_prefix
, prefix
, prefix_len
);
1852 strbuf_addch(&ce_prefix
, '/');
1855 * If the prefix points to a sparse directory or a path inside a sparse
1856 * directory, the index should be expanded. This is accomplished in one
1858 * - if the prefix is inside a sparse directory, it will be expanded by
1859 * the 'ensure_full_index(...)' call in 'index_name_pos(...)'.
1860 * - if the prefix matches an existing sparse directory entry,
1861 * 'index_name_pos(...)' will return its index position, triggering
1862 * the 'ensure_full_index(...)' below.
1864 if (!path_in_cone_mode_sparse_checkout(ce_prefix
.buf
, istate
) &&
1865 index_name_pos(istate
, ce_prefix
.buf
, ce_prefix
.len
) >= 0)
1866 ensure_full_index(istate
);
1868 strbuf_release(&ce_prefix
);
1871 static int verify_absent(const struct cache_entry
*,
1872 enum unpack_trees_error_types
,
1873 struct unpack_trees_options
*);
1875 * N-way merge "len" trees. Returns 0 on success, -1 on failure to manipulate the
1876 * resulting index, -2 on failure to reflect the changes to the work tree.
1878 * CE_ADDED, CE_UNPACKED and CE_NEW_SKIP_WORKTREE are used internally
1880 int unpack_trees(unsigned len
, struct tree_desc
*t
, struct unpack_trees_options
*o
)
1882 struct repository
*repo
= the_repository
;
1884 static struct cache_entry
*dfc
;
1885 struct pattern_list pl
;
1886 int free_pattern_list
= 0;
1887 struct dir_struct dir
= DIR_INIT
;
1889 if (o
->reset
== UNPACK_RESET_INVALID
)
1890 BUG("o->reset had a value of 1; should be UNPACK_TREES_*_UNTRACKED");
1892 if (len
> MAX_UNPACK_TREES
)
1893 die("unpack_trees takes at most %d trees", MAX_UNPACK_TREES
);
1894 if (o
->internal
.dir
)
1895 BUG("o->internal.dir is for internal use only");
1897 BUG("o->internal.pl is for internal use only");
1898 if (o
->df_conflict_entry
)
1899 BUG("o->df_conflict_entry is an output only field");
1901 trace_performance_enter();
1902 trace2_region_enter("unpack_trees", "unpack_trees", the_repository
);
1904 prepare_repo_settings(repo
);
1905 if (repo
->settings
.command_requires_full_index
) {
1906 ensure_full_index(o
->src_index
);
1908 ensure_full_index(o
->dst_index
);
1911 if (o
->reset
== UNPACK_RESET_OVERWRITE_UNTRACKED
&&
1912 o
->preserve_ignored
)
1913 BUG("UNPACK_RESET_OVERWRITE_UNTRACKED incompatible with preserved ignored files");
1915 if (!o
->preserve_ignored
) {
1916 o
->internal
.dir
= &dir
;
1917 o
->internal
.dir
->flags
|= DIR_SHOW_IGNORED
;
1918 setup_standard_excludes(o
->internal
.dir
);
1922 update_sparsity_for_prefix(o
->prefix
, o
->src_index
);
1924 if (!core_apply_sparse_checkout
|| !o
->update
)
1925 o
->skip_sparse_checkout
= 1;
1926 if (!o
->skip_sparse_checkout
) {
1927 memset(&pl
, 0, sizeof(pl
));
1928 free_pattern_list
= 1;
1929 populate_from_existing_patterns(o
, &pl
);
1932 index_state_init(&o
->internal
.result
, o
->src_index
->repo
);
1933 o
->internal
.result
.initialized
= 1;
1934 o
->internal
.result
.timestamp
.sec
= o
->src_index
->timestamp
.sec
;
1935 o
->internal
.result
.timestamp
.nsec
= o
->src_index
->timestamp
.nsec
;
1936 o
->internal
.result
.version
= o
->src_index
->version
;
1937 if (!o
->src_index
->split_index
) {
1938 o
->internal
.result
.split_index
= NULL
;
1939 } else if (o
->src_index
== o
->dst_index
) {
1941 * o->dst_index (and thus o->src_index) will be discarded
1942 * and overwritten with o->internal.result at the end of
1943 * this function, so just use src_index's split_index to
1944 * avoid having to create a new one.
1946 o
->internal
.result
.split_index
= o
->src_index
->split_index
;
1947 if (o
->src_index
->cache_changed
& SPLIT_INDEX_ORDERED
)
1948 o
->internal
.result
.cache_changed
|= SPLIT_INDEX_ORDERED
;
1949 o
->internal
.result
.split_index
->refcount
++;
1951 o
->internal
.result
.split_index
=
1952 init_split_index(&o
->internal
.result
);
1954 oidcpy(&o
->internal
.result
.oid
, &o
->src_index
->oid
);
1955 o
->internal
.merge_size
= len
;
1956 mark_all_ce_unused(o
->src_index
);
1958 o
->internal
.result
.fsmonitor_last_update
=
1959 xstrdup_or_null(o
->src_index
->fsmonitor_last_update
);
1960 o
->internal
.result
.fsmonitor_has_run_once
= o
->src_index
->fsmonitor_has_run_once
;
1962 if (!o
->src_index
->initialized
&&
1963 !repo
->settings
.command_requires_full_index
&&
1964 is_sparse_index_allowed(&o
->internal
.result
, 0))
1965 o
->internal
.result
.sparse_index
= 1;
1968 * Sparse checkout loop #1: set NEW_SKIP_WORKTREE on existing entries
1970 if (!o
->skip_sparse_checkout
)
1971 mark_new_skip_worktree(o
->internal
.pl
, o
->src_index
, 0,
1972 CE_NEW_SKIP_WORKTREE
, o
->verbose_update
);
1975 dfc
= xcalloc(1, cache_entry_size(0));
1976 o
->df_conflict_entry
= dfc
;
1979 const char *prefix
= o
->prefix
? o
->prefix
: "";
1980 struct traverse_info info
;
1982 setup_traverse_info(&info
, prefix
);
1983 info
.fn
= unpack_callback
;
1985 info
.show_all_errors
= o
->internal
.show_all_errors
;
1986 info
.pathspec
= o
->pathspec
;
1990 * Unpack existing index entries that sort before the
1991 * prefix the tree is spliced into. Note that o->merge
1992 * is always true in this case.
1995 struct cache_entry
*ce
= next_cache_entry(o
);
1998 if (ce_in_traverse_path(ce
, &info
))
2000 if (unpack_index_entry(ce
, o
) < 0)
2005 trace_performance_enter();
2006 trace2_region_enter("unpack_trees", "traverse_trees", the_repository
);
2007 ret
= traverse_trees(o
->src_index
, len
, t
, &info
);
2008 trace2_region_leave("unpack_trees", "traverse_trees", the_repository
);
2009 trace_performance_leave("traverse_trees");
2014 /* Any left-over entries in the index? */
2017 struct cache_entry
*ce
= next_cache_entry(o
);
2020 if (unpack_index_entry(ce
, o
) < 0)
2024 mark_all_ce_unused(o
->src_index
);
2026 if (o
->trivial_merges_only
&& o
->internal
.nontrivial_merge
) {
2027 ret
= unpack_failed(o
, "Merge requires file-level merging");
2031 if (!o
->skip_sparse_checkout
) {
2033 * Sparse checkout loop #2: set NEW_SKIP_WORKTREE on entries not in loop #1
2034 * If they will have NEW_SKIP_WORKTREE, also set CE_SKIP_WORKTREE
2035 * so apply_sparse_checkout() won't attempt to remove it from worktree
2037 mark_new_skip_worktree(o
->internal
.pl
, &o
->internal
.result
,
2038 CE_ADDED
, CE_SKIP_WORKTREE
| CE_NEW_SKIP_WORKTREE
,
2042 for (i
= 0; i
< o
->internal
.result
.cache_nr
; i
++) {
2043 struct cache_entry
*ce
= o
->internal
.result
.cache
[i
];
2046 * Entries marked with CE_ADDED in merged_entry() do not have
2047 * verify_absent() check (the check is effectively disabled
2048 * because CE_NEW_SKIP_WORKTREE is set unconditionally).
2050 * Do the real check now because we have had
2051 * correct CE_NEW_SKIP_WORKTREE
2053 if (ce
->ce_flags
& CE_ADDED
&&
2054 verify_absent(ce
, WARNING_SPARSE_ORPHANED_NOT_OVERWRITTEN
, o
))
2057 if (apply_sparse_checkout(&o
->internal
.result
, ce
, o
))
2062 * Inability to sparsify or de-sparsify individual
2063 * paths is not an error, but just a warning.
2065 if (o
->internal
.show_all_errors
)
2066 display_warning_msgs(o
);
2071 ret
= check_updates(o
, &o
->internal
.result
) ? (-2) : 0;
2073 move_index_extensions(&o
->internal
.result
, o
->src_index
);
2075 if (git_env_bool("GIT_TEST_CHECK_CACHE_TREE", 0) &&
2076 cache_tree_verify(the_repository
,
2077 &o
->internal
.result
) < 0) {
2082 if (!o
->skip_cache_tree_update
&&
2083 !cache_tree_fully_valid(o
->internal
.result
.cache_tree
))
2084 cache_tree_update(&o
->internal
.result
,
2089 o
->internal
.result
.updated_workdir
= 1;
2090 discard_index(o
->dst_index
);
2091 *o
->dst_index
= o
->internal
.result
;
2092 memset(&o
->internal
.result
, 0, sizeof(o
->internal
.result
));
2094 discard_index(&o
->internal
.result
);
2096 o
->src_index
= NULL
;
2099 if (free_pattern_list
)
2100 clear_pattern_list(&pl
);
2101 if (o
->internal
.dir
) {
2102 dir_clear(o
->internal
.dir
);
2103 o
->internal
.dir
= NULL
;
2105 trace2_region_leave("unpack_trees", "unpack_trees", the_repository
);
2106 trace_performance_leave("unpack_trees");
2110 if (o
->internal
.show_all_errors
)
2111 display_error_msgs(o
);
2112 mark_all_ce_unused(o
->src_index
);
2113 ret
= unpack_failed(o
, NULL
);
2114 if (o
->exiting_early
)
2120 * Update SKIP_WORKTREE bits according to sparsity patterns, and update
2121 * working directory to match.
2123 * CE_NEW_SKIP_WORKTREE is used internally.
2125 enum update_sparsity_result
update_sparsity(struct unpack_trees_options
*o
,
2126 struct pattern_list
*pl
)
2128 enum update_sparsity_result ret
= UPDATE_SPARSITY_SUCCESS
;
2130 unsigned old_show_all_errors
;
2131 int free_pattern_list
= 0;
2133 old_show_all_errors
= o
->internal
.show_all_errors
;
2134 o
->internal
.show_all_errors
= 1;
2135 index_state_init(&o
->internal
.result
, o
->src_index
->repo
);
2138 if (!o
->update
|| o
->index_only
|| o
->skip_sparse_checkout
)
2139 BUG("update_sparsity() is for reflecting sparsity patterns in working directory");
2140 if (o
->src_index
!= o
->dst_index
|| o
->fn
)
2141 BUG("update_sparsity() called wrong");
2143 trace_performance_enter();
2145 /* If we weren't given patterns, use the recorded ones */
2147 free_pattern_list
= 1;
2148 pl
= xcalloc(1, sizeof(*pl
));
2149 populate_from_existing_patterns(o
, pl
);
2151 o
->internal
.pl
= pl
;
2153 /* Expand sparse directories as needed */
2154 expand_index(o
->src_index
, o
->internal
.pl
);
2156 /* Set NEW_SKIP_WORKTREE on existing entries. */
2157 mark_all_ce_unused(o
->src_index
);
2158 mark_new_skip_worktree(o
->internal
.pl
, o
->src_index
, 0,
2159 CE_NEW_SKIP_WORKTREE
, o
->verbose_update
);
2161 /* Then loop over entries and update/remove as needed */
2162 ret
= UPDATE_SPARSITY_SUCCESS
;
2163 for (i
= 0; i
< o
->src_index
->cache_nr
; i
++) {
2164 struct cache_entry
*ce
= o
->src_index
->cache
[i
];
2168 /* -1 because for loop will increment by 1 */
2169 i
+= warn_conflicted_path(o
->src_index
, i
, o
) - 1;
2170 ret
= UPDATE_SPARSITY_WARNINGS
;
2174 if (apply_sparse_checkout(o
->src_index
, ce
, o
))
2175 ret
= UPDATE_SPARSITY_WARNINGS
;
2178 if (check_updates(o
, o
->src_index
))
2179 ret
= UPDATE_SPARSITY_WORKTREE_UPDATE_FAILURES
;
2181 display_warning_msgs(o
);
2182 o
->internal
.show_all_errors
= old_show_all_errors
;
2183 if (free_pattern_list
) {
2184 clear_pattern_list(pl
);
2186 o
->internal
.pl
= NULL
;
2188 trace_performance_leave("update_sparsity");
2192 /* Here come the merge functions */
2194 static int reject_merge(const struct cache_entry
*ce
,
2195 struct unpack_trees_options
*o
)
2197 return add_rejected_path(o
, ERROR_WOULD_OVERWRITE
, ce
->name
);
2200 static int same(const struct cache_entry
*a
, const struct cache_entry
*b
)
2206 if ((a
->ce_flags
| b
->ce_flags
) & CE_CONFLICTED
)
2208 return a
->ce_mode
== b
->ce_mode
&&
2209 oideq(&a
->oid
, &b
->oid
);
2214 * When a CE gets turned into an unmerged entry, we
2215 * want it to be up-to-date
2217 static int verify_uptodate_1(const struct cache_entry
*ce
,
2218 struct unpack_trees_options
*o
,
2219 enum unpack_trees_error_types error_type
)
2227 * CE_VALID and CE_SKIP_WORKTREE cheat, we better check again
2228 * if this entry is truly up-to-date because this file may be
2231 if ((ce
->ce_flags
& CE_VALID
) || ce_skip_worktree(ce
))
2232 ; /* keep checking */
2233 else if (o
->reset
|| ce_uptodate(ce
))
2236 if (!lstat(ce
->name
, &st
)) {
2237 int flags
= CE_MATCH_IGNORE_VALID
|CE_MATCH_IGNORE_SKIP_WORKTREE
;
2238 unsigned changed
= ie_match_stat(o
->src_index
, ce
, &st
, flags
);
2240 if (submodule_from_ce(ce
)) {
2241 int r
= check_submodule_move_head(ce
,
2242 "HEAD", oid_to_hex(&ce
->oid
), o
);
2244 return add_rejected_path(o
, error_type
, ce
->name
);
2251 * Historic default policy was to allow submodule to be out
2252 * of sync wrt the superproject index. If the submodule was
2253 * not considered interesting above, we don't care here.
2255 if (S_ISGITLINK(ce
->ce_mode
))
2260 if (errno
== ENOENT
)
2262 return add_rejected_path(o
, error_type
, ce
->name
);
2265 int verify_uptodate(const struct cache_entry
*ce
,
2266 struct unpack_trees_options
*o
)
2268 if (!o
->skip_sparse_checkout
&&
2269 (ce
->ce_flags
& CE_SKIP_WORKTREE
) &&
2270 (ce
->ce_flags
& CE_NEW_SKIP_WORKTREE
))
2272 return verify_uptodate_1(ce
, o
, ERROR_NOT_UPTODATE_FILE
);
2275 static int verify_uptodate_sparse(const struct cache_entry
*ce
,
2276 struct unpack_trees_options
*o
)
2278 return verify_uptodate_1(ce
, o
, WARNING_SPARSE_NOT_UPTODATE_FILE
);
2282 * TODO: We should actually invalidate o->internal.result, not src_index [1].
2283 * But since cache tree and untracked cache both are not copied to
2284 * o->internal.result until unpacking is complete, we invalidate them on
2285 * src_index instead with the assumption that they will be copied to
2286 * dst_index at the end.
2288 * [1] src_index->cache_tree is also used in unpack_callback() so if
2289 * we invalidate o->internal.result, we need to update it to use
2290 * o->internal.result.cache_tree as well.
2292 static void invalidate_ce_path(const struct cache_entry
*ce
,
2293 struct unpack_trees_options
*o
)
2297 cache_tree_invalidate_path(o
->src_index
, ce
->name
);
2298 untracked_cache_invalidate_path(o
->src_index
, ce
->name
, 1);
2302 * Check that checking out ce->sha1 in subdir ce->name is not
2303 * going to overwrite any working files.
2305 static int verify_clean_submodule(const char *old_sha1
,
2306 const struct cache_entry
*ce
,
2307 struct unpack_trees_options
*o
)
2309 if (!submodule_from_ce(ce
))
2312 return check_submodule_move_head(ce
, old_sha1
,
2313 oid_to_hex(&ce
->oid
), o
);
2316 static int verify_clean_subdirectory(const struct cache_entry
*ce
,
2317 struct unpack_trees_options
*o
)
2320 * we are about to extract "ce->name"; we would not want to lose
2321 * anything in the existing directory there.
2325 struct dir_struct d
;
2329 if (S_ISGITLINK(ce
->ce_mode
)) {
2330 struct object_id oid
;
2331 int sub_head
= repo_resolve_gitlink_ref(the_repository
, ce
->name
,
2334 * If we are not going to update the submodule, then
2337 if (!sub_head
&& oideq(&oid
, &ce
->oid
))
2339 return verify_clean_submodule(sub_head
? NULL
: oid_to_hex(&oid
),
2344 * First let's make sure we do not have a local modification
2345 * in that directory.
2347 namelen
= ce_namelen(ce
);
2348 for (i
= locate_in_src_index(ce
, o
);
2349 i
< o
->src_index
->cache_nr
;
2351 struct cache_entry
*ce2
= o
->src_index
->cache
[i
];
2352 int len
= ce_namelen(ce2
);
2353 if (len
< namelen
||
2354 strncmp(ce
->name
, ce2
->name
, namelen
) ||
2355 ce2
->name
[namelen
] != '/')
2358 * ce2->name is an entry in the subdirectory to be
2361 if (!ce_stage(ce2
)) {
2362 if (verify_uptodate(ce2
, o
))
2364 add_entry(o
, ce2
, CE_REMOVE
, 0);
2365 invalidate_ce_path(ce
, o
);
2366 mark_ce_used(ce2
, o
);
2371 /* Do not lose a locally present file that is not ignored. */
2372 pathbuf
= xstrfmt("%.*s/", namelen
, ce
->name
);
2374 memset(&d
, 0, sizeof(d
));
2375 if (o
->internal
.dir
)
2376 setup_standard_excludes(&d
);
2377 i
= read_directory(&d
, o
->src_index
, pathbuf
, namelen
+1, NULL
);
2381 return add_rejected_path(o
, ERROR_NOT_UPTODATE_DIR
, ce
->name
);
2383 /* Do not lose startup_info->original_cwd */
2384 if (startup_info
->original_cwd
&&
2385 !strcmp(startup_info
->original_cwd
, ce
->name
))
2386 return add_rejected_path(o
, ERROR_CWD_IN_THE_WAY
, ce
->name
);
2392 * This gets called when there was no index entry for the tree entry 'dst',
2393 * but we found a file in the working tree that 'lstat()' said was fine,
2394 * and we're on a case-insensitive filesystem.
2396 * See if we can find a case-insensitive match in the index that also
2397 * matches the stat information, and assume it's that other file!
2399 static int icase_exists(struct unpack_trees_options
*o
, const char *name
, int len
, struct stat
*st
)
2401 const struct cache_entry
*src
;
2403 src
= index_file_exists(o
->src_index
, name
, len
, 1);
2404 return src
&& !ie_match_stat(o
->src_index
, src
, st
, CE_MATCH_IGNORE_VALID
|CE_MATCH_IGNORE_SKIP_WORKTREE
);
2407 enum absent_checking_type
{
2409 ABSENT_ANY_DIRECTORY
2412 static int check_ok_to_remove(const char *name
, int len
, int dtype
,
2413 const struct cache_entry
*ce
, struct stat
*st
,
2414 enum unpack_trees_error_types error_type
,
2415 enum absent_checking_type absent_type
,
2416 struct unpack_trees_options
*o
)
2418 const struct cache_entry
*result
;
2421 * It may be that the 'lstat()' succeeded even though
2422 * target 'ce' was absent, because there is an old
2423 * entry that is different only in case..
2425 * Ignore that lstat() if it matches.
2427 if (ignore_case
&& icase_exists(o
, name
, len
, st
))
2430 if (o
->internal
.dir
&&
2431 is_excluded(o
->internal
.dir
, o
->src_index
, name
, &dtype
))
2433 * ce->name is explicitly excluded, so it is Ok to
2437 if (S_ISDIR(st
->st_mode
)) {
2439 * We are checking out path "foo" and
2440 * found "foo/." in the working tree.
2441 * This is tricky -- if we have modified
2442 * files that are in "foo/" we would lose
2445 if (verify_clean_subdirectory(ce
, o
) < 0)
2450 /* If we only care about directories, then we can remove */
2451 if (absent_type
== ABSENT_ANY_DIRECTORY
)
2455 * The previous round may already have decided to
2456 * delete this path, which is in a subdirectory that
2457 * is being replaced with a blob.
2459 result
= index_file_exists(&o
->internal
.result
, name
, len
, 0);
2461 if (result
->ce_flags
& CE_REMOVE
)
2465 return add_rejected_path(o
, error_type
, name
);
2469 * We do not want to remove or overwrite a working tree file that
2470 * is not tracked, unless it is ignored.
2472 static int verify_absent_1(const struct cache_entry
*ce
,
2473 enum unpack_trees_error_types error_type
,
2474 enum absent_checking_type absent_type
,
2475 struct unpack_trees_options
*o
)
2480 if (o
->index_only
|| !o
->update
)
2483 if (o
->reset
== UNPACK_RESET_OVERWRITE_UNTRACKED
) {
2484 /* Avoid nuking startup_info->original_cwd... */
2485 if (startup_info
->original_cwd
&&
2486 !strcmp(startup_info
->original_cwd
, ce
->name
))
2487 return add_rejected_path(o
, ERROR_CWD_IN_THE_WAY
,
2489 /* ...but nuke anything else. */
2493 len
= check_leading_path(ce
->name
, ce_namelen(ce
), 0);
2500 path
= xmemdupz(ce
->name
, len
);
2501 if (lstat(path
, &st
))
2502 ret
= error_errno("cannot stat '%s'", path
);
2504 if (submodule_from_ce(ce
))
2505 ret
= check_submodule_move_head(ce
,
2506 oid_to_hex(&ce
->oid
),
2509 ret
= check_ok_to_remove(path
, len
, DT_UNKNOWN
, NULL
,
2515 } else if (lstat(ce
->name
, &st
)) {
2516 if (errno
!= ENOENT
)
2517 return error_errno("cannot stat '%s'", ce
->name
);
2520 if (submodule_from_ce(ce
))
2521 return check_submodule_move_head(ce
, oid_to_hex(&ce
->oid
),
2524 return check_ok_to_remove(ce
->name
, ce_namelen(ce
),
2525 ce_to_dtype(ce
), ce
, &st
,
2526 error_type
, absent_type
, o
);
2530 static int verify_absent(const struct cache_entry
*ce
,
2531 enum unpack_trees_error_types error_type
,
2532 struct unpack_trees_options
*o
)
2534 if (!o
->skip_sparse_checkout
&& (ce
->ce_flags
& CE_NEW_SKIP_WORKTREE
))
2536 return verify_absent_1(ce
, error_type
, COMPLETELY_ABSENT
, o
);
2539 static int verify_absent_if_directory(const struct cache_entry
*ce
,
2540 enum unpack_trees_error_types error_type
,
2541 struct unpack_trees_options
*o
)
2543 if (!o
->skip_sparse_checkout
&& (ce
->ce_flags
& CE_NEW_SKIP_WORKTREE
))
2545 return verify_absent_1(ce
, error_type
, ABSENT_ANY_DIRECTORY
, o
);
2548 static int verify_absent_sparse(const struct cache_entry
*ce
,
2549 enum unpack_trees_error_types error_type
,
2550 struct unpack_trees_options
*o
)
2552 return verify_absent_1(ce
, error_type
, COMPLETELY_ABSENT
, o
);
2555 static int merged_entry(const struct cache_entry
*ce
,
2556 const struct cache_entry
*old
,
2557 struct unpack_trees_options
*o
)
2559 int update
= CE_UPDATE
;
2560 struct cache_entry
*merge
= dup_cache_entry(ce
, &o
->internal
.result
);
2564 * New index entries. In sparse checkout, the following
2565 * verify_absent() will be delayed until after
2566 * traverse_trees() finishes in unpack_trees(), then:
2568 * - CE_NEW_SKIP_WORKTREE will be computed correctly
2569 * - verify_absent() be called again, this time with
2570 * correct CE_NEW_SKIP_WORKTREE
2572 * verify_absent() call here does nothing in sparse
2573 * checkout (i.e. o->skip_sparse_checkout == 0)
2576 merge
->ce_flags
|= CE_NEW_SKIP_WORKTREE
;
2578 if (verify_absent(merge
,
2579 ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN
, o
)) {
2580 discard_cache_entry(merge
);
2583 invalidate_ce_path(merge
, o
);
2585 if (submodule_from_ce(ce
) && file_exists(ce
->name
)) {
2586 int ret
= check_submodule_move_head(ce
, NULL
,
2587 oid_to_hex(&ce
->oid
),
2593 } else if (!(old
->ce_flags
& CE_CONFLICTED
)) {
2595 * See if we can re-use the old CE directly?
2596 * That way we get the uptodate stat info.
2598 * This also removes the UPDATE flag on a match; otherwise
2599 * we will end up overwriting local changes in the work tree.
2601 if (same(old
, merge
)) {
2602 copy_cache_entry(merge
, old
);
2605 if (verify_uptodate(old
, o
)) {
2606 discard_cache_entry(merge
);
2609 /* Migrate old flags over */
2610 update
|= old
->ce_flags
& (CE_SKIP_WORKTREE
| CE_NEW_SKIP_WORKTREE
);
2611 invalidate_ce_path(old
, o
);
2614 if (submodule_from_ce(ce
) && file_exists(ce
->name
)) {
2615 int ret
= check_submodule_move_head(ce
, oid_to_hex(&old
->oid
),
2616 oid_to_hex(&ce
->oid
),
2623 * Previously unmerged entry left as an existence
2624 * marker by read_index_unmerged();
2626 if (verify_absent_if_directory(merge
,
2627 ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN
, o
)) {
2628 discard_cache_entry(merge
);
2632 invalidate_ce_path(old
, o
);
2635 if (do_add_entry(o
, merge
, update
, CE_STAGEMASK
) < 0)
2640 static int merged_sparse_dir(const struct cache_entry
* const *src
, int n
,
2641 struct unpack_trees_options
*o
)
2643 struct tree_desc t
[MAX_UNPACK_TREES
+ 1];
2644 void * tree_bufs
[MAX_UNPACK_TREES
+ 1];
2645 struct traverse_info info
;
2649 * Create the tree traversal information for traversing into *only* the
2652 setup_traverse_info(&info
, src
[0]->name
);
2653 info
.fn
= unpack_sparse_callback
;
2655 info
.show_all_errors
= o
->internal
.show_all_errors
;
2656 info
.pathspec
= o
->pathspec
;
2658 /* Get the tree descriptors of the sparse directory in each of the merging trees */
2659 for (i
= 0; i
< n
; i
++)
2660 tree_bufs
[i
] = fill_tree_descriptor(o
->src_index
->repo
, &t
[i
],
2661 src
[i
] && !is_null_oid(&src
[i
]->oid
) ? &src
[i
]->oid
: NULL
);
2663 ret
= traverse_trees(o
->src_index
, n
, t
, &info
);
2665 for (i
= 0; i
< n
; i
++)
2671 static int deleted_entry(const struct cache_entry
*ce
,
2672 const struct cache_entry
*old
,
2673 struct unpack_trees_options
*o
)
2675 /* Did it exist in the index? */
2677 if (verify_absent(ce
, ERROR_WOULD_LOSE_UNTRACKED_REMOVED
, o
))
2680 } else if (verify_absent_if_directory(ce
, ERROR_WOULD_LOSE_UNTRACKED_REMOVED
, o
)) {
2684 if (!(old
->ce_flags
& CE_CONFLICTED
) && verify_uptodate(old
, o
))
2686 add_entry(o
, ce
, CE_REMOVE
, 0);
2687 invalidate_ce_path(ce
, o
);
2691 static int keep_entry(const struct cache_entry
*ce
,
2692 struct unpack_trees_options
*o
)
2694 add_entry(o
, ce
, 0, 0);
2696 invalidate_ce_path(ce
, o
);
2701 static void show_stage_entry(FILE *o
,
2702 const char *label
, const struct cache_entry
*ce
)
2705 fprintf(o
, "%s (missing)\n", label
);
2707 fprintf(o
, "%s%06o %s %d\t%s\n",
2710 oid_to_hex(&ce
->oid
),
2716 int threeway_merge(const struct cache_entry
* const *stages
,
2717 struct unpack_trees_options
*o
)
2719 const struct cache_entry
*index
;
2720 const struct cache_entry
*head
;
2721 const struct cache_entry
*remote
= stages
[o
->head_idx
+ 1];
2724 int remote_match
= 0;
2726 int df_conflict_head
= 0;
2727 int df_conflict_remote
= 0;
2729 int any_anc_missing
= 0;
2730 int no_anc_exists
= 1;
2733 for (i
= 1; i
< o
->head_idx
; i
++) {
2734 if (!stages
[i
] || stages
[i
] == o
->df_conflict_entry
)
2735 any_anc_missing
= 1;
2741 head
= stages
[o
->head_idx
];
2743 if (head
== o
->df_conflict_entry
) {
2744 df_conflict_head
= 1;
2748 if (remote
== o
->df_conflict_entry
) {
2749 df_conflict_remote
= 1;
2754 * First, if there's a #16 situation, note that to prevent #13
2757 if (!same(remote
, head
)) {
2758 for (i
= 1; i
< o
->head_idx
; i
++) {
2759 if (same(stages
[i
], head
)) {
2762 if (same(stages
[i
], remote
)) {
2769 * We start with cases where the index is allowed to match
2770 * something other than the head: #14(ALT) and #2ALT, where it
2771 * is permitted to match the result instead.
2773 /* #14, #14ALT, #2ALT */
2774 if (remote
&& !df_conflict_head
&& head_match
&& !remote_match
) {
2775 if (index
&& !same(index
, remote
) && !same(index
, head
)) {
2776 if (S_ISSPARSEDIR(index
->ce_mode
))
2777 return merged_sparse_dir(stages
, 4, o
);
2779 return reject_merge(index
, o
);
2781 return merged_entry(remote
, index
, o
);
2784 * If we have an entry in the index cache, then we want to
2785 * make sure that it matches head.
2787 if (index
&& !same(index
, head
)) {
2788 if (S_ISSPARSEDIR(index
->ce_mode
))
2789 return merged_sparse_dir(stages
, 4, o
);
2791 return reject_merge(index
, o
);
2796 if (same(head
, remote
))
2797 return merged_entry(head
, index
, o
);
2799 if (!df_conflict_remote
&& remote_match
&& !head_match
)
2800 return merged_entry(head
, index
, o
);
2804 if (!head
&& !remote
&& any_anc_missing
)
2808 * Under the "aggressive" rule, we resolve mostly trivial
2809 * cases that we historically had git-merge-one-file resolve.
2811 if (o
->aggressive
) {
2812 int head_deleted
= !head
;
2813 int remote_deleted
= !remote
;
2814 const struct cache_entry
*ce
= NULL
;
2823 for (i
= 1; i
< o
->head_idx
; i
++) {
2824 if (stages
[i
] && stages
[i
] != o
->df_conflict_entry
) {
2833 * Deleted in one and unchanged in the other.
2835 if ((head_deleted
&& remote_deleted
) ||
2836 (head_deleted
&& remote
&& remote_match
) ||
2837 (remote_deleted
&& head
&& head_match
)) {
2839 return deleted_entry(index
, index
, o
);
2840 if (ce
&& !head_deleted
) {
2841 if (verify_absent(ce
, ERROR_WOULD_LOSE_UNTRACKED_REMOVED
, o
))
2847 * Added in both, identically.
2849 if (no_anc_exists
&& head
&& remote
&& same(head
, remote
))
2850 return merged_entry(head
, index
, o
);
2854 /* Handle "no merge" cases (see t/t1000-read-tree-m-3way.sh) */
2857 * If we've reached the "no merge" cases and we're merging
2858 * a sparse directory, we may have an "edit/edit" conflict that
2859 * can be resolved by individually merging directory contents.
2861 if (S_ISSPARSEDIR(index
->ce_mode
))
2862 return merged_sparse_dir(stages
, 4, o
);
2865 * If we're not merging a sparse directory, ensure the index is
2866 * up-to-date to avoid files getting overwritten with conflict
2869 if (verify_uptodate(index
, o
))
2873 o
->internal
.nontrivial_merge
= 1;
2875 /* #2, #3, #4, #6, #7, #9, #10, #11. */
2877 if (!head_match
|| !remote_match
) {
2878 for (i
= 1; i
< o
->head_idx
; i
++) {
2879 if (stages
[i
] && stages
[i
] != o
->df_conflict_entry
) {
2880 keep_entry(stages
[i
], o
);
2888 fprintf(stderr
, "read-tree: warning #16 detected\n");
2889 show_stage_entry(stderr
, "head ", stages
[head_match
]);
2890 show_stage_entry(stderr
, "remote ", stages
[remote_match
]);
2893 if (head
) { count
+= keep_entry(head
, o
); }
2894 if (remote
) { count
+= keep_entry(remote
, o
); }
2901 * The rule is to "carry forward" what is in the index without losing
2902 * information across a "fast-forward", favoring a successful merge
2903 * over a merge failure when it makes sense. For details of the
2904 * "carry forward" rule, please see <Documentation/git-read-tree.txt>.
2907 int twoway_merge(const struct cache_entry
* const *src
,
2908 struct unpack_trees_options
*o
)
2910 const struct cache_entry
*current
= src
[0];
2911 const struct cache_entry
*oldtree
= src
[1];
2912 const struct cache_entry
*newtree
= src
[2];
2914 if (o
->internal
.merge_size
!= 2)
2915 return error("Cannot do a twoway merge of %d trees",
2916 o
->internal
.merge_size
);
2918 if (oldtree
== o
->df_conflict_entry
)
2920 if (newtree
== o
->df_conflict_entry
)
2924 if (current
->ce_flags
& CE_CONFLICTED
) {
2925 if (same(oldtree
, newtree
) || o
->reset
) {
2927 return deleted_entry(current
, current
, o
);
2929 return merged_entry(newtree
, current
, o
);
2931 return reject_merge(current
, o
);
2932 } else if ((!oldtree
&& !newtree
) || /* 4 and 5 */
2933 (!oldtree
&& newtree
&&
2934 same(current
, newtree
)) || /* 6 and 7 */
2935 (oldtree
&& newtree
&&
2936 same(oldtree
, newtree
)) || /* 14 and 15 */
2937 (oldtree
&& newtree
&&
2938 !same(oldtree
, newtree
) && /* 18 and 19 */
2939 same(current
, newtree
))) {
2940 return keep_entry(current
, o
);
2941 } else if (oldtree
&& !newtree
&& same(current
, oldtree
)) {
2943 return deleted_entry(oldtree
, current
, o
);
2944 } else if (oldtree
&& newtree
&&
2945 same(current
, oldtree
) && !same(current
, newtree
)) {
2947 return merged_entry(newtree
, current
, o
);
2948 } else if (current
&& !oldtree
&& newtree
&&
2949 S_ISSPARSEDIR(current
->ce_mode
) != S_ISSPARSEDIR(newtree
->ce_mode
) &&
2950 ce_stage(current
) == 0) {
2952 * This case is a directory/file conflict across the sparse-index
2953 * boundary. When we are changing from one path to another via
2954 * 'git checkout', then we want to replace one entry with another
2955 * via merged_entry(). If there are staged changes, then we should
2956 * reject the merge instead.
2958 return merged_entry(newtree
, current
, o
);
2959 } else if (S_ISSPARSEDIR(current
->ce_mode
)) {
2961 * The sparse directories differ, but we don't know whether that's
2962 * because of two different files in the directory being modified
2963 * (can be trivially merged) or if there is a real file conflict.
2964 * Merge the sparse directory by OID to compare file-by-file.
2966 return merged_sparse_dir(src
, 3, o
);
2968 return reject_merge(current
, o
);
2971 if (oldtree
&& !o
->initial_checkout
) {
2973 * deletion of the path was staged;
2975 if (same(oldtree
, newtree
))
2977 return reject_merge(oldtree
, o
);
2979 return merged_entry(newtree
, current
, o
);
2981 return deleted_entry(oldtree
, current
, o
);
2987 * Keep the index entries at stage0, collapse stage1 but make sure
2988 * stage0 does not have anything there.
2990 int bind_merge(const struct cache_entry
* const *src
,
2991 struct unpack_trees_options
*o
)
2993 const struct cache_entry
*old
= src
[0];
2994 const struct cache_entry
*a
= src
[1];
2996 if (o
->internal
.merge_size
!= 1)
2997 return error("Cannot do a bind merge of %d trees",
2998 o
->internal
.merge_size
);
3000 return o
->quiet
? -1 :
3001 error(ERRORMSG(o
, ERROR_BIND_OVERLAP
),
3002 super_prefixed(a
->name
, o
->super_prefix
),
3003 super_prefixed(old
->name
, o
->super_prefix
));
3005 return keep_entry(old
, o
);
3007 return merged_entry(a
, NULL
, o
);
3014 * - take the stat information from stage0, take the data from stage1
3016 int oneway_merge(const struct cache_entry
* const *src
,
3017 struct unpack_trees_options
*o
)
3019 const struct cache_entry
*old
= src
[0];
3020 const struct cache_entry
*a
= src
[1];
3022 if (o
->internal
.merge_size
!= 1)
3023 return error("Cannot do a oneway merge of %d trees",
3024 o
->internal
.merge_size
);
3026 if (!a
|| a
== o
->df_conflict_entry
)
3027 return deleted_entry(old
, old
, o
);
3029 if (old
&& same(old
, a
)) {
3031 if (o
->reset
&& o
->update
&& !ce_uptodate(old
) && !ce_skip_worktree(old
) &&
3032 !(old
->ce_flags
& CE_FSMONITOR_VALID
)) {
3034 if (lstat(old
->name
, &st
) ||
3035 ie_match_stat(o
->src_index
, old
, &st
, CE_MATCH_IGNORE_VALID
|CE_MATCH_IGNORE_SKIP_WORKTREE
))
3036 update
|= CE_UPDATE
;
3038 if (o
->update
&& S_ISGITLINK(old
->ce_mode
) &&
3039 should_update_submodules() && !verify_uptodate(old
, o
))
3040 update
|= CE_UPDATE
;
3041 add_entry(o
, old
, update
, CE_STAGEMASK
);
3044 return merged_entry(a
, old
, o
);
3048 * Merge worktree and untracked entries in a stash entry.
3050 * Ignore all index entries. Collapse remaining trees but make sure that they
3051 * don't have any conflicting files.
3053 int stash_worktree_untracked_merge(const struct cache_entry
* const *src
,
3054 struct unpack_trees_options
*o
)
3056 const struct cache_entry
*worktree
= src
[1];
3057 const struct cache_entry
*untracked
= src
[2];
3059 if (o
->internal
.merge_size
!= 2)
3060 BUG("invalid merge_size: %d", o
->internal
.merge_size
);
3062 if (worktree
&& untracked
)
3063 return error(_("worktree and untracked commit have duplicate entries: %s"),
3064 super_prefixed(worktree
->name
, o
->super_prefix
));
3066 return merged_entry(worktree
? worktree
: untracked
, NULL
, o
);