1 #include "git-compat-util.h"
4 #include "repository.h"
7 #include "environment.h"
10 #include "name-hash.h"
12 #include "tree-walk.h"
13 #include "cache-tree.h"
14 #include "unpack-trees.h"
18 #include "read-cache.h"
19 #include "split-index.h"
20 #include "sparse-index.h"
21 #include "submodule.h"
22 #include "submodule-config.h"
25 #include "fsmonitor.h"
26 #include "object-store-ll.h"
27 #include "promisor-remote.h"
29 #include "parallel-checkout.h"
33 * Error messages expected by scripts out of plumbing commands such as
34 * read-tree. Non-scripted Porcelain is not required to use these messages
35 * and in fact are encouraged to reword them to better suit their particular
36 * situation better. See how "git checkout" and "git merge" replaces
37 * them using setup_unpack_trees_porcelain(), for example.
39 static const char *unpack_plumbing_errors
[NB_UNPACK_TREES_WARNING_TYPES
] = {
40 /* ERROR_WOULD_OVERWRITE */
41 "Entry '%s' would be overwritten by merge. Cannot merge.",
43 /* ERROR_NOT_UPTODATE_FILE */
44 "Entry '%s' not uptodate. Cannot merge.",
46 /* ERROR_NOT_UPTODATE_DIR */
47 "Updating '%s' would lose untracked files in it",
49 /* ERROR_CWD_IN_THE_WAY */
50 "Refusing to remove '%s' since it is the current working directory.",
52 /* ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN */
53 "Untracked working tree file '%s' would be overwritten by merge.",
55 /* ERROR_WOULD_LOSE_UNTRACKED_REMOVED */
56 "Untracked working tree file '%s' would be removed by merge.",
58 /* ERROR_BIND_OVERLAP */
59 "Entry '%s' overlaps with '%s'. Cannot bind.",
61 /* ERROR_WOULD_LOSE_SUBMODULE */
62 "Submodule '%s' cannot checkout new HEAD.",
64 /* NB_UNPACK_TREES_ERROR_TYPES; just a meta value */
67 /* WARNING_SPARSE_NOT_UPTODATE_FILE */
68 "Path '%s' not uptodate; will not remove from working tree.",
70 /* WARNING_SPARSE_UNMERGED_FILE */
71 "Path '%s' unmerged; will not remove from working tree.",
73 /* WARNING_SPARSE_ORPHANED_NOT_OVERWRITTEN */
74 "Path '%s' already present; will not overwrite with sparse update.",
77 #define ERRORMSG(o,type) \
78 ( ((o) && (o)->internal.msgs[(type)]) \
79 ? ((o)->internal.msgs[(type)]) \
80 : (unpack_plumbing_errors[(type)]) )
82 static const char *super_prefixed(const char *path
, const char *super_prefix
)
85 * It is necessary and sufficient to have two static buffers
86 * here, as the return value of this function is fed to
87 * error() using the unpack_*_errors[] templates we see above.
89 static struct strbuf buf
[2] = {STRBUF_INIT
, STRBUF_INIT
};
90 static int super_prefix_len
= -1;
91 static unsigned idx
= ARRAY_SIZE(buf
) - 1;
93 if (super_prefix_len
< 0) {
98 for (i
= 0; i
< ARRAY_SIZE(buf
); i
++)
99 strbuf_addstr(&buf
[i
], super_prefix
);
100 super_prefix_len
= buf
[0].len
;
104 if (!super_prefix_len
)
107 if (++idx
>= ARRAY_SIZE(buf
))
110 strbuf_setlen(&buf
[idx
], super_prefix_len
);
111 strbuf_addstr(&buf
[idx
], path
);
116 void setup_unpack_trees_porcelain(struct unpack_trees_options
*opts
,
120 const char **msgs
= opts
->internal
.msgs
;
123 strvec_init(&opts
->internal
.msgs_to_free
);
125 if (!strcmp(cmd
, "checkout"))
126 msg
= advice_enabled(ADVICE_COMMIT_BEFORE_MERGE
)
127 ? _("Your local changes to the following files would be overwritten by checkout:\n%%s"
128 "Please commit your changes or stash them before you switch branches.")
129 : _("Your local changes to the following files would be overwritten by checkout:\n%%s");
130 else if (!strcmp(cmd
, "merge"))
131 msg
= advice_enabled(ADVICE_COMMIT_BEFORE_MERGE
)
132 ? _("Your local changes to the following files would be overwritten by merge:\n%%s"
133 "Please commit your changes or stash them before you merge.")
134 : _("Your local changes to the following files would be overwritten by merge:\n%%s");
136 msg
= advice_enabled(ADVICE_COMMIT_BEFORE_MERGE
)
137 ? _("Your local changes to the following files would be overwritten by %s:\n%%s"
138 "Please commit your changes or stash them before you %s.")
139 : _("Your local changes to the following files would be overwritten by %s:\n%%s");
140 msgs
[ERROR_WOULD_OVERWRITE
] = msgs
[ERROR_NOT_UPTODATE_FILE
] =
141 strvec_pushf(&opts
->internal
.msgs_to_free
, msg
, cmd
, cmd
);
143 msgs
[ERROR_NOT_UPTODATE_DIR
] =
144 _("Updating the following directories would lose untracked files in them:\n%s");
146 msgs
[ERROR_CWD_IN_THE_WAY
] =
147 _("Refusing to remove the current working directory:\n%s");
149 if (!strcmp(cmd
, "checkout"))
150 msg
= advice_enabled(ADVICE_COMMIT_BEFORE_MERGE
)
151 ? _("The following untracked working tree files would be removed by checkout:\n%%s"
152 "Please move or remove them before you switch branches.")
153 : _("The following untracked working tree files would be removed by checkout:\n%%s");
154 else if (!strcmp(cmd
, "merge"))
155 msg
= advice_enabled(ADVICE_COMMIT_BEFORE_MERGE
)
156 ? _("The following untracked working tree files would be removed by merge:\n%%s"
157 "Please move or remove them before you merge.")
158 : _("The following untracked working tree files would be removed by merge:\n%%s");
160 msg
= advice_enabled(ADVICE_COMMIT_BEFORE_MERGE
)
161 ? _("The following untracked working tree files would be removed by %s:\n%%s"
162 "Please move or remove them before you %s.")
163 : _("The following untracked working tree files would be removed by %s:\n%%s");
164 msgs
[ERROR_WOULD_LOSE_UNTRACKED_REMOVED
] =
165 strvec_pushf(&opts
->internal
.msgs_to_free
, msg
, cmd
, cmd
);
167 if (!strcmp(cmd
, "checkout"))
168 msg
= advice_enabled(ADVICE_COMMIT_BEFORE_MERGE
)
169 ? _("The following untracked working tree files would be overwritten by checkout:\n%%s"
170 "Please move or remove them before you switch branches.")
171 : _("The following untracked working tree files would be overwritten by checkout:\n%%s");
172 else if (!strcmp(cmd
, "merge"))
173 msg
= advice_enabled(ADVICE_COMMIT_BEFORE_MERGE
)
174 ? _("The following untracked working tree files would be overwritten by merge:\n%%s"
175 "Please move or remove them before you merge.")
176 : _("The following untracked working tree files would be overwritten by merge:\n%%s");
178 msg
= advice_enabled(ADVICE_COMMIT_BEFORE_MERGE
)
179 ? _("The following untracked working tree files would be overwritten by %s:\n%%s"
180 "Please move or remove them before you %s.")
181 : _("The following untracked working tree files would be overwritten by %s:\n%%s");
182 msgs
[ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN
] =
183 strvec_pushf(&opts
->internal
.msgs_to_free
, msg
, cmd
, cmd
);
186 * Special case: ERROR_BIND_OVERLAP refers to a pair of paths, we
187 * cannot easily display it as a list.
189 msgs
[ERROR_BIND_OVERLAP
] = _("Entry '%s' overlaps with '%s'. Cannot bind.");
191 msgs
[ERROR_WOULD_LOSE_SUBMODULE
] =
192 _("Cannot update submodule:\n%s");
194 msgs
[WARNING_SPARSE_NOT_UPTODATE_FILE
] =
195 _("The following paths are not up to date and were left despite sparse patterns:\n%s");
196 msgs
[WARNING_SPARSE_UNMERGED_FILE
] =
197 _("The following paths are unmerged and were left despite sparse patterns:\n%s");
198 msgs
[WARNING_SPARSE_ORPHANED_NOT_OVERWRITTEN
] =
199 _("The following paths were already present and thus not updated despite sparse patterns:\n%s");
201 opts
->internal
.show_all_errors
= 1;
202 /* rejected paths may not have a static buffer */
203 for (i
= 0; i
< ARRAY_SIZE(opts
->internal
.unpack_rejects
); i
++)
204 opts
->internal
.unpack_rejects
[i
].strdup_strings
= 1;
207 void clear_unpack_trees_porcelain(struct unpack_trees_options
*opts
)
209 strvec_clear(&opts
->internal
.msgs_to_free
);
210 memset(opts
->internal
.msgs
, 0, sizeof(opts
->internal
.msgs
));
213 static int do_add_entry(struct unpack_trees_options
*o
, struct cache_entry
*ce
,
214 unsigned int set
, unsigned int clear
)
221 ce
->ce_flags
= (ce
->ce_flags
& ~clear
) | set
;
222 return add_index_entry(&o
->internal
.result
, ce
,
223 ADD_CACHE_OK_TO_ADD
| ADD_CACHE_OK_TO_REPLACE
);
226 static void add_entry(struct unpack_trees_options
*o
,
227 const struct cache_entry
*ce
,
228 unsigned int set
, unsigned int clear
)
230 do_add_entry(o
, dup_cache_entry(ce
, &o
->internal
.result
), set
, clear
);
234 * add error messages on path <path>
235 * corresponding to the type <e> with the message <msg>
236 * indicating if it should be display in porcelain or not
238 static int add_rejected_path(struct unpack_trees_options
*o
,
239 enum unpack_trees_error_types e
,
245 if (!o
->internal
.show_all_errors
)
246 return error(ERRORMSG(o
, e
), super_prefixed(path
,
250 * Otherwise, insert in a list for future display by
251 * display_(error|warning)_msgs()
253 string_list_append(&o
->internal
.unpack_rejects
[e
], path
);
258 * display all the error messages stored in a nice way
260 static void display_error_msgs(struct unpack_trees_options
*o
)
263 unsigned error_displayed
= 0;
264 for (e
= 0; e
< NB_UNPACK_TREES_ERROR_TYPES
; e
++) {
265 struct string_list
*rejects
= &o
->internal
.unpack_rejects
[e
];
267 if (rejects
->nr
> 0) {
269 struct strbuf path
= STRBUF_INIT
;
272 for (i
= 0; i
< rejects
->nr
; i
++)
273 strbuf_addf(&path
, "\t%s\n", rejects
->items
[i
].string
);
274 error(ERRORMSG(o
, e
), super_prefixed(path
.buf
,
276 strbuf_release(&path
);
278 string_list_clear(rejects
, 0);
281 fprintf(stderr
, _("Aborting\n"));
285 * display all the warning messages stored in a nice way
287 static void display_warning_msgs(struct unpack_trees_options
*o
)
290 unsigned warning_displayed
= 0;
291 for (e
= NB_UNPACK_TREES_ERROR_TYPES
+ 1;
292 e
< NB_UNPACK_TREES_WARNING_TYPES
; e
++) {
293 struct string_list
*rejects
= &o
->internal
.unpack_rejects
[e
];
295 if (rejects
->nr
> 0) {
297 struct strbuf path
= STRBUF_INIT
;
299 warning_displayed
= 1;
300 for (i
= 0; i
< rejects
->nr
; i
++)
301 strbuf_addf(&path
, "\t%s\n", rejects
->items
[i
].string
);
302 warning(ERRORMSG(o
, e
), super_prefixed(path
.buf
,
304 strbuf_release(&path
);
306 string_list_clear(rejects
, 0);
308 if (warning_displayed
)
309 fprintf(stderr
, _("After fixing the above paths, you may want to run `git sparse-checkout reapply`.\n"));
311 static int check_submodule_move_head(const struct cache_entry
*ce
,
314 struct unpack_trees_options
*o
)
316 unsigned flags
= SUBMODULE_MOVE_HEAD_DRY_RUN
;
317 const struct submodule
*sub
= submodule_from_ce(ce
);
323 flags
|= SUBMODULE_MOVE_HEAD_FORCE
;
325 if (submodule_move_head(ce
->name
, o
->super_prefix
, old_id
, new_id
,
327 return add_rejected_path(o
, ERROR_WOULD_LOSE_SUBMODULE
, ce
->name
);
332 * Perform the loading of the repository's gitmodules file. This function is
333 * used by 'check_update()' to perform loading of the gitmodules file in two
334 * different situations:
335 * (1) before removing entries from the working tree if the gitmodules file has
336 * been marked for removal. This situation is specified by 'state' == NULL.
337 * (2) before checking out entries to the working tree if the gitmodules file
338 * has been marked for update. This situation is specified by 'state' != NULL.
340 static void load_gitmodules_file(struct index_state
*index
,
341 struct checkout
*state
)
343 int pos
= index_name_pos(index
, GITMODULES_FILE
, strlen(GITMODULES_FILE
));
346 struct cache_entry
*ce
= index
->cache
[pos
];
347 if (!state
&& ce
->ce_flags
& CE_WT_REMOVE
) {
348 repo_read_gitmodules(the_repository
, 0);
349 } else if (state
&& (ce
->ce_flags
& CE_UPDATE
)) {
350 submodule_free(the_repository
);
351 checkout_entry(ce
, state
, NULL
, NULL
);
352 repo_read_gitmodules(the_repository
, 0);
357 static struct progress
*get_progress(struct unpack_trees_options
*o
,
358 struct index_state
*index
)
360 unsigned cnt
= 0, total
= 0;
362 if (!o
->update
|| !o
->verbose_update
)
365 for (; cnt
< index
->cache_nr
; cnt
++) {
366 const struct cache_entry
*ce
= index
->cache
[cnt
];
367 if (ce
->ce_flags
& (CE_UPDATE
| CE_WT_REMOVE
))
371 return start_delayed_progress(_("Updating files"), total
);
374 static void setup_collided_checkout_detection(struct checkout
*state
,
375 struct index_state
*index
)
380 for (i
= 0; i
< index
->cache_nr
; i
++)
381 index
->cache
[i
]->ce_flags
&= ~CE_MATCHED
;
384 static void report_collided_checkout(struct index_state
*index
)
386 struct string_list list
= STRING_LIST_INIT_NODUP
;
389 for (i
= 0; i
< index
->cache_nr
; i
++) {
390 struct cache_entry
*ce
= index
->cache
[i
];
392 if (!(ce
->ce_flags
& CE_MATCHED
))
395 string_list_append(&list
, ce
->name
);
396 ce
->ce_flags
&= ~CE_MATCHED
;
399 list
.cmp
= fspathcmp
;
400 string_list_sort(&list
);
403 warning(_("the following paths have collided (e.g. case-sensitive paths\n"
404 "on a case-insensitive filesystem) and only one from the same\n"
405 "colliding group is in the working tree:\n"));
407 for (i
= 0; i
< list
.nr
; i
++)
408 fprintf(stderr
, " '%s'\n", list
.items
[i
].string
);
411 string_list_clear(&list
, 0);
414 static int must_checkout(const struct cache_entry
*ce
)
416 return ce
->ce_flags
& CE_UPDATE
;
419 static int check_updates(struct unpack_trees_options
*o
,
420 struct index_state
*index
)
424 struct progress
*progress
;
425 struct checkout state
= CHECKOUT_INIT
;
426 int i
, pc_workers
, pc_threshold
;
428 trace_performance_enter();
429 state
.super_prefix
= o
->super_prefix
;
432 state
.refresh_cache
= 1;
433 state
.istate
= index
;
434 clone_checkout_metadata(&state
.meta
, &o
->meta
, NULL
);
436 if (!o
->update
|| o
->dry_run
) {
437 remove_marked_cache_entries(index
, 0);
438 trace_performance_leave("check_updates");
443 setup_collided_checkout_detection(&state
, index
);
445 progress
= get_progress(o
, index
);
447 /* Start with clean cache to avoid using any possibly outdated info. */
448 invalidate_lstat_cache();
450 git_attr_set_direction(GIT_ATTR_CHECKOUT
);
452 if (should_update_submodules())
453 load_gitmodules_file(index
, NULL
);
455 for (i
= 0; i
< index
->cache_nr
; i
++) {
456 const struct cache_entry
*ce
= index
->cache
[i
];
458 if (ce
->ce_flags
& CE_WT_REMOVE
) {
459 display_progress(progress
, ++cnt
);
460 unlink_entry(ce
, o
->super_prefix
);
464 remove_marked_cache_entries(index
, 0);
465 remove_scheduled_dirs();
467 if (should_update_submodules())
468 load_gitmodules_file(index
, &state
);
470 if (repo_has_promisor_remote(the_repository
))
472 * Prefetch the objects that are to be checked out in the loop
475 prefetch_cache_entries(index
, must_checkout
);
477 get_parallel_checkout_configs(&pc_workers
, &pc_threshold
);
479 enable_delayed_checkout(&state
);
481 init_parallel_checkout();
482 for (i
= 0; i
< index
->cache_nr
; i
++) {
483 struct cache_entry
*ce
= index
->cache
[i
];
485 if (must_checkout(ce
)) {
486 size_t last_pc_queue_size
= pc_queue_size();
488 if (ce
->ce_flags
& CE_WT_REMOVE
)
489 BUG("both update and delete flags are set on %s",
491 ce
->ce_flags
&= ~CE_UPDATE
;
492 errs
|= checkout_entry(ce
, &state
, NULL
, NULL
);
494 if (last_pc_queue_size
== pc_queue_size())
495 display_progress(progress
, ++cnt
);
499 errs
|= run_parallel_checkout(&state
, pc_workers
, pc_threshold
,
501 stop_progress(&progress
);
502 errs
|= finish_delayed_checkout(&state
, o
->verbose_update
);
503 git_attr_set_direction(GIT_ATTR_CHECKIN
);
506 report_collided_checkout(index
);
508 trace_performance_leave("check_updates");
512 static int verify_uptodate_sparse(const struct cache_entry
*ce
,
513 struct unpack_trees_options
*o
);
514 static int verify_absent_sparse(const struct cache_entry
*ce
,
515 enum unpack_trees_error_types
,
516 struct unpack_trees_options
*o
);
518 static int apply_sparse_checkout(struct index_state
*istate
,
519 struct cache_entry
*ce
,
520 struct unpack_trees_options
*o
)
522 int was_skip_worktree
= ce_skip_worktree(ce
);
524 if (ce
->ce_flags
& CE_NEW_SKIP_WORKTREE
)
525 ce
->ce_flags
|= CE_SKIP_WORKTREE
;
527 ce
->ce_flags
&= ~CE_SKIP_WORKTREE
;
528 if (was_skip_worktree
!= ce_skip_worktree(ce
)) {
529 ce
->ce_flags
|= CE_UPDATE_IN_BASE
;
530 mark_fsmonitor_invalid(istate
, ce
);
531 istate
->cache_changed
|= CE_ENTRY_CHANGED
;
535 * if (!was_skip_worktree && !ce_skip_worktree()) {
536 * This is perfectly normal. Move on;
541 * Merge strategies may set CE_UPDATE|CE_REMOVE outside checkout
542 * area as a result of ce_skip_worktree() shortcuts in
543 * verify_absent() and verify_uptodate().
544 * Make sure they don't modify worktree if they are already
545 * outside checkout area
547 if (was_skip_worktree
&& ce_skip_worktree(ce
)) {
548 ce
->ce_flags
&= ~CE_UPDATE
;
551 * By default, when CE_REMOVE is on, CE_WT_REMOVE is also
552 * on to get that file removed from both index and worktree.
553 * If that file is already outside worktree area, don't
556 if (ce
->ce_flags
& CE_REMOVE
)
557 ce
->ce_flags
&= ~CE_WT_REMOVE
;
560 if (!was_skip_worktree
&& ce_skip_worktree(ce
)) {
562 * If CE_UPDATE is set, verify_uptodate() must be called already
563 * also stat info may have lost after merged_entry() so calling
564 * verify_uptodate() again may fail
566 if (!(ce
->ce_flags
& CE_UPDATE
) &&
567 verify_uptodate_sparse(ce
, o
)) {
568 ce
->ce_flags
&= ~CE_SKIP_WORKTREE
;
571 ce
->ce_flags
|= CE_WT_REMOVE
;
572 ce
->ce_flags
&= ~CE_UPDATE
;
574 if (was_skip_worktree
&& !ce_skip_worktree(ce
)) {
575 if (verify_absent_sparse(ce
, WARNING_SPARSE_ORPHANED_NOT_OVERWRITTEN
, o
))
577 ce
->ce_flags
|= CE_UPDATE
;
582 static int warn_conflicted_path(struct index_state
*istate
,
584 struct unpack_trees_options
*o
)
586 char *conflicting_path
= istate
->cache
[i
]->name
;
589 add_rejected_path(o
, WARNING_SPARSE_UNMERGED_FILE
, conflicting_path
);
591 /* Find out how many higher stage entries are at same path */
592 while ((++count
) + i
< istate
->cache_nr
&&
593 !strcmp(conflicting_path
, istate
->cache
[count
+ i
]->name
))
599 static inline int call_unpack_fn(const struct cache_entry
* const *src
,
600 struct unpack_trees_options
*o
)
602 int ret
= o
->fn(src
, o
);
608 static void mark_ce_used(struct cache_entry
*ce
, struct unpack_trees_options
*o
)
610 ce
->ce_flags
|= CE_UNPACKED
;
612 if (o
->internal
.cache_bottom
< o
->src_index
->cache_nr
&&
613 o
->src_index
->cache
[o
->internal
.cache_bottom
] == ce
) {
614 int bottom
= o
->internal
.cache_bottom
;
616 while (bottom
< o
->src_index
->cache_nr
&&
617 o
->src_index
->cache
[bottom
]->ce_flags
& CE_UNPACKED
)
619 o
->internal
.cache_bottom
= bottom
;
623 static void mark_all_ce_unused(struct index_state
*index
)
626 for (i
= 0; i
< index
->cache_nr
; i
++)
627 index
->cache
[i
]->ce_flags
&= ~(CE_UNPACKED
| CE_ADDED
| CE_NEW_SKIP_WORKTREE
);
630 static int locate_in_src_index(const struct cache_entry
*ce
,
631 struct unpack_trees_options
*o
)
633 struct index_state
*index
= o
->src_index
;
634 int len
= ce_namelen(ce
);
635 int pos
= index_name_pos(index
, ce
->name
, len
);
642 * We call unpack_index_entry() with an unmerged cache entry
643 * only in diff-index, and it wants a single callback. Skip
644 * the other unmerged entry with the same name.
646 static void mark_ce_used_same_name(struct cache_entry
*ce
,
647 struct unpack_trees_options
*o
)
649 struct index_state
*index
= o
->src_index
;
650 int len
= ce_namelen(ce
);
653 for (pos
= locate_in_src_index(ce
, o
); pos
< index
->cache_nr
; pos
++) {
654 struct cache_entry
*next
= index
->cache
[pos
];
655 if (len
!= ce_namelen(next
) ||
656 memcmp(ce
->name
, next
->name
, len
))
658 mark_ce_used(next
, o
);
662 static struct cache_entry
*next_cache_entry(struct unpack_trees_options
*o
)
664 const struct index_state
*index
= o
->src_index
;
665 int pos
= o
->internal
.cache_bottom
;
667 while (pos
< index
->cache_nr
) {
668 struct cache_entry
*ce
= index
->cache
[pos
];
669 if (!(ce
->ce_flags
& CE_UNPACKED
))
676 static void add_same_unmerged(const struct cache_entry
*ce
,
677 struct unpack_trees_options
*o
)
679 struct index_state
*index
= o
->src_index
;
680 int len
= ce_namelen(ce
);
681 int pos
= index_name_pos(index
, ce
->name
, len
);
684 die("programming error in a caller of mark_ce_used_same_name");
685 for (pos
= -pos
- 1; pos
< index
->cache_nr
; pos
++) {
686 struct cache_entry
*next
= index
->cache
[pos
];
687 if (len
!= ce_namelen(next
) ||
688 memcmp(ce
->name
, next
->name
, len
))
690 add_entry(o
, next
, 0, 0);
691 mark_ce_used(next
, o
);
695 static int unpack_index_entry(struct cache_entry
*ce
,
696 struct unpack_trees_options
*o
)
698 const struct cache_entry
*src
[MAX_UNPACK_TREES
+ 1] = { NULL
, };
705 if (o
->skip_unmerged
) {
706 add_entry(o
, ce
, 0, 0);
710 ret
= call_unpack_fn(src
, o
);
712 mark_ce_used_same_name(ce
, o
);
716 static int find_cache_pos(struct traverse_info
*, const char *p
, size_t len
);
718 static void restore_cache_bottom(struct traverse_info
*info
, int bottom
)
720 struct unpack_trees_options
*o
= info
->data
;
722 if (o
->diff_index_cached
)
724 o
->internal
.cache_bottom
= bottom
;
727 static int switch_cache_bottom(struct traverse_info
*info
)
729 struct unpack_trees_options
*o
= info
->data
;
732 if (o
->diff_index_cached
)
734 ret
= o
->internal
.cache_bottom
;
735 pos
= find_cache_pos(info
->prev
, info
->name
, info
->namelen
);
738 o
->internal
.cache_bottom
= -2 - pos
;
740 o
->internal
.cache_bottom
= o
->src_index
->cache_nr
;
744 static inline int are_same_oid(struct name_entry
*name_j
, struct name_entry
*name_k
)
746 return !is_null_oid(&name_j
->oid
) && !is_null_oid(&name_k
->oid
) && oideq(&name_j
->oid
, &name_k
->oid
);
749 static int all_trees_same_as_cache_tree(int n
, unsigned long dirmask
,
750 struct name_entry
*names
,
751 struct traverse_info
*info
)
753 struct unpack_trees_options
*o
= info
->data
;
756 if (!o
->merge
|| dirmask
!= ((1 << n
) - 1))
759 for (i
= 1; i
< n
; i
++)
760 if (!are_same_oid(names
, names
+ i
))
763 return cache_tree_matches_traversal(o
->src_index
->cache_tree
, names
, info
);
766 static int index_pos_by_traverse_info(struct name_entry
*names
,
767 struct traverse_info
*info
)
769 struct unpack_trees_options
*o
= info
->data
;
770 struct strbuf name
= STRBUF_INIT
;
773 strbuf_make_traverse_path(&name
, info
, names
->path
, names
->pathlen
);
774 strbuf_addch(&name
, '/');
775 pos
= index_name_pos(o
->src_index
, name
.buf
, name
.len
);
777 if (!o
->src_index
->sparse_index
||
778 !(o
->src_index
->cache
[pos
]->ce_flags
& CE_SKIP_WORKTREE
))
779 BUG("This is a directory and should not exist in index");
783 if (pos
>= o
->src_index
->cache_nr
||
784 !starts_with(o
->src_index
->cache
[pos
]->name
, name
.buf
) ||
785 (pos
> 0 && starts_with(o
->src_index
->cache
[pos
-1]->name
, name
.buf
)))
786 BUG("pos %d doesn't point to the first entry of %s in index",
788 strbuf_release(&name
);
793 * Fast path if we detect that all trees are the same as cache-tree at this
794 * path. We'll walk these trees in an iterative loop using cache-tree/index
795 * instead of ODB since we already know what these trees contain.
797 static int traverse_by_cache_tree(int pos
, int nr_entries
, int nr_names
,
798 struct traverse_info
*info
)
800 struct cache_entry
*src
[MAX_UNPACK_TREES
+ 1] = { NULL
, };
801 struct unpack_trees_options
*o
= info
->data
;
802 struct cache_entry
*tree_ce
= NULL
;
807 BUG("We need cache-tree to do this optimization");
810 * Do what unpack_callback() and unpack_single_entry() normally
811 * do. But we walk all paths in an iterative loop instead.
813 * D/F conflicts and higher stage entries are not a concern
814 * because cache-tree would be invalidated and we would never
815 * get here in the first place.
817 for (i
= 0; i
< nr_entries
; i
++) {
818 int new_ce_len
, len
, rc
;
820 src
[0] = o
->src_index
->cache
[pos
+ i
];
822 len
= ce_namelen(src
[0]);
823 new_ce_len
= cache_entry_size(len
);
825 if (new_ce_len
> ce_len
) {
827 tree_ce
= xrealloc(tree_ce
, new_ce_len
);
828 memset(tree_ce
, 0, new_ce_len
);
831 tree_ce
->ce_flags
= create_ce_flags(0);
833 for (d
= 1; d
<= nr_names
; d
++)
837 tree_ce
->ce_mode
= src
[0]->ce_mode
;
838 tree_ce
->ce_namelen
= len
;
839 oidcpy(&tree_ce
->oid
, &src
[0]->oid
);
840 memcpy(tree_ce
->name
, src
[0]->name
, len
+ 1);
842 rc
= call_unpack_fn((const struct cache_entry
* const *)src
, o
);
848 mark_ce_used(src
[0], o
);
851 if (o
->internal
.debug_unpack
)
852 printf("Unpacked %d entries from %s to %s using cache-tree\n",
854 o
->src_index
->cache
[pos
]->name
,
855 o
->src_index
->cache
[pos
+ nr_entries
- 1]->name
);
859 static int traverse_trees_recursive(int n
, unsigned long dirmask
,
860 unsigned long df_conflicts
,
861 struct name_entry
*names
,
862 struct traverse_info
*info
)
864 struct unpack_trees_options
*o
= info
->data
;
869 struct traverse_info newinfo
;
870 struct name_entry
*p
;
873 nr_entries
= all_trees_same_as_cache_tree(n
, dirmask
, names
, info
);
874 if (nr_entries
> 0) {
875 int pos
= index_pos_by_traverse_info(names
, info
);
877 if (!o
->merge
|| df_conflicts
)
878 BUG("Wrong condition to get here buddy");
881 * All entries up to 'pos' must have been processed
882 * (i.e. marked CE_UNPACKED) at this point. But to be safe,
883 * save and restore cache_bottom anyway to not miss
884 * unprocessed entries before 'pos'.
886 bottom
= o
->internal
.cache_bottom
;
887 ret
= traverse_by_cache_tree(pos
, nr_entries
, n
, info
);
888 o
->internal
.cache_bottom
= bottom
;
898 newinfo
.pathspec
= info
->pathspec
;
899 newinfo
.name
= p
->path
;
900 newinfo
.namelen
= p
->pathlen
;
901 newinfo
.mode
= p
->mode
;
902 newinfo
.pathlen
= st_add3(newinfo
.pathlen
, tree_entry_len(p
), 1);
903 newinfo
.df_conflicts
|= df_conflicts
;
909 * Fetch the tree from the ODB for each peer directory in the
912 * For 2- and 3-way traversals, we try to avoid hitting the
913 * ODB twice for the same OID. This should yield a nice speed
914 * up in checkouts and merges when the commits are similar.
916 * We don't bother doing the full O(n^2) search for larger n,
917 * because wider traversals don't happen that often and we
918 * avoid the search setup.
920 * When 2 peer OIDs are the same, we just copy the tree
921 * descriptor data. This implicitly borrows the buffer
922 * data from the earlier cell.
924 for (i
= 0; i
< n
; i
++, dirmask
>>= 1) {
925 if (i
> 0 && are_same_oid(&names
[i
], &names
[i
- 1]))
927 else if (i
> 1 && are_same_oid(&names
[i
], &names
[i
- 2]))
930 const struct object_id
*oid
= NULL
;
933 buf
[nr_buf
++] = fill_tree_descriptor(the_repository
, t
+ i
, oid
);
937 bottom
= switch_cache_bottom(&newinfo
);
938 ret
= traverse_trees(o
->src_index
, n
, t
, &newinfo
);
939 restore_cache_bottom(&newinfo
, bottom
);
941 for (i
= 0; i
< nr_buf
; i
++)
950 * Compare the traverse-path to the cache entry without actually
951 * having to generate the textual representation of the traverse
954 * NOTE! This *only* compares up to the size of the traverse path
955 * itself - the caller needs to do the final check for the cache
956 * entry having more data at the end!
958 static int do_compare_entry_piecewise(const struct cache_entry
*ce
,
959 const struct traverse_info
*info
,
960 const char *name
, size_t namelen
,
967 int cmp
= do_compare_entry_piecewise(ce
, info
->prev
,
968 info
->name
, info
->namelen
,
973 pathlen
= info
->pathlen
;
974 ce_len
= ce_namelen(ce
);
976 /* If ce_len < pathlen then we must have previously hit "name == directory" entry */
977 if (ce_len
< pathlen
)
981 ce_name
= ce
->name
+ pathlen
;
983 return df_name_compare(ce_name
, ce_len
, S_IFREG
, name
, namelen
, mode
);
986 static int do_compare_entry(const struct cache_entry
*ce
,
987 const struct traverse_info
*info
,
988 const char *name
, size_t namelen
,
997 * If we have not precomputed the traverse path, it is quicker
998 * to avoid doing so. But if we have precomputed it,
999 * it is quicker to use the precomputed version.
1001 if (!info
->traverse_path
)
1002 return do_compare_entry_piecewise(ce
, info
, name
, namelen
, mode
);
1004 cmp
= strncmp(ce
->name
, info
->traverse_path
, info
->pathlen
);
1008 pathlen
= info
->pathlen
;
1009 ce_len
= ce_namelen(ce
);
1011 if (ce_len
< pathlen
)
1015 ce_name
= ce
->name
+ pathlen
;
1017 ce_mode
= S_ISSPARSEDIR(ce
->ce_mode
) ? S_IFDIR
: S_IFREG
;
1018 return df_name_compare(ce_name
, ce_len
, ce_mode
, name
, namelen
, mode
);
1021 static int compare_entry(const struct cache_entry
*ce
, const struct traverse_info
*info
, const struct name_entry
*n
)
1023 int cmp
= do_compare_entry(ce
, info
, n
->path
, n
->pathlen
, n
->mode
);
1028 * At this point, we know that we have a prefix match. If ce
1029 * is a sparse directory, then allow an exact match. This only
1030 * works when the input name is a directory, since ce->name
1031 * ends in a directory separator.
1033 if (S_ISSPARSEDIR(ce
->ce_mode
) &&
1034 ce
->ce_namelen
== traverse_path_len(info
, tree_entry_len(n
)) + 1)
1038 * Even if the beginning compared identically, the ce should
1039 * compare as bigger than a directory leading up to it!
1041 return ce_namelen(ce
) > traverse_path_len(info
, tree_entry_len(n
));
1044 static int ce_in_traverse_path(const struct cache_entry
*ce
,
1045 const struct traverse_info
*info
)
1049 if (do_compare_entry(ce
, info
->prev
,
1050 info
->name
, info
->namelen
, info
->mode
))
1053 * If ce (blob) is the same name as the path (which is a tree
1054 * we will be descending into), it won't be inside it.
1056 return (info
->pathlen
< ce_namelen(ce
));
1059 static struct cache_entry
*create_ce_entry(const struct traverse_info
*info
,
1060 const struct name_entry
*n
,
1062 struct index_state
*istate
,
1064 int is_sparse_directory
)
1066 size_t len
= traverse_path_len(info
, tree_entry_len(n
));
1067 size_t alloc_len
= is_sparse_directory
? len
+ 1 : len
;
1068 struct cache_entry
*ce
=
1070 make_empty_transient_cache_entry(alloc_len
, NULL
) :
1071 make_empty_cache_entry(istate
, alloc_len
);
1073 ce
->ce_mode
= create_ce_mode(n
->mode
);
1074 ce
->ce_flags
= create_ce_flags(stage
);
1075 ce
->ce_namelen
= len
;
1076 oidcpy(&ce
->oid
, &n
->oid
);
1077 /* len+1 because the cache_entry allocates space for NUL */
1078 make_traverse_path(ce
->name
, len
+ 1, info
, n
->path
, n
->pathlen
);
1080 if (is_sparse_directory
) {
1081 ce
->name
[len
] = '/';
1082 ce
->name
[len
+ 1] = '\0';
1084 ce
->ce_flags
|= CE_SKIP_WORKTREE
;
1091 * Determine whether the path specified by 'p' should be unpacked as a new
1092 * sparse directory in a sparse index. A new sparse directory 'A/':
1093 * - must be outside the sparse cone.
1094 * - must not already be in the index (i.e., no index entry with name 'A/'
1096 * - must not have any child entries in the index (i.e., no index entry
1097 * 'A/<something>' exists).
1098 * If 'p' meets the above requirements, return 1; otherwise, return 0.
1100 static int entry_is_new_sparse_dir(const struct traverse_info
*info
,
1101 const struct name_entry
*p
)
1104 struct strbuf dirpath
= STRBUF_INIT
;
1105 struct unpack_trees_options
*o
= info
->data
;
1107 if (!S_ISDIR(p
->mode
))
1111 * If the path is inside the sparse cone, it can't be a sparse directory.
1113 strbuf_add(&dirpath
, info
->traverse_path
, info
->pathlen
);
1114 strbuf_add(&dirpath
, p
->path
, p
->pathlen
);
1115 strbuf_addch(&dirpath
, '/');
1116 if (path_in_cone_mode_sparse_checkout(dirpath
.buf
, o
->src_index
)) {
1121 pos
= index_name_pos_sparse(o
->src_index
, dirpath
.buf
, dirpath
.len
);
1123 /* Path is already in the index, not a new sparse dir */
1128 /* Where would this sparse dir be inserted into the index? */
1130 if (pos
>= o
->src_index
->cache_nr
) {
1132 * Sparse dir would be inserted at the end of the index, so we
1133 * know it has no child entries.
1140 * If the dir has child entries in the index, the first would be at the
1141 * position the sparse directory would be inserted. If the entry at this
1142 * position is inside the dir, not a new sparse dir.
1144 res
= strncmp(o
->src_index
->cache
[pos
]->name
, dirpath
.buf
, dirpath
.len
);
1147 strbuf_release(&dirpath
);
1152 * Note that traverse_by_cache_tree() duplicates some logic in this function
1153 * without actually calling it. If you change the logic here you may need to
1154 * check and change there as well.
1156 static int unpack_single_entry(int n
, unsigned long mask
,
1157 unsigned long dirmask
,
1158 struct cache_entry
**src
,
1159 const struct name_entry
*names
,
1160 const struct traverse_info
*info
,
1161 int *is_new_sparse_dir
)
1164 struct unpack_trees_options
*o
= info
->data
;
1165 unsigned long conflicts
= info
->df_conflicts
| dirmask
;
1166 const struct name_entry
*p
= names
;
1168 *is_new_sparse_dir
= 0;
1169 if (mask
== dirmask
&& !src
[0]) {
1171 * If we're not in a sparse index, we can't unpack a directory
1172 * without recursing into it, so we return.
1174 if (!o
->src_index
->sparse_index
)
1177 /* Find first entry with a real name (we could use "mask" too) */
1182 * If the directory is completely missing from the index but
1183 * would otherwise be a sparse directory, we should unpack it.
1184 * If not, we'll return and continue recursively traversing the
1187 *is_new_sparse_dir
= entry_is_new_sparse_dir(info
, p
);
1188 if (!*is_new_sparse_dir
)
1193 * When we are unpacking a sparse directory, then this isn't necessarily
1194 * a directory-file conflict.
1196 if (mask
== dirmask
&&
1197 (*is_new_sparse_dir
|| (src
[0] && S_ISSPARSEDIR(src
[0]->ce_mode
))))
1201 * Ok, we've filled in up to any potential index entry in src[0],
1204 for (i
= 0; i
< n
; i
++) {
1206 unsigned int bit
= 1ul << i
;
1207 if (conflicts
& bit
) {
1208 src
[i
+ o
->merge
] = o
->df_conflict_entry
;
1215 else if (i
+ 1 < o
->head_idx
)
1217 else if (i
+ 1 > o
->head_idx
)
1223 * If the merge bit is set, then the cache entries are
1224 * discarded in the following block. In this case,
1225 * construct "transient" cache_entries, as they are
1226 * not stored in the index. otherwise construct the
1227 * cache entry from the index aware logic.
1229 src
[i
+ o
->merge
] = create_ce_entry(info
, names
+ i
, stage
,
1230 &o
->internal
.result
,
1231 o
->merge
, bit
& dirmask
);
1235 int rc
= call_unpack_fn((const struct cache_entry
* const *)src
,
1237 for (i
= 0; i
< n
; i
++) {
1238 struct cache_entry
*ce
= src
[i
+ o
->merge
];
1239 if (ce
!= o
->df_conflict_entry
)
1240 discard_cache_entry(ce
);
1245 for (i
= 0; i
< n
; i
++)
1246 if (src
[i
] && src
[i
] != o
->df_conflict_entry
)
1247 if (do_add_entry(o
, src
[i
], 0, 0))
1253 static int unpack_failed(struct unpack_trees_options
*o
, const char *message
)
1255 discard_index(&o
->internal
.result
);
1256 if (!o
->quiet
&& !o
->exiting_early
) {
1258 return error("%s", message
);
1265 * The tree traversal is looking at name p. If we have a matching entry,
1266 * return it. If name p is a directory in the index, do not return
1267 * anything, as we will want to match it when the traversal descends into
1270 static int find_cache_pos(struct traverse_info
*info
,
1271 const char *p
, size_t p_len
)
1274 struct unpack_trees_options
*o
= info
->data
;
1275 struct index_state
*index
= o
->src_index
;
1276 int pfxlen
= info
->pathlen
;
1278 for (pos
= o
->internal
.cache_bottom
; pos
< index
->cache_nr
; pos
++) {
1279 const struct cache_entry
*ce
= index
->cache
[pos
];
1280 const char *ce_name
, *ce_slash
;
1283 if (ce
->ce_flags
& CE_UNPACKED
) {
1285 * cache_bottom entry is already unpacked, so
1286 * we can never match it; don't check it
1289 if (pos
== o
->internal
.cache_bottom
)
1290 ++o
->internal
.cache_bottom
;
1293 if (!ce_in_traverse_path(ce
, info
)) {
1295 * Check if we can skip future cache checks
1296 * (because we're already past all possible
1297 * entries in the traverse path).
1299 if (info
->traverse_path
) {
1300 if (strncmp(ce
->name
, info
->traverse_path
,
1306 ce_name
= ce
->name
+ pfxlen
;
1307 ce_slash
= strchr(ce_name
, '/');
1309 ce_len
= ce_slash
- ce_name
;
1311 ce_len
= ce_namelen(ce
) - pfxlen
;
1312 cmp
= name_compare(p
, p_len
, ce_name
, ce_len
);
1314 * Exact match; if we have a directory we need to
1315 * delay returning it.
1318 return ce_slash
? -2 - pos
: pos
;
1320 continue; /* keep looking */
1322 * ce_name sorts after p->path; could it be that we
1323 * have files under p->path directory in the index?
1324 * E.g. ce_name == "t-i", and p->path == "t"; we may
1325 * have "t/a" in the index.
1327 if (p_len
< ce_len
&& !memcmp(ce_name
, p
, p_len
) &&
1328 ce_name
[p_len
] < '/')
1329 continue; /* keep looking */
1336 * Given a sparse directory entry 'ce', compare ce->name to
1337 * info->traverse_path + p->path + '/' if info->traverse_path
1340 * Compare ce->name to p->path + '/' otherwise. Note that
1341 * ce->name must end in a trailing '/' because it is a sparse
1344 static int sparse_dir_matches_path(const struct cache_entry
*ce
,
1345 struct traverse_info
*info
,
1346 const struct name_entry
*p
)
1348 assert(S_ISSPARSEDIR(ce
->ce_mode
));
1349 assert(ce
->name
[ce
->ce_namelen
- 1] == '/');
1352 return ce
->ce_namelen
== info
->pathlen
+ p
->pathlen
+ 1 &&
1353 ce
->name
[info
->pathlen
- 1] == '/' &&
1354 !strncmp(ce
->name
, info
->traverse_path
, info
->pathlen
) &&
1355 !strncmp(ce
->name
+ info
->pathlen
, p
->path
, p
->pathlen
);
1356 return ce
->ce_namelen
== p
->pathlen
+ 1 &&
1357 !strncmp(ce
->name
, p
->path
, p
->pathlen
);
1360 static struct cache_entry
*find_cache_entry(struct traverse_info
*info
,
1361 const struct name_entry
*p
)
1364 int pos
= find_cache_pos(info
, p
->path
, p
->pathlen
);
1365 struct unpack_trees_options
*o
= info
->data
;
1368 return o
->src_index
->cache
[pos
];
1371 * Check for a sparse-directory entry named "path/".
1372 * Due to the input p->path not having a trailing
1373 * slash, the negative 'pos' value overshoots the
1374 * expected position, hence "-2" instead of "-1".
1378 if (pos
< 0 || pos
>= o
->src_index
->cache_nr
)
1382 * Due to lexicographic sorting and sparse directory
1383 * entries ending with a trailing slash, our path as a
1384 * sparse directory (e.g "subdir/") and our path as a
1385 * file (e.g. "subdir") might be separated by other
1386 * paths (e.g. "subdir-").
1389 struct cache_entry
*ce
= o
->src_index
->cache
[pos
];
1391 if (!skip_prefix(ce
->name
, info
->traverse_path
, &path
) ||
1392 strncmp(path
, p
->path
, p
->pathlen
) ||
1393 path
[p
->pathlen
] != '/')
1396 if (S_ISSPARSEDIR(ce
->ce_mode
) &&
1397 sparse_dir_matches_path(ce
, info
, p
))
1406 static void debug_path(struct traverse_info
*info
)
1409 debug_path(info
->prev
);
1410 if (*info
->prev
->name
)
1413 printf("%s", info
->name
);
1416 static void debug_name_entry(int i
, struct name_entry
*n
)
1418 printf("ent#%d %06o %s\n", i
,
1419 n
->path
? n
->mode
: 0,
1420 n
->path
? n
->path
: "(missing)");
1423 static void debug_unpack_callback(int n
,
1425 unsigned long dirmask
,
1426 struct name_entry
*names
,
1427 struct traverse_info
*info
)
1430 printf("* unpack mask %lu, dirmask %lu, cnt %d ",
1434 for (i
= 0; i
< n
; i
++)
1435 debug_name_entry(i
, names
+ i
);
1439 * Returns true if and only if the given cache_entry is a
1440 * sparse-directory entry that matches the given name_entry
1441 * from the tree walk at the given traverse_info.
1443 static int is_sparse_directory_entry(struct cache_entry
*ce
,
1444 const struct name_entry
*name
,
1445 struct traverse_info
*info
)
1447 if (!ce
|| !name
|| !S_ISSPARSEDIR(ce
->ce_mode
))
1450 return sparse_dir_matches_path(ce
, info
, name
);
1453 static int unpack_sparse_callback(int n
, unsigned long mask
, unsigned long dirmask
, struct name_entry
*names
, struct traverse_info
*info
)
1455 struct cache_entry
*src
[MAX_UNPACK_TREES
+ 1] = { NULL
, };
1456 struct unpack_trees_options
*o
= info
->data
;
1457 int ret
, is_new_sparse_dir
;
1462 * Unlike in 'unpack_callback', where src[0] is derived from the index when
1463 * merging, src[0] is a transient cache entry derived from the first tree
1464 * provided. Create the temporary entry as if it came from a non-sparse index.
1466 if (!is_null_oid(&names
[0].oid
)) {
1467 src
[0] = create_ce_entry(info
, &names
[0], 0,
1468 &o
->internal
.result
, 1,
1469 dirmask
& (1ul << 0));
1470 src
[0]->ce_flags
|= (CE_SKIP_WORKTREE
| CE_NEW_SKIP_WORKTREE
);
1474 * 'unpack_single_entry' assumes that src[0] is derived directly from
1475 * the index, rather than from an entry in 'names'. This is *not* true when
1476 * merging a sparse directory, in which case names[0] is the "index" source
1477 * entry. To match the expectations of 'unpack_single_entry', shift past the
1478 * "index" tree (i.e., names[0]) and adjust 'names', 'n', 'mask', and
1479 * 'dirmask' accordingly.
1481 ret
= unpack_single_entry(n
- 1, mask
>> 1, dirmask
>> 1, src
, names
+ 1, info
, &is_new_sparse_dir
);
1484 discard_cache_entry(src
[0]);
1486 return ret
>= 0 ? mask
: -1;
1490 * Note that traverse_by_cache_tree() duplicates some logic in this function
1491 * without actually calling it. If you change the logic here you may need to
1492 * check and change there as well.
1494 static int unpack_callback(int n
, unsigned long mask
, unsigned long dirmask
, struct name_entry
*names
, struct traverse_info
*info
)
1496 struct cache_entry
*src
[MAX_UNPACK_TREES
+ 1] = { NULL
, };
1497 struct unpack_trees_options
*o
= info
->data
;
1498 const struct name_entry
*p
= names
;
1499 int is_new_sparse_dir
;
1501 /* Find first entry with a real name (we could use "mask" too) */
1505 if (o
->internal
.debug_unpack
)
1506 debug_unpack_callback(n
, mask
, dirmask
, names
, info
);
1508 /* Are we supposed to look at the index too? */
1512 struct cache_entry
*ce
;
1514 if (o
->diff_index_cached
)
1515 ce
= next_cache_entry(o
);
1517 ce
= find_cache_entry(info
, p
);
1521 cmp
= compare_entry(ce
, info
, p
);
1523 if (unpack_index_entry(ce
, o
) < 0)
1524 return unpack_failed(o
, NULL
);
1530 * If we skip unmerged index
1531 * entries, we'll skip this
1532 * entry *and* the tree
1533 * entries associated with it!
1535 if (o
->skip_unmerged
) {
1536 add_same_unmerged(ce
, o
);
1546 if (unpack_single_entry(n
, mask
, dirmask
, src
, names
, info
, &is_new_sparse_dir
))
1549 if (o
->merge
&& src
[0]) {
1550 if (ce_stage(src
[0]))
1551 mark_ce_used_same_name(src
[0], o
);
1553 mark_ce_used(src
[0], o
);
1556 /* Now handle any directories.. */
1558 /* special case: "diff-index --cached" looking at a tree */
1559 if (o
->diff_index_cached
&&
1560 n
== 1 && dirmask
== 1 && S_ISDIR(names
->mode
)) {
1562 matches
= cache_tree_matches_traversal(o
->src_index
->cache_tree
,
1565 * Everything under the name matches; skip the
1566 * entire hierarchy. diff_index_cached codepath
1567 * special cases D/F conflicts in such a way that
1568 * it does not do any look-ahead, so this is safe.
1572 * Only increment the cache_bottom if the
1573 * directory isn't a sparse directory index
1574 * entry (if it is, it was already incremented)
1575 * in 'mark_ce_used()'
1577 if (!src
[0] || !S_ISSPARSEDIR(src
[0]->ce_mode
))
1578 o
->internal
.cache_bottom
+= matches
;
1583 if (!is_sparse_directory_entry(src
[0], p
, info
) &&
1584 !is_new_sparse_dir
&&
1585 traverse_trees_recursive(n
, dirmask
, mask
& ~dirmask
,
1596 static int clear_ce_flags_1(struct index_state
*istate
,
1597 struct cache_entry
**cache
, int nr
,
1598 struct strbuf
*prefix
,
1599 int select_mask
, int clear_mask
,
1600 struct pattern_list
*pl
,
1601 enum pattern_match_result default_match
,
1604 /* Whole directory matching */
1605 static int clear_ce_flags_dir(struct index_state
*istate
,
1606 struct cache_entry
**cache
, int nr
,
1607 struct strbuf
*prefix
,
1609 int select_mask
, int clear_mask
,
1610 struct pattern_list
*pl
,
1611 enum pattern_match_result default_match
,
1614 struct cache_entry
**cache_end
;
1617 enum pattern_match_result ret
, orig_ret
;
1618 orig_ret
= path_matches_pattern_list(prefix
->buf
, prefix
->len
,
1619 basename
, &dtype
, pl
, istate
);
1621 strbuf_addch(prefix
, '/');
1623 /* If undecided, use matching result of parent dir in defval */
1624 if (orig_ret
== UNDECIDED
)
1625 ret
= default_match
;
1629 for (cache_end
= cache
; cache_end
!= cache
+ nr
; cache_end
++) {
1630 struct cache_entry
*ce
= *cache_end
;
1631 if (strncmp(ce
->name
, prefix
->buf
, prefix
->len
))
1635 if (pl
->use_cone_patterns
&& orig_ret
== MATCHED_RECURSIVE
) {
1636 struct cache_entry
**ce
= cache
;
1637 rc
= cache_end
- cache
;
1639 while (ce
< cache_end
) {
1640 (*ce
)->ce_flags
&= ~clear_mask
;
1643 } else if (pl
->use_cone_patterns
&& orig_ret
== NOT_MATCHED
) {
1644 rc
= cache_end
- cache
;
1646 rc
= clear_ce_flags_1(istate
, cache
, cache_end
- cache
,
1648 select_mask
, clear_mask
,
1653 strbuf_setlen(prefix
, prefix
->len
- 1);
1658 * Traverse the index, find every entry that matches according to
1659 * o->pl. Do "ce_flags &= ~clear_mask" on those entries. Return the
1660 * number of traversed entries.
1662 * If select_mask is non-zero, only entries whose ce_flags has on of
1663 * those bits enabled are traversed.
1665 * cache : pointer to an index entry
1666 * prefix_len : an offset to its path
1668 * The current path ("prefix") including the trailing '/' is
1669 * cache[0]->name[0..(prefix_len-1)]
1670 * Top level path has prefix_len zero.
1672 static int clear_ce_flags_1(struct index_state
*istate
,
1673 struct cache_entry
**cache
, int nr
,
1674 struct strbuf
*prefix
,
1675 int select_mask
, int clear_mask
,
1676 struct pattern_list
*pl
,
1677 enum pattern_match_result default_match
,
1680 struct cache_entry
**cache_end
= nr
? cache
+ nr
: cache
;
1683 * Process all entries that have the given prefix and meet
1684 * select_mask condition
1686 while(cache
!= cache_end
) {
1687 struct cache_entry
*ce
= *cache
;
1688 const char *name
, *slash
;
1690 enum pattern_match_result ret
;
1692 display_progress(istate
->progress
, progress_nr
);
1694 if (select_mask
&& !(ce
->ce_flags
& select_mask
)) {
1700 if (prefix
->len
&& strncmp(ce
->name
, prefix
->buf
, prefix
->len
))
1703 name
= ce
->name
+ prefix
->len
;
1704 slash
= strchr(name
, '/');
1706 /* If it's a directory, try whole directory match first */
1711 strbuf_add(prefix
, name
, len
);
1713 processed
= clear_ce_flags_dir(istate
, cache
, cache_end
- cache
,
1715 prefix
->buf
+ prefix
->len
- len
,
1716 select_mask
, clear_mask
,
1720 /* clear_c_f_dir eats a whole dir already? */
1723 progress_nr
+= processed
;
1724 strbuf_setlen(prefix
, prefix
->len
- len
);
1728 strbuf_addch(prefix
, '/');
1729 processed
= clear_ce_flags_1(istate
, cache
, cache_end
- cache
,
1731 select_mask
, clear_mask
, pl
,
1732 default_match
, progress_nr
);
1735 progress_nr
+= processed
;
1737 strbuf_setlen(prefix
, prefix
->len
- len
- 1);
1742 dtype
= ce_to_dtype(ce
);
1743 ret
= path_matches_pattern_list(ce
->name
,
1745 name
, &dtype
, pl
, istate
);
1746 if (ret
== UNDECIDED
)
1747 ret
= default_match
;
1748 if (ret
== MATCHED
|| ret
== MATCHED_RECURSIVE
)
1749 ce
->ce_flags
&= ~clear_mask
;
1754 display_progress(istate
->progress
, progress_nr
);
1755 return nr
- (cache_end
- cache
);
1758 static int clear_ce_flags(struct index_state
*istate
,
1759 int select_mask
, int clear_mask
,
1760 struct pattern_list
*pl
,
1763 static struct strbuf prefix
= STRBUF_INIT
;
1767 strbuf_reset(&prefix
);
1769 istate
->progress
= start_delayed_progress(
1770 _("Updating index flags"),
1773 xsnprintf(label
, sizeof(label
), "clear_ce_flags(0x%08lx,0x%08lx)",
1774 (unsigned long)select_mask
, (unsigned long)clear_mask
);
1775 trace2_region_enter("unpack_trees", label
, the_repository
);
1776 rval
= clear_ce_flags_1(istate
,
1780 select_mask
, clear_mask
,
1782 trace2_region_leave("unpack_trees", label
, the_repository
);
1784 stop_progress(&istate
->progress
);
1789 * Set/Clear CE_NEW_SKIP_WORKTREE according to $GIT_DIR/info/sparse-checkout
1791 static void mark_new_skip_worktree(struct pattern_list
*pl
,
1792 struct index_state
*istate
,
1793 int select_flag
, int skip_wt_flag
,
1799 * 1. Pretend the narrowest worktree: only unmerged entries
1802 for (i
= 0; i
< istate
->cache_nr
; i
++) {
1803 struct cache_entry
*ce
= istate
->cache
[i
];
1805 if (select_flag
&& !(ce
->ce_flags
& select_flag
))
1808 if (!ce_stage(ce
) && !(ce
->ce_flags
& CE_CONFLICTED
))
1809 ce
->ce_flags
|= skip_wt_flag
;
1811 ce
->ce_flags
&= ~skip_wt_flag
;
1815 * 2. Widen worktree according to sparse-checkout file.
1816 * Matched entries will have skip_wt_flag cleared (i.e. "in")
1818 clear_ce_flags(istate
, select_flag
, skip_wt_flag
, pl
, show_progress
);
1821 static void populate_from_existing_patterns(struct unpack_trees_options
*o
,
1822 struct pattern_list
*pl
)
1824 if (get_sparse_checkout_patterns(pl
) < 0)
1825 o
->skip_sparse_checkout
= 1;
1827 o
->internal
.pl
= pl
;
1830 static void update_sparsity_for_prefix(const char *prefix
,
1831 struct index_state
*istate
)
1833 int prefix_len
= strlen(prefix
);
1834 struct strbuf ce_prefix
= STRBUF_INIT
;
1836 if (!istate
->sparse_index
)
1839 while (prefix_len
> 0 && prefix
[prefix_len
- 1] == '/')
1842 if (prefix_len
<= 0)
1843 BUG("Invalid prefix passed to update_sparsity_for_prefix");
1845 strbuf_grow(&ce_prefix
, prefix_len
+ 1);
1846 strbuf_add(&ce_prefix
, prefix
, prefix_len
);
1847 strbuf_addch(&ce_prefix
, '/');
1850 * If the prefix points to a sparse directory or a path inside a sparse
1851 * directory, the index should be expanded. This is accomplished in one
1853 * - if the prefix is inside a sparse directory, it will be expanded by
1854 * the 'ensure_full_index(...)' call in 'index_name_pos(...)'.
1855 * - if the prefix matches an existing sparse directory entry,
1856 * 'index_name_pos(...)' will return its index position, triggering
1857 * the 'ensure_full_index(...)' below.
1859 if (!path_in_cone_mode_sparse_checkout(ce_prefix
.buf
, istate
) &&
1860 index_name_pos(istate
, ce_prefix
.buf
, ce_prefix
.len
) >= 0)
1861 ensure_full_index(istate
);
1863 strbuf_release(&ce_prefix
);
1866 static int verify_absent(const struct cache_entry
*,
1867 enum unpack_trees_error_types
,
1868 struct unpack_trees_options
*);
1870 * N-way merge "len" trees. Returns 0 on success, -1 on failure to manipulate the
1871 * resulting index, -2 on failure to reflect the changes to the work tree.
1873 * CE_ADDED, CE_UNPACKED and CE_NEW_SKIP_WORKTREE are used internally
1875 int unpack_trees(unsigned len
, struct tree_desc
*t
, struct unpack_trees_options
*o
)
1877 struct repository
*repo
= the_repository
;
1879 static struct cache_entry
*dfc
;
1880 struct pattern_list pl
;
1881 int free_pattern_list
= 0;
1882 struct dir_struct dir
= DIR_INIT
;
1884 if (o
->reset
== UNPACK_RESET_INVALID
)
1885 BUG("o->reset had a value of 1; should be UNPACK_TREES_*_UNTRACKED");
1887 if (len
> MAX_UNPACK_TREES
)
1888 die("unpack_trees takes at most %d trees", MAX_UNPACK_TREES
);
1889 if (o
->internal
.dir
)
1890 BUG("o->internal.dir is for internal use only");
1892 BUG("o->internal.pl is for internal use only");
1893 if (o
->df_conflict_entry
)
1894 BUG("o->df_conflict_entry is an output only field");
1896 trace_performance_enter();
1897 trace2_region_enter("unpack_trees", "unpack_trees", the_repository
);
1899 prepare_repo_settings(repo
);
1900 if (repo
->settings
.command_requires_full_index
) {
1901 ensure_full_index(o
->src_index
);
1903 ensure_full_index(o
->dst_index
);
1906 if (o
->reset
== UNPACK_RESET_OVERWRITE_UNTRACKED
&&
1907 o
->preserve_ignored
)
1908 BUG("UNPACK_RESET_OVERWRITE_UNTRACKED incompatible with preserved ignored files");
1910 if (!o
->preserve_ignored
) {
1911 o
->internal
.dir
= &dir
;
1912 o
->internal
.dir
->flags
|= DIR_SHOW_IGNORED
;
1913 setup_standard_excludes(o
->internal
.dir
);
1917 update_sparsity_for_prefix(o
->prefix
, o
->src_index
);
1919 if (!core_apply_sparse_checkout
|| !o
->update
)
1920 o
->skip_sparse_checkout
= 1;
1921 if (!o
->skip_sparse_checkout
) {
1922 memset(&pl
, 0, sizeof(pl
));
1923 free_pattern_list
= 1;
1924 populate_from_existing_patterns(o
, &pl
);
1927 index_state_init(&o
->internal
.result
, o
->src_index
->repo
);
1928 o
->internal
.result
.initialized
= 1;
1929 o
->internal
.result
.timestamp
.sec
= o
->src_index
->timestamp
.sec
;
1930 o
->internal
.result
.timestamp
.nsec
= o
->src_index
->timestamp
.nsec
;
1931 o
->internal
.result
.version
= o
->src_index
->version
;
1932 if (!o
->src_index
->split_index
) {
1933 o
->internal
.result
.split_index
= NULL
;
1934 } else if (o
->src_index
== o
->dst_index
) {
1936 * o->dst_index (and thus o->src_index) will be discarded
1937 * and overwritten with o->internal.result at the end of
1938 * this function, so just use src_index's split_index to
1939 * avoid having to create a new one.
1941 o
->internal
.result
.split_index
= o
->src_index
->split_index
;
1942 if (o
->src_index
->cache_changed
& SPLIT_INDEX_ORDERED
)
1943 o
->internal
.result
.cache_changed
|= SPLIT_INDEX_ORDERED
;
1944 o
->internal
.result
.split_index
->refcount
++;
1946 o
->internal
.result
.split_index
=
1947 init_split_index(&o
->internal
.result
);
1949 oidcpy(&o
->internal
.result
.oid
, &o
->src_index
->oid
);
1950 o
->internal
.merge_size
= len
;
1951 mark_all_ce_unused(o
->src_index
);
1953 o
->internal
.result
.fsmonitor_last_update
=
1954 xstrdup_or_null(o
->src_index
->fsmonitor_last_update
);
1955 o
->internal
.result
.fsmonitor_has_run_once
= o
->src_index
->fsmonitor_has_run_once
;
1957 if (!o
->src_index
->initialized
&&
1958 !repo
->settings
.command_requires_full_index
&&
1959 is_sparse_index_allowed(&o
->internal
.result
, 0))
1960 o
->internal
.result
.sparse_index
= 1;
1963 * Sparse checkout loop #1: set NEW_SKIP_WORKTREE on existing entries
1965 if (!o
->skip_sparse_checkout
)
1966 mark_new_skip_worktree(o
->internal
.pl
, o
->src_index
, 0,
1967 CE_NEW_SKIP_WORKTREE
, o
->verbose_update
);
1970 dfc
= xcalloc(1, cache_entry_size(0));
1971 o
->df_conflict_entry
= dfc
;
1974 const char *prefix
= o
->prefix
? o
->prefix
: "";
1975 struct traverse_info info
;
1977 setup_traverse_info(&info
, prefix
);
1978 info
.fn
= unpack_callback
;
1980 info
.show_all_errors
= o
->internal
.show_all_errors
;
1981 info
.pathspec
= o
->pathspec
;
1985 * Unpack existing index entries that sort before the
1986 * prefix the tree is spliced into. Note that o->merge
1987 * is always true in this case.
1990 struct cache_entry
*ce
= next_cache_entry(o
);
1993 if (ce_in_traverse_path(ce
, &info
))
1995 if (unpack_index_entry(ce
, o
) < 0)
2000 trace_performance_enter();
2001 trace2_region_enter("unpack_trees", "traverse_trees", the_repository
);
2002 ret
= traverse_trees(o
->src_index
, len
, t
, &info
);
2003 trace2_region_leave("unpack_trees", "traverse_trees", the_repository
);
2004 trace_performance_leave("traverse_trees");
2009 /* Any left-over entries in the index? */
2012 struct cache_entry
*ce
= next_cache_entry(o
);
2015 if (unpack_index_entry(ce
, o
) < 0)
2019 mark_all_ce_unused(o
->src_index
);
2021 if (o
->trivial_merges_only
&& o
->internal
.nontrivial_merge
) {
2022 ret
= unpack_failed(o
, "Merge requires file-level merging");
2026 if (!o
->skip_sparse_checkout
) {
2028 * Sparse checkout loop #2: set NEW_SKIP_WORKTREE on entries not in loop #1
2029 * If they will have NEW_SKIP_WORKTREE, also set CE_SKIP_WORKTREE
2030 * so apply_sparse_checkout() won't attempt to remove it from worktree
2032 mark_new_skip_worktree(o
->internal
.pl
, &o
->internal
.result
,
2033 CE_ADDED
, CE_SKIP_WORKTREE
| CE_NEW_SKIP_WORKTREE
,
2037 for (i
= 0; i
< o
->internal
.result
.cache_nr
; i
++) {
2038 struct cache_entry
*ce
= o
->internal
.result
.cache
[i
];
2041 * Entries marked with CE_ADDED in merged_entry() do not have
2042 * verify_absent() check (the check is effectively disabled
2043 * because CE_NEW_SKIP_WORKTREE is set unconditionally).
2045 * Do the real check now because we have had
2046 * correct CE_NEW_SKIP_WORKTREE
2048 if (ce
->ce_flags
& CE_ADDED
&&
2049 verify_absent(ce
, WARNING_SPARSE_ORPHANED_NOT_OVERWRITTEN
, o
))
2052 if (apply_sparse_checkout(&o
->internal
.result
, ce
, o
))
2057 * Inability to sparsify or de-sparsify individual
2058 * paths is not an error, but just a warning.
2060 if (o
->internal
.show_all_errors
)
2061 display_warning_msgs(o
);
2066 ret
= check_updates(o
, &o
->internal
.result
) ? (-2) : 0;
2068 move_index_extensions(&o
->internal
.result
, o
->src_index
);
2070 if (git_env_bool("GIT_TEST_CHECK_CACHE_TREE", 0))
2071 cache_tree_verify(the_repository
,
2072 &o
->internal
.result
);
2073 if (!o
->skip_cache_tree_update
&&
2074 !cache_tree_fully_valid(o
->internal
.result
.cache_tree
))
2075 cache_tree_update(&o
->internal
.result
,
2080 o
->internal
.result
.updated_workdir
= 1;
2081 discard_index(o
->dst_index
);
2082 *o
->dst_index
= o
->internal
.result
;
2084 discard_index(&o
->internal
.result
);
2086 o
->src_index
= NULL
;
2089 if (free_pattern_list
)
2090 clear_pattern_list(&pl
);
2091 if (o
->internal
.dir
) {
2092 dir_clear(o
->internal
.dir
);
2093 o
->internal
.dir
= NULL
;
2095 trace2_region_leave("unpack_trees", "unpack_trees", the_repository
);
2096 trace_performance_leave("unpack_trees");
2100 if (o
->internal
.show_all_errors
)
2101 display_error_msgs(o
);
2102 mark_all_ce_unused(o
->src_index
);
2103 ret
= unpack_failed(o
, NULL
);
2104 if (o
->exiting_early
)
2110 * Update SKIP_WORKTREE bits according to sparsity patterns, and update
2111 * working directory to match.
2113 * CE_NEW_SKIP_WORKTREE is used internally.
2115 enum update_sparsity_result
update_sparsity(struct unpack_trees_options
*o
,
2116 struct pattern_list
*pl
)
2118 enum update_sparsity_result ret
= UPDATE_SPARSITY_SUCCESS
;
2120 unsigned old_show_all_errors
;
2121 int free_pattern_list
= 0;
2123 old_show_all_errors
= o
->internal
.show_all_errors
;
2124 o
->internal
.show_all_errors
= 1;
2125 index_state_init(&o
->internal
.result
, o
->src_index
->repo
);
2128 if (!o
->update
|| o
->index_only
|| o
->skip_sparse_checkout
)
2129 BUG("update_sparsity() is for reflecting sparsity patterns in working directory");
2130 if (o
->src_index
!= o
->dst_index
|| o
->fn
)
2131 BUG("update_sparsity() called wrong");
2133 trace_performance_enter();
2135 /* If we weren't given patterns, use the recorded ones */
2137 free_pattern_list
= 1;
2138 pl
= xcalloc(1, sizeof(*pl
));
2139 populate_from_existing_patterns(o
, pl
);
2141 o
->internal
.pl
= pl
;
2143 /* Expand sparse directories as needed */
2144 expand_index(o
->src_index
, o
->internal
.pl
);
2146 /* Set NEW_SKIP_WORKTREE on existing entries. */
2147 mark_all_ce_unused(o
->src_index
);
2148 mark_new_skip_worktree(o
->internal
.pl
, o
->src_index
, 0,
2149 CE_NEW_SKIP_WORKTREE
, o
->verbose_update
);
2151 /* Then loop over entries and update/remove as needed */
2152 ret
= UPDATE_SPARSITY_SUCCESS
;
2153 for (i
= 0; i
< o
->src_index
->cache_nr
; i
++) {
2154 struct cache_entry
*ce
= o
->src_index
->cache
[i
];
2158 /* -1 because for loop will increment by 1 */
2159 i
+= warn_conflicted_path(o
->src_index
, i
, o
) - 1;
2160 ret
= UPDATE_SPARSITY_WARNINGS
;
2164 if (apply_sparse_checkout(o
->src_index
, ce
, o
))
2165 ret
= UPDATE_SPARSITY_WARNINGS
;
2168 if (check_updates(o
, o
->src_index
))
2169 ret
= UPDATE_SPARSITY_WORKTREE_UPDATE_FAILURES
;
2171 display_warning_msgs(o
);
2172 o
->internal
.show_all_errors
= old_show_all_errors
;
2173 if (free_pattern_list
) {
2174 clear_pattern_list(pl
);
2176 o
->internal
.pl
= NULL
;
2178 trace_performance_leave("update_sparsity");
2182 /* Here come the merge functions */
2184 static int reject_merge(const struct cache_entry
*ce
,
2185 struct unpack_trees_options
*o
)
2187 return add_rejected_path(o
, ERROR_WOULD_OVERWRITE
, ce
->name
);
2190 static int same(const struct cache_entry
*a
, const struct cache_entry
*b
)
2196 if ((a
->ce_flags
| b
->ce_flags
) & CE_CONFLICTED
)
2198 return a
->ce_mode
== b
->ce_mode
&&
2199 oideq(&a
->oid
, &b
->oid
);
2204 * When a CE gets turned into an unmerged entry, we
2205 * want it to be up-to-date
2207 static int verify_uptodate_1(const struct cache_entry
*ce
,
2208 struct unpack_trees_options
*o
,
2209 enum unpack_trees_error_types error_type
)
2217 * CE_VALID and CE_SKIP_WORKTREE cheat, we better check again
2218 * if this entry is truly up-to-date because this file may be
2221 if ((ce
->ce_flags
& CE_VALID
) || ce_skip_worktree(ce
))
2222 ; /* keep checking */
2223 else if (o
->reset
|| ce_uptodate(ce
))
2226 if (!lstat(ce
->name
, &st
)) {
2227 int flags
= CE_MATCH_IGNORE_VALID
|CE_MATCH_IGNORE_SKIP_WORKTREE
;
2228 unsigned changed
= ie_match_stat(o
->src_index
, ce
, &st
, flags
);
2230 if (submodule_from_ce(ce
)) {
2231 int r
= check_submodule_move_head(ce
,
2232 "HEAD", oid_to_hex(&ce
->oid
), o
);
2234 return add_rejected_path(o
, error_type
, ce
->name
);
2241 * Historic default policy was to allow submodule to be out
2242 * of sync wrt the superproject index. If the submodule was
2243 * not considered interesting above, we don't care here.
2245 if (S_ISGITLINK(ce
->ce_mode
))
2250 if (errno
== ENOENT
)
2252 return add_rejected_path(o
, error_type
, ce
->name
);
2255 int verify_uptodate(const struct cache_entry
*ce
,
2256 struct unpack_trees_options
*o
)
2258 if (!o
->skip_sparse_checkout
&&
2259 (ce
->ce_flags
& CE_SKIP_WORKTREE
) &&
2260 (ce
->ce_flags
& CE_NEW_SKIP_WORKTREE
))
2262 return verify_uptodate_1(ce
, o
, ERROR_NOT_UPTODATE_FILE
);
2265 static int verify_uptodate_sparse(const struct cache_entry
*ce
,
2266 struct unpack_trees_options
*o
)
2268 return verify_uptodate_1(ce
, o
, WARNING_SPARSE_NOT_UPTODATE_FILE
);
2272 * TODO: We should actually invalidate o->internal.result, not src_index [1].
2273 * But since cache tree and untracked cache both are not copied to
2274 * o->internal.result until unpacking is complete, we invalidate them on
2275 * src_index instead with the assumption that they will be copied to
2276 * dst_index at the end.
2278 * [1] src_index->cache_tree is also used in unpack_callback() so if
2279 * we invalidate o->internal.result, we need to update it to use
2280 * o->internal.result.cache_tree as well.
2282 static void invalidate_ce_path(const struct cache_entry
*ce
,
2283 struct unpack_trees_options
*o
)
2287 cache_tree_invalidate_path(o
->src_index
, ce
->name
);
2288 untracked_cache_invalidate_path(o
->src_index
, ce
->name
, 1);
2292 * Check that checking out ce->sha1 in subdir ce->name is not
2293 * going to overwrite any working files.
2295 static int verify_clean_submodule(const char *old_sha1
,
2296 const struct cache_entry
*ce
,
2297 struct unpack_trees_options
*o
)
2299 if (!submodule_from_ce(ce
))
2302 return check_submodule_move_head(ce
, old_sha1
,
2303 oid_to_hex(&ce
->oid
), o
);
2306 static int verify_clean_subdirectory(const struct cache_entry
*ce
,
2307 struct unpack_trees_options
*o
)
2310 * we are about to extract "ce->name"; we would not want to lose
2311 * anything in the existing directory there.
2315 struct dir_struct d
;
2319 if (S_ISGITLINK(ce
->ce_mode
)) {
2320 struct object_id oid
;
2321 int sub_head
= resolve_gitlink_ref(ce
->name
, "HEAD", &oid
);
2323 * If we are not going to update the submodule, then
2326 if (!sub_head
&& oideq(&oid
, &ce
->oid
))
2328 return verify_clean_submodule(sub_head
? NULL
: oid_to_hex(&oid
),
2333 * First let's make sure we do not have a local modification
2334 * in that directory.
2336 namelen
= ce_namelen(ce
);
2337 for (i
= locate_in_src_index(ce
, o
);
2338 i
< o
->src_index
->cache_nr
;
2340 struct cache_entry
*ce2
= o
->src_index
->cache
[i
];
2341 int len
= ce_namelen(ce2
);
2342 if (len
< namelen
||
2343 strncmp(ce
->name
, ce2
->name
, namelen
) ||
2344 ce2
->name
[namelen
] != '/')
2347 * ce2->name is an entry in the subdirectory to be
2350 if (!ce_stage(ce2
)) {
2351 if (verify_uptodate(ce2
, o
))
2353 add_entry(o
, ce2
, CE_REMOVE
, 0);
2354 invalidate_ce_path(ce
, o
);
2355 mark_ce_used(ce2
, o
);
2360 /* Do not lose a locally present file that is not ignored. */
2361 pathbuf
= xstrfmt("%.*s/", namelen
, ce
->name
);
2363 memset(&d
, 0, sizeof(d
));
2364 if (o
->internal
.dir
)
2365 setup_standard_excludes(&d
);
2366 i
= read_directory(&d
, o
->src_index
, pathbuf
, namelen
+1, NULL
);
2370 return add_rejected_path(o
, ERROR_NOT_UPTODATE_DIR
, ce
->name
);
2372 /* Do not lose startup_info->original_cwd */
2373 if (startup_info
->original_cwd
&&
2374 !strcmp(startup_info
->original_cwd
, ce
->name
))
2375 return add_rejected_path(o
, ERROR_CWD_IN_THE_WAY
, ce
->name
);
2381 * This gets called when there was no index entry for the tree entry 'dst',
2382 * but we found a file in the working tree that 'lstat()' said was fine,
2383 * and we're on a case-insensitive filesystem.
2385 * See if we can find a case-insensitive match in the index that also
2386 * matches the stat information, and assume it's that other file!
2388 static int icase_exists(struct unpack_trees_options
*o
, const char *name
, int len
, struct stat
*st
)
2390 const struct cache_entry
*src
;
2392 src
= index_file_exists(o
->src_index
, name
, len
, 1);
2393 return src
&& !ie_match_stat(o
->src_index
, src
, st
, CE_MATCH_IGNORE_VALID
|CE_MATCH_IGNORE_SKIP_WORKTREE
);
2396 enum absent_checking_type
{
2398 ABSENT_ANY_DIRECTORY
2401 static int check_ok_to_remove(const char *name
, int len
, int dtype
,
2402 const struct cache_entry
*ce
, struct stat
*st
,
2403 enum unpack_trees_error_types error_type
,
2404 enum absent_checking_type absent_type
,
2405 struct unpack_trees_options
*o
)
2407 const struct cache_entry
*result
;
2410 * It may be that the 'lstat()' succeeded even though
2411 * target 'ce' was absent, because there is an old
2412 * entry that is different only in case..
2414 * Ignore that lstat() if it matches.
2416 if (ignore_case
&& icase_exists(o
, name
, len
, st
))
2419 if (o
->internal
.dir
&&
2420 is_excluded(o
->internal
.dir
, o
->src_index
, name
, &dtype
))
2422 * ce->name is explicitly excluded, so it is Ok to
2426 if (S_ISDIR(st
->st_mode
)) {
2428 * We are checking out path "foo" and
2429 * found "foo/." in the working tree.
2430 * This is tricky -- if we have modified
2431 * files that are in "foo/" we would lose
2434 if (verify_clean_subdirectory(ce
, o
) < 0)
2439 /* If we only care about directories, then we can remove */
2440 if (absent_type
== ABSENT_ANY_DIRECTORY
)
2444 * The previous round may already have decided to
2445 * delete this path, which is in a subdirectory that
2446 * is being replaced with a blob.
2448 result
= index_file_exists(&o
->internal
.result
, name
, len
, 0);
2450 if (result
->ce_flags
& CE_REMOVE
)
2454 return add_rejected_path(o
, error_type
, name
);
2458 * We do not want to remove or overwrite a working tree file that
2459 * is not tracked, unless it is ignored.
2461 static int verify_absent_1(const struct cache_entry
*ce
,
2462 enum unpack_trees_error_types error_type
,
2463 enum absent_checking_type absent_type
,
2464 struct unpack_trees_options
*o
)
2469 if (o
->index_only
|| !o
->update
)
2472 if (o
->reset
== UNPACK_RESET_OVERWRITE_UNTRACKED
) {
2473 /* Avoid nuking startup_info->original_cwd... */
2474 if (startup_info
->original_cwd
&&
2475 !strcmp(startup_info
->original_cwd
, ce
->name
))
2476 return add_rejected_path(o
, ERROR_CWD_IN_THE_WAY
,
2478 /* ...but nuke anything else. */
2482 len
= check_leading_path(ce
->name
, ce_namelen(ce
), 0);
2489 path
= xmemdupz(ce
->name
, len
);
2490 if (lstat(path
, &st
))
2491 ret
= error_errno("cannot stat '%s'", path
);
2493 if (submodule_from_ce(ce
))
2494 ret
= check_submodule_move_head(ce
,
2495 oid_to_hex(&ce
->oid
),
2498 ret
= check_ok_to_remove(path
, len
, DT_UNKNOWN
, NULL
,
2504 } else if (lstat(ce
->name
, &st
)) {
2505 if (errno
!= ENOENT
)
2506 return error_errno("cannot stat '%s'", ce
->name
);
2509 if (submodule_from_ce(ce
))
2510 return check_submodule_move_head(ce
, oid_to_hex(&ce
->oid
),
2513 return check_ok_to_remove(ce
->name
, ce_namelen(ce
),
2514 ce_to_dtype(ce
), ce
, &st
,
2515 error_type
, absent_type
, o
);
2519 static int verify_absent(const struct cache_entry
*ce
,
2520 enum unpack_trees_error_types error_type
,
2521 struct unpack_trees_options
*o
)
2523 if (!o
->skip_sparse_checkout
&& (ce
->ce_flags
& CE_NEW_SKIP_WORKTREE
))
2525 return verify_absent_1(ce
, error_type
, COMPLETELY_ABSENT
, o
);
2528 static int verify_absent_if_directory(const struct cache_entry
*ce
,
2529 enum unpack_trees_error_types error_type
,
2530 struct unpack_trees_options
*o
)
2532 if (!o
->skip_sparse_checkout
&& (ce
->ce_flags
& CE_NEW_SKIP_WORKTREE
))
2534 return verify_absent_1(ce
, error_type
, ABSENT_ANY_DIRECTORY
, o
);
2537 static int verify_absent_sparse(const struct cache_entry
*ce
,
2538 enum unpack_trees_error_types error_type
,
2539 struct unpack_trees_options
*o
)
2541 return verify_absent_1(ce
, error_type
, COMPLETELY_ABSENT
, o
);
2544 static int merged_entry(const struct cache_entry
*ce
,
2545 const struct cache_entry
*old
,
2546 struct unpack_trees_options
*o
)
2548 int update
= CE_UPDATE
;
2549 struct cache_entry
*merge
= dup_cache_entry(ce
, &o
->internal
.result
);
2553 * New index entries. In sparse checkout, the following
2554 * verify_absent() will be delayed until after
2555 * traverse_trees() finishes in unpack_trees(), then:
2557 * - CE_NEW_SKIP_WORKTREE will be computed correctly
2558 * - verify_absent() be called again, this time with
2559 * correct CE_NEW_SKIP_WORKTREE
2561 * verify_absent() call here does nothing in sparse
2562 * checkout (i.e. o->skip_sparse_checkout == 0)
2565 merge
->ce_flags
|= CE_NEW_SKIP_WORKTREE
;
2567 if (verify_absent(merge
,
2568 ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN
, o
)) {
2569 discard_cache_entry(merge
);
2572 invalidate_ce_path(merge
, o
);
2574 if (submodule_from_ce(ce
) && file_exists(ce
->name
)) {
2575 int ret
= check_submodule_move_head(ce
, NULL
,
2576 oid_to_hex(&ce
->oid
),
2582 } else if (!(old
->ce_flags
& CE_CONFLICTED
)) {
2584 * See if we can re-use the old CE directly?
2585 * That way we get the uptodate stat info.
2587 * This also removes the UPDATE flag on a match; otherwise
2588 * we will end up overwriting local changes in the work tree.
2590 if (same(old
, merge
)) {
2591 copy_cache_entry(merge
, old
);
2594 if (verify_uptodate(old
, o
)) {
2595 discard_cache_entry(merge
);
2598 /* Migrate old flags over */
2599 update
|= old
->ce_flags
& (CE_SKIP_WORKTREE
| CE_NEW_SKIP_WORKTREE
);
2600 invalidate_ce_path(old
, o
);
2603 if (submodule_from_ce(ce
) && file_exists(ce
->name
)) {
2604 int ret
= check_submodule_move_head(ce
, oid_to_hex(&old
->oid
),
2605 oid_to_hex(&ce
->oid
),
2612 * Previously unmerged entry left as an existence
2613 * marker by read_index_unmerged();
2615 if (verify_absent_if_directory(merge
,
2616 ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN
, o
)) {
2617 discard_cache_entry(merge
);
2621 invalidate_ce_path(old
, o
);
2624 if (do_add_entry(o
, merge
, update
, CE_STAGEMASK
) < 0)
2629 static int merged_sparse_dir(const struct cache_entry
* const *src
, int n
,
2630 struct unpack_trees_options
*o
)
2632 struct tree_desc t
[MAX_UNPACK_TREES
+ 1];
2633 void * tree_bufs
[MAX_UNPACK_TREES
+ 1];
2634 struct traverse_info info
;
2638 * Create the tree traversal information for traversing into *only* the
2641 setup_traverse_info(&info
, src
[0]->name
);
2642 info
.fn
= unpack_sparse_callback
;
2644 info
.show_all_errors
= o
->internal
.show_all_errors
;
2645 info
.pathspec
= o
->pathspec
;
2647 /* Get the tree descriptors of the sparse directory in each of the merging trees */
2648 for (i
= 0; i
< n
; i
++)
2649 tree_bufs
[i
] = fill_tree_descriptor(o
->src_index
->repo
, &t
[i
],
2650 src
[i
] && !is_null_oid(&src
[i
]->oid
) ? &src
[i
]->oid
: NULL
);
2652 ret
= traverse_trees(o
->src_index
, n
, t
, &info
);
2654 for (i
= 0; i
< n
; i
++)
2660 static int deleted_entry(const struct cache_entry
*ce
,
2661 const struct cache_entry
*old
,
2662 struct unpack_trees_options
*o
)
2664 /* Did it exist in the index? */
2666 if (verify_absent(ce
, ERROR_WOULD_LOSE_UNTRACKED_REMOVED
, o
))
2669 } else if (verify_absent_if_directory(ce
, ERROR_WOULD_LOSE_UNTRACKED_REMOVED
, o
)) {
2673 if (!(old
->ce_flags
& CE_CONFLICTED
) && verify_uptodate(old
, o
))
2675 add_entry(o
, ce
, CE_REMOVE
, 0);
2676 invalidate_ce_path(ce
, o
);
2680 static int keep_entry(const struct cache_entry
*ce
,
2681 struct unpack_trees_options
*o
)
2683 add_entry(o
, ce
, 0, 0);
2685 invalidate_ce_path(ce
, o
);
2690 static void show_stage_entry(FILE *o
,
2691 const char *label
, const struct cache_entry
*ce
)
2694 fprintf(o
, "%s (missing)\n", label
);
2696 fprintf(o
, "%s%06o %s %d\t%s\n",
2699 oid_to_hex(&ce
->oid
),
2705 int threeway_merge(const struct cache_entry
* const *stages
,
2706 struct unpack_trees_options
*o
)
2708 const struct cache_entry
*index
;
2709 const struct cache_entry
*head
;
2710 const struct cache_entry
*remote
= stages
[o
->head_idx
+ 1];
2713 int remote_match
= 0;
2715 int df_conflict_head
= 0;
2716 int df_conflict_remote
= 0;
2718 int any_anc_missing
= 0;
2719 int no_anc_exists
= 1;
2722 for (i
= 1; i
< o
->head_idx
; i
++) {
2723 if (!stages
[i
] || stages
[i
] == o
->df_conflict_entry
)
2724 any_anc_missing
= 1;
2730 head
= stages
[o
->head_idx
];
2732 if (head
== o
->df_conflict_entry
) {
2733 df_conflict_head
= 1;
2737 if (remote
== o
->df_conflict_entry
) {
2738 df_conflict_remote
= 1;
2743 * First, if there's a #16 situation, note that to prevent #13
2746 if (!same(remote
, head
)) {
2747 for (i
= 1; i
< o
->head_idx
; i
++) {
2748 if (same(stages
[i
], head
)) {
2751 if (same(stages
[i
], remote
)) {
2758 * We start with cases where the index is allowed to match
2759 * something other than the head: #14(ALT) and #2ALT, where it
2760 * is permitted to match the result instead.
2762 /* #14, #14ALT, #2ALT */
2763 if (remote
&& !df_conflict_head
&& head_match
&& !remote_match
) {
2764 if (index
&& !same(index
, remote
) && !same(index
, head
)) {
2765 if (S_ISSPARSEDIR(index
->ce_mode
))
2766 return merged_sparse_dir(stages
, 4, o
);
2768 return reject_merge(index
, o
);
2770 return merged_entry(remote
, index
, o
);
2773 * If we have an entry in the index cache, then we want to
2774 * make sure that it matches head.
2776 if (index
&& !same(index
, head
)) {
2777 if (S_ISSPARSEDIR(index
->ce_mode
))
2778 return merged_sparse_dir(stages
, 4, o
);
2780 return reject_merge(index
, o
);
2785 if (same(head
, remote
))
2786 return merged_entry(head
, index
, o
);
2788 if (!df_conflict_remote
&& remote_match
&& !head_match
)
2789 return merged_entry(head
, index
, o
);
2793 if (!head
&& !remote
&& any_anc_missing
)
2797 * Under the "aggressive" rule, we resolve mostly trivial
2798 * cases that we historically had git-merge-one-file resolve.
2800 if (o
->aggressive
) {
2801 int head_deleted
= !head
;
2802 int remote_deleted
= !remote
;
2803 const struct cache_entry
*ce
= NULL
;
2812 for (i
= 1; i
< o
->head_idx
; i
++) {
2813 if (stages
[i
] && stages
[i
] != o
->df_conflict_entry
) {
2822 * Deleted in one and unchanged in the other.
2824 if ((head_deleted
&& remote_deleted
) ||
2825 (head_deleted
&& remote
&& remote_match
) ||
2826 (remote_deleted
&& head
&& head_match
)) {
2828 return deleted_entry(index
, index
, o
);
2829 if (ce
&& !head_deleted
) {
2830 if (verify_absent(ce
, ERROR_WOULD_LOSE_UNTRACKED_REMOVED
, o
))
2836 * Added in both, identically.
2838 if (no_anc_exists
&& head
&& remote
&& same(head
, remote
))
2839 return merged_entry(head
, index
, o
);
2843 /* Handle "no merge" cases (see t/t1000-read-tree-m-3way.sh) */
2846 * If we've reached the "no merge" cases and we're merging
2847 * a sparse directory, we may have an "edit/edit" conflict that
2848 * can be resolved by individually merging directory contents.
2850 if (S_ISSPARSEDIR(index
->ce_mode
))
2851 return merged_sparse_dir(stages
, 4, o
);
2854 * If we're not merging a sparse directory, ensure the index is
2855 * up-to-date to avoid files getting overwritten with conflict
2858 if (verify_uptodate(index
, o
))
2862 o
->internal
.nontrivial_merge
= 1;
2864 /* #2, #3, #4, #6, #7, #9, #10, #11. */
2866 if (!head_match
|| !remote_match
) {
2867 for (i
= 1; i
< o
->head_idx
; i
++) {
2868 if (stages
[i
] && stages
[i
] != o
->df_conflict_entry
) {
2869 keep_entry(stages
[i
], o
);
2877 fprintf(stderr
, "read-tree: warning #16 detected\n");
2878 show_stage_entry(stderr
, "head ", stages
[head_match
]);
2879 show_stage_entry(stderr
, "remote ", stages
[remote_match
]);
2882 if (head
) { count
+= keep_entry(head
, o
); }
2883 if (remote
) { count
+= keep_entry(remote
, o
); }
2890 * The rule is to "carry forward" what is in the index without losing
2891 * information across a "fast-forward", favoring a successful merge
2892 * over a merge failure when it makes sense. For details of the
2893 * "carry forward" rule, please see <Documentation/git-read-tree.txt>.
2896 int twoway_merge(const struct cache_entry
* const *src
,
2897 struct unpack_trees_options
*o
)
2899 const struct cache_entry
*current
= src
[0];
2900 const struct cache_entry
*oldtree
= src
[1];
2901 const struct cache_entry
*newtree
= src
[2];
2903 if (o
->internal
.merge_size
!= 2)
2904 return error("Cannot do a twoway merge of %d trees",
2905 o
->internal
.merge_size
);
2907 if (oldtree
== o
->df_conflict_entry
)
2909 if (newtree
== o
->df_conflict_entry
)
2913 if (current
->ce_flags
& CE_CONFLICTED
) {
2914 if (same(oldtree
, newtree
) || o
->reset
) {
2916 return deleted_entry(current
, current
, o
);
2918 return merged_entry(newtree
, current
, o
);
2920 return reject_merge(current
, o
);
2921 } else if ((!oldtree
&& !newtree
) || /* 4 and 5 */
2922 (!oldtree
&& newtree
&&
2923 same(current
, newtree
)) || /* 6 and 7 */
2924 (oldtree
&& newtree
&&
2925 same(oldtree
, newtree
)) || /* 14 and 15 */
2926 (oldtree
&& newtree
&&
2927 !same(oldtree
, newtree
) && /* 18 and 19 */
2928 same(current
, newtree
))) {
2929 return keep_entry(current
, o
);
2930 } else if (oldtree
&& !newtree
&& same(current
, oldtree
)) {
2932 return deleted_entry(oldtree
, current
, o
);
2933 } else if (oldtree
&& newtree
&&
2934 same(current
, oldtree
) && !same(current
, newtree
)) {
2936 return merged_entry(newtree
, current
, o
);
2937 } else if (current
&& !oldtree
&& newtree
&&
2938 S_ISSPARSEDIR(current
->ce_mode
) != S_ISSPARSEDIR(newtree
->ce_mode
) &&
2939 ce_stage(current
) == 0) {
2941 * This case is a directory/file conflict across the sparse-index
2942 * boundary. When we are changing from one path to another via
2943 * 'git checkout', then we want to replace one entry with another
2944 * via merged_entry(). If there are staged changes, then we should
2945 * reject the merge instead.
2947 return merged_entry(newtree
, current
, o
);
2948 } else if (S_ISSPARSEDIR(current
->ce_mode
)) {
2950 * The sparse directories differ, but we don't know whether that's
2951 * because of two different files in the directory being modified
2952 * (can be trivially merged) or if there is a real file conflict.
2953 * Merge the sparse directory by OID to compare file-by-file.
2955 return merged_sparse_dir(src
, 3, o
);
2957 return reject_merge(current
, o
);
2960 if (oldtree
&& !o
->initial_checkout
) {
2962 * deletion of the path was staged;
2964 if (same(oldtree
, newtree
))
2966 return reject_merge(oldtree
, o
);
2968 return merged_entry(newtree
, current
, o
);
2970 return deleted_entry(oldtree
, current
, o
);
2976 * Keep the index entries at stage0, collapse stage1 but make sure
2977 * stage0 does not have anything there.
2979 int bind_merge(const struct cache_entry
* const *src
,
2980 struct unpack_trees_options
*o
)
2982 const struct cache_entry
*old
= src
[0];
2983 const struct cache_entry
*a
= src
[1];
2985 if (o
->internal
.merge_size
!= 1)
2986 return error("Cannot do a bind merge of %d trees",
2987 o
->internal
.merge_size
);
2989 return o
->quiet
? -1 :
2990 error(ERRORMSG(o
, ERROR_BIND_OVERLAP
),
2991 super_prefixed(a
->name
, o
->super_prefix
),
2992 super_prefixed(old
->name
, o
->super_prefix
));
2994 return keep_entry(old
, o
);
2996 return merged_entry(a
, NULL
, o
);
3003 * - take the stat information from stage0, take the data from stage1
3005 int oneway_merge(const struct cache_entry
* const *src
,
3006 struct unpack_trees_options
*o
)
3008 const struct cache_entry
*old
= src
[0];
3009 const struct cache_entry
*a
= src
[1];
3011 if (o
->internal
.merge_size
!= 1)
3012 return error("Cannot do a oneway merge of %d trees",
3013 o
->internal
.merge_size
);
3015 if (!a
|| a
== o
->df_conflict_entry
)
3016 return deleted_entry(old
, old
, o
);
3018 if (old
&& same(old
, a
)) {
3020 if (o
->reset
&& o
->update
&& !ce_uptodate(old
) && !ce_skip_worktree(old
) &&
3021 !(old
->ce_flags
& CE_FSMONITOR_VALID
)) {
3023 if (lstat(old
->name
, &st
) ||
3024 ie_match_stat(o
->src_index
, old
, &st
, CE_MATCH_IGNORE_VALID
|CE_MATCH_IGNORE_SKIP_WORKTREE
))
3025 update
|= CE_UPDATE
;
3027 if (o
->update
&& S_ISGITLINK(old
->ce_mode
) &&
3028 should_update_submodules() && !verify_uptodate(old
, o
))
3029 update
|= CE_UPDATE
;
3030 add_entry(o
, old
, update
, CE_STAGEMASK
);
3033 return merged_entry(a
, old
, o
);
3037 * Merge worktree and untracked entries in a stash entry.
3039 * Ignore all index entries. Collapse remaining trees but make sure that they
3040 * don't have any conflicting files.
3042 int stash_worktree_untracked_merge(const struct cache_entry
* const *src
,
3043 struct unpack_trees_options
*o
)
3045 const struct cache_entry
*worktree
= src
[1];
3046 const struct cache_entry
*untracked
= src
[2];
3048 if (o
->internal
.merge_size
!= 2)
3049 BUG("invalid merge_size: %d", o
->internal
.merge_size
);
3051 if (worktree
&& untracked
)
3052 return error(_("worktree and untracked commit have duplicate entries: %s"),
3053 super_prefixed(worktree
->name
, o
->super_prefix
));
3055 return merged_entry(worktree
? worktree
: untracked
, NULL
, o
);