3 #include "repository.h"
6 #include "environment.h"
10 #include "tree-walk.h"
11 #include "cache-tree.h"
12 #include "unpack-trees.h"
16 #include "split-index.h"
17 #include "sparse-index.h"
18 #include "submodule.h"
19 #include "submodule-config.h"
20 #include "fsmonitor.h"
21 #include "object-store.h"
22 #include "promisor-remote.h"
24 #include "parallel-checkout.h"
28 * Error messages expected by scripts out of plumbing commands such as
29 * read-tree. Non-scripted Porcelain is not required to use these messages
30 * and in fact are encouraged to reword them to better suit their particular
31 * situation better. See how "git checkout" and "git merge" replaces
32 * them using setup_unpack_trees_porcelain(), for example.
34 static const char *unpack_plumbing_errors
[NB_UNPACK_TREES_WARNING_TYPES
] = {
35 /* ERROR_WOULD_OVERWRITE */
36 "Entry '%s' would be overwritten by merge. Cannot merge.",
38 /* ERROR_NOT_UPTODATE_FILE */
39 "Entry '%s' not uptodate. Cannot merge.",
41 /* ERROR_NOT_UPTODATE_DIR */
42 "Updating '%s' would lose untracked files in it",
44 /* ERROR_CWD_IN_THE_WAY */
45 "Refusing to remove '%s' since it is the current working directory.",
47 /* ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN */
48 "Untracked working tree file '%s' would be overwritten by merge.",
50 /* ERROR_WOULD_LOSE_UNTRACKED_REMOVED */
51 "Untracked working tree file '%s' would be removed by merge.",
53 /* ERROR_BIND_OVERLAP */
54 "Entry '%s' overlaps with '%s'. Cannot bind.",
56 /* ERROR_WOULD_LOSE_SUBMODULE */
57 "Submodule '%s' cannot checkout new HEAD.",
59 /* NB_UNPACK_TREES_ERROR_TYPES; just a meta value */
62 /* WARNING_SPARSE_NOT_UPTODATE_FILE */
63 "Path '%s' not uptodate; will not remove from working tree.",
65 /* WARNING_SPARSE_UNMERGED_FILE */
66 "Path '%s' unmerged; will not remove from working tree.",
68 /* WARNING_SPARSE_ORPHANED_NOT_OVERWRITTEN */
69 "Path '%s' already present; will not overwrite with sparse update.",
72 #define ERRORMSG(o,type) \
73 ( ((o) && (o)->internal.msgs[(type)]) \
74 ? ((o)->internal.msgs[(type)]) \
75 : (unpack_plumbing_errors[(type)]) )
77 static const char *super_prefixed(const char *path
, const char *super_prefix
)
80 * It is necessary and sufficient to have two static buffers
81 * here, as the return value of this function is fed to
82 * error() using the unpack_*_errors[] templates we see above.
84 static struct strbuf buf
[2] = {STRBUF_INIT
, STRBUF_INIT
};
85 static int super_prefix_len
= -1;
86 static unsigned idx
= ARRAY_SIZE(buf
) - 1;
88 if (super_prefix_len
< 0) {
93 for (i
= 0; i
< ARRAY_SIZE(buf
); i
++)
94 strbuf_addstr(&buf
[i
], super_prefix
);
95 super_prefix_len
= buf
[0].len
;
99 if (!super_prefix_len
)
102 if (++idx
>= ARRAY_SIZE(buf
))
105 strbuf_setlen(&buf
[idx
], super_prefix_len
);
106 strbuf_addstr(&buf
[idx
], path
);
111 void setup_unpack_trees_porcelain(struct unpack_trees_options
*opts
,
115 const char **msgs
= opts
->internal
.msgs
;
118 strvec_init(&opts
->internal
.msgs_to_free
);
120 if (!strcmp(cmd
, "checkout"))
121 msg
= advice_enabled(ADVICE_COMMIT_BEFORE_MERGE
)
122 ? _("Your local changes to the following files would be overwritten by checkout:\n%%s"
123 "Please commit your changes or stash them before you switch branches.")
124 : _("Your local changes to the following files would be overwritten by checkout:\n%%s");
125 else if (!strcmp(cmd
, "merge"))
126 msg
= advice_enabled(ADVICE_COMMIT_BEFORE_MERGE
)
127 ? _("Your local changes to the following files would be overwritten by merge:\n%%s"
128 "Please commit your changes or stash them before you merge.")
129 : _("Your local changes to the following files would be overwritten by merge:\n%%s");
131 msg
= advice_enabled(ADVICE_COMMIT_BEFORE_MERGE
)
132 ? _("Your local changes to the following files would be overwritten by %s:\n%%s"
133 "Please commit your changes or stash them before you %s.")
134 : _("Your local changes to the following files would be overwritten by %s:\n%%s");
135 msgs
[ERROR_WOULD_OVERWRITE
] = msgs
[ERROR_NOT_UPTODATE_FILE
] =
136 strvec_pushf(&opts
->internal
.msgs_to_free
, msg
, cmd
, cmd
);
138 msgs
[ERROR_NOT_UPTODATE_DIR
] =
139 _("Updating the following directories would lose untracked files in them:\n%s");
141 msgs
[ERROR_CWD_IN_THE_WAY
] =
142 _("Refusing to remove the current working directory:\n%s");
144 if (!strcmp(cmd
, "checkout"))
145 msg
= advice_enabled(ADVICE_COMMIT_BEFORE_MERGE
)
146 ? _("The following untracked working tree files would be removed by checkout:\n%%s"
147 "Please move or remove them before you switch branches.")
148 : _("The following untracked working tree files would be removed by checkout:\n%%s");
149 else if (!strcmp(cmd
, "merge"))
150 msg
= advice_enabled(ADVICE_COMMIT_BEFORE_MERGE
)
151 ? _("The following untracked working tree files would be removed by merge:\n%%s"
152 "Please move or remove them before you merge.")
153 : _("The following untracked working tree files would be removed by merge:\n%%s");
155 msg
= advice_enabled(ADVICE_COMMIT_BEFORE_MERGE
)
156 ? _("The following untracked working tree files would be removed by %s:\n%%s"
157 "Please move or remove them before you %s.")
158 : _("The following untracked working tree files would be removed by %s:\n%%s");
159 msgs
[ERROR_WOULD_LOSE_UNTRACKED_REMOVED
] =
160 strvec_pushf(&opts
->internal
.msgs_to_free
, msg
, cmd
, cmd
);
162 if (!strcmp(cmd
, "checkout"))
163 msg
= advice_enabled(ADVICE_COMMIT_BEFORE_MERGE
)
164 ? _("The following untracked working tree files would be overwritten by checkout:\n%%s"
165 "Please move or remove them before you switch branches.")
166 : _("The following untracked working tree files would be overwritten by checkout:\n%%s");
167 else if (!strcmp(cmd
, "merge"))
168 msg
= advice_enabled(ADVICE_COMMIT_BEFORE_MERGE
)
169 ? _("The following untracked working tree files would be overwritten by merge:\n%%s"
170 "Please move or remove them before you merge.")
171 : _("The following untracked working tree files would be overwritten by merge:\n%%s");
173 msg
= advice_enabled(ADVICE_COMMIT_BEFORE_MERGE
)
174 ? _("The following untracked working tree files would be overwritten by %s:\n%%s"
175 "Please move or remove them before you %s.")
176 : _("The following untracked working tree files would be overwritten by %s:\n%%s");
177 msgs
[ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN
] =
178 strvec_pushf(&opts
->internal
.msgs_to_free
, msg
, cmd
, cmd
);
181 * Special case: ERROR_BIND_OVERLAP refers to a pair of paths, we
182 * cannot easily display it as a list.
184 msgs
[ERROR_BIND_OVERLAP
] = _("Entry '%s' overlaps with '%s'. Cannot bind.");
186 msgs
[ERROR_WOULD_LOSE_SUBMODULE
] =
187 _("Cannot update submodule:\n%s");
189 msgs
[WARNING_SPARSE_NOT_UPTODATE_FILE
] =
190 _("The following paths are not up to date and were left despite sparse patterns:\n%s");
191 msgs
[WARNING_SPARSE_UNMERGED_FILE
] =
192 _("The following paths are unmerged and were left despite sparse patterns:\n%s");
193 msgs
[WARNING_SPARSE_ORPHANED_NOT_OVERWRITTEN
] =
194 _("The following paths were already present and thus not updated despite sparse patterns:\n%s");
196 opts
->internal
.show_all_errors
= 1;
197 /* rejected paths may not have a static buffer */
198 for (i
= 0; i
< ARRAY_SIZE(opts
->internal
.unpack_rejects
); i
++)
199 opts
->internal
.unpack_rejects
[i
].strdup_strings
= 1;
202 void clear_unpack_trees_porcelain(struct unpack_trees_options
*opts
)
204 strvec_clear(&opts
->internal
.msgs_to_free
);
205 memset(opts
->internal
.msgs
, 0, sizeof(opts
->internal
.msgs
));
208 static int do_add_entry(struct unpack_trees_options
*o
, struct cache_entry
*ce
,
209 unsigned int set
, unsigned int clear
)
216 ce
->ce_flags
= (ce
->ce_flags
& ~clear
) | set
;
217 return add_index_entry(&o
->internal
.result
, ce
,
218 ADD_CACHE_OK_TO_ADD
| ADD_CACHE_OK_TO_REPLACE
);
221 static void add_entry(struct unpack_trees_options
*o
,
222 const struct cache_entry
*ce
,
223 unsigned int set
, unsigned int clear
)
225 do_add_entry(o
, dup_cache_entry(ce
, &o
->internal
.result
), set
, clear
);
229 * add error messages on path <path>
230 * corresponding to the type <e> with the message <msg>
231 * indicating if it should be display in porcelain or not
233 static int add_rejected_path(struct unpack_trees_options
*o
,
234 enum unpack_trees_error_types e
,
240 if (!o
->internal
.show_all_errors
)
241 return error(ERRORMSG(o
, e
), super_prefixed(path
,
245 * Otherwise, insert in a list for future display by
246 * display_(error|warning)_msgs()
248 string_list_append(&o
->internal
.unpack_rejects
[e
], path
);
253 * display all the error messages stored in a nice way
255 static void display_error_msgs(struct unpack_trees_options
*o
)
258 unsigned error_displayed
= 0;
259 for (e
= 0; e
< NB_UNPACK_TREES_ERROR_TYPES
; e
++) {
260 struct string_list
*rejects
= &o
->internal
.unpack_rejects
[e
];
262 if (rejects
->nr
> 0) {
264 struct strbuf path
= STRBUF_INIT
;
267 for (i
= 0; i
< rejects
->nr
; i
++)
268 strbuf_addf(&path
, "\t%s\n", rejects
->items
[i
].string
);
269 error(ERRORMSG(o
, e
), super_prefixed(path
.buf
,
271 strbuf_release(&path
);
273 string_list_clear(rejects
, 0);
276 fprintf(stderr
, _("Aborting\n"));
280 * display all the warning messages stored in a nice way
282 static void display_warning_msgs(struct unpack_trees_options
*o
)
285 unsigned warning_displayed
= 0;
286 for (e
= NB_UNPACK_TREES_ERROR_TYPES
+ 1;
287 e
< NB_UNPACK_TREES_WARNING_TYPES
; e
++) {
288 struct string_list
*rejects
= &o
->internal
.unpack_rejects
[e
];
290 if (rejects
->nr
> 0) {
292 struct strbuf path
= STRBUF_INIT
;
294 warning_displayed
= 1;
295 for (i
= 0; i
< rejects
->nr
; i
++)
296 strbuf_addf(&path
, "\t%s\n", rejects
->items
[i
].string
);
297 warning(ERRORMSG(o
, e
), super_prefixed(path
.buf
,
299 strbuf_release(&path
);
301 string_list_clear(rejects
, 0);
303 if (warning_displayed
)
304 fprintf(stderr
, _("After fixing the above paths, you may want to run `git sparse-checkout reapply`.\n"));
306 static int check_submodule_move_head(const struct cache_entry
*ce
,
309 struct unpack_trees_options
*o
)
311 unsigned flags
= SUBMODULE_MOVE_HEAD_DRY_RUN
;
312 const struct submodule
*sub
= submodule_from_ce(ce
);
318 flags
|= SUBMODULE_MOVE_HEAD_FORCE
;
320 if (submodule_move_head(ce
->name
, o
->super_prefix
, old_id
, new_id
,
322 return add_rejected_path(o
, ERROR_WOULD_LOSE_SUBMODULE
, ce
->name
);
327 * Perform the loading of the repository's gitmodules file. This function is
328 * used by 'check_update()' to perform loading of the gitmodules file in two
329 * different situations:
330 * (1) before removing entries from the working tree if the gitmodules file has
331 * been marked for removal. This situation is specified by 'state' == NULL.
332 * (2) before checking out entries to the working tree if the gitmodules file
333 * has been marked for update. This situation is specified by 'state' != NULL.
335 static void load_gitmodules_file(struct index_state
*index
,
336 struct checkout
*state
)
338 int pos
= index_name_pos(index
, GITMODULES_FILE
, strlen(GITMODULES_FILE
));
341 struct cache_entry
*ce
= index
->cache
[pos
];
342 if (!state
&& ce
->ce_flags
& CE_WT_REMOVE
) {
343 repo_read_gitmodules(the_repository
, 0);
344 } else if (state
&& (ce
->ce_flags
& CE_UPDATE
)) {
345 submodule_free(the_repository
);
346 checkout_entry(ce
, state
, NULL
, NULL
);
347 repo_read_gitmodules(the_repository
, 0);
352 static struct progress
*get_progress(struct unpack_trees_options
*o
,
353 struct index_state
*index
)
355 unsigned cnt
= 0, total
= 0;
357 if (!o
->update
|| !o
->verbose_update
)
360 for (; cnt
< index
->cache_nr
; cnt
++) {
361 const struct cache_entry
*ce
= index
->cache
[cnt
];
362 if (ce
->ce_flags
& (CE_UPDATE
| CE_WT_REMOVE
))
366 return start_delayed_progress(_("Updating files"), total
);
369 static void setup_collided_checkout_detection(struct checkout
*state
,
370 struct index_state
*index
)
375 for (i
= 0; i
< index
->cache_nr
; i
++)
376 index
->cache
[i
]->ce_flags
&= ~CE_MATCHED
;
379 static void report_collided_checkout(struct index_state
*index
)
381 struct string_list list
= STRING_LIST_INIT_NODUP
;
384 for (i
= 0; i
< index
->cache_nr
; i
++) {
385 struct cache_entry
*ce
= index
->cache
[i
];
387 if (!(ce
->ce_flags
& CE_MATCHED
))
390 string_list_append(&list
, ce
->name
);
391 ce
->ce_flags
&= ~CE_MATCHED
;
394 list
.cmp
= fspathcmp
;
395 string_list_sort(&list
);
398 warning(_("the following paths have collided (e.g. case-sensitive paths\n"
399 "on a case-insensitive filesystem) and only one from the same\n"
400 "colliding group is in the working tree:\n"));
402 for (i
= 0; i
< list
.nr
; i
++)
403 fprintf(stderr
, " '%s'\n", list
.items
[i
].string
);
406 string_list_clear(&list
, 0);
409 static int must_checkout(const struct cache_entry
*ce
)
411 return ce
->ce_flags
& CE_UPDATE
;
414 static int check_updates(struct unpack_trees_options
*o
,
415 struct index_state
*index
)
419 struct progress
*progress
;
420 struct checkout state
= CHECKOUT_INIT
;
421 int i
, pc_workers
, pc_threshold
;
423 trace_performance_enter();
424 state
.super_prefix
= o
->super_prefix
;
427 state
.refresh_cache
= 1;
428 state
.istate
= index
;
429 clone_checkout_metadata(&state
.meta
, &o
->meta
, NULL
);
431 if (!o
->update
|| o
->dry_run
) {
432 remove_marked_cache_entries(index
, 0);
433 trace_performance_leave("check_updates");
438 setup_collided_checkout_detection(&state
, index
);
440 progress
= get_progress(o
, index
);
442 /* Start with clean cache to avoid using any possibly outdated info. */
443 invalidate_lstat_cache();
445 git_attr_set_direction(GIT_ATTR_CHECKOUT
);
447 if (should_update_submodules())
448 load_gitmodules_file(index
, NULL
);
450 for (i
= 0; i
< index
->cache_nr
; i
++) {
451 const struct cache_entry
*ce
= index
->cache
[i
];
453 if (ce
->ce_flags
& CE_WT_REMOVE
) {
454 display_progress(progress
, ++cnt
);
455 unlink_entry(ce
, o
->super_prefix
);
459 remove_marked_cache_entries(index
, 0);
460 remove_scheduled_dirs();
462 if (should_update_submodules())
463 load_gitmodules_file(index
, &state
);
465 if (has_promisor_remote())
467 * Prefetch the objects that are to be checked out in the loop
470 prefetch_cache_entries(index
, must_checkout
);
472 get_parallel_checkout_configs(&pc_workers
, &pc_threshold
);
474 enable_delayed_checkout(&state
);
476 init_parallel_checkout();
477 for (i
= 0; i
< index
->cache_nr
; i
++) {
478 struct cache_entry
*ce
= index
->cache
[i
];
480 if (must_checkout(ce
)) {
481 size_t last_pc_queue_size
= pc_queue_size();
483 if (ce
->ce_flags
& CE_WT_REMOVE
)
484 BUG("both update and delete flags are set on %s",
486 ce
->ce_flags
&= ~CE_UPDATE
;
487 errs
|= checkout_entry(ce
, &state
, NULL
, NULL
);
489 if (last_pc_queue_size
== pc_queue_size())
490 display_progress(progress
, ++cnt
);
494 errs
|= run_parallel_checkout(&state
, pc_workers
, pc_threshold
,
496 stop_progress(&progress
);
497 errs
|= finish_delayed_checkout(&state
, o
->verbose_update
);
498 git_attr_set_direction(GIT_ATTR_CHECKIN
);
501 report_collided_checkout(index
);
503 trace_performance_leave("check_updates");
507 static int verify_uptodate_sparse(const struct cache_entry
*ce
,
508 struct unpack_trees_options
*o
);
509 static int verify_absent_sparse(const struct cache_entry
*ce
,
510 enum unpack_trees_error_types
,
511 struct unpack_trees_options
*o
);
513 static int apply_sparse_checkout(struct index_state
*istate
,
514 struct cache_entry
*ce
,
515 struct unpack_trees_options
*o
)
517 int was_skip_worktree
= ce_skip_worktree(ce
);
519 if (ce
->ce_flags
& CE_NEW_SKIP_WORKTREE
)
520 ce
->ce_flags
|= CE_SKIP_WORKTREE
;
522 ce
->ce_flags
&= ~CE_SKIP_WORKTREE
;
523 if (was_skip_worktree
!= ce_skip_worktree(ce
)) {
524 ce
->ce_flags
|= CE_UPDATE_IN_BASE
;
525 mark_fsmonitor_invalid(istate
, ce
);
526 istate
->cache_changed
|= CE_ENTRY_CHANGED
;
530 * if (!was_skip_worktree && !ce_skip_worktree()) {
531 * This is perfectly normal. Move on;
536 * Merge strategies may set CE_UPDATE|CE_REMOVE outside checkout
537 * area as a result of ce_skip_worktree() shortcuts in
538 * verify_absent() and verify_uptodate().
539 * Make sure they don't modify worktree if they are already
540 * outside checkout area
542 if (was_skip_worktree
&& ce_skip_worktree(ce
)) {
543 ce
->ce_flags
&= ~CE_UPDATE
;
546 * By default, when CE_REMOVE is on, CE_WT_REMOVE is also
547 * on to get that file removed from both index and worktree.
548 * If that file is already outside worktree area, don't
551 if (ce
->ce_flags
& CE_REMOVE
)
552 ce
->ce_flags
&= ~CE_WT_REMOVE
;
555 if (!was_skip_worktree
&& ce_skip_worktree(ce
)) {
557 * If CE_UPDATE is set, verify_uptodate() must be called already
558 * also stat info may have lost after merged_entry() so calling
559 * verify_uptodate() again may fail
561 if (!(ce
->ce_flags
& CE_UPDATE
) &&
562 verify_uptodate_sparse(ce
, o
)) {
563 ce
->ce_flags
&= ~CE_SKIP_WORKTREE
;
566 ce
->ce_flags
|= CE_WT_REMOVE
;
567 ce
->ce_flags
&= ~CE_UPDATE
;
569 if (was_skip_worktree
&& !ce_skip_worktree(ce
)) {
570 if (verify_absent_sparse(ce
, WARNING_SPARSE_ORPHANED_NOT_OVERWRITTEN
, o
))
572 ce
->ce_flags
|= CE_UPDATE
;
577 static int warn_conflicted_path(struct index_state
*istate
,
579 struct unpack_trees_options
*o
)
581 char *conflicting_path
= istate
->cache
[i
]->name
;
584 add_rejected_path(o
, WARNING_SPARSE_UNMERGED_FILE
, conflicting_path
);
586 /* Find out how many higher stage entries are at same path */
587 while ((++count
) + i
< istate
->cache_nr
&&
588 !strcmp(conflicting_path
, istate
->cache
[count
+ i
]->name
))
594 static inline int call_unpack_fn(const struct cache_entry
* const *src
,
595 struct unpack_trees_options
*o
)
597 int ret
= o
->fn(src
, o
);
603 static void mark_ce_used(struct cache_entry
*ce
, struct unpack_trees_options
*o
)
605 ce
->ce_flags
|= CE_UNPACKED
;
607 if (o
->internal
.cache_bottom
< o
->src_index
->cache_nr
&&
608 o
->src_index
->cache
[o
->internal
.cache_bottom
] == ce
) {
609 int bottom
= o
->internal
.cache_bottom
;
611 while (bottom
< o
->src_index
->cache_nr
&&
612 o
->src_index
->cache
[bottom
]->ce_flags
& CE_UNPACKED
)
614 o
->internal
.cache_bottom
= bottom
;
618 static void mark_all_ce_unused(struct index_state
*index
)
621 for (i
= 0; i
< index
->cache_nr
; i
++)
622 index
->cache
[i
]->ce_flags
&= ~(CE_UNPACKED
| CE_ADDED
| CE_NEW_SKIP_WORKTREE
);
625 static int locate_in_src_index(const struct cache_entry
*ce
,
626 struct unpack_trees_options
*o
)
628 struct index_state
*index
= o
->src_index
;
629 int len
= ce_namelen(ce
);
630 int pos
= index_name_pos(index
, ce
->name
, len
);
637 * We call unpack_index_entry() with an unmerged cache entry
638 * only in diff-index, and it wants a single callback. Skip
639 * the other unmerged entry with the same name.
641 static void mark_ce_used_same_name(struct cache_entry
*ce
,
642 struct unpack_trees_options
*o
)
644 struct index_state
*index
= o
->src_index
;
645 int len
= ce_namelen(ce
);
648 for (pos
= locate_in_src_index(ce
, o
); pos
< index
->cache_nr
; pos
++) {
649 struct cache_entry
*next
= index
->cache
[pos
];
650 if (len
!= ce_namelen(next
) ||
651 memcmp(ce
->name
, next
->name
, len
))
653 mark_ce_used(next
, o
);
657 static struct cache_entry
*next_cache_entry(struct unpack_trees_options
*o
)
659 const struct index_state
*index
= o
->src_index
;
660 int pos
= o
->internal
.cache_bottom
;
662 while (pos
< index
->cache_nr
) {
663 struct cache_entry
*ce
= index
->cache
[pos
];
664 if (!(ce
->ce_flags
& CE_UNPACKED
))
671 static void add_same_unmerged(const struct cache_entry
*ce
,
672 struct unpack_trees_options
*o
)
674 struct index_state
*index
= o
->src_index
;
675 int len
= ce_namelen(ce
);
676 int pos
= index_name_pos(index
, ce
->name
, len
);
679 die("programming error in a caller of mark_ce_used_same_name");
680 for (pos
= -pos
- 1; pos
< index
->cache_nr
; pos
++) {
681 struct cache_entry
*next
= index
->cache
[pos
];
682 if (len
!= ce_namelen(next
) ||
683 memcmp(ce
->name
, next
->name
, len
))
685 add_entry(o
, next
, 0, 0);
686 mark_ce_used(next
, o
);
690 static int unpack_index_entry(struct cache_entry
*ce
,
691 struct unpack_trees_options
*o
)
693 const struct cache_entry
*src
[MAX_UNPACK_TREES
+ 1] = { NULL
, };
700 if (o
->skip_unmerged
) {
701 add_entry(o
, ce
, 0, 0);
705 ret
= call_unpack_fn(src
, o
);
707 mark_ce_used_same_name(ce
, o
);
711 static int find_cache_pos(struct traverse_info
*, const char *p
, size_t len
);
713 static void restore_cache_bottom(struct traverse_info
*info
, int bottom
)
715 struct unpack_trees_options
*o
= info
->data
;
717 if (o
->diff_index_cached
)
719 o
->internal
.cache_bottom
= bottom
;
722 static int switch_cache_bottom(struct traverse_info
*info
)
724 struct unpack_trees_options
*o
= info
->data
;
727 if (o
->diff_index_cached
)
729 ret
= o
->internal
.cache_bottom
;
730 pos
= find_cache_pos(info
->prev
, info
->name
, info
->namelen
);
733 o
->internal
.cache_bottom
= -2 - pos
;
735 o
->internal
.cache_bottom
= o
->src_index
->cache_nr
;
739 static inline int are_same_oid(struct name_entry
*name_j
, struct name_entry
*name_k
)
741 return !is_null_oid(&name_j
->oid
) && !is_null_oid(&name_k
->oid
) && oideq(&name_j
->oid
, &name_k
->oid
);
744 static int all_trees_same_as_cache_tree(int n
, unsigned long dirmask
,
745 struct name_entry
*names
,
746 struct traverse_info
*info
)
748 struct unpack_trees_options
*o
= info
->data
;
751 if (!o
->merge
|| dirmask
!= ((1 << n
) - 1))
754 for (i
= 1; i
< n
; i
++)
755 if (!are_same_oid(names
, names
+ i
))
758 return cache_tree_matches_traversal(o
->src_index
->cache_tree
, names
, info
);
761 static int index_pos_by_traverse_info(struct name_entry
*names
,
762 struct traverse_info
*info
)
764 struct unpack_trees_options
*o
= info
->data
;
765 struct strbuf name
= STRBUF_INIT
;
768 strbuf_make_traverse_path(&name
, info
, names
->path
, names
->pathlen
);
769 strbuf_addch(&name
, '/');
770 pos
= index_name_pos(o
->src_index
, name
.buf
, name
.len
);
772 if (!o
->src_index
->sparse_index
||
773 !(o
->src_index
->cache
[pos
]->ce_flags
& CE_SKIP_WORKTREE
))
774 BUG("This is a directory and should not exist in index");
778 if (pos
>= o
->src_index
->cache_nr
||
779 !starts_with(o
->src_index
->cache
[pos
]->name
, name
.buf
) ||
780 (pos
> 0 && starts_with(o
->src_index
->cache
[pos
-1]->name
, name
.buf
)))
781 BUG("pos %d doesn't point to the first entry of %s in index",
783 strbuf_release(&name
);
788 * Fast path if we detect that all trees are the same as cache-tree at this
789 * path. We'll walk these trees in an iterative loop using cache-tree/index
790 * instead of ODB since we already know what these trees contain.
792 static int traverse_by_cache_tree(int pos
, int nr_entries
, int nr_names
,
793 struct traverse_info
*info
)
795 struct cache_entry
*src
[MAX_UNPACK_TREES
+ 1] = { NULL
, };
796 struct unpack_trees_options
*o
= info
->data
;
797 struct cache_entry
*tree_ce
= NULL
;
802 BUG("We need cache-tree to do this optimization");
805 * Do what unpack_callback() and unpack_single_entry() normally
806 * do. But we walk all paths in an iterative loop instead.
808 * D/F conflicts and higher stage entries are not a concern
809 * because cache-tree would be invalidated and we would never
810 * get here in the first place.
812 for (i
= 0; i
< nr_entries
; i
++) {
813 int new_ce_len
, len
, rc
;
815 src
[0] = o
->src_index
->cache
[pos
+ i
];
817 len
= ce_namelen(src
[0]);
818 new_ce_len
= cache_entry_size(len
);
820 if (new_ce_len
> ce_len
) {
822 tree_ce
= xrealloc(tree_ce
, new_ce_len
);
823 memset(tree_ce
, 0, new_ce_len
);
826 tree_ce
->ce_flags
= create_ce_flags(0);
828 for (d
= 1; d
<= nr_names
; d
++)
832 tree_ce
->ce_mode
= src
[0]->ce_mode
;
833 tree_ce
->ce_namelen
= len
;
834 oidcpy(&tree_ce
->oid
, &src
[0]->oid
);
835 memcpy(tree_ce
->name
, src
[0]->name
, len
+ 1);
837 rc
= call_unpack_fn((const struct cache_entry
* const *)src
, o
);
843 mark_ce_used(src
[0], o
);
846 if (o
->internal
.debug_unpack
)
847 printf("Unpacked %d entries from %s to %s using cache-tree\n",
849 o
->src_index
->cache
[pos
]->name
,
850 o
->src_index
->cache
[pos
+ nr_entries
- 1]->name
);
854 static int traverse_trees_recursive(int n
, unsigned long dirmask
,
855 unsigned long df_conflicts
,
856 struct name_entry
*names
,
857 struct traverse_info
*info
)
859 struct unpack_trees_options
*o
= info
->data
;
862 struct tree_desc t
[MAX_UNPACK_TREES
];
863 void *buf
[MAX_UNPACK_TREES
];
864 struct traverse_info newinfo
;
865 struct name_entry
*p
;
868 nr_entries
= all_trees_same_as_cache_tree(n
, dirmask
, names
, info
);
869 if (nr_entries
> 0) {
870 int pos
= index_pos_by_traverse_info(names
, info
);
872 if (!o
->merge
|| df_conflicts
)
873 BUG("Wrong condition to get here buddy");
876 * All entries up to 'pos' must have been processed
877 * (i.e. marked CE_UNPACKED) at this point. But to be safe,
878 * save and restore cache_bottom anyway to not miss
879 * unprocessed entries before 'pos'.
881 bottom
= o
->internal
.cache_bottom
;
882 ret
= traverse_by_cache_tree(pos
, nr_entries
, n
, info
);
883 o
->internal
.cache_bottom
= bottom
;
893 newinfo
.pathspec
= info
->pathspec
;
894 newinfo
.name
= p
->path
;
895 newinfo
.namelen
= p
->pathlen
;
896 newinfo
.mode
= p
->mode
;
897 newinfo
.pathlen
= st_add3(newinfo
.pathlen
, tree_entry_len(p
), 1);
898 newinfo
.df_conflicts
|= df_conflicts
;
901 * Fetch the tree from the ODB for each peer directory in the
904 * For 2- and 3-way traversals, we try to avoid hitting the
905 * ODB twice for the same OID. This should yield a nice speed
906 * up in checkouts and merges when the commits are similar.
908 * We don't bother doing the full O(n^2) search for larger n,
909 * because wider traversals don't happen that often and we
910 * avoid the search setup.
912 * When 2 peer OIDs are the same, we just copy the tree
913 * descriptor data. This implicitly borrows the buffer
914 * data from the earlier cell.
916 for (i
= 0; i
< n
; i
++, dirmask
>>= 1) {
917 if (i
> 0 && are_same_oid(&names
[i
], &names
[i
- 1]))
919 else if (i
> 1 && are_same_oid(&names
[i
], &names
[i
- 2]))
922 const struct object_id
*oid
= NULL
;
925 buf
[nr_buf
++] = fill_tree_descriptor(the_repository
, t
+ i
, oid
);
929 bottom
= switch_cache_bottom(&newinfo
);
930 ret
= traverse_trees(o
->src_index
, n
, t
, &newinfo
);
931 restore_cache_bottom(&newinfo
, bottom
);
933 for (i
= 0; i
< nr_buf
; i
++)
940 * Compare the traverse-path to the cache entry without actually
941 * having to generate the textual representation of the traverse
944 * NOTE! This *only* compares up to the size of the traverse path
945 * itself - the caller needs to do the final check for the cache
946 * entry having more data at the end!
948 static int do_compare_entry_piecewise(const struct cache_entry
*ce
,
949 const struct traverse_info
*info
,
950 const char *name
, size_t namelen
,
957 int cmp
= do_compare_entry_piecewise(ce
, info
->prev
,
958 info
->name
, info
->namelen
,
963 pathlen
= info
->pathlen
;
964 ce_len
= ce_namelen(ce
);
966 /* If ce_len < pathlen then we must have previously hit "name == directory" entry */
967 if (ce_len
< pathlen
)
971 ce_name
= ce
->name
+ pathlen
;
973 return df_name_compare(ce_name
, ce_len
, S_IFREG
, name
, namelen
, mode
);
976 static int do_compare_entry(const struct cache_entry
*ce
,
977 const struct traverse_info
*info
,
978 const char *name
, size_t namelen
,
987 * If we have not precomputed the traverse path, it is quicker
988 * to avoid doing so. But if we have precomputed it,
989 * it is quicker to use the precomputed version.
991 if (!info
->traverse_path
)
992 return do_compare_entry_piecewise(ce
, info
, name
, namelen
, mode
);
994 cmp
= strncmp(ce
->name
, info
->traverse_path
, info
->pathlen
);
998 pathlen
= info
->pathlen
;
999 ce_len
= ce_namelen(ce
);
1001 if (ce_len
< pathlen
)
1005 ce_name
= ce
->name
+ pathlen
;
1007 ce_mode
= S_ISSPARSEDIR(ce
->ce_mode
) ? S_IFDIR
: S_IFREG
;
1008 return df_name_compare(ce_name
, ce_len
, ce_mode
, name
, namelen
, mode
);
1011 static int compare_entry(const struct cache_entry
*ce
, const struct traverse_info
*info
, const struct name_entry
*n
)
1013 int cmp
= do_compare_entry(ce
, info
, n
->path
, n
->pathlen
, n
->mode
);
1018 * At this point, we know that we have a prefix match. If ce
1019 * is a sparse directory, then allow an exact match. This only
1020 * works when the input name is a directory, since ce->name
1021 * ends in a directory separator.
1023 if (S_ISSPARSEDIR(ce
->ce_mode
) &&
1024 ce
->ce_namelen
== traverse_path_len(info
, tree_entry_len(n
)) + 1)
1028 * Even if the beginning compared identically, the ce should
1029 * compare as bigger than a directory leading up to it!
1031 return ce_namelen(ce
) > traverse_path_len(info
, tree_entry_len(n
));
1034 static int ce_in_traverse_path(const struct cache_entry
*ce
,
1035 const struct traverse_info
*info
)
1039 if (do_compare_entry(ce
, info
->prev
,
1040 info
->name
, info
->namelen
, info
->mode
))
1043 * If ce (blob) is the same name as the path (which is a tree
1044 * we will be descending into), it won't be inside it.
1046 return (info
->pathlen
< ce_namelen(ce
));
1049 static struct cache_entry
*create_ce_entry(const struct traverse_info
*info
,
1050 const struct name_entry
*n
,
1052 struct index_state
*istate
,
1054 int is_sparse_directory
)
1056 size_t len
= traverse_path_len(info
, tree_entry_len(n
));
1057 size_t alloc_len
= is_sparse_directory
? len
+ 1 : len
;
1058 struct cache_entry
*ce
=
1060 make_empty_transient_cache_entry(alloc_len
, NULL
) :
1061 make_empty_cache_entry(istate
, alloc_len
);
1063 ce
->ce_mode
= create_ce_mode(n
->mode
);
1064 ce
->ce_flags
= create_ce_flags(stage
);
1065 ce
->ce_namelen
= len
;
1066 oidcpy(&ce
->oid
, &n
->oid
);
1067 /* len+1 because the cache_entry allocates space for NUL */
1068 make_traverse_path(ce
->name
, len
+ 1, info
, n
->path
, n
->pathlen
);
1070 if (is_sparse_directory
) {
1071 ce
->name
[len
] = '/';
1072 ce
->name
[len
+ 1] = '\0';
1074 ce
->ce_flags
|= CE_SKIP_WORKTREE
;
1081 * Determine whether the path specified by 'p' should be unpacked as a new
1082 * sparse directory in a sparse index. A new sparse directory 'A/':
1083 * - must be outside the sparse cone.
1084 * - must not already be in the index (i.e., no index entry with name 'A/'
1086 * - must not have any child entries in the index (i.e., no index entry
1087 * 'A/<something>' exists).
1088 * If 'p' meets the above requirements, return 1; otherwise, return 0.
1090 static int entry_is_new_sparse_dir(const struct traverse_info
*info
,
1091 const struct name_entry
*p
)
1094 struct strbuf dirpath
= STRBUF_INIT
;
1095 struct unpack_trees_options
*o
= info
->data
;
1097 if (!S_ISDIR(p
->mode
))
1101 * If the path is inside the sparse cone, it can't be a sparse directory.
1103 strbuf_add(&dirpath
, info
->traverse_path
, info
->pathlen
);
1104 strbuf_add(&dirpath
, p
->path
, p
->pathlen
);
1105 strbuf_addch(&dirpath
, '/');
1106 if (path_in_cone_mode_sparse_checkout(dirpath
.buf
, o
->src_index
)) {
1111 pos
= index_name_pos_sparse(o
->src_index
, dirpath
.buf
, dirpath
.len
);
1113 /* Path is already in the index, not a new sparse dir */
1118 /* Where would this sparse dir be inserted into the index? */
1120 if (pos
>= o
->src_index
->cache_nr
) {
1122 * Sparse dir would be inserted at the end of the index, so we
1123 * know it has no child entries.
1130 * If the dir has child entries in the index, the first would be at the
1131 * position the sparse directory would be inserted. If the entry at this
1132 * position is inside the dir, not a new sparse dir.
1134 res
= strncmp(o
->src_index
->cache
[pos
]->name
, dirpath
.buf
, dirpath
.len
);
1137 strbuf_release(&dirpath
);
1142 * Note that traverse_by_cache_tree() duplicates some logic in this function
1143 * without actually calling it. If you change the logic here you may need to
1144 * check and change there as well.
1146 static int unpack_single_entry(int n
, unsigned long mask
,
1147 unsigned long dirmask
,
1148 struct cache_entry
**src
,
1149 const struct name_entry
*names
,
1150 const struct traverse_info
*info
,
1151 int *is_new_sparse_dir
)
1154 struct unpack_trees_options
*o
= info
->data
;
1155 unsigned long conflicts
= info
->df_conflicts
| dirmask
;
1156 const struct name_entry
*p
= names
;
1158 *is_new_sparse_dir
= 0;
1159 if (mask
== dirmask
&& !src
[0]) {
1161 * If we're not in a sparse index, we can't unpack a directory
1162 * without recursing into it, so we return.
1164 if (!o
->src_index
->sparse_index
)
1167 /* Find first entry with a real name (we could use "mask" too) */
1172 * If the directory is completely missing from the index but
1173 * would otherwise be a sparse directory, we should unpack it.
1174 * If not, we'll return and continue recursively traversing the
1177 *is_new_sparse_dir
= entry_is_new_sparse_dir(info
, p
);
1178 if (!*is_new_sparse_dir
)
1183 * When we are unpacking a sparse directory, then this isn't necessarily
1184 * a directory-file conflict.
1186 if (mask
== dirmask
&&
1187 (*is_new_sparse_dir
|| (src
[0] && S_ISSPARSEDIR(src
[0]->ce_mode
))))
1191 * Ok, we've filled in up to any potential index entry in src[0],
1194 for (i
= 0; i
< n
; i
++) {
1196 unsigned int bit
= 1ul << i
;
1197 if (conflicts
& bit
) {
1198 src
[i
+ o
->merge
] = o
->df_conflict_entry
;
1205 else if (i
+ 1 < o
->head_idx
)
1207 else if (i
+ 1 > o
->head_idx
)
1213 * If the merge bit is set, then the cache entries are
1214 * discarded in the following block. In this case,
1215 * construct "transient" cache_entries, as they are
1216 * not stored in the index. otherwise construct the
1217 * cache entry from the index aware logic.
1219 src
[i
+ o
->merge
] = create_ce_entry(info
, names
+ i
, stage
,
1220 &o
->internal
.result
,
1221 o
->merge
, bit
& dirmask
);
1225 int rc
= call_unpack_fn((const struct cache_entry
* const *)src
,
1227 for (i
= 0; i
< n
; i
++) {
1228 struct cache_entry
*ce
= src
[i
+ o
->merge
];
1229 if (ce
!= o
->df_conflict_entry
)
1230 discard_cache_entry(ce
);
1235 for (i
= 0; i
< n
; i
++)
1236 if (src
[i
] && src
[i
] != o
->df_conflict_entry
)
1237 if (do_add_entry(o
, src
[i
], 0, 0))
1243 static int unpack_failed(struct unpack_trees_options
*o
, const char *message
)
1245 discard_index(&o
->internal
.result
);
1246 if (!o
->quiet
&& !o
->exiting_early
) {
1248 return error("%s", message
);
1255 * The tree traversal is looking at name p. If we have a matching entry,
1256 * return it. If name p is a directory in the index, do not return
1257 * anything, as we will want to match it when the traversal descends into
1260 static int find_cache_pos(struct traverse_info
*info
,
1261 const char *p
, size_t p_len
)
1264 struct unpack_trees_options
*o
= info
->data
;
1265 struct index_state
*index
= o
->src_index
;
1266 int pfxlen
= info
->pathlen
;
1268 for (pos
= o
->internal
.cache_bottom
; pos
< index
->cache_nr
; pos
++) {
1269 const struct cache_entry
*ce
= index
->cache
[pos
];
1270 const char *ce_name
, *ce_slash
;
1273 if (ce
->ce_flags
& CE_UNPACKED
) {
1275 * cache_bottom entry is already unpacked, so
1276 * we can never match it; don't check it
1279 if (pos
== o
->internal
.cache_bottom
)
1280 ++o
->internal
.cache_bottom
;
1283 if (!ce_in_traverse_path(ce
, info
)) {
1285 * Check if we can skip future cache checks
1286 * (because we're already past all possible
1287 * entries in the traverse path).
1289 if (info
->traverse_path
) {
1290 if (strncmp(ce
->name
, info
->traverse_path
,
1296 ce_name
= ce
->name
+ pfxlen
;
1297 ce_slash
= strchr(ce_name
, '/');
1299 ce_len
= ce_slash
- ce_name
;
1301 ce_len
= ce_namelen(ce
) - pfxlen
;
1302 cmp
= name_compare(p
, p_len
, ce_name
, ce_len
);
1304 * Exact match; if we have a directory we need to
1305 * delay returning it.
1308 return ce_slash
? -2 - pos
: pos
;
1310 continue; /* keep looking */
1312 * ce_name sorts after p->path; could it be that we
1313 * have files under p->path directory in the index?
1314 * E.g. ce_name == "t-i", and p->path == "t"; we may
1315 * have "t/a" in the index.
1317 if (p_len
< ce_len
&& !memcmp(ce_name
, p
, p_len
) &&
1318 ce_name
[p_len
] < '/')
1319 continue; /* keep looking */
1326 * Given a sparse directory entry 'ce', compare ce->name to
1327 * info->traverse_path + p->path + '/' if info->traverse_path
1330 * Compare ce->name to p->path + '/' otherwise. Note that
1331 * ce->name must end in a trailing '/' because it is a sparse
1334 static int sparse_dir_matches_path(const struct cache_entry
*ce
,
1335 struct traverse_info
*info
,
1336 const struct name_entry
*p
)
1338 assert(S_ISSPARSEDIR(ce
->ce_mode
));
1339 assert(ce
->name
[ce
->ce_namelen
- 1] == '/');
1342 return ce
->ce_namelen
== info
->pathlen
+ p
->pathlen
+ 1 &&
1343 ce
->name
[info
->pathlen
- 1] == '/' &&
1344 !strncmp(ce
->name
, info
->traverse_path
, info
->pathlen
) &&
1345 !strncmp(ce
->name
+ info
->pathlen
, p
->path
, p
->pathlen
);
1346 return ce
->ce_namelen
== p
->pathlen
+ 1 &&
1347 !strncmp(ce
->name
, p
->path
, p
->pathlen
);
1350 static struct cache_entry
*find_cache_entry(struct traverse_info
*info
,
1351 const struct name_entry
*p
)
1354 int pos
= find_cache_pos(info
, p
->path
, p
->pathlen
);
1355 struct unpack_trees_options
*o
= info
->data
;
1358 return o
->src_index
->cache
[pos
];
1361 * Check for a sparse-directory entry named "path/".
1362 * Due to the input p->path not having a trailing
1363 * slash, the negative 'pos' value overshoots the
1364 * expected position, hence "-2" instead of "-1".
1368 if (pos
< 0 || pos
>= o
->src_index
->cache_nr
)
1372 * Due to lexicographic sorting and sparse directory
1373 * entries ending with a trailing slash, our path as a
1374 * sparse directory (e.g "subdir/") and our path as a
1375 * file (e.g. "subdir") might be separated by other
1376 * paths (e.g. "subdir-").
1379 struct cache_entry
*ce
= o
->src_index
->cache
[pos
];
1381 if (!skip_prefix(ce
->name
, info
->traverse_path
, &path
) ||
1382 strncmp(path
, p
->path
, p
->pathlen
) ||
1383 path
[p
->pathlen
] != '/')
1386 if (S_ISSPARSEDIR(ce
->ce_mode
) &&
1387 sparse_dir_matches_path(ce
, info
, p
))
1396 static void debug_path(struct traverse_info
*info
)
1399 debug_path(info
->prev
);
1400 if (*info
->prev
->name
)
1403 printf("%s", info
->name
);
1406 static void debug_name_entry(int i
, struct name_entry
*n
)
1408 printf("ent#%d %06o %s\n", i
,
1409 n
->path
? n
->mode
: 0,
1410 n
->path
? n
->path
: "(missing)");
1413 static void debug_unpack_callback(int n
,
1415 unsigned long dirmask
,
1416 struct name_entry
*names
,
1417 struct traverse_info
*info
)
1420 printf("* unpack mask %lu, dirmask %lu, cnt %d ",
1424 for (i
= 0; i
< n
; i
++)
1425 debug_name_entry(i
, names
+ i
);
1429 * Returns true if and only if the given cache_entry is a
1430 * sparse-directory entry that matches the given name_entry
1431 * from the tree walk at the given traverse_info.
1433 static int is_sparse_directory_entry(struct cache_entry
*ce
,
1434 const struct name_entry
*name
,
1435 struct traverse_info
*info
)
1437 if (!ce
|| !name
|| !S_ISSPARSEDIR(ce
->ce_mode
))
1440 return sparse_dir_matches_path(ce
, info
, name
);
1443 static int unpack_sparse_callback(int n
, unsigned long mask
, unsigned long dirmask
, struct name_entry
*names
, struct traverse_info
*info
)
1445 struct cache_entry
*src
[MAX_UNPACK_TREES
+ 1] = { NULL
, };
1446 struct unpack_trees_options
*o
= info
->data
;
1447 int ret
, is_new_sparse_dir
;
1452 * Unlike in 'unpack_callback', where src[0] is derived from the index when
1453 * merging, src[0] is a transient cache entry derived from the first tree
1454 * provided. Create the temporary entry as if it came from a non-sparse index.
1456 if (!is_null_oid(&names
[0].oid
)) {
1457 src
[0] = create_ce_entry(info
, &names
[0], 0,
1458 &o
->internal
.result
, 1,
1459 dirmask
& (1ul << 0));
1460 src
[0]->ce_flags
|= (CE_SKIP_WORKTREE
| CE_NEW_SKIP_WORKTREE
);
1464 * 'unpack_single_entry' assumes that src[0] is derived directly from
1465 * the index, rather than from an entry in 'names'. This is *not* true when
1466 * merging a sparse directory, in which case names[0] is the "index" source
1467 * entry. To match the expectations of 'unpack_single_entry', shift past the
1468 * "index" tree (i.e., names[0]) and adjust 'names', 'n', 'mask', and
1469 * 'dirmask' accordingly.
1471 ret
= unpack_single_entry(n
- 1, mask
>> 1, dirmask
>> 1, src
, names
+ 1, info
, &is_new_sparse_dir
);
1474 discard_cache_entry(src
[0]);
1476 return ret
>= 0 ? mask
: -1;
1480 * Note that traverse_by_cache_tree() duplicates some logic in this function
1481 * without actually calling it. If you change the logic here you may need to
1482 * check and change there as well.
1484 static int unpack_callback(int n
, unsigned long mask
, unsigned long dirmask
, struct name_entry
*names
, struct traverse_info
*info
)
1486 struct cache_entry
*src
[MAX_UNPACK_TREES
+ 1] = { NULL
, };
1487 struct unpack_trees_options
*o
= info
->data
;
1488 const struct name_entry
*p
= names
;
1489 int is_new_sparse_dir
;
1491 /* Find first entry with a real name (we could use "mask" too) */
1495 if (o
->internal
.debug_unpack
)
1496 debug_unpack_callback(n
, mask
, dirmask
, names
, info
);
1498 /* Are we supposed to look at the index too? */
1502 struct cache_entry
*ce
;
1504 if (o
->diff_index_cached
)
1505 ce
= next_cache_entry(o
);
1507 ce
= find_cache_entry(info
, p
);
1511 cmp
= compare_entry(ce
, info
, p
);
1513 if (unpack_index_entry(ce
, o
) < 0)
1514 return unpack_failed(o
, NULL
);
1520 * If we skip unmerged index
1521 * entries, we'll skip this
1522 * entry *and* the tree
1523 * entries associated with it!
1525 if (o
->skip_unmerged
) {
1526 add_same_unmerged(ce
, o
);
1536 if (unpack_single_entry(n
, mask
, dirmask
, src
, names
, info
, &is_new_sparse_dir
))
1539 if (o
->merge
&& src
[0]) {
1540 if (ce_stage(src
[0]))
1541 mark_ce_used_same_name(src
[0], o
);
1543 mark_ce_used(src
[0], o
);
1546 /* Now handle any directories.. */
1548 /* special case: "diff-index --cached" looking at a tree */
1549 if (o
->diff_index_cached
&&
1550 n
== 1 && dirmask
== 1 && S_ISDIR(names
->mode
)) {
1552 matches
= cache_tree_matches_traversal(o
->src_index
->cache_tree
,
1555 * Everything under the name matches; skip the
1556 * entire hierarchy. diff_index_cached codepath
1557 * special cases D/F conflicts in such a way that
1558 * it does not do any look-ahead, so this is safe.
1562 * Only increment the cache_bottom if the
1563 * directory isn't a sparse directory index
1564 * entry (if it is, it was already incremented)
1565 * in 'mark_ce_used()'
1567 if (!src
[0] || !S_ISSPARSEDIR(src
[0]->ce_mode
))
1568 o
->internal
.cache_bottom
+= matches
;
1573 if (!is_sparse_directory_entry(src
[0], p
, info
) &&
1574 !is_new_sparse_dir
&&
1575 traverse_trees_recursive(n
, dirmask
, mask
& ~dirmask
,
1586 static int clear_ce_flags_1(struct index_state
*istate
,
1587 struct cache_entry
**cache
, int nr
,
1588 struct strbuf
*prefix
,
1589 int select_mask
, int clear_mask
,
1590 struct pattern_list
*pl
,
1591 enum pattern_match_result default_match
,
1594 /* Whole directory matching */
1595 static int clear_ce_flags_dir(struct index_state
*istate
,
1596 struct cache_entry
**cache
, int nr
,
1597 struct strbuf
*prefix
,
1599 int select_mask
, int clear_mask
,
1600 struct pattern_list
*pl
,
1601 enum pattern_match_result default_match
,
1604 struct cache_entry
**cache_end
;
1607 enum pattern_match_result ret
, orig_ret
;
1608 orig_ret
= path_matches_pattern_list(prefix
->buf
, prefix
->len
,
1609 basename
, &dtype
, pl
, istate
);
1611 strbuf_addch(prefix
, '/');
1613 /* If undecided, use matching result of parent dir in defval */
1614 if (orig_ret
== UNDECIDED
)
1615 ret
= default_match
;
1619 for (cache_end
= cache
; cache_end
!= cache
+ nr
; cache_end
++) {
1620 struct cache_entry
*ce
= *cache_end
;
1621 if (strncmp(ce
->name
, prefix
->buf
, prefix
->len
))
1625 if (pl
->use_cone_patterns
&& orig_ret
== MATCHED_RECURSIVE
) {
1626 struct cache_entry
**ce
= cache
;
1627 rc
= cache_end
- cache
;
1629 while (ce
< cache_end
) {
1630 (*ce
)->ce_flags
&= ~clear_mask
;
1633 } else if (pl
->use_cone_patterns
&& orig_ret
== NOT_MATCHED
) {
1634 rc
= cache_end
- cache
;
1636 rc
= clear_ce_flags_1(istate
, cache
, cache_end
- cache
,
1638 select_mask
, clear_mask
,
1643 strbuf_setlen(prefix
, prefix
->len
- 1);
1648 * Traverse the index, find every entry that matches according to
1649 * o->pl. Do "ce_flags &= ~clear_mask" on those entries. Return the
1650 * number of traversed entries.
1652 * If select_mask is non-zero, only entries whose ce_flags has on of
1653 * those bits enabled are traversed.
1655 * cache : pointer to an index entry
1656 * prefix_len : an offset to its path
1658 * The current path ("prefix") including the trailing '/' is
1659 * cache[0]->name[0..(prefix_len-1)]
1660 * Top level path has prefix_len zero.
1662 static int clear_ce_flags_1(struct index_state
*istate
,
1663 struct cache_entry
**cache
, int nr
,
1664 struct strbuf
*prefix
,
1665 int select_mask
, int clear_mask
,
1666 struct pattern_list
*pl
,
1667 enum pattern_match_result default_match
,
1670 struct cache_entry
**cache_end
= nr
? cache
+ nr
: cache
;
1673 * Process all entries that have the given prefix and meet
1674 * select_mask condition
1676 while(cache
!= cache_end
) {
1677 struct cache_entry
*ce
= *cache
;
1678 const char *name
, *slash
;
1680 enum pattern_match_result ret
;
1682 display_progress(istate
->progress
, progress_nr
);
1684 if (select_mask
&& !(ce
->ce_flags
& select_mask
)) {
1690 if (prefix
->len
&& strncmp(ce
->name
, prefix
->buf
, prefix
->len
))
1693 name
= ce
->name
+ prefix
->len
;
1694 slash
= strchr(name
, '/');
1696 /* If it's a directory, try whole directory match first */
1701 strbuf_add(prefix
, name
, len
);
1703 processed
= clear_ce_flags_dir(istate
, cache
, cache_end
- cache
,
1705 prefix
->buf
+ prefix
->len
- len
,
1706 select_mask
, clear_mask
,
1710 /* clear_c_f_dir eats a whole dir already? */
1713 progress_nr
+= processed
;
1714 strbuf_setlen(prefix
, prefix
->len
- len
);
1718 strbuf_addch(prefix
, '/');
1719 processed
= clear_ce_flags_1(istate
, cache
, cache_end
- cache
,
1721 select_mask
, clear_mask
, pl
,
1722 default_match
, progress_nr
);
1725 progress_nr
+= processed
;
1727 strbuf_setlen(prefix
, prefix
->len
- len
- 1);
1732 dtype
= ce_to_dtype(ce
);
1733 ret
= path_matches_pattern_list(ce
->name
,
1735 name
, &dtype
, pl
, istate
);
1736 if (ret
== UNDECIDED
)
1737 ret
= default_match
;
1738 if (ret
== MATCHED
|| ret
== MATCHED_RECURSIVE
)
1739 ce
->ce_flags
&= ~clear_mask
;
1744 display_progress(istate
->progress
, progress_nr
);
1745 return nr
- (cache_end
- cache
);
1748 static int clear_ce_flags(struct index_state
*istate
,
1749 int select_mask
, int clear_mask
,
1750 struct pattern_list
*pl
,
1753 static struct strbuf prefix
= STRBUF_INIT
;
1757 strbuf_reset(&prefix
);
1759 istate
->progress
= start_delayed_progress(
1760 _("Updating index flags"),
1763 xsnprintf(label
, sizeof(label
), "clear_ce_flags(0x%08lx,0x%08lx)",
1764 (unsigned long)select_mask
, (unsigned long)clear_mask
);
1765 trace2_region_enter("unpack_trees", label
, the_repository
);
1766 rval
= clear_ce_flags_1(istate
,
1770 select_mask
, clear_mask
,
1772 trace2_region_leave("unpack_trees", label
, the_repository
);
1774 stop_progress(&istate
->progress
);
1779 * Set/Clear CE_NEW_SKIP_WORKTREE according to $GIT_DIR/info/sparse-checkout
1781 static void mark_new_skip_worktree(struct pattern_list
*pl
,
1782 struct index_state
*istate
,
1783 int select_flag
, int skip_wt_flag
,
1789 * 1. Pretend the narrowest worktree: only unmerged entries
1792 for (i
= 0; i
< istate
->cache_nr
; i
++) {
1793 struct cache_entry
*ce
= istate
->cache
[i
];
1795 if (select_flag
&& !(ce
->ce_flags
& select_flag
))
1798 if (!ce_stage(ce
) && !(ce
->ce_flags
& CE_CONFLICTED
))
1799 ce
->ce_flags
|= skip_wt_flag
;
1801 ce
->ce_flags
&= ~skip_wt_flag
;
1805 * 2. Widen worktree according to sparse-checkout file.
1806 * Matched entries will have skip_wt_flag cleared (i.e. "in")
1808 clear_ce_flags(istate
, select_flag
, skip_wt_flag
, pl
, show_progress
);
1811 static void populate_from_existing_patterns(struct unpack_trees_options
*o
,
1812 struct pattern_list
*pl
)
1814 if (get_sparse_checkout_patterns(pl
) < 0)
1815 o
->skip_sparse_checkout
= 1;
1817 o
->internal
.pl
= pl
;
1820 static void update_sparsity_for_prefix(const char *prefix
,
1821 struct index_state
*istate
)
1823 int prefix_len
= strlen(prefix
);
1824 struct strbuf ce_prefix
= STRBUF_INIT
;
1826 if (!istate
->sparse_index
)
1829 while (prefix_len
> 0 && prefix
[prefix_len
- 1] == '/')
1832 if (prefix_len
<= 0)
1833 BUG("Invalid prefix passed to update_sparsity_for_prefix");
1835 strbuf_grow(&ce_prefix
, prefix_len
+ 1);
1836 strbuf_add(&ce_prefix
, prefix
, prefix_len
);
1837 strbuf_addch(&ce_prefix
, '/');
1840 * If the prefix points to a sparse directory or a path inside a sparse
1841 * directory, the index should be expanded. This is accomplished in one
1843 * - if the prefix is inside a sparse directory, it will be expanded by
1844 * the 'ensure_full_index(...)' call in 'index_name_pos(...)'.
1845 * - if the prefix matches an existing sparse directory entry,
1846 * 'index_name_pos(...)' will return its index position, triggering
1847 * the 'ensure_full_index(...)' below.
1849 if (!path_in_cone_mode_sparse_checkout(ce_prefix
.buf
, istate
) &&
1850 index_name_pos(istate
, ce_prefix
.buf
, ce_prefix
.len
) >= 0)
1851 ensure_full_index(istate
);
1853 strbuf_release(&ce_prefix
);
1856 static int verify_absent(const struct cache_entry
*,
1857 enum unpack_trees_error_types
,
1858 struct unpack_trees_options
*);
1860 * N-way merge "len" trees. Returns 0 on success, -1 on failure to manipulate the
1861 * resulting index, -2 on failure to reflect the changes to the work tree.
1863 * CE_ADDED, CE_UNPACKED and CE_NEW_SKIP_WORKTREE are used internally
1865 int unpack_trees(unsigned len
, struct tree_desc
*t
, struct unpack_trees_options
*o
)
1867 struct repository
*repo
= the_repository
;
1869 static struct cache_entry
*dfc
;
1870 struct pattern_list pl
;
1871 int free_pattern_list
= 0;
1872 struct dir_struct dir
= DIR_INIT
;
1874 if (o
->reset
== UNPACK_RESET_INVALID
)
1875 BUG("o->reset had a value of 1; should be UNPACK_TREES_*_UNTRACKED");
1877 if (len
> MAX_UNPACK_TREES
)
1878 die("unpack_trees takes at most %d trees", MAX_UNPACK_TREES
);
1879 if (o
->internal
.dir
)
1880 BUG("o->internal.dir is for internal use only");
1882 BUG("o->internal.pl is for internal use only");
1883 if (o
->df_conflict_entry
)
1884 BUG("o->df_conflict_entry is an output only field");
1886 trace_performance_enter();
1887 trace2_region_enter("unpack_trees", "unpack_trees", the_repository
);
1889 prepare_repo_settings(repo
);
1890 if (repo
->settings
.command_requires_full_index
) {
1891 ensure_full_index(o
->src_index
);
1893 ensure_full_index(o
->dst_index
);
1896 if (o
->reset
== UNPACK_RESET_OVERWRITE_UNTRACKED
&&
1897 o
->preserve_ignored
)
1898 BUG("UNPACK_RESET_OVERWRITE_UNTRACKED incompatible with preserved ignored files");
1900 if (!o
->preserve_ignored
) {
1901 o
->internal
.dir
= &dir
;
1902 o
->internal
.dir
->flags
|= DIR_SHOW_IGNORED
;
1903 setup_standard_excludes(o
->internal
.dir
);
1907 update_sparsity_for_prefix(o
->prefix
, o
->src_index
);
1909 if (!core_apply_sparse_checkout
|| !o
->update
)
1910 o
->skip_sparse_checkout
= 1;
1911 if (!o
->skip_sparse_checkout
) {
1912 memset(&pl
, 0, sizeof(pl
));
1913 free_pattern_list
= 1;
1914 populate_from_existing_patterns(o
, &pl
);
1917 index_state_init(&o
->internal
.result
, o
->src_index
->repo
);
1918 o
->internal
.result
.initialized
= 1;
1919 o
->internal
.result
.timestamp
.sec
= o
->src_index
->timestamp
.sec
;
1920 o
->internal
.result
.timestamp
.nsec
= o
->src_index
->timestamp
.nsec
;
1921 o
->internal
.result
.version
= o
->src_index
->version
;
1922 if (!o
->src_index
->split_index
) {
1923 o
->internal
.result
.split_index
= NULL
;
1924 } else if (o
->src_index
== o
->dst_index
) {
1926 * o->dst_index (and thus o->src_index) will be discarded
1927 * and overwritten with o->internal.result at the end of
1928 * this function, so just use src_index's split_index to
1929 * avoid having to create a new one.
1931 o
->internal
.result
.split_index
= o
->src_index
->split_index
;
1932 o
->internal
.result
.split_index
->refcount
++;
1934 o
->internal
.result
.split_index
=
1935 init_split_index(&o
->internal
.result
);
1937 oidcpy(&o
->internal
.result
.oid
, &o
->src_index
->oid
);
1938 o
->internal
.merge_size
= len
;
1939 mark_all_ce_unused(o
->src_index
);
1941 o
->internal
.result
.fsmonitor_last_update
=
1942 xstrdup_or_null(o
->src_index
->fsmonitor_last_update
);
1943 o
->internal
.result
.fsmonitor_has_run_once
= o
->src_index
->fsmonitor_has_run_once
;
1945 if (!o
->src_index
->initialized
&&
1946 !repo
->settings
.command_requires_full_index
&&
1947 is_sparse_index_allowed(&o
->internal
.result
, 0))
1948 o
->internal
.result
.sparse_index
= 1;
1951 * Sparse checkout loop #1: set NEW_SKIP_WORKTREE on existing entries
1953 if (!o
->skip_sparse_checkout
)
1954 mark_new_skip_worktree(o
->internal
.pl
, o
->src_index
, 0,
1955 CE_NEW_SKIP_WORKTREE
, o
->verbose_update
);
1958 dfc
= xcalloc(1, cache_entry_size(0));
1959 o
->df_conflict_entry
= dfc
;
1962 const char *prefix
= o
->prefix
? o
->prefix
: "";
1963 struct traverse_info info
;
1965 setup_traverse_info(&info
, prefix
);
1966 info
.fn
= unpack_callback
;
1968 info
.show_all_errors
= o
->internal
.show_all_errors
;
1969 info
.pathspec
= o
->pathspec
;
1973 * Unpack existing index entries that sort before the
1974 * prefix the tree is spliced into. Note that o->merge
1975 * is always true in this case.
1978 struct cache_entry
*ce
= next_cache_entry(o
);
1981 if (ce_in_traverse_path(ce
, &info
))
1983 if (unpack_index_entry(ce
, o
) < 0)
1988 trace_performance_enter();
1989 trace2_region_enter("unpack_trees", "traverse_trees", the_repository
);
1990 ret
= traverse_trees(o
->src_index
, len
, t
, &info
);
1991 trace2_region_leave("unpack_trees", "traverse_trees", the_repository
);
1992 trace_performance_leave("traverse_trees");
1997 /* Any left-over entries in the index? */
2000 struct cache_entry
*ce
= next_cache_entry(o
);
2003 if (unpack_index_entry(ce
, o
) < 0)
2007 mark_all_ce_unused(o
->src_index
);
2009 if (o
->trivial_merges_only
&& o
->internal
.nontrivial_merge
) {
2010 ret
= unpack_failed(o
, "Merge requires file-level merging");
2014 if (!o
->skip_sparse_checkout
) {
2016 * Sparse checkout loop #2: set NEW_SKIP_WORKTREE on entries not in loop #1
2017 * If they will have NEW_SKIP_WORKTREE, also set CE_SKIP_WORKTREE
2018 * so apply_sparse_checkout() won't attempt to remove it from worktree
2020 mark_new_skip_worktree(o
->internal
.pl
, &o
->internal
.result
,
2021 CE_ADDED
, CE_SKIP_WORKTREE
| CE_NEW_SKIP_WORKTREE
,
2025 for (i
= 0; i
< o
->internal
.result
.cache_nr
; i
++) {
2026 struct cache_entry
*ce
= o
->internal
.result
.cache
[i
];
2029 * Entries marked with CE_ADDED in merged_entry() do not have
2030 * verify_absent() check (the check is effectively disabled
2031 * because CE_NEW_SKIP_WORKTREE is set unconditionally).
2033 * Do the real check now because we have had
2034 * correct CE_NEW_SKIP_WORKTREE
2036 if (ce
->ce_flags
& CE_ADDED
&&
2037 verify_absent(ce
, WARNING_SPARSE_ORPHANED_NOT_OVERWRITTEN
, o
))
2040 if (apply_sparse_checkout(&o
->internal
.result
, ce
, o
))
2045 * Inability to sparsify or de-sparsify individual
2046 * paths is not an error, but just a warning.
2048 if (o
->internal
.show_all_errors
)
2049 display_warning_msgs(o
);
2054 ret
= check_updates(o
, &o
->internal
.result
) ? (-2) : 0;
2056 move_index_extensions(&o
->internal
.result
, o
->src_index
);
2058 if (git_env_bool("GIT_TEST_CHECK_CACHE_TREE", 0))
2059 cache_tree_verify(the_repository
,
2060 &o
->internal
.result
);
2061 if (!o
->skip_cache_tree_update
&&
2062 !cache_tree_fully_valid(o
->internal
.result
.cache_tree
))
2063 cache_tree_update(&o
->internal
.result
,
2068 o
->internal
.result
.updated_workdir
= 1;
2069 discard_index(o
->dst_index
);
2070 *o
->dst_index
= o
->internal
.result
;
2072 discard_index(&o
->internal
.result
);
2074 o
->src_index
= NULL
;
2077 if (free_pattern_list
)
2078 clear_pattern_list(&pl
);
2079 if (o
->internal
.dir
) {
2080 dir_clear(o
->internal
.dir
);
2081 o
->internal
.dir
= NULL
;
2083 trace2_region_leave("unpack_trees", "unpack_trees", the_repository
);
2084 trace_performance_leave("unpack_trees");
2088 if (o
->internal
.show_all_errors
)
2089 display_error_msgs(o
);
2090 mark_all_ce_unused(o
->src_index
);
2091 ret
= unpack_failed(o
, NULL
);
2092 if (o
->exiting_early
)
2098 * Update SKIP_WORKTREE bits according to sparsity patterns, and update
2099 * working directory to match.
2101 * CE_NEW_SKIP_WORKTREE is used internally.
2103 enum update_sparsity_result
update_sparsity(struct unpack_trees_options
*o
,
2104 struct pattern_list
*pl
)
2106 enum update_sparsity_result ret
= UPDATE_SPARSITY_SUCCESS
;
2108 unsigned old_show_all_errors
;
2109 int free_pattern_list
= 0;
2111 old_show_all_errors
= o
->internal
.show_all_errors
;
2112 o
->internal
.show_all_errors
= 1;
2113 index_state_init(&o
->internal
.result
, o
->src_index
->repo
);
2116 if (!o
->update
|| o
->index_only
|| o
->skip_sparse_checkout
)
2117 BUG("update_sparsity() is for reflecting sparsity patterns in working directory");
2118 if (o
->src_index
!= o
->dst_index
|| o
->fn
)
2119 BUG("update_sparsity() called wrong");
2121 trace_performance_enter();
2123 /* If we weren't given patterns, use the recorded ones */
2125 free_pattern_list
= 1;
2126 pl
= xcalloc(1, sizeof(*pl
));
2127 populate_from_existing_patterns(o
, pl
);
2129 o
->internal
.pl
= pl
;
2131 /* Expand sparse directories as needed */
2132 expand_index(o
->src_index
, o
->internal
.pl
);
2134 /* Set NEW_SKIP_WORKTREE on existing entries. */
2135 mark_all_ce_unused(o
->src_index
);
2136 mark_new_skip_worktree(o
->internal
.pl
, o
->src_index
, 0,
2137 CE_NEW_SKIP_WORKTREE
, o
->verbose_update
);
2139 /* Then loop over entries and update/remove as needed */
2140 ret
= UPDATE_SPARSITY_SUCCESS
;
2141 for (i
= 0; i
< o
->src_index
->cache_nr
; i
++) {
2142 struct cache_entry
*ce
= o
->src_index
->cache
[i
];
2146 /* -1 because for loop will increment by 1 */
2147 i
+= warn_conflicted_path(o
->src_index
, i
, o
) - 1;
2148 ret
= UPDATE_SPARSITY_WARNINGS
;
2152 if (apply_sparse_checkout(o
->src_index
, ce
, o
))
2153 ret
= UPDATE_SPARSITY_WARNINGS
;
2156 if (check_updates(o
, o
->src_index
))
2157 ret
= UPDATE_SPARSITY_WORKTREE_UPDATE_FAILURES
;
2159 display_warning_msgs(o
);
2160 o
->internal
.show_all_errors
= old_show_all_errors
;
2161 if (free_pattern_list
) {
2162 clear_pattern_list(pl
);
2164 o
->internal
.pl
= NULL
;
2166 trace_performance_leave("update_sparsity");
2170 /* Here come the merge functions */
2172 static int reject_merge(const struct cache_entry
*ce
,
2173 struct unpack_trees_options
*o
)
2175 return add_rejected_path(o
, ERROR_WOULD_OVERWRITE
, ce
->name
);
2178 static int same(const struct cache_entry
*a
, const struct cache_entry
*b
)
2184 if ((a
->ce_flags
| b
->ce_flags
) & CE_CONFLICTED
)
2186 return a
->ce_mode
== b
->ce_mode
&&
2187 oideq(&a
->oid
, &b
->oid
);
2192 * When a CE gets turned into an unmerged entry, we
2193 * want it to be up-to-date
2195 static int verify_uptodate_1(const struct cache_entry
*ce
,
2196 struct unpack_trees_options
*o
,
2197 enum unpack_trees_error_types error_type
)
2205 * CE_VALID and CE_SKIP_WORKTREE cheat, we better check again
2206 * if this entry is truly up-to-date because this file may be
2209 if ((ce
->ce_flags
& CE_VALID
) || ce_skip_worktree(ce
))
2210 ; /* keep checking */
2211 else if (o
->reset
|| ce_uptodate(ce
))
2214 if (!lstat(ce
->name
, &st
)) {
2215 int flags
= CE_MATCH_IGNORE_VALID
|CE_MATCH_IGNORE_SKIP_WORKTREE
;
2216 unsigned changed
= ie_match_stat(o
->src_index
, ce
, &st
, flags
);
2218 if (submodule_from_ce(ce
)) {
2219 int r
= check_submodule_move_head(ce
,
2220 "HEAD", oid_to_hex(&ce
->oid
), o
);
2222 return add_rejected_path(o
, error_type
, ce
->name
);
2229 * Historic default policy was to allow submodule to be out
2230 * of sync wrt the superproject index. If the submodule was
2231 * not considered interesting above, we don't care here.
2233 if (S_ISGITLINK(ce
->ce_mode
))
2238 if (errno
== ENOENT
)
2240 return add_rejected_path(o
, error_type
, ce
->name
);
2243 int verify_uptodate(const struct cache_entry
*ce
,
2244 struct unpack_trees_options
*o
)
2246 if (!o
->skip_sparse_checkout
&&
2247 (ce
->ce_flags
& CE_SKIP_WORKTREE
) &&
2248 (ce
->ce_flags
& CE_NEW_SKIP_WORKTREE
))
2250 return verify_uptodate_1(ce
, o
, ERROR_NOT_UPTODATE_FILE
);
2253 static int verify_uptodate_sparse(const struct cache_entry
*ce
,
2254 struct unpack_trees_options
*o
)
2256 return verify_uptodate_1(ce
, o
, WARNING_SPARSE_NOT_UPTODATE_FILE
);
2260 * TODO: We should actually invalidate o->internal.result, not src_index [1].
2261 * But since cache tree and untracked cache both are not copied to
2262 * o->internal.result until unpacking is complete, we invalidate them on
2263 * src_index instead with the assumption that they will be copied to
2264 * dst_index at the end.
2266 * [1] src_index->cache_tree is also used in unpack_callback() so if
2267 * we invalidate o->internal.result, we need to update it to use
2268 * o->internal.result.cache_tree as well.
2270 static void invalidate_ce_path(const struct cache_entry
*ce
,
2271 struct unpack_trees_options
*o
)
2275 cache_tree_invalidate_path(o
->src_index
, ce
->name
);
2276 untracked_cache_invalidate_path(o
->src_index
, ce
->name
, 1);
2280 * Check that checking out ce->sha1 in subdir ce->name is not
2281 * going to overwrite any working files.
2283 static int verify_clean_submodule(const char *old_sha1
,
2284 const struct cache_entry
*ce
,
2285 struct unpack_trees_options
*o
)
2287 if (!submodule_from_ce(ce
))
2290 return check_submodule_move_head(ce
, old_sha1
,
2291 oid_to_hex(&ce
->oid
), o
);
2294 static int verify_clean_subdirectory(const struct cache_entry
*ce
,
2295 struct unpack_trees_options
*o
)
2298 * we are about to extract "ce->name"; we would not want to lose
2299 * anything in the existing directory there.
2303 struct dir_struct d
;
2307 if (S_ISGITLINK(ce
->ce_mode
)) {
2308 struct object_id oid
;
2309 int sub_head
= resolve_gitlink_ref(ce
->name
, "HEAD", &oid
);
2311 * If we are not going to update the submodule, then
2314 if (!sub_head
&& oideq(&oid
, &ce
->oid
))
2316 return verify_clean_submodule(sub_head
? NULL
: oid_to_hex(&oid
),
2321 * First let's make sure we do not have a local modification
2322 * in that directory.
2324 namelen
= ce_namelen(ce
);
2325 for (i
= locate_in_src_index(ce
, o
);
2326 i
< o
->src_index
->cache_nr
;
2328 struct cache_entry
*ce2
= o
->src_index
->cache
[i
];
2329 int len
= ce_namelen(ce2
);
2330 if (len
< namelen
||
2331 strncmp(ce
->name
, ce2
->name
, namelen
) ||
2332 ce2
->name
[namelen
] != '/')
2335 * ce2->name is an entry in the subdirectory to be
2338 if (!ce_stage(ce2
)) {
2339 if (verify_uptodate(ce2
, o
))
2341 add_entry(o
, ce2
, CE_REMOVE
, 0);
2342 invalidate_ce_path(ce
, o
);
2343 mark_ce_used(ce2
, o
);
2348 /* Do not lose a locally present file that is not ignored. */
2349 pathbuf
= xstrfmt("%.*s/", namelen
, ce
->name
);
2351 memset(&d
, 0, sizeof(d
));
2352 if (o
->internal
.dir
)
2353 setup_standard_excludes(&d
);
2354 i
= read_directory(&d
, o
->src_index
, pathbuf
, namelen
+1, NULL
);
2358 return add_rejected_path(o
, ERROR_NOT_UPTODATE_DIR
, ce
->name
);
2360 /* Do not lose startup_info->original_cwd */
2361 if (startup_info
->original_cwd
&&
2362 !strcmp(startup_info
->original_cwd
, ce
->name
))
2363 return add_rejected_path(o
, ERROR_CWD_IN_THE_WAY
, ce
->name
);
2369 * This gets called when there was no index entry for the tree entry 'dst',
2370 * but we found a file in the working tree that 'lstat()' said was fine,
2371 * and we're on a case-insensitive filesystem.
2373 * See if we can find a case-insensitive match in the index that also
2374 * matches the stat information, and assume it's that other file!
2376 static int icase_exists(struct unpack_trees_options
*o
, const char *name
, int len
, struct stat
*st
)
2378 const struct cache_entry
*src
;
2380 src
= index_file_exists(o
->src_index
, name
, len
, 1);
2381 return src
&& !ie_match_stat(o
->src_index
, src
, st
, CE_MATCH_IGNORE_VALID
|CE_MATCH_IGNORE_SKIP_WORKTREE
);
2384 enum absent_checking_type
{
2386 ABSENT_ANY_DIRECTORY
2389 static int check_ok_to_remove(const char *name
, int len
, int dtype
,
2390 const struct cache_entry
*ce
, struct stat
*st
,
2391 enum unpack_trees_error_types error_type
,
2392 enum absent_checking_type absent_type
,
2393 struct unpack_trees_options
*o
)
2395 const struct cache_entry
*result
;
2398 * It may be that the 'lstat()' succeeded even though
2399 * target 'ce' was absent, because there is an old
2400 * entry that is different only in case..
2402 * Ignore that lstat() if it matches.
2404 if (ignore_case
&& icase_exists(o
, name
, len
, st
))
2407 if (o
->internal
.dir
&&
2408 is_excluded(o
->internal
.dir
, o
->src_index
, name
, &dtype
))
2410 * ce->name is explicitly excluded, so it is Ok to
2414 if (S_ISDIR(st
->st_mode
)) {
2416 * We are checking out path "foo" and
2417 * found "foo/." in the working tree.
2418 * This is tricky -- if we have modified
2419 * files that are in "foo/" we would lose
2422 if (verify_clean_subdirectory(ce
, o
) < 0)
2427 /* If we only care about directories, then we can remove */
2428 if (absent_type
== ABSENT_ANY_DIRECTORY
)
2432 * The previous round may already have decided to
2433 * delete this path, which is in a subdirectory that
2434 * is being replaced with a blob.
2436 result
= index_file_exists(&o
->internal
.result
, name
, len
, 0);
2438 if (result
->ce_flags
& CE_REMOVE
)
2442 return add_rejected_path(o
, error_type
, name
);
2446 * We do not want to remove or overwrite a working tree file that
2447 * is not tracked, unless it is ignored.
2449 static int verify_absent_1(const struct cache_entry
*ce
,
2450 enum unpack_trees_error_types error_type
,
2451 enum absent_checking_type absent_type
,
2452 struct unpack_trees_options
*o
)
2457 if (o
->index_only
|| !o
->update
)
2460 if (o
->reset
== UNPACK_RESET_OVERWRITE_UNTRACKED
) {
2461 /* Avoid nuking startup_info->original_cwd... */
2462 if (startup_info
->original_cwd
&&
2463 !strcmp(startup_info
->original_cwd
, ce
->name
))
2464 return add_rejected_path(o
, ERROR_CWD_IN_THE_WAY
,
2466 /* ...but nuke anything else. */
2470 len
= check_leading_path(ce
->name
, ce_namelen(ce
), 0);
2477 path
= xmemdupz(ce
->name
, len
);
2478 if (lstat(path
, &st
))
2479 ret
= error_errno("cannot stat '%s'", path
);
2481 if (submodule_from_ce(ce
))
2482 ret
= check_submodule_move_head(ce
,
2483 oid_to_hex(&ce
->oid
),
2486 ret
= check_ok_to_remove(path
, len
, DT_UNKNOWN
, NULL
,
2492 } else if (lstat(ce
->name
, &st
)) {
2493 if (errno
!= ENOENT
)
2494 return error_errno("cannot stat '%s'", ce
->name
);
2497 if (submodule_from_ce(ce
))
2498 return check_submodule_move_head(ce
, oid_to_hex(&ce
->oid
),
2501 return check_ok_to_remove(ce
->name
, ce_namelen(ce
),
2502 ce_to_dtype(ce
), ce
, &st
,
2503 error_type
, absent_type
, o
);
2507 static int verify_absent(const struct cache_entry
*ce
,
2508 enum unpack_trees_error_types error_type
,
2509 struct unpack_trees_options
*o
)
2511 if (!o
->skip_sparse_checkout
&& (ce
->ce_flags
& CE_NEW_SKIP_WORKTREE
))
2513 return verify_absent_1(ce
, error_type
, COMPLETELY_ABSENT
, o
);
2516 static int verify_absent_if_directory(const struct cache_entry
*ce
,
2517 enum unpack_trees_error_types error_type
,
2518 struct unpack_trees_options
*o
)
2520 if (!o
->skip_sparse_checkout
&& (ce
->ce_flags
& CE_NEW_SKIP_WORKTREE
))
2522 return verify_absent_1(ce
, error_type
, ABSENT_ANY_DIRECTORY
, o
);
2525 static int verify_absent_sparse(const struct cache_entry
*ce
,
2526 enum unpack_trees_error_types error_type
,
2527 struct unpack_trees_options
*o
)
2529 return verify_absent_1(ce
, error_type
, COMPLETELY_ABSENT
, o
);
2532 static int merged_entry(const struct cache_entry
*ce
,
2533 const struct cache_entry
*old
,
2534 struct unpack_trees_options
*o
)
2536 int update
= CE_UPDATE
;
2537 struct cache_entry
*merge
= dup_cache_entry(ce
, &o
->internal
.result
);
2541 * New index entries. In sparse checkout, the following
2542 * verify_absent() will be delayed until after
2543 * traverse_trees() finishes in unpack_trees(), then:
2545 * - CE_NEW_SKIP_WORKTREE will be computed correctly
2546 * - verify_absent() be called again, this time with
2547 * correct CE_NEW_SKIP_WORKTREE
2549 * verify_absent() call here does nothing in sparse
2550 * checkout (i.e. o->skip_sparse_checkout == 0)
2553 merge
->ce_flags
|= CE_NEW_SKIP_WORKTREE
;
2555 if (verify_absent(merge
,
2556 ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN
, o
)) {
2557 discard_cache_entry(merge
);
2560 invalidate_ce_path(merge
, o
);
2562 if (submodule_from_ce(ce
) && file_exists(ce
->name
)) {
2563 int ret
= check_submodule_move_head(ce
, NULL
,
2564 oid_to_hex(&ce
->oid
),
2570 } else if (!(old
->ce_flags
& CE_CONFLICTED
)) {
2572 * See if we can re-use the old CE directly?
2573 * That way we get the uptodate stat info.
2575 * This also removes the UPDATE flag on a match; otherwise
2576 * we will end up overwriting local changes in the work tree.
2578 if (same(old
, merge
)) {
2579 copy_cache_entry(merge
, old
);
2582 if (verify_uptodate(old
, o
)) {
2583 discard_cache_entry(merge
);
2586 /* Migrate old flags over */
2587 update
|= old
->ce_flags
& (CE_SKIP_WORKTREE
| CE_NEW_SKIP_WORKTREE
);
2588 invalidate_ce_path(old
, o
);
2591 if (submodule_from_ce(ce
) && file_exists(ce
->name
)) {
2592 int ret
= check_submodule_move_head(ce
, oid_to_hex(&old
->oid
),
2593 oid_to_hex(&ce
->oid
),
2600 * Previously unmerged entry left as an existence
2601 * marker by read_index_unmerged();
2603 if (verify_absent_if_directory(merge
,
2604 ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN
, o
)) {
2605 discard_cache_entry(merge
);
2609 invalidate_ce_path(old
, o
);
2612 if (do_add_entry(o
, merge
, update
, CE_STAGEMASK
) < 0)
2617 static int merged_sparse_dir(const struct cache_entry
* const *src
, int n
,
2618 struct unpack_trees_options
*o
)
2620 struct tree_desc t
[MAX_UNPACK_TREES
+ 1];
2621 void * tree_bufs
[MAX_UNPACK_TREES
+ 1];
2622 struct traverse_info info
;
2626 * Create the tree traversal information for traversing into *only* the
2629 setup_traverse_info(&info
, src
[0]->name
);
2630 info
.fn
= unpack_sparse_callback
;
2632 info
.show_all_errors
= o
->internal
.show_all_errors
;
2633 info
.pathspec
= o
->pathspec
;
2635 /* Get the tree descriptors of the sparse directory in each of the merging trees */
2636 for (i
= 0; i
< n
; i
++)
2637 tree_bufs
[i
] = fill_tree_descriptor(o
->src_index
->repo
, &t
[i
],
2638 src
[i
] && !is_null_oid(&src
[i
]->oid
) ? &src
[i
]->oid
: NULL
);
2640 ret
= traverse_trees(o
->src_index
, n
, t
, &info
);
2642 for (i
= 0; i
< n
; i
++)
2648 static int deleted_entry(const struct cache_entry
*ce
,
2649 const struct cache_entry
*old
,
2650 struct unpack_trees_options
*o
)
2652 /* Did it exist in the index? */
2654 if (verify_absent(ce
, ERROR_WOULD_LOSE_UNTRACKED_REMOVED
, o
))
2657 } else if (verify_absent_if_directory(ce
, ERROR_WOULD_LOSE_UNTRACKED_REMOVED
, o
)) {
2661 if (!(old
->ce_flags
& CE_CONFLICTED
) && verify_uptodate(old
, o
))
2663 add_entry(o
, ce
, CE_REMOVE
, 0);
2664 invalidate_ce_path(ce
, o
);
2668 static int keep_entry(const struct cache_entry
*ce
,
2669 struct unpack_trees_options
*o
)
2671 add_entry(o
, ce
, 0, 0);
2673 invalidate_ce_path(ce
, o
);
2678 static void show_stage_entry(FILE *o
,
2679 const char *label
, const struct cache_entry
*ce
)
2682 fprintf(o
, "%s (missing)\n", label
);
2684 fprintf(o
, "%s%06o %s %d\t%s\n",
2687 oid_to_hex(&ce
->oid
),
2693 int threeway_merge(const struct cache_entry
* const *stages
,
2694 struct unpack_trees_options
*o
)
2696 const struct cache_entry
*index
;
2697 const struct cache_entry
*head
;
2698 const struct cache_entry
*remote
= stages
[o
->head_idx
+ 1];
2701 int remote_match
= 0;
2703 int df_conflict_head
= 0;
2704 int df_conflict_remote
= 0;
2706 int any_anc_missing
= 0;
2707 int no_anc_exists
= 1;
2710 for (i
= 1; i
< o
->head_idx
; i
++) {
2711 if (!stages
[i
] || stages
[i
] == o
->df_conflict_entry
)
2712 any_anc_missing
= 1;
2718 head
= stages
[o
->head_idx
];
2720 if (head
== o
->df_conflict_entry
) {
2721 df_conflict_head
= 1;
2725 if (remote
== o
->df_conflict_entry
) {
2726 df_conflict_remote
= 1;
2731 * First, if there's a #16 situation, note that to prevent #13
2734 if (!same(remote
, head
)) {
2735 for (i
= 1; i
< o
->head_idx
; i
++) {
2736 if (same(stages
[i
], head
)) {
2739 if (same(stages
[i
], remote
)) {
2746 * We start with cases where the index is allowed to match
2747 * something other than the head: #14(ALT) and #2ALT, where it
2748 * is permitted to match the result instead.
2750 /* #14, #14ALT, #2ALT */
2751 if (remote
&& !df_conflict_head
&& head_match
&& !remote_match
) {
2752 if (index
&& !same(index
, remote
) && !same(index
, head
)) {
2753 if (S_ISSPARSEDIR(index
->ce_mode
))
2754 return merged_sparse_dir(stages
, 4, o
);
2756 return reject_merge(index
, o
);
2758 return merged_entry(remote
, index
, o
);
2761 * If we have an entry in the index cache, then we want to
2762 * make sure that it matches head.
2764 if (index
&& !same(index
, head
)) {
2765 if (S_ISSPARSEDIR(index
->ce_mode
))
2766 return merged_sparse_dir(stages
, 4, o
);
2768 return reject_merge(index
, o
);
2773 if (same(head
, remote
))
2774 return merged_entry(head
, index
, o
);
2776 if (!df_conflict_remote
&& remote_match
&& !head_match
)
2777 return merged_entry(head
, index
, o
);
2781 if (!head
&& !remote
&& any_anc_missing
)
2785 * Under the "aggressive" rule, we resolve mostly trivial
2786 * cases that we historically had git-merge-one-file resolve.
2788 if (o
->aggressive
) {
2789 int head_deleted
= !head
;
2790 int remote_deleted
= !remote
;
2791 const struct cache_entry
*ce
= NULL
;
2800 for (i
= 1; i
< o
->head_idx
; i
++) {
2801 if (stages
[i
] && stages
[i
] != o
->df_conflict_entry
) {
2810 * Deleted in one and unchanged in the other.
2812 if ((head_deleted
&& remote_deleted
) ||
2813 (head_deleted
&& remote
&& remote_match
) ||
2814 (remote_deleted
&& head
&& head_match
)) {
2816 return deleted_entry(index
, index
, o
);
2817 if (ce
&& !head_deleted
) {
2818 if (verify_absent(ce
, ERROR_WOULD_LOSE_UNTRACKED_REMOVED
, o
))
2824 * Added in both, identically.
2826 if (no_anc_exists
&& head
&& remote
&& same(head
, remote
))
2827 return merged_entry(head
, index
, o
);
2831 /* Handle "no merge" cases (see t/t1000-read-tree-m-3way.sh) */
2834 * If we've reached the "no merge" cases and we're merging
2835 * a sparse directory, we may have an "edit/edit" conflict that
2836 * can be resolved by individually merging directory contents.
2838 if (S_ISSPARSEDIR(index
->ce_mode
))
2839 return merged_sparse_dir(stages
, 4, o
);
2842 * If we're not merging a sparse directory, ensure the index is
2843 * up-to-date to avoid files getting overwritten with conflict
2846 if (verify_uptodate(index
, o
))
2850 o
->internal
.nontrivial_merge
= 1;
2852 /* #2, #3, #4, #6, #7, #9, #10, #11. */
2854 if (!head_match
|| !remote_match
) {
2855 for (i
= 1; i
< o
->head_idx
; i
++) {
2856 if (stages
[i
] && stages
[i
] != o
->df_conflict_entry
) {
2857 keep_entry(stages
[i
], o
);
2865 fprintf(stderr
, "read-tree: warning #16 detected\n");
2866 show_stage_entry(stderr
, "head ", stages
[head_match
]);
2867 show_stage_entry(stderr
, "remote ", stages
[remote_match
]);
2870 if (head
) { count
+= keep_entry(head
, o
); }
2871 if (remote
) { count
+= keep_entry(remote
, o
); }
2878 * The rule is to "carry forward" what is in the index without losing
2879 * information across a "fast-forward", favoring a successful merge
2880 * over a merge failure when it makes sense. For details of the
2881 * "carry forward" rule, please see <Documentation/git-read-tree.txt>.
2884 int twoway_merge(const struct cache_entry
* const *src
,
2885 struct unpack_trees_options
*o
)
2887 const struct cache_entry
*current
= src
[0];
2888 const struct cache_entry
*oldtree
= src
[1];
2889 const struct cache_entry
*newtree
= src
[2];
2891 if (o
->internal
.merge_size
!= 2)
2892 return error("Cannot do a twoway merge of %d trees",
2893 o
->internal
.merge_size
);
2895 if (oldtree
== o
->df_conflict_entry
)
2897 if (newtree
== o
->df_conflict_entry
)
2901 if (current
->ce_flags
& CE_CONFLICTED
) {
2902 if (same(oldtree
, newtree
) || o
->reset
) {
2904 return deleted_entry(current
, current
, o
);
2906 return merged_entry(newtree
, current
, o
);
2908 return reject_merge(current
, o
);
2909 } else if ((!oldtree
&& !newtree
) || /* 4 and 5 */
2910 (!oldtree
&& newtree
&&
2911 same(current
, newtree
)) || /* 6 and 7 */
2912 (oldtree
&& newtree
&&
2913 same(oldtree
, newtree
)) || /* 14 and 15 */
2914 (oldtree
&& newtree
&&
2915 !same(oldtree
, newtree
) && /* 18 and 19 */
2916 same(current
, newtree
))) {
2917 return keep_entry(current
, o
);
2918 } else if (oldtree
&& !newtree
&& same(current
, oldtree
)) {
2920 return deleted_entry(oldtree
, current
, o
);
2921 } else if (oldtree
&& newtree
&&
2922 same(current
, oldtree
) && !same(current
, newtree
)) {
2924 return merged_entry(newtree
, current
, o
);
2925 } else if (current
&& !oldtree
&& newtree
&&
2926 S_ISSPARSEDIR(current
->ce_mode
) != S_ISSPARSEDIR(newtree
->ce_mode
) &&
2927 ce_stage(current
) == 0) {
2929 * This case is a directory/file conflict across the sparse-index
2930 * boundary. When we are changing from one path to another via
2931 * 'git checkout', then we want to replace one entry with another
2932 * via merged_entry(). If there are staged changes, then we should
2933 * reject the merge instead.
2935 return merged_entry(newtree
, current
, o
);
2936 } else if (S_ISSPARSEDIR(current
->ce_mode
)) {
2938 * The sparse directories differ, but we don't know whether that's
2939 * because of two different files in the directory being modified
2940 * (can be trivially merged) or if there is a real file conflict.
2941 * Merge the sparse directory by OID to compare file-by-file.
2943 return merged_sparse_dir(src
, 3, o
);
2945 return reject_merge(current
, o
);
2948 if (oldtree
&& !o
->initial_checkout
) {
2950 * deletion of the path was staged;
2952 if (same(oldtree
, newtree
))
2954 return reject_merge(oldtree
, o
);
2956 return merged_entry(newtree
, current
, o
);
2958 return deleted_entry(oldtree
, current
, o
);
2964 * Keep the index entries at stage0, collapse stage1 but make sure
2965 * stage0 does not have anything there.
2967 int bind_merge(const struct cache_entry
* const *src
,
2968 struct unpack_trees_options
*o
)
2970 const struct cache_entry
*old
= src
[0];
2971 const struct cache_entry
*a
= src
[1];
2973 if (o
->internal
.merge_size
!= 1)
2974 return error("Cannot do a bind merge of %d trees",
2975 o
->internal
.merge_size
);
2977 return o
->quiet
? -1 :
2978 error(ERRORMSG(o
, ERROR_BIND_OVERLAP
),
2979 super_prefixed(a
->name
, o
->super_prefix
),
2980 super_prefixed(old
->name
, o
->super_prefix
));
2982 return keep_entry(old
, o
);
2984 return merged_entry(a
, NULL
, o
);
2991 * - take the stat information from stage0, take the data from stage1
2993 int oneway_merge(const struct cache_entry
* const *src
,
2994 struct unpack_trees_options
*o
)
2996 const struct cache_entry
*old
= src
[0];
2997 const struct cache_entry
*a
= src
[1];
2999 if (o
->internal
.merge_size
!= 1)
3000 return error("Cannot do a oneway merge of %d trees",
3001 o
->internal
.merge_size
);
3003 if (!a
|| a
== o
->df_conflict_entry
)
3004 return deleted_entry(old
, old
, o
);
3006 if (old
&& same(old
, a
)) {
3008 if (o
->reset
&& o
->update
&& !ce_uptodate(old
) && !ce_skip_worktree(old
) &&
3009 !(old
->ce_flags
& CE_FSMONITOR_VALID
)) {
3011 if (lstat(old
->name
, &st
) ||
3012 ie_match_stat(o
->src_index
, old
, &st
, CE_MATCH_IGNORE_VALID
|CE_MATCH_IGNORE_SKIP_WORKTREE
))
3013 update
|= CE_UPDATE
;
3015 if (o
->update
&& S_ISGITLINK(old
->ce_mode
) &&
3016 should_update_submodules() && !verify_uptodate(old
, o
))
3017 update
|= CE_UPDATE
;
3018 add_entry(o
, old
, update
, CE_STAGEMASK
);
3021 return merged_entry(a
, old
, o
);
3025 * Merge worktree and untracked entries in a stash entry.
3027 * Ignore all index entries. Collapse remaining trees but make sure that they
3028 * don't have any conflicting files.
3030 int stash_worktree_untracked_merge(const struct cache_entry
* const *src
,
3031 struct unpack_trees_options
*o
)
3033 const struct cache_entry
*worktree
= src
[1];
3034 const struct cache_entry
*untracked
= src
[2];
3036 if (o
->internal
.merge_size
!= 2)
3037 BUG("invalid merge_size: %d", o
->internal
.merge_size
);
3039 if (worktree
&& untracked
)
3040 return error(_("worktree and untracked commit have duplicate entries: %s"),
3041 super_prefixed(worktree
->name
, o
->super_prefix
));
3043 return merged_entry(worktree
? worktree
: untracked
, NULL
, o
);