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 "split-index.h"
19 #include "sparse-index.h"
20 #include "submodule.h"
21 #include "submodule-config.h"
24 #include "fsmonitor.h"
25 #include "object-store.h"
26 #include "promisor-remote.h"
28 #include "parallel-checkout.h"
32 * Error messages expected by scripts out of plumbing commands such as
33 * read-tree. Non-scripted Porcelain is not required to use these messages
34 * and in fact are encouraged to reword them to better suit their particular
35 * situation better. See how "git checkout" and "git merge" replaces
36 * them using setup_unpack_trees_porcelain(), for example.
38 static const char *unpack_plumbing_errors
[NB_UNPACK_TREES_WARNING_TYPES
] = {
39 /* ERROR_WOULD_OVERWRITE */
40 "Entry '%s' would be overwritten by merge. Cannot merge.",
42 /* ERROR_NOT_UPTODATE_FILE */
43 "Entry '%s' not uptodate. Cannot merge.",
45 /* ERROR_NOT_UPTODATE_DIR */
46 "Updating '%s' would lose untracked files in it",
48 /* ERROR_CWD_IN_THE_WAY */
49 "Refusing to remove '%s' since it is the current working directory.",
51 /* ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN */
52 "Untracked working tree file '%s' would be overwritten by merge.",
54 /* ERROR_WOULD_LOSE_UNTRACKED_REMOVED */
55 "Untracked working tree file '%s' would be removed by merge.",
57 /* ERROR_BIND_OVERLAP */
58 "Entry '%s' overlaps with '%s'. Cannot bind.",
60 /* ERROR_WOULD_LOSE_SUBMODULE */
61 "Submodule '%s' cannot checkout new HEAD.",
63 /* NB_UNPACK_TREES_ERROR_TYPES; just a meta value */
66 /* WARNING_SPARSE_NOT_UPTODATE_FILE */
67 "Path '%s' not uptodate; will not remove from working tree.",
69 /* WARNING_SPARSE_UNMERGED_FILE */
70 "Path '%s' unmerged; will not remove from working tree.",
72 /* WARNING_SPARSE_ORPHANED_NOT_OVERWRITTEN */
73 "Path '%s' already present; will not overwrite with sparse update.",
76 #define ERRORMSG(o,type) \
77 ( ((o) && (o)->internal.msgs[(type)]) \
78 ? ((o)->internal.msgs[(type)]) \
79 : (unpack_plumbing_errors[(type)]) )
81 static const char *super_prefixed(const char *path
, const char *super_prefix
)
84 * It is necessary and sufficient to have two static buffers
85 * here, as the return value of this function is fed to
86 * error() using the unpack_*_errors[] templates we see above.
88 static struct strbuf buf
[2] = {STRBUF_INIT
, STRBUF_INIT
};
89 static int super_prefix_len
= -1;
90 static unsigned idx
= ARRAY_SIZE(buf
) - 1;
92 if (super_prefix_len
< 0) {
97 for (i
= 0; i
< ARRAY_SIZE(buf
); i
++)
98 strbuf_addstr(&buf
[i
], super_prefix
);
99 super_prefix_len
= buf
[0].len
;
103 if (!super_prefix_len
)
106 if (++idx
>= ARRAY_SIZE(buf
))
109 strbuf_setlen(&buf
[idx
], super_prefix_len
);
110 strbuf_addstr(&buf
[idx
], path
);
115 void setup_unpack_trees_porcelain(struct unpack_trees_options
*opts
,
119 const char **msgs
= opts
->internal
.msgs
;
122 strvec_init(&opts
->internal
.msgs_to_free
);
124 if (!strcmp(cmd
, "checkout"))
125 msg
= advice_enabled(ADVICE_COMMIT_BEFORE_MERGE
)
126 ? _("Your local changes to the following files would be overwritten by checkout:\n%%s"
127 "Please commit your changes or stash them before you switch branches.")
128 : _("Your local changes to the following files would be overwritten by checkout:\n%%s");
129 else if (!strcmp(cmd
, "merge"))
130 msg
= advice_enabled(ADVICE_COMMIT_BEFORE_MERGE
)
131 ? _("Your local changes to the following files would be overwritten by merge:\n%%s"
132 "Please commit your changes or stash them before you merge.")
133 : _("Your local changes to the following files would be overwritten by merge:\n%%s");
135 msg
= advice_enabled(ADVICE_COMMIT_BEFORE_MERGE
)
136 ? _("Your local changes to the following files would be overwritten by %s:\n%%s"
137 "Please commit your changes or stash them before you %s.")
138 : _("Your local changes to the following files would be overwritten by %s:\n%%s");
139 msgs
[ERROR_WOULD_OVERWRITE
] = msgs
[ERROR_NOT_UPTODATE_FILE
] =
140 strvec_pushf(&opts
->internal
.msgs_to_free
, msg
, cmd
, cmd
);
142 msgs
[ERROR_NOT_UPTODATE_DIR
] =
143 _("Updating the following directories would lose untracked files in them:\n%s");
145 msgs
[ERROR_CWD_IN_THE_WAY
] =
146 _("Refusing to remove the current working directory:\n%s");
148 if (!strcmp(cmd
, "checkout"))
149 msg
= advice_enabled(ADVICE_COMMIT_BEFORE_MERGE
)
150 ? _("The following untracked working tree files would be removed by checkout:\n%%s"
151 "Please move or remove them before you switch branches.")
152 : _("The following untracked working tree files would be removed by checkout:\n%%s");
153 else if (!strcmp(cmd
, "merge"))
154 msg
= advice_enabled(ADVICE_COMMIT_BEFORE_MERGE
)
155 ? _("The following untracked working tree files would be removed by merge:\n%%s"
156 "Please move or remove them before you merge.")
157 : _("The following untracked working tree files would be removed by merge:\n%%s");
159 msg
= advice_enabled(ADVICE_COMMIT_BEFORE_MERGE
)
160 ? _("The following untracked working tree files would be removed by %s:\n%%s"
161 "Please move or remove them before you %s.")
162 : _("The following untracked working tree files would be removed by %s:\n%%s");
163 msgs
[ERROR_WOULD_LOSE_UNTRACKED_REMOVED
] =
164 strvec_pushf(&opts
->internal
.msgs_to_free
, msg
, cmd
, cmd
);
166 if (!strcmp(cmd
, "checkout"))
167 msg
= advice_enabled(ADVICE_COMMIT_BEFORE_MERGE
)
168 ? _("The following untracked working tree files would be overwritten by checkout:\n%%s"
169 "Please move or remove them before you switch branches.")
170 : _("The following untracked working tree files would be overwritten by checkout:\n%%s");
171 else if (!strcmp(cmd
, "merge"))
172 msg
= advice_enabled(ADVICE_COMMIT_BEFORE_MERGE
)
173 ? _("The following untracked working tree files would be overwritten by merge:\n%%s"
174 "Please move or remove them before you merge.")
175 : _("The following untracked working tree files would be overwritten by merge:\n%%s");
177 msg
= advice_enabled(ADVICE_COMMIT_BEFORE_MERGE
)
178 ? _("The following untracked working tree files would be overwritten by %s:\n%%s"
179 "Please move or remove them before you %s.")
180 : _("The following untracked working tree files would be overwritten by %s:\n%%s");
181 msgs
[ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN
] =
182 strvec_pushf(&opts
->internal
.msgs_to_free
, msg
, cmd
, cmd
);
185 * Special case: ERROR_BIND_OVERLAP refers to a pair of paths, we
186 * cannot easily display it as a list.
188 msgs
[ERROR_BIND_OVERLAP
] = _("Entry '%s' overlaps with '%s'. Cannot bind.");
190 msgs
[ERROR_WOULD_LOSE_SUBMODULE
] =
191 _("Cannot update submodule:\n%s");
193 msgs
[WARNING_SPARSE_NOT_UPTODATE_FILE
] =
194 _("The following paths are not up to date and were left despite sparse patterns:\n%s");
195 msgs
[WARNING_SPARSE_UNMERGED_FILE
] =
196 _("The following paths are unmerged and were left despite sparse patterns:\n%s");
197 msgs
[WARNING_SPARSE_ORPHANED_NOT_OVERWRITTEN
] =
198 _("The following paths were already present and thus not updated despite sparse patterns:\n%s");
200 opts
->internal
.show_all_errors
= 1;
201 /* rejected paths may not have a static buffer */
202 for (i
= 0; i
< ARRAY_SIZE(opts
->internal
.unpack_rejects
); i
++)
203 opts
->internal
.unpack_rejects
[i
].strdup_strings
= 1;
206 void clear_unpack_trees_porcelain(struct unpack_trees_options
*opts
)
208 strvec_clear(&opts
->internal
.msgs_to_free
);
209 memset(opts
->internal
.msgs
, 0, sizeof(opts
->internal
.msgs
));
212 static int do_add_entry(struct unpack_trees_options
*o
, struct cache_entry
*ce
,
213 unsigned int set
, unsigned int clear
)
220 ce
->ce_flags
= (ce
->ce_flags
& ~clear
) | set
;
221 return add_index_entry(&o
->internal
.result
, ce
,
222 ADD_CACHE_OK_TO_ADD
| ADD_CACHE_OK_TO_REPLACE
);
225 static void add_entry(struct unpack_trees_options
*o
,
226 const struct cache_entry
*ce
,
227 unsigned int set
, unsigned int clear
)
229 do_add_entry(o
, dup_cache_entry(ce
, &o
->internal
.result
), set
, clear
);
233 * add error messages on path <path>
234 * corresponding to the type <e> with the message <msg>
235 * indicating if it should be display in porcelain or not
237 static int add_rejected_path(struct unpack_trees_options
*o
,
238 enum unpack_trees_error_types e
,
244 if (!o
->internal
.show_all_errors
)
245 return error(ERRORMSG(o
, e
), super_prefixed(path
,
249 * Otherwise, insert in a list for future display by
250 * display_(error|warning)_msgs()
252 string_list_append(&o
->internal
.unpack_rejects
[e
], path
);
257 * display all the error messages stored in a nice way
259 static void display_error_msgs(struct unpack_trees_options
*o
)
262 unsigned error_displayed
= 0;
263 for (e
= 0; e
< NB_UNPACK_TREES_ERROR_TYPES
; e
++) {
264 struct string_list
*rejects
= &o
->internal
.unpack_rejects
[e
];
266 if (rejects
->nr
> 0) {
268 struct strbuf path
= STRBUF_INIT
;
271 for (i
= 0; i
< rejects
->nr
; i
++)
272 strbuf_addf(&path
, "\t%s\n", rejects
->items
[i
].string
);
273 error(ERRORMSG(o
, e
), super_prefixed(path
.buf
,
275 strbuf_release(&path
);
277 string_list_clear(rejects
, 0);
280 fprintf(stderr
, _("Aborting\n"));
284 * display all the warning messages stored in a nice way
286 static void display_warning_msgs(struct unpack_trees_options
*o
)
289 unsigned warning_displayed
= 0;
290 for (e
= NB_UNPACK_TREES_ERROR_TYPES
+ 1;
291 e
< NB_UNPACK_TREES_WARNING_TYPES
; e
++) {
292 struct string_list
*rejects
= &o
->internal
.unpack_rejects
[e
];
294 if (rejects
->nr
> 0) {
296 struct strbuf path
= STRBUF_INIT
;
298 warning_displayed
= 1;
299 for (i
= 0; i
< rejects
->nr
; i
++)
300 strbuf_addf(&path
, "\t%s\n", rejects
->items
[i
].string
);
301 warning(ERRORMSG(o
, e
), super_prefixed(path
.buf
,
303 strbuf_release(&path
);
305 string_list_clear(rejects
, 0);
307 if (warning_displayed
)
308 fprintf(stderr
, _("After fixing the above paths, you may want to run `git sparse-checkout reapply`.\n"));
310 static int check_submodule_move_head(const struct cache_entry
*ce
,
313 struct unpack_trees_options
*o
)
315 unsigned flags
= SUBMODULE_MOVE_HEAD_DRY_RUN
;
316 const struct submodule
*sub
= submodule_from_ce(ce
);
322 flags
|= SUBMODULE_MOVE_HEAD_FORCE
;
324 if (submodule_move_head(ce
->name
, o
->super_prefix
, old_id
, new_id
,
326 return add_rejected_path(o
, ERROR_WOULD_LOSE_SUBMODULE
, ce
->name
);
331 * Perform the loading of the repository's gitmodules file. This function is
332 * used by 'check_update()' to perform loading of the gitmodules file in two
333 * different situations:
334 * (1) before removing entries from the working tree if the gitmodules file has
335 * been marked for removal. This situation is specified by 'state' == NULL.
336 * (2) before checking out entries to the working tree if the gitmodules file
337 * has been marked for update. This situation is specified by 'state' != NULL.
339 static void load_gitmodules_file(struct index_state
*index
,
340 struct checkout
*state
)
342 int pos
= index_name_pos(index
, GITMODULES_FILE
, strlen(GITMODULES_FILE
));
345 struct cache_entry
*ce
= index
->cache
[pos
];
346 if (!state
&& ce
->ce_flags
& CE_WT_REMOVE
) {
347 repo_read_gitmodules(the_repository
, 0);
348 } else if (state
&& (ce
->ce_flags
& CE_UPDATE
)) {
349 submodule_free(the_repository
);
350 checkout_entry(ce
, state
, NULL
, NULL
);
351 repo_read_gitmodules(the_repository
, 0);
356 static struct progress
*get_progress(struct unpack_trees_options
*o
,
357 struct index_state
*index
)
359 unsigned cnt
= 0, total
= 0;
361 if (!o
->update
|| !o
->verbose_update
)
364 for (; cnt
< index
->cache_nr
; cnt
++) {
365 const struct cache_entry
*ce
= index
->cache
[cnt
];
366 if (ce
->ce_flags
& (CE_UPDATE
| CE_WT_REMOVE
))
370 return start_delayed_progress(_("Updating files"), total
);
373 static void setup_collided_checkout_detection(struct checkout
*state
,
374 struct index_state
*index
)
379 for (i
= 0; i
< index
->cache_nr
; i
++)
380 index
->cache
[i
]->ce_flags
&= ~CE_MATCHED
;
383 static void report_collided_checkout(struct index_state
*index
)
385 struct string_list list
= STRING_LIST_INIT_NODUP
;
388 for (i
= 0; i
< index
->cache_nr
; i
++) {
389 struct cache_entry
*ce
= index
->cache
[i
];
391 if (!(ce
->ce_flags
& CE_MATCHED
))
394 string_list_append(&list
, ce
->name
);
395 ce
->ce_flags
&= ~CE_MATCHED
;
398 list
.cmp
= fspathcmp
;
399 string_list_sort(&list
);
402 warning(_("the following paths have collided (e.g. case-sensitive paths\n"
403 "on a case-insensitive filesystem) and only one from the same\n"
404 "colliding group is in the working tree:\n"));
406 for (i
= 0; i
< list
.nr
; i
++)
407 fprintf(stderr
, " '%s'\n", list
.items
[i
].string
);
410 string_list_clear(&list
, 0);
413 static int must_checkout(const struct cache_entry
*ce
)
415 return ce
->ce_flags
& CE_UPDATE
;
418 static int check_updates(struct unpack_trees_options
*o
,
419 struct index_state
*index
)
423 struct progress
*progress
;
424 struct checkout state
= CHECKOUT_INIT
;
425 int i
, pc_workers
, pc_threshold
;
427 trace_performance_enter();
428 state
.super_prefix
= o
->super_prefix
;
431 state
.refresh_cache
= 1;
432 state
.istate
= index
;
433 clone_checkout_metadata(&state
.meta
, &o
->meta
, NULL
);
435 if (!o
->update
|| o
->dry_run
) {
436 remove_marked_cache_entries(index
, 0);
437 trace_performance_leave("check_updates");
442 setup_collided_checkout_detection(&state
, index
);
444 progress
= get_progress(o
, index
);
446 /* Start with clean cache to avoid using any possibly outdated info. */
447 invalidate_lstat_cache();
449 git_attr_set_direction(GIT_ATTR_CHECKOUT
);
451 if (should_update_submodules())
452 load_gitmodules_file(index
, NULL
);
454 for (i
= 0; i
< index
->cache_nr
; i
++) {
455 const struct cache_entry
*ce
= index
->cache
[i
];
457 if (ce
->ce_flags
& CE_WT_REMOVE
) {
458 display_progress(progress
, ++cnt
);
459 unlink_entry(ce
, o
->super_prefix
);
463 remove_marked_cache_entries(index
, 0);
464 remove_scheduled_dirs();
466 if (should_update_submodules())
467 load_gitmodules_file(index
, &state
);
469 if (repo_has_promisor_remote(the_repository
))
471 * Prefetch the objects that are to be checked out in the loop
474 prefetch_cache_entries(index
, must_checkout
);
476 get_parallel_checkout_configs(&pc_workers
, &pc_threshold
);
478 enable_delayed_checkout(&state
);
480 init_parallel_checkout();
481 for (i
= 0; i
< index
->cache_nr
; i
++) {
482 struct cache_entry
*ce
= index
->cache
[i
];
484 if (must_checkout(ce
)) {
485 size_t last_pc_queue_size
= pc_queue_size();
487 if (ce
->ce_flags
& CE_WT_REMOVE
)
488 BUG("both update and delete flags are set on %s",
490 ce
->ce_flags
&= ~CE_UPDATE
;
491 errs
|= checkout_entry(ce
, &state
, NULL
, NULL
);
493 if (last_pc_queue_size
== pc_queue_size())
494 display_progress(progress
, ++cnt
);
498 errs
|= run_parallel_checkout(&state
, pc_workers
, pc_threshold
,
500 stop_progress(&progress
);
501 errs
|= finish_delayed_checkout(&state
, o
->verbose_update
);
502 git_attr_set_direction(GIT_ATTR_CHECKIN
);
505 report_collided_checkout(index
);
507 trace_performance_leave("check_updates");
511 static int verify_uptodate_sparse(const struct cache_entry
*ce
,
512 struct unpack_trees_options
*o
);
513 static int verify_absent_sparse(const struct cache_entry
*ce
,
514 enum unpack_trees_error_types
,
515 struct unpack_trees_options
*o
);
517 static int apply_sparse_checkout(struct index_state
*istate
,
518 struct cache_entry
*ce
,
519 struct unpack_trees_options
*o
)
521 int was_skip_worktree
= ce_skip_worktree(ce
);
523 if (ce
->ce_flags
& CE_NEW_SKIP_WORKTREE
)
524 ce
->ce_flags
|= CE_SKIP_WORKTREE
;
526 ce
->ce_flags
&= ~CE_SKIP_WORKTREE
;
527 if (was_skip_worktree
!= ce_skip_worktree(ce
)) {
528 ce
->ce_flags
|= CE_UPDATE_IN_BASE
;
529 mark_fsmonitor_invalid(istate
, ce
);
530 istate
->cache_changed
|= CE_ENTRY_CHANGED
;
534 * if (!was_skip_worktree && !ce_skip_worktree()) {
535 * This is perfectly normal. Move on;
540 * Merge strategies may set CE_UPDATE|CE_REMOVE outside checkout
541 * area as a result of ce_skip_worktree() shortcuts in
542 * verify_absent() and verify_uptodate().
543 * Make sure they don't modify worktree if they are already
544 * outside checkout area
546 if (was_skip_worktree
&& ce_skip_worktree(ce
)) {
547 ce
->ce_flags
&= ~CE_UPDATE
;
550 * By default, when CE_REMOVE is on, CE_WT_REMOVE is also
551 * on to get that file removed from both index and worktree.
552 * If that file is already outside worktree area, don't
555 if (ce
->ce_flags
& CE_REMOVE
)
556 ce
->ce_flags
&= ~CE_WT_REMOVE
;
559 if (!was_skip_worktree
&& ce_skip_worktree(ce
)) {
561 * If CE_UPDATE is set, verify_uptodate() must be called already
562 * also stat info may have lost after merged_entry() so calling
563 * verify_uptodate() again may fail
565 if (!(ce
->ce_flags
& CE_UPDATE
) &&
566 verify_uptodate_sparse(ce
, o
)) {
567 ce
->ce_flags
&= ~CE_SKIP_WORKTREE
;
570 ce
->ce_flags
|= CE_WT_REMOVE
;
571 ce
->ce_flags
&= ~CE_UPDATE
;
573 if (was_skip_worktree
&& !ce_skip_worktree(ce
)) {
574 if (verify_absent_sparse(ce
, WARNING_SPARSE_ORPHANED_NOT_OVERWRITTEN
, o
))
576 ce
->ce_flags
|= CE_UPDATE
;
581 static int warn_conflicted_path(struct index_state
*istate
,
583 struct unpack_trees_options
*o
)
585 char *conflicting_path
= istate
->cache
[i
]->name
;
588 add_rejected_path(o
, WARNING_SPARSE_UNMERGED_FILE
, conflicting_path
);
590 /* Find out how many higher stage entries are at same path */
591 while ((++count
) + i
< istate
->cache_nr
&&
592 !strcmp(conflicting_path
, istate
->cache
[count
+ i
]->name
))
598 static inline int call_unpack_fn(const struct cache_entry
* const *src
,
599 struct unpack_trees_options
*o
)
601 int ret
= o
->fn(src
, o
);
607 static void mark_ce_used(struct cache_entry
*ce
, struct unpack_trees_options
*o
)
609 ce
->ce_flags
|= CE_UNPACKED
;
611 if (o
->internal
.cache_bottom
< o
->src_index
->cache_nr
&&
612 o
->src_index
->cache
[o
->internal
.cache_bottom
] == ce
) {
613 int bottom
= o
->internal
.cache_bottom
;
615 while (bottom
< o
->src_index
->cache_nr
&&
616 o
->src_index
->cache
[bottom
]->ce_flags
& CE_UNPACKED
)
618 o
->internal
.cache_bottom
= bottom
;
622 static void mark_all_ce_unused(struct index_state
*index
)
625 for (i
= 0; i
< index
->cache_nr
; i
++)
626 index
->cache
[i
]->ce_flags
&= ~(CE_UNPACKED
| CE_ADDED
| CE_NEW_SKIP_WORKTREE
);
629 static int locate_in_src_index(const struct cache_entry
*ce
,
630 struct unpack_trees_options
*o
)
632 struct index_state
*index
= o
->src_index
;
633 int len
= ce_namelen(ce
);
634 int pos
= index_name_pos(index
, ce
->name
, len
);
641 * We call unpack_index_entry() with an unmerged cache entry
642 * only in diff-index, and it wants a single callback. Skip
643 * the other unmerged entry with the same name.
645 static void mark_ce_used_same_name(struct cache_entry
*ce
,
646 struct unpack_trees_options
*o
)
648 struct index_state
*index
= o
->src_index
;
649 int len
= ce_namelen(ce
);
652 for (pos
= locate_in_src_index(ce
, o
); pos
< index
->cache_nr
; pos
++) {
653 struct cache_entry
*next
= index
->cache
[pos
];
654 if (len
!= ce_namelen(next
) ||
655 memcmp(ce
->name
, next
->name
, len
))
657 mark_ce_used(next
, o
);
661 static struct cache_entry
*next_cache_entry(struct unpack_trees_options
*o
)
663 const struct index_state
*index
= o
->src_index
;
664 int pos
= o
->internal
.cache_bottom
;
666 while (pos
< index
->cache_nr
) {
667 struct cache_entry
*ce
= index
->cache
[pos
];
668 if (!(ce
->ce_flags
& CE_UNPACKED
))
675 static void add_same_unmerged(const struct cache_entry
*ce
,
676 struct unpack_trees_options
*o
)
678 struct index_state
*index
= o
->src_index
;
679 int len
= ce_namelen(ce
);
680 int pos
= index_name_pos(index
, ce
->name
, len
);
683 die("programming error in a caller of mark_ce_used_same_name");
684 for (pos
= -pos
- 1; pos
< index
->cache_nr
; pos
++) {
685 struct cache_entry
*next
= index
->cache
[pos
];
686 if (len
!= ce_namelen(next
) ||
687 memcmp(ce
->name
, next
->name
, len
))
689 add_entry(o
, next
, 0, 0);
690 mark_ce_used(next
, o
);
694 static int unpack_index_entry(struct cache_entry
*ce
,
695 struct unpack_trees_options
*o
)
697 const struct cache_entry
*src
[MAX_UNPACK_TREES
+ 1] = { NULL
, };
704 if (o
->skip_unmerged
) {
705 add_entry(o
, ce
, 0, 0);
709 ret
= call_unpack_fn(src
, o
);
711 mark_ce_used_same_name(ce
, o
);
715 static int find_cache_pos(struct traverse_info
*, const char *p
, size_t len
);
717 static void restore_cache_bottom(struct traverse_info
*info
, int bottom
)
719 struct unpack_trees_options
*o
= info
->data
;
721 if (o
->diff_index_cached
)
723 o
->internal
.cache_bottom
= bottom
;
726 static int switch_cache_bottom(struct traverse_info
*info
)
728 struct unpack_trees_options
*o
= info
->data
;
731 if (o
->diff_index_cached
)
733 ret
= o
->internal
.cache_bottom
;
734 pos
= find_cache_pos(info
->prev
, info
->name
, info
->namelen
);
737 o
->internal
.cache_bottom
= -2 - pos
;
739 o
->internal
.cache_bottom
= o
->src_index
->cache_nr
;
743 static inline int are_same_oid(struct name_entry
*name_j
, struct name_entry
*name_k
)
745 return !is_null_oid(&name_j
->oid
) && !is_null_oid(&name_k
->oid
) && oideq(&name_j
->oid
, &name_k
->oid
);
748 static int all_trees_same_as_cache_tree(int n
, unsigned long dirmask
,
749 struct name_entry
*names
,
750 struct traverse_info
*info
)
752 struct unpack_trees_options
*o
= info
->data
;
755 if (!o
->merge
|| dirmask
!= ((1 << n
) - 1))
758 for (i
= 1; i
< n
; i
++)
759 if (!are_same_oid(names
, names
+ i
))
762 return cache_tree_matches_traversal(o
->src_index
->cache_tree
, names
, info
);
765 static int index_pos_by_traverse_info(struct name_entry
*names
,
766 struct traverse_info
*info
)
768 struct unpack_trees_options
*o
= info
->data
;
769 struct strbuf name
= STRBUF_INIT
;
772 strbuf_make_traverse_path(&name
, info
, names
->path
, names
->pathlen
);
773 strbuf_addch(&name
, '/');
774 pos
= index_name_pos(o
->src_index
, name
.buf
, name
.len
);
776 if (!o
->src_index
->sparse_index
||
777 !(o
->src_index
->cache
[pos
]->ce_flags
& CE_SKIP_WORKTREE
))
778 BUG("This is a directory and should not exist in index");
782 if (pos
>= o
->src_index
->cache_nr
||
783 !starts_with(o
->src_index
->cache
[pos
]->name
, name
.buf
) ||
784 (pos
> 0 && starts_with(o
->src_index
->cache
[pos
-1]->name
, name
.buf
)))
785 BUG("pos %d doesn't point to the first entry of %s in index",
787 strbuf_release(&name
);
792 * Fast path if we detect that all trees are the same as cache-tree at this
793 * path. We'll walk these trees in an iterative loop using cache-tree/index
794 * instead of ODB since we already know what these trees contain.
796 static int traverse_by_cache_tree(int pos
, int nr_entries
, int nr_names
,
797 struct traverse_info
*info
)
799 struct cache_entry
*src
[MAX_UNPACK_TREES
+ 1] = { NULL
, };
800 struct unpack_trees_options
*o
= info
->data
;
801 struct cache_entry
*tree_ce
= NULL
;
806 BUG("We need cache-tree to do this optimization");
809 * Do what unpack_callback() and unpack_single_entry() normally
810 * do. But we walk all paths in an iterative loop instead.
812 * D/F conflicts and higher stage entries are not a concern
813 * because cache-tree would be invalidated and we would never
814 * get here in the first place.
816 for (i
= 0; i
< nr_entries
; i
++) {
817 int new_ce_len
, len
, rc
;
819 src
[0] = o
->src_index
->cache
[pos
+ i
];
821 len
= ce_namelen(src
[0]);
822 new_ce_len
= cache_entry_size(len
);
824 if (new_ce_len
> ce_len
) {
826 tree_ce
= xrealloc(tree_ce
, new_ce_len
);
827 memset(tree_ce
, 0, new_ce_len
);
830 tree_ce
->ce_flags
= create_ce_flags(0);
832 for (d
= 1; d
<= nr_names
; d
++)
836 tree_ce
->ce_mode
= src
[0]->ce_mode
;
837 tree_ce
->ce_namelen
= len
;
838 oidcpy(&tree_ce
->oid
, &src
[0]->oid
);
839 memcpy(tree_ce
->name
, src
[0]->name
, len
+ 1);
841 rc
= call_unpack_fn((const struct cache_entry
* const *)src
, o
);
847 mark_ce_used(src
[0], o
);
850 if (o
->internal
.debug_unpack
)
851 printf("Unpacked %d entries from %s to %s using cache-tree\n",
853 o
->src_index
->cache
[pos
]->name
,
854 o
->src_index
->cache
[pos
+ nr_entries
- 1]->name
);
858 static int traverse_trees_recursive(int n
, unsigned long dirmask
,
859 unsigned long df_conflicts
,
860 struct name_entry
*names
,
861 struct traverse_info
*info
)
863 struct unpack_trees_options
*o
= info
->data
;
866 struct tree_desc t
[MAX_UNPACK_TREES
];
867 void *buf
[MAX_UNPACK_TREES
];
868 struct traverse_info newinfo
;
869 struct name_entry
*p
;
872 nr_entries
= all_trees_same_as_cache_tree(n
, dirmask
, names
, info
);
873 if (nr_entries
> 0) {
874 int pos
= index_pos_by_traverse_info(names
, info
);
876 if (!o
->merge
|| df_conflicts
)
877 BUG("Wrong condition to get here buddy");
880 * All entries up to 'pos' must have been processed
881 * (i.e. marked CE_UNPACKED) at this point. But to be safe,
882 * save and restore cache_bottom anyway to not miss
883 * unprocessed entries before 'pos'.
885 bottom
= o
->internal
.cache_bottom
;
886 ret
= traverse_by_cache_tree(pos
, nr_entries
, n
, info
);
887 o
->internal
.cache_bottom
= bottom
;
897 newinfo
.pathspec
= info
->pathspec
;
898 newinfo
.name
= p
->path
;
899 newinfo
.namelen
= p
->pathlen
;
900 newinfo
.mode
= p
->mode
;
901 newinfo
.pathlen
= st_add3(newinfo
.pathlen
, tree_entry_len(p
), 1);
902 newinfo
.df_conflicts
|= df_conflicts
;
905 * Fetch the tree from the ODB for each peer directory in the
908 * For 2- and 3-way traversals, we try to avoid hitting the
909 * ODB twice for the same OID. This should yield a nice speed
910 * up in checkouts and merges when the commits are similar.
912 * We don't bother doing the full O(n^2) search for larger n,
913 * because wider traversals don't happen that often and we
914 * avoid the search setup.
916 * When 2 peer OIDs are the same, we just copy the tree
917 * descriptor data. This implicitly borrows the buffer
918 * data from the earlier cell.
920 for (i
= 0; i
< n
; i
++, dirmask
>>= 1) {
921 if (i
> 0 && are_same_oid(&names
[i
], &names
[i
- 1]))
923 else if (i
> 1 && are_same_oid(&names
[i
], &names
[i
- 2]))
926 const struct object_id
*oid
= NULL
;
929 buf
[nr_buf
++] = fill_tree_descriptor(the_repository
, t
+ i
, oid
);
933 bottom
= switch_cache_bottom(&newinfo
);
934 ret
= traverse_trees(o
->src_index
, n
, t
, &newinfo
);
935 restore_cache_bottom(&newinfo
, bottom
);
937 for (i
= 0; i
< nr_buf
; i
++)
944 * Compare the traverse-path to the cache entry without actually
945 * having to generate the textual representation of the traverse
948 * NOTE! This *only* compares up to the size of the traverse path
949 * itself - the caller needs to do the final check for the cache
950 * entry having more data at the end!
952 static int do_compare_entry_piecewise(const struct cache_entry
*ce
,
953 const struct traverse_info
*info
,
954 const char *name
, size_t namelen
,
961 int cmp
= do_compare_entry_piecewise(ce
, info
->prev
,
962 info
->name
, info
->namelen
,
967 pathlen
= info
->pathlen
;
968 ce_len
= ce_namelen(ce
);
970 /* If ce_len < pathlen then we must have previously hit "name == directory" entry */
971 if (ce_len
< pathlen
)
975 ce_name
= ce
->name
+ pathlen
;
977 return df_name_compare(ce_name
, ce_len
, S_IFREG
, name
, namelen
, mode
);
980 static int do_compare_entry(const struct cache_entry
*ce
,
981 const struct traverse_info
*info
,
982 const char *name
, size_t namelen
,
991 * If we have not precomputed the traverse path, it is quicker
992 * to avoid doing so. But if we have precomputed it,
993 * it is quicker to use the precomputed version.
995 if (!info
->traverse_path
)
996 return do_compare_entry_piecewise(ce
, info
, name
, namelen
, mode
);
998 cmp
= strncmp(ce
->name
, info
->traverse_path
, info
->pathlen
);
1002 pathlen
= info
->pathlen
;
1003 ce_len
= ce_namelen(ce
);
1005 if (ce_len
< pathlen
)
1009 ce_name
= ce
->name
+ pathlen
;
1011 ce_mode
= S_ISSPARSEDIR(ce
->ce_mode
) ? S_IFDIR
: S_IFREG
;
1012 return df_name_compare(ce_name
, ce_len
, ce_mode
, name
, namelen
, mode
);
1015 static int compare_entry(const struct cache_entry
*ce
, const struct traverse_info
*info
, const struct name_entry
*n
)
1017 int cmp
= do_compare_entry(ce
, info
, n
->path
, n
->pathlen
, n
->mode
);
1022 * At this point, we know that we have a prefix match. If ce
1023 * is a sparse directory, then allow an exact match. This only
1024 * works when the input name is a directory, since ce->name
1025 * ends in a directory separator.
1027 if (S_ISSPARSEDIR(ce
->ce_mode
) &&
1028 ce
->ce_namelen
== traverse_path_len(info
, tree_entry_len(n
)) + 1)
1032 * Even if the beginning compared identically, the ce should
1033 * compare as bigger than a directory leading up to it!
1035 return ce_namelen(ce
) > traverse_path_len(info
, tree_entry_len(n
));
1038 static int ce_in_traverse_path(const struct cache_entry
*ce
,
1039 const struct traverse_info
*info
)
1043 if (do_compare_entry(ce
, info
->prev
,
1044 info
->name
, info
->namelen
, info
->mode
))
1047 * If ce (blob) is the same name as the path (which is a tree
1048 * we will be descending into), it won't be inside it.
1050 return (info
->pathlen
< ce_namelen(ce
));
1053 static struct cache_entry
*create_ce_entry(const struct traverse_info
*info
,
1054 const struct name_entry
*n
,
1056 struct index_state
*istate
,
1058 int is_sparse_directory
)
1060 size_t len
= traverse_path_len(info
, tree_entry_len(n
));
1061 size_t alloc_len
= is_sparse_directory
? len
+ 1 : len
;
1062 struct cache_entry
*ce
=
1064 make_empty_transient_cache_entry(alloc_len
, NULL
) :
1065 make_empty_cache_entry(istate
, alloc_len
);
1067 ce
->ce_mode
= create_ce_mode(n
->mode
);
1068 ce
->ce_flags
= create_ce_flags(stage
);
1069 ce
->ce_namelen
= len
;
1070 oidcpy(&ce
->oid
, &n
->oid
);
1071 /* len+1 because the cache_entry allocates space for NUL */
1072 make_traverse_path(ce
->name
, len
+ 1, info
, n
->path
, n
->pathlen
);
1074 if (is_sparse_directory
) {
1075 ce
->name
[len
] = '/';
1076 ce
->name
[len
+ 1] = '\0';
1078 ce
->ce_flags
|= CE_SKIP_WORKTREE
;
1085 * Determine whether the path specified by 'p' should be unpacked as a new
1086 * sparse directory in a sparse index. A new sparse directory 'A/':
1087 * - must be outside the sparse cone.
1088 * - must not already be in the index (i.e., no index entry with name 'A/'
1090 * - must not have any child entries in the index (i.e., no index entry
1091 * 'A/<something>' exists).
1092 * If 'p' meets the above requirements, return 1; otherwise, return 0.
1094 static int entry_is_new_sparse_dir(const struct traverse_info
*info
,
1095 const struct name_entry
*p
)
1098 struct strbuf dirpath
= STRBUF_INIT
;
1099 struct unpack_trees_options
*o
= info
->data
;
1101 if (!S_ISDIR(p
->mode
))
1105 * If the path is inside the sparse cone, it can't be a sparse directory.
1107 strbuf_add(&dirpath
, info
->traverse_path
, info
->pathlen
);
1108 strbuf_add(&dirpath
, p
->path
, p
->pathlen
);
1109 strbuf_addch(&dirpath
, '/');
1110 if (path_in_cone_mode_sparse_checkout(dirpath
.buf
, o
->src_index
)) {
1115 pos
= index_name_pos_sparse(o
->src_index
, dirpath
.buf
, dirpath
.len
);
1117 /* Path is already in the index, not a new sparse dir */
1122 /* Where would this sparse dir be inserted into the index? */
1124 if (pos
>= o
->src_index
->cache_nr
) {
1126 * Sparse dir would be inserted at the end of the index, so we
1127 * know it has no child entries.
1134 * If the dir has child entries in the index, the first would be at the
1135 * position the sparse directory would be inserted. If the entry at this
1136 * position is inside the dir, not a new sparse dir.
1138 res
= strncmp(o
->src_index
->cache
[pos
]->name
, dirpath
.buf
, dirpath
.len
);
1141 strbuf_release(&dirpath
);
1146 * Note that traverse_by_cache_tree() duplicates some logic in this function
1147 * without actually calling it. If you change the logic here you may need to
1148 * check and change there as well.
1150 static int unpack_single_entry(int n
, unsigned long mask
,
1151 unsigned long dirmask
,
1152 struct cache_entry
**src
,
1153 const struct name_entry
*names
,
1154 const struct traverse_info
*info
,
1155 int *is_new_sparse_dir
)
1158 struct unpack_trees_options
*o
= info
->data
;
1159 unsigned long conflicts
= info
->df_conflicts
| dirmask
;
1160 const struct name_entry
*p
= names
;
1162 *is_new_sparse_dir
= 0;
1163 if (mask
== dirmask
&& !src
[0]) {
1165 * If we're not in a sparse index, we can't unpack a directory
1166 * without recursing into it, so we return.
1168 if (!o
->src_index
->sparse_index
)
1171 /* Find first entry with a real name (we could use "mask" too) */
1176 * If the directory is completely missing from the index but
1177 * would otherwise be a sparse directory, we should unpack it.
1178 * If not, we'll return and continue recursively traversing the
1181 *is_new_sparse_dir
= entry_is_new_sparse_dir(info
, p
);
1182 if (!*is_new_sparse_dir
)
1187 * When we are unpacking a sparse directory, then this isn't necessarily
1188 * a directory-file conflict.
1190 if (mask
== dirmask
&&
1191 (*is_new_sparse_dir
|| (src
[0] && S_ISSPARSEDIR(src
[0]->ce_mode
))))
1195 * Ok, we've filled in up to any potential index entry in src[0],
1198 for (i
= 0; i
< n
; i
++) {
1200 unsigned int bit
= 1ul << i
;
1201 if (conflicts
& bit
) {
1202 src
[i
+ o
->merge
] = o
->df_conflict_entry
;
1209 else if (i
+ 1 < o
->head_idx
)
1211 else if (i
+ 1 > o
->head_idx
)
1217 * If the merge bit is set, then the cache entries are
1218 * discarded in the following block. In this case,
1219 * construct "transient" cache_entries, as they are
1220 * not stored in the index. otherwise construct the
1221 * cache entry from the index aware logic.
1223 src
[i
+ o
->merge
] = create_ce_entry(info
, names
+ i
, stage
,
1224 &o
->internal
.result
,
1225 o
->merge
, bit
& dirmask
);
1229 int rc
= call_unpack_fn((const struct cache_entry
* const *)src
,
1231 for (i
= 0; i
< n
; i
++) {
1232 struct cache_entry
*ce
= src
[i
+ o
->merge
];
1233 if (ce
!= o
->df_conflict_entry
)
1234 discard_cache_entry(ce
);
1239 for (i
= 0; i
< n
; i
++)
1240 if (src
[i
] && src
[i
] != o
->df_conflict_entry
)
1241 if (do_add_entry(o
, src
[i
], 0, 0))
1247 static int unpack_failed(struct unpack_trees_options
*o
, const char *message
)
1249 discard_index(&o
->internal
.result
);
1250 if (!o
->quiet
&& !o
->exiting_early
) {
1252 return error("%s", message
);
1259 * The tree traversal is looking at name p. If we have a matching entry,
1260 * return it. If name p is a directory in the index, do not return
1261 * anything, as we will want to match it when the traversal descends into
1264 static int find_cache_pos(struct traverse_info
*info
,
1265 const char *p
, size_t p_len
)
1268 struct unpack_trees_options
*o
= info
->data
;
1269 struct index_state
*index
= o
->src_index
;
1270 int pfxlen
= info
->pathlen
;
1272 for (pos
= o
->internal
.cache_bottom
; pos
< index
->cache_nr
; pos
++) {
1273 const struct cache_entry
*ce
= index
->cache
[pos
];
1274 const char *ce_name
, *ce_slash
;
1277 if (ce
->ce_flags
& CE_UNPACKED
) {
1279 * cache_bottom entry is already unpacked, so
1280 * we can never match it; don't check it
1283 if (pos
== o
->internal
.cache_bottom
)
1284 ++o
->internal
.cache_bottom
;
1287 if (!ce_in_traverse_path(ce
, info
)) {
1289 * Check if we can skip future cache checks
1290 * (because we're already past all possible
1291 * entries in the traverse path).
1293 if (info
->traverse_path
) {
1294 if (strncmp(ce
->name
, info
->traverse_path
,
1300 ce_name
= ce
->name
+ pfxlen
;
1301 ce_slash
= strchr(ce_name
, '/');
1303 ce_len
= ce_slash
- ce_name
;
1305 ce_len
= ce_namelen(ce
) - pfxlen
;
1306 cmp
= name_compare(p
, p_len
, ce_name
, ce_len
);
1308 * Exact match; if we have a directory we need to
1309 * delay returning it.
1312 return ce_slash
? -2 - pos
: pos
;
1314 continue; /* keep looking */
1316 * ce_name sorts after p->path; could it be that we
1317 * have files under p->path directory in the index?
1318 * E.g. ce_name == "t-i", and p->path == "t"; we may
1319 * have "t/a" in the index.
1321 if (p_len
< ce_len
&& !memcmp(ce_name
, p
, p_len
) &&
1322 ce_name
[p_len
] < '/')
1323 continue; /* keep looking */
1330 * Given a sparse directory entry 'ce', compare ce->name to
1331 * info->traverse_path + p->path + '/' if info->traverse_path
1334 * Compare ce->name to p->path + '/' otherwise. Note that
1335 * ce->name must end in a trailing '/' because it is a sparse
1338 static int sparse_dir_matches_path(const struct cache_entry
*ce
,
1339 struct traverse_info
*info
,
1340 const struct name_entry
*p
)
1342 assert(S_ISSPARSEDIR(ce
->ce_mode
));
1343 assert(ce
->name
[ce
->ce_namelen
- 1] == '/');
1346 return ce
->ce_namelen
== info
->pathlen
+ p
->pathlen
+ 1 &&
1347 ce
->name
[info
->pathlen
- 1] == '/' &&
1348 !strncmp(ce
->name
, info
->traverse_path
, info
->pathlen
) &&
1349 !strncmp(ce
->name
+ info
->pathlen
, p
->path
, p
->pathlen
);
1350 return ce
->ce_namelen
== p
->pathlen
+ 1 &&
1351 !strncmp(ce
->name
, p
->path
, p
->pathlen
);
1354 static struct cache_entry
*find_cache_entry(struct traverse_info
*info
,
1355 const struct name_entry
*p
)
1358 int pos
= find_cache_pos(info
, p
->path
, p
->pathlen
);
1359 struct unpack_trees_options
*o
= info
->data
;
1362 return o
->src_index
->cache
[pos
];
1365 * Check for a sparse-directory entry named "path/".
1366 * Due to the input p->path not having a trailing
1367 * slash, the negative 'pos' value overshoots the
1368 * expected position, hence "-2" instead of "-1".
1372 if (pos
< 0 || pos
>= o
->src_index
->cache_nr
)
1376 * Due to lexicographic sorting and sparse directory
1377 * entries ending with a trailing slash, our path as a
1378 * sparse directory (e.g "subdir/") and our path as a
1379 * file (e.g. "subdir") might be separated by other
1380 * paths (e.g. "subdir-").
1383 struct cache_entry
*ce
= o
->src_index
->cache
[pos
];
1385 if (!skip_prefix(ce
->name
, info
->traverse_path
, &path
) ||
1386 strncmp(path
, p
->path
, p
->pathlen
) ||
1387 path
[p
->pathlen
] != '/')
1390 if (S_ISSPARSEDIR(ce
->ce_mode
) &&
1391 sparse_dir_matches_path(ce
, info
, p
))
1400 static void debug_path(struct traverse_info
*info
)
1403 debug_path(info
->prev
);
1404 if (*info
->prev
->name
)
1407 printf("%s", info
->name
);
1410 static void debug_name_entry(int i
, struct name_entry
*n
)
1412 printf("ent#%d %06o %s\n", i
,
1413 n
->path
? n
->mode
: 0,
1414 n
->path
? n
->path
: "(missing)");
1417 static void debug_unpack_callback(int n
,
1419 unsigned long dirmask
,
1420 struct name_entry
*names
,
1421 struct traverse_info
*info
)
1424 printf("* unpack mask %lu, dirmask %lu, cnt %d ",
1428 for (i
= 0; i
< n
; i
++)
1429 debug_name_entry(i
, names
+ i
);
1433 * Returns true if and only if the given cache_entry is a
1434 * sparse-directory entry that matches the given name_entry
1435 * from the tree walk at the given traverse_info.
1437 static int is_sparse_directory_entry(struct cache_entry
*ce
,
1438 const struct name_entry
*name
,
1439 struct traverse_info
*info
)
1441 if (!ce
|| !name
|| !S_ISSPARSEDIR(ce
->ce_mode
))
1444 return sparse_dir_matches_path(ce
, info
, name
);
1447 static int unpack_sparse_callback(int n
, unsigned long mask
, unsigned long dirmask
, struct name_entry
*names
, struct traverse_info
*info
)
1449 struct cache_entry
*src
[MAX_UNPACK_TREES
+ 1] = { NULL
, };
1450 struct unpack_trees_options
*o
= info
->data
;
1451 int ret
, is_new_sparse_dir
;
1456 * Unlike in 'unpack_callback', where src[0] is derived from the index when
1457 * merging, src[0] is a transient cache entry derived from the first tree
1458 * provided. Create the temporary entry as if it came from a non-sparse index.
1460 if (!is_null_oid(&names
[0].oid
)) {
1461 src
[0] = create_ce_entry(info
, &names
[0], 0,
1462 &o
->internal
.result
, 1,
1463 dirmask
& (1ul << 0));
1464 src
[0]->ce_flags
|= (CE_SKIP_WORKTREE
| CE_NEW_SKIP_WORKTREE
);
1468 * 'unpack_single_entry' assumes that src[0] is derived directly from
1469 * the index, rather than from an entry in 'names'. This is *not* true when
1470 * merging a sparse directory, in which case names[0] is the "index" source
1471 * entry. To match the expectations of 'unpack_single_entry', shift past the
1472 * "index" tree (i.e., names[0]) and adjust 'names', 'n', 'mask', and
1473 * 'dirmask' accordingly.
1475 ret
= unpack_single_entry(n
- 1, mask
>> 1, dirmask
>> 1, src
, names
+ 1, info
, &is_new_sparse_dir
);
1478 discard_cache_entry(src
[0]);
1480 return ret
>= 0 ? mask
: -1;
1484 * Note that traverse_by_cache_tree() duplicates some logic in this function
1485 * without actually calling it. If you change the logic here you may need to
1486 * check and change there as well.
1488 static int unpack_callback(int n
, unsigned long mask
, unsigned long dirmask
, struct name_entry
*names
, struct traverse_info
*info
)
1490 struct cache_entry
*src
[MAX_UNPACK_TREES
+ 1] = { NULL
, };
1491 struct unpack_trees_options
*o
= info
->data
;
1492 const struct name_entry
*p
= names
;
1493 int is_new_sparse_dir
;
1495 /* Find first entry with a real name (we could use "mask" too) */
1499 if (o
->internal
.debug_unpack
)
1500 debug_unpack_callback(n
, mask
, dirmask
, names
, info
);
1502 /* Are we supposed to look at the index too? */
1506 struct cache_entry
*ce
;
1508 if (o
->diff_index_cached
)
1509 ce
= next_cache_entry(o
);
1511 ce
= find_cache_entry(info
, p
);
1515 cmp
= compare_entry(ce
, info
, p
);
1517 if (unpack_index_entry(ce
, o
) < 0)
1518 return unpack_failed(o
, NULL
);
1524 * If we skip unmerged index
1525 * entries, we'll skip this
1526 * entry *and* the tree
1527 * entries associated with it!
1529 if (o
->skip_unmerged
) {
1530 add_same_unmerged(ce
, o
);
1540 if (unpack_single_entry(n
, mask
, dirmask
, src
, names
, info
, &is_new_sparse_dir
))
1543 if (o
->merge
&& src
[0]) {
1544 if (ce_stage(src
[0]))
1545 mark_ce_used_same_name(src
[0], o
);
1547 mark_ce_used(src
[0], o
);
1550 /* Now handle any directories.. */
1552 /* special case: "diff-index --cached" looking at a tree */
1553 if (o
->diff_index_cached
&&
1554 n
== 1 && dirmask
== 1 && S_ISDIR(names
->mode
)) {
1556 matches
= cache_tree_matches_traversal(o
->src_index
->cache_tree
,
1559 * Everything under the name matches; skip the
1560 * entire hierarchy. diff_index_cached codepath
1561 * special cases D/F conflicts in such a way that
1562 * it does not do any look-ahead, so this is safe.
1566 * Only increment the cache_bottom if the
1567 * directory isn't a sparse directory index
1568 * entry (if it is, it was already incremented)
1569 * in 'mark_ce_used()'
1571 if (!src
[0] || !S_ISSPARSEDIR(src
[0]->ce_mode
))
1572 o
->internal
.cache_bottom
+= matches
;
1577 if (!is_sparse_directory_entry(src
[0], p
, info
) &&
1578 !is_new_sparse_dir
&&
1579 traverse_trees_recursive(n
, dirmask
, mask
& ~dirmask
,
1590 static int clear_ce_flags_1(struct index_state
*istate
,
1591 struct cache_entry
**cache
, int nr
,
1592 struct strbuf
*prefix
,
1593 int select_mask
, int clear_mask
,
1594 struct pattern_list
*pl
,
1595 enum pattern_match_result default_match
,
1598 /* Whole directory matching */
1599 static int clear_ce_flags_dir(struct index_state
*istate
,
1600 struct cache_entry
**cache
, int nr
,
1601 struct strbuf
*prefix
,
1603 int select_mask
, int clear_mask
,
1604 struct pattern_list
*pl
,
1605 enum pattern_match_result default_match
,
1608 struct cache_entry
**cache_end
;
1611 enum pattern_match_result ret
, orig_ret
;
1612 orig_ret
= path_matches_pattern_list(prefix
->buf
, prefix
->len
,
1613 basename
, &dtype
, pl
, istate
);
1615 strbuf_addch(prefix
, '/');
1617 /* If undecided, use matching result of parent dir in defval */
1618 if (orig_ret
== UNDECIDED
)
1619 ret
= default_match
;
1623 for (cache_end
= cache
; cache_end
!= cache
+ nr
; cache_end
++) {
1624 struct cache_entry
*ce
= *cache_end
;
1625 if (strncmp(ce
->name
, prefix
->buf
, prefix
->len
))
1629 if (pl
->use_cone_patterns
&& orig_ret
== MATCHED_RECURSIVE
) {
1630 struct cache_entry
**ce
= cache
;
1631 rc
= cache_end
- cache
;
1633 while (ce
< cache_end
) {
1634 (*ce
)->ce_flags
&= ~clear_mask
;
1637 } else if (pl
->use_cone_patterns
&& orig_ret
== NOT_MATCHED
) {
1638 rc
= cache_end
- cache
;
1640 rc
= clear_ce_flags_1(istate
, cache
, cache_end
- cache
,
1642 select_mask
, clear_mask
,
1647 strbuf_setlen(prefix
, prefix
->len
- 1);
1652 * Traverse the index, find every entry that matches according to
1653 * o->pl. Do "ce_flags &= ~clear_mask" on those entries. Return the
1654 * number of traversed entries.
1656 * If select_mask is non-zero, only entries whose ce_flags has on of
1657 * those bits enabled are traversed.
1659 * cache : pointer to an index entry
1660 * prefix_len : an offset to its path
1662 * The current path ("prefix") including the trailing '/' is
1663 * cache[0]->name[0..(prefix_len-1)]
1664 * Top level path has prefix_len zero.
1666 static int clear_ce_flags_1(struct index_state
*istate
,
1667 struct cache_entry
**cache
, int nr
,
1668 struct strbuf
*prefix
,
1669 int select_mask
, int clear_mask
,
1670 struct pattern_list
*pl
,
1671 enum pattern_match_result default_match
,
1674 struct cache_entry
**cache_end
= nr
? cache
+ nr
: cache
;
1677 * Process all entries that have the given prefix and meet
1678 * select_mask condition
1680 while(cache
!= cache_end
) {
1681 struct cache_entry
*ce
= *cache
;
1682 const char *name
, *slash
;
1684 enum pattern_match_result ret
;
1686 display_progress(istate
->progress
, progress_nr
);
1688 if (select_mask
&& !(ce
->ce_flags
& select_mask
)) {
1694 if (prefix
->len
&& strncmp(ce
->name
, prefix
->buf
, prefix
->len
))
1697 name
= ce
->name
+ prefix
->len
;
1698 slash
= strchr(name
, '/');
1700 /* If it's a directory, try whole directory match first */
1705 strbuf_add(prefix
, name
, len
);
1707 processed
= clear_ce_flags_dir(istate
, cache
, cache_end
- cache
,
1709 prefix
->buf
+ prefix
->len
- len
,
1710 select_mask
, clear_mask
,
1714 /* clear_c_f_dir eats a whole dir already? */
1717 progress_nr
+= processed
;
1718 strbuf_setlen(prefix
, prefix
->len
- len
);
1722 strbuf_addch(prefix
, '/');
1723 processed
= clear_ce_flags_1(istate
, cache
, cache_end
- cache
,
1725 select_mask
, clear_mask
, pl
,
1726 default_match
, progress_nr
);
1729 progress_nr
+= processed
;
1731 strbuf_setlen(prefix
, prefix
->len
- len
- 1);
1736 dtype
= ce_to_dtype(ce
);
1737 ret
= path_matches_pattern_list(ce
->name
,
1739 name
, &dtype
, pl
, istate
);
1740 if (ret
== UNDECIDED
)
1741 ret
= default_match
;
1742 if (ret
== MATCHED
|| ret
== MATCHED_RECURSIVE
)
1743 ce
->ce_flags
&= ~clear_mask
;
1748 display_progress(istate
->progress
, progress_nr
);
1749 return nr
- (cache_end
- cache
);
1752 static int clear_ce_flags(struct index_state
*istate
,
1753 int select_mask
, int clear_mask
,
1754 struct pattern_list
*pl
,
1757 static struct strbuf prefix
= STRBUF_INIT
;
1761 strbuf_reset(&prefix
);
1763 istate
->progress
= start_delayed_progress(
1764 _("Updating index flags"),
1767 xsnprintf(label
, sizeof(label
), "clear_ce_flags(0x%08lx,0x%08lx)",
1768 (unsigned long)select_mask
, (unsigned long)clear_mask
);
1769 trace2_region_enter("unpack_trees", label
, the_repository
);
1770 rval
= clear_ce_flags_1(istate
,
1774 select_mask
, clear_mask
,
1776 trace2_region_leave("unpack_trees", label
, the_repository
);
1778 stop_progress(&istate
->progress
);
1783 * Set/Clear CE_NEW_SKIP_WORKTREE according to $GIT_DIR/info/sparse-checkout
1785 static void mark_new_skip_worktree(struct pattern_list
*pl
,
1786 struct index_state
*istate
,
1787 int select_flag
, int skip_wt_flag
,
1793 * 1. Pretend the narrowest worktree: only unmerged entries
1796 for (i
= 0; i
< istate
->cache_nr
; i
++) {
1797 struct cache_entry
*ce
= istate
->cache
[i
];
1799 if (select_flag
&& !(ce
->ce_flags
& select_flag
))
1802 if (!ce_stage(ce
) && !(ce
->ce_flags
& CE_CONFLICTED
))
1803 ce
->ce_flags
|= skip_wt_flag
;
1805 ce
->ce_flags
&= ~skip_wt_flag
;
1809 * 2. Widen worktree according to sparse-checkout file.
1810 * Matched entries will have skip_wt_flag cleared (i.e. "in")
1812 clear_ce_flags(istate
, select_flag
, skip_wt_flag
, pl
, show_progress
);
1815 static void populate_from_existing_patterns(struct unpack_trees_options
*o
,
1816 struct pattern_list
*pl
)
1818 if (get_sparse_checkout_patterns(pl
) < 0)
1819 o
->skip_sparse_checkout
= 1;
1821 o
->internal
.pl
= pl
;
1824 static void update_sparsity_for_prefix(const char *prefix
,
1825 struct index_state
*istate
)
1827 int prefix_len
= strlen(prefix
);
1828 struct strbuf ce_prefix
= STRBUF_INIT
;
1830 if (!istate
->sparse_index
)
1833 while (prefix_len
> 0 && prefix
[prefix_len
- 1] == '/')
1836 if (prefix_len
<= 0)
1837 BUG("Invalid prefix passed to update_sparsity_for_prefix");
1839 strbuf_grow(&ce_prefix
, prefix_len
+ 1);
1840 strbuf_add(&ce_prefix
, prefix
, prefix_len
);
1841 strbuf_addch(&ce_prefix
, '/');
1844 * If the prefix points to a sparse directory or a path inside a sparse
1845 * directory, the index should be expanded. This is accomplished in one
1847 * - if the prefix is inside a sparse directory, it will be expanded by
1848 * the 'ensure_full_index(...)' call in 'index_name_pos(...)'.
1849 * - if the prefix matches an existing sparse directory entry,
1850 * 'index_name_pos(...)' will return its index position, triggering
1851 * the 'ensure_full_index(...)' below.
1853 if (!path_in_cone_mode_sparse_checkout(ce_prefix
.buf
, istate
) &&
1854 index_name_pos(istate
, ce_prefix
.buf
, ce_prefix
.len
) >= 0)
1855 ensure_full_index(istate
);
1857 strbuf_release(&ce_prefix
);
1860 static int verify_absent(const struct cache_entry
*,
1861 enum unpack_trees_error_types
,
1862 struct unpack_trees_options
*);
1864 * N-way merge "len" trees. Returns 0 on success, -1 on failure to manipulate the
1865 * resulting index, -2 on failure to reflect the changes to the work tree.
1867 * CE_ADDED, CE_UNPACKED and CE_NEW_SKIP_WORKTREE are used internally
1869 int unpack_trees(unsigned len
, struct tree_desc
*t
, struct unpack_trees_options
*o
)
1871 struct repository
*repo
= the_repository
;
1873 static struct cache_entry
*dfc
;
1874 struct pattern_list pl
;
1875 int free_pattern_list
= 0;
1876 struct dir_struct dir
= DIR_INIT
;
1878 if (o
->reset
== UNPACK_RESET_INVALID
)
1879 BUG("o->reset had a value of 1; should be UNPACK_TREES_*_UNTRACKED");
1881 if (len
> MAX_UNPACK_TREES
)
1882 die("unpack_trees takes at most %d trees", MAX_UNPACK_TREES
);
1883 if (o
->internal
.dir
)
1884 BUG("o->internal.dir is for internal use only");
1886 BUG("o->internal.pl is for internal use only");
1887 if (o
->df_conflict_entry
)
1888 BUG("o->df_conflict_entry is an output only field");
1890 trace_performance_enter();
1891 trace2_region_enter("unpack_trees", "unpack_trees", the_repository
);
1893 prepare_repo_settings(repo
);
1894 if (repo
->settings
.command_requires_full_index
) {
1895 ensure_full_index(o
->src_index
);
1897 ensure_full_index(o
->dst_index
);
1900 if (o
->reset
== UNPACK_RESET_OVERWRITE_UNTRACKED
&&
1901 o
->preserve_ignored
)
1902 BUG("UNPACK_RESET_OVERWRITE_UNTRACKED incompatible with preserved ignored files");
1904 if (!o
->preserve_ignored
) {
1905 o
->internal
.dir
= &dir
;
1906 o
->internal
.dir
->flags
|= DIR_SHOW_IGNORED
;
1907 setup_standard_excludes(o
->internal
.dir
);
1911 update_sparsity_for_prefix(o
->prefix
, o
->src_index
);
1913 if (!core_apply_sparse_checkout
|| !o
->update
)
1914 o
->skip_sparse_checkout
= 1;
1915 if (!o
->skip_sparse_checkout
) {
1916 memset(&pl
, 0, sizeof(pl
));
1917 free_pattern_list
= 1;
1918 populate_from_existing_patterns(o
, &pl
);
1921 index_state_init(&o
->internal
.result
, o
->src_index
->repo
);
1922 o
->internal
.result
.initialized
= 1;
1923 o
->internal
.result
.timestamp
.sec
= o
->src_index
->timestamp
.sec
;
1924 o
->internal
.result
.timestamp
.nsec
= o
->src_index
->timestamp
.nsec
;
1925 o
->internal
.result
.version
= o
->src_index
->version
;
1926 if (!o
->src_index
->split_index
) {
1927 o
->internal
.result
.split_index
= NULL
;
1928 } else if (o
->src_index
== o
->dst_index
) {
1930 * o->dst_index (and thus o->src_index) will be discarded
1931 * and overwritten with o->internal.result at the end of
1932 * this function, so just use src_index's split_index to
1933 * avoid having to create a new one.
1935 o
->internal
.result
.split_index
= o
->src_index
->split_index
;
1936 if (o
->src_index
->cache_changed
& SPLIT_INDEX_ORDERED
)
1937 o
->internal
.result
.cache_changed
|= SPLIT_INDEX_ORDERED
;
1938 o
->internal
.result
.split_index
->refcount
++;
1940 o
->internal
.result
.split_index
=
1941 init_split_index(&o
->internal
.result
);
1943 oidcpy(&o
->internal
.result
.oid
, &o
->src_index
->oid
);
1944 o
->internal
.merge_size
= len
;
1945 mark_all_ce_unused(o
->src_index
);
1947 o
->internal
.result
.fsmonitor_last_update
=
1948 xstrdup_or_null(o
->src_index
->fsmonitor_last_update
);
1949 o
->internal
.result
.fsmonitor_has_run_once
= o
->src_index
->fsmonitor_has_run_once
;
1951 if (!o
->src_index
->initialized
&&
1952 !repo
->settings
.command_requires_full_index
&&
1953 is_sparse_index_allowed(&o
->internal
.result
, 0))
1954 o
->internal
.result
.sparse_index
= 1;
1957 * Sparse checkout loop #1: set NEW_SKIP_WORKTREE on existing entries
1959 if (!o
->skip_sparse_checkout
)
1960 mark_new_skip_worktree(o
->internal
.pl
, o
->src_index
, 0,
1961 CE_NEW_SKIP_WORKTREE
, o
->verbose_update
);
1964 dfc
= xcalloc(1, cache_entry_size(0));
1965 o
->df_conflict_entry
= dfc
;
1968 const char *prefix
= o
->prefix
? o
->prefix
: "";
1969 struct traverse_info info
;
1971 setup_traverse_info(&info
, prefix
);
1972 info
.fn
= unpack_callback
;
1974 info
.show_all_errors
= o
->internal
.show_all_errors
;
1975 info
.pathspec
= o
->pathspec
;
1979 * Unpack existing index entries that sort before the
1980 * prefix the tree is spliced into. Note that o->merge
1981 * is always true in this case.
1984 struct cache_entry
*ce
= next_cache_entry(o
);
1987 if (ce_in_traverse_path(ce
, &info
))
1989 if (unpack_index_entry(ce
, o
) < 0)
1994 trace_performance_enter();
1995 trace2_region_enter("unpack_trees", "traverse_trees", the_repository
);
1996 ret
= traverse_trees(o
->src_index
, len
, t
, &info
);
1997 trace2_region_leave("unpack_trees", "traverse_trees", the_repository
);
1998 trace_performance_leave("traverse_trees");
2003 /* Any left-over entries in the index? */
2006 struct cache_entry
*ce
= next_cache_entry(o
);
2009 if (unpack_index_entry(ce
, o
) < 0)
2013 mark_all_ce_unused(o
->src_index
);
2015 if (o
->trivial_merges_only
&& o
->internal
.nontrivial_merge
) {
2016 ret
= unpack_failed(o
, "Merge requires file-level merging");
2020 if (!o
->skip_sparse_checkout
) {
2022 * Sparse checkout loop #2: set NEW_SKIP_WORKTREE on entries not in loop #1
2023 * If they will have NEW_SKIP_WORKTREE, also set CE_SKIP_WORKTREE
2024 * so apply_sparse_checkout() won't attempt to remove it from worktree
2026 mark_new_skip_worktree(o
->internal
.pl
, &o
->internal
.result
,
2027 CE_ADDED
, CE_SKIP_WORKTREE
| CE_NEW_SKIP_WORKTREE
,
2031 for (i
= 0; i
< o
->internal
.result
.cache_nr
; i
++) {
2032 struct cache_entry
*ce
= o
->internal
.result
.cache
[i
];
2035 * Entries marked with CE_ADDED in merged_entry() do not have
2036 * verify_absent() check (the check is effectively disabled
2037 * because CE_NEW_SKIP_WORKTREE is set unconditionally).
2039 * Do the real check now because we have had
2040 * correct CE_NEW_SKIP_WORKTREE
2042 if (ce
->ce_flags
& CE_ADDED
&&
2043 verify_absent(ce
, WARNING_SPARSE_ORPHANED_NOT_OVERWRITTEN
, o
))
2046 if (apply_sparse_checkout(&o
->internal
.result
, ce
, o
))
2051 * Inability to sparsify or de-sparsify individual
2052 * paths is not an error, but just a warning.
2054 if (o
->internal
.show_all_errors
)
2055 display_warning_msgs(o
);
2060 ret
= check_updates(o
, &o
->internal
.result
) ? (-2) : 0;
2062 move_index_extensions(&o
->internal
.result
, o
->src_index
);
2064 if (git_env_bool("GIT_TEST_CHECK_CACHE_TREE", 0))
2065 cache_tree_verify(the_repository
,
2066 &o
->internal
.result
);
2067 if (!o
->skip_cache_tree_update
&&
2068 !cache_tree_fully_valid(o
->internal
.result
.cache_tree
))
2069 cache_tree_update(&o
->internal
.result
,
2074 o
->internal
.result
.updated_workdir
= 1;
2075 discard_index(o
->dst_index
);
2076 *o
->dst_index
= o
->internal
.result
;
2078 discard_index(&o
->internal
.result
);
2080 o
->src_index
= NULL
;
2083 if (free_pattern_list
)
2084 clear_pattern_list(&pl
);
2085 if (o
->internal
.dir
) {
2086 dir_clear(o
->internal
.dir
);
2087 o
->internal
.dir
= NULL
;
2089 trace2_region_leave("unpack_trees", "unpack_trees", the_repository
);
2090 trace_performance_leave("unpack_trees");
2094 if (o
->internal
.show_all_errors
)
2095 display_error_msgs(o
);
2096 mark_all_ce_unused(o
->src_index
);
2097 ret
= unpack_failed(o
, NULL
);
2098 if (o
->exiting_early
)
2104 * Update SKIP_WORKTREE bits according to sparsity patterns, and update
2105 * working directory to match.
2107 * CE_NEW_SKIP_WORKTREE is used internally.
2109 enum update_sparsity_result
update_sparsity(struct unpack_trees_options
*o
,
2110 struct pattern_list
*pl
)
2112 enum update_sparsity_result ret
= UPDATE_SPARSITY_SUCCESS
;
2114 unsigned old_show_all_errors
;
2115 int free_pattern_list
= 0;
2117 old_show_all_errors
= o
->internal
.show_all_errors
;
2118 o
->internal
.show_all_errors
= 1;
2119 index_state_init(&o
->internal
.result
, o
->src_index
->repo
);
2122 if (!o
->update
|| o
->index_only
|| o
->skip_sparse_checkout
)
2123 BUG("update_sparsity() is for reflecting sparsity patterns in working directory");
2124 if (o
->src_index
!= o
->dst_index
|| o
->fn
)
2125 BUG("update_sparsity() called wrong");
2127 trace_performance_enter();
2129 /* If we weren't given patterns, use the recorded ones */
2131 free_pattern_list
= 1;
2132 pl
= xcalloc(1, sizeof(*pl
));
2133 populate_from_existing_patterns(o
, pl
);
2135 o
->internal
.pl
= pl
;
2137 /* Expand sparse directories as needed */
2138 expand_index(o
->src_index
, o
->internal
.pl
);
2140 /* Set NEW_SKIP_WORKTREE on existing entries. */
2141 mark_all_ce_unused(o
->src_index
);
2142 mark_new_skip_worktree(o
->internal
.pl
, o
->src_index
, 0,
2143 CE_NEW_SKIP_WORKTREE
, o
->verbose_update
);
2145 /* Then loop over entries and update/remove as needed */
2146 ret
= UPDATE_SPARSITY_SUCCESS
;
2147 for (i
= 0; i
< o
->src_index
->cache_nr
; i
++) {
2148 struct cache_entry
*ce
= o
->src_index
->cache
[i
];
2152 /* -1 because for loop will increment by 1 */
2153 i
+= warn_conflicted_path(o
->src_index
, i
, o
) - 1;
2154 ret
= UPDATE_SPARSITY_WARNINGS
;
2158 if (apply_sparse_checkout(o
->src_index
, ce
, o
))
2159 ret
= UPDATE_SPARSITY_WARNINGS
;
2162 if (check_updates(o
, o
->src_index
))
2163 ret
= UPDATE_SPARSITY_WORKTREE_UPDATE_FAILURES
;
2165 display_warning_msgs(o
);
2166 o
->internal
.show_all_errors
= old_show_all_errors
;
2167 if (free_pattern_list
) {
2168 clear_pattern_list(pl
);
2170 o
->internal
.pl
= NULL
;
2172 trace_performance_leave("update_sparsity");
2176 /* Here come the merge functions */
2178 static int reject_merge(const struct cache_entry
*ce
,
2179 struct unpack_trees_options
*o
)
2181 return add_rejected_path(o
, ERROR_WOULD_OVERWRITE
, ce
->name
);
2184 static int same(const struct cache_entry
*a
, const struct cache_entry
*b
)
2190 if ((a
->ce_flags
| b
->ce_flags
) & CE_CONFLICTED
)
2192 return a
->ce_mode
== b
->ce_mode
&&
2193 oideq(&a
->oid
, &b
->oid
);
2198 * When a CE gets turned into an unmerged entry, we
2199 * want it to be up-to-date
2201 static int verify_uptodate_1(const struct cache_entry
*ce
,
2202 struct unpack_trees_options
*o
,
2203 enum unpack_trees_error_types error_type
)
2211 * CE_VALID and CE_SKIP_WORKTREE cheat, we better check again
2212 * if this entry is truly up-to-date because this file may be
2215 if ((ce
->ce_flags
& CE_VALID
) || ce_skip_worktree(ce
))
2216 ; /* keep checking */
2217 else if (o
->reset
|| ce_uptodate(ce
))
2220 if (!lstat(ce
->name
, &st
)) {
2221 int flags
= CE_MATCH_IGNORE_VALID
|CE_MATCH_IGNORE_SKIP_WORKTREE
;
2222 unsigned changed
= ie_match_stat(o
->src_index
, ce
, &st
, flags
);
2224 if (submodule_from_ce(ce
)) {
2225 int r
= check_submodule_move_head(ce
,
2226 "HEAD", oid_to_hex(&ce
->oid
), o
);
2228 return add_rejected_path(o
, error_type
, ce
->name
);
2235 * Historic default policy was to allow submodule to be out
2236 * of sync wrt the superproject index. If the submodule was
2237 * not considered interesting above, we don't care here.
2239 if (S_ISGITLINK(ce
->ce_mode
))
2244 if (errno
== ENOENT
)
2246 return add_rejected_path(o
, error_type
, ce
->name
);
2249 int verify_uptodate(const struct cache_entry
*ce
,
2250 struct unpack_trees_options
*o
)
2252 if (!o
->skip_sparse_checkout
&&
2253 (ce
->ce_flags
& CE_SKIP_WORKTREE
) &&
2254 (ce
->ce_flags
& CE_NEW_SKIP_WORKTREE
))
2256 return verify_uptodate_1(ce
, o
, ERROR_NOT_UPTODATE_FILE
);
2259 static int verify_uptodate_sparse(const struct cache_entry
*ce
,
2260 struct unpack_trees_options
*o
)
2262 return verify_uptodate_1(ce
, o
, WARNING_SPARSE_NOT_UPTODATE_FILE
);
2266 * TODO: We should actually invalidate o->internal.result, not src_index [1].
2267 * But since cache tree and untracked cache both are not copied to
2268 * o->internal.result until unpacking is complete, we invalidate them on
2269 * src_index instead with the assumption that they will be copied to
2270 * dst_index at the end.
2272 * [1] src_index->cache_tree is also used in unpack_callback() so if
2273 * we invalidate o->internal.result, we need to update it to use
2274 * o->internal.result.cache_tree as well.
2276 static void invalidate_ce_path(const struct cache_entry
*ce
,
2277 struct unpack_trees_options
*o
)
2281 cache_tree_invalidate_path(o
->src_index
, ce
->name
);
2282 untracked_cache_invalidate_path(o
->src_index
, ce
->name
, 1);
2286 * Check that checking out ce->sha1 in subdir ce->name is not
2287 * going to overwrite any working files.
2289 static int verify_clean_submodule(const char *old_sha1
,
2290 const struct cache_entry
*ce
,
2291 struct unpack_trees_options
*o
)
2293 if (!submodule_from_ce(ce
))
2296 return check_submodule_move_head(ce
, old_sha1
,
2297 oid_to_hex(&ce
->oid
), o
);
2300 static int verify_clean_subdirectory(const struct cache_entry
*ce
,
2301 struct unpack_trees_options
*o
)
2304 * we are about to extract "ce->name"; we would not want to lose
2305 * anything in the existing directory there.
2309 struct dir_struct d
;
2313 if (S_ISGITLINK(ce
->ce_mode
)) {
2314 struct object_id oid
;
2315 int sub_head
= resolve_gitlink_ref(ce
->name
, "HEAD", &oid
);
2317 * If we are not going to update the submodule, then
2320 if (!sub_head
&& oideq(&oid
, &ce
->oid
))
2322 return verify_clean_submodule(sub_head
? NULL
: oid_to_hex(&oid
),
2327 * First let's make sure we do not have a local modification
2328 * in that directory.
2330 namelen
= ce_namelen(ce
);
2331 for (i
= locate_in_src_index(ce
, o
);
2332 i
< o
->src_index
->cache_nr
;
2334 struct cache_entry
*ce2
= o
->src_index
->cache
[i
];
2335 int len
= ce_namelen(ce2
);
2336 if (len
< namelen
||
2337 strncmp(ce
->name
, ce2
->name
, namelen
) ||
2338 ce2
->name
[namelen
] != '/')
2341 * ce2->name is an entry in the subdirectory to be
2344 if (!ce_stage(ce2
)) {
2345 if (verify_uptodate(ce2
, o
))
2347 add_entry(o
, ce2
, CE_REMOVE
, 0);
2348 invalidate_ce_path(ce
, o
);
2349 mark_ce_used(ce2
, o
);
2354 /* Do not lose a locally present file that is not ignored. */
2355 pathbuf
= xstrfmt("%.*s/", namelen
, ce
->name
);
2357 memset(&d
, 0, sizeof(d
));
2358 if (o
->internal
.dir
)
2359 setup_standard_excludes(&d
);
2360 i
= read_directory(&d
, o
->src_index
, pathbuf
, namelen
+1, NULL
);
2364 return add_rejected_path(o
, ERROR_NOT_UPTODATE_DIR
, ce
->name
);
2366 /* Do not lose startup_info->original_cwd */
2367 if (startup_info
->original_cwd
&&
2368 !strcmp(startup_info
->original_cwd
, ce
->name
))
2369 return add_rejected_path(o
, ERROR_CWD_IN_THE_WAY
, ce
->name
);
2375 * This gets called when there was no index entry for the tree entry 'dst',
2376 * but we found a file in the working tree that 'lstat()' said was fine,
2377 * and we're on a case-insensitive filesystem.
2379 * See if we can find a case-insensitive match in the index that also
2380 * matches the stat information, and assume it's that other file!
2382 static int icase_exists(struct unpack_trees_options
*o
, const char *name
, int len
, struct stat
*st
)
2384 const struct cache_entry
*src
;
2386 src
= index_file_exists(o
->src_index
, name
, len
, 1);
2387 return src
&& !ie_match_stat(o
->src_index
, src
, st
, CE_MATCH_IGNORE_VALID
|CE_MATCH_IGNORE_SKIP_WORKTREE
);
2390 enum absent_checking_type
{
2392 ABSENT_ANY_DIRECTORY
2395 static int check_ok_to_remove(const char *name
, int len
, int dtype
,
2396 const struct cache_entry
*ce
, struct stat
*st
,
2397 enum unpack_trees_error_types error_type
,
2398 enum absent_checking_type absent_type
,
2399 struct unpack_trees_options
*o
)
2401 const struct cache_entry
*result
;
2404 * It may be that the 'lstat()' succeeded even though
2405 * target 'ce' was absent, because there is an old
2406 * entry that is different only in case..
2408 * Ignore that lstat() if it matches.
2410 if (ignore_case
&& icase_exists(o
, name
, len
, st
))
2413 if (o
->internal
.dir
&&
2414 is_excluded(o
->internal
.dir
, o
->src_index
, name
, &dtype
))
2416 * ce->name is explicitly excluded, so it is Ok to
2420 if (S_ISDIR(st
->st_mode
)) {
2422 * We are checking out path "foo" and
2423 * found "foo/." in the working tree.
2424 * This is tricky -- if we have modified
2425 * files that are in "foo/" we would lose
2428 if (verify_clean_subdirectory(ce
, o
) < 0)
2433 /* If we only care about directories, then we can remove */
2434 if (absent_type
== ABSENT_ANY_DIRECTORY
)
2438 * The previous round may already have decided to
2439 * delete this path, which is in a subdirectory that
2440 * is being replaced with a blob.
2442 result
= index_file_exists(&o
->internal
.result
, name
, len
, 0);
2444 if (result
->ce_flags
& CE_REMOVE
)
2448 return add_rejected_path(o
, error_type
, name
);
2452 * We do not want to remove or overwrite a working tree file that
2453 * is not tracked, unless it is ignored.
2455 static int verify_absent_1(const struct cache_entry
*ce
,
2456 enum unpack_trees_error_types error_type
,
2457 enum absent_checking_type absent_type
,
2458 struct unpack_trees_options
*o
)
2463 if (o
->index_only
|| !o
->update
)
2466 if (o
->reset
== UNPACK_RESET_OVERWRITE_UNTRACKED
) {
2467 /* Avoid nuking startup_info->original_cwd... */
2468 if (startup_info
->original_cwd
&&
2469 !strcmp(startup_info
->original_cwd
, ce
->name
))
2470 return add_rejected_path(o
, ERROR_CWD_IN_THE_WAY
,
2472 /* ...but nuke anything else. */
2476 len
= check_leading_path(ce
->name
, ce_namelen(ce
), 0);
2483 path
= xmemdupz(ce
->name
, len
);
2484 if (lstat(path
, &st
))
2485 ret
= error_errno("cannot stat '%s'", path
);
2487 if (submodule_from_ce(ce
))
2488 ret
= check_submodule_move_head(ce
,
2489 oid_to_hex(&ce
->oid
),
2492 ret
= check_ok_to_remove(path
, len
, DT_UNKNOWN
, NULL
,
2498 } else if (lstat(ce
->name
, &st
)) {
2499 if (errno
!= ENOENT
)
2500 return error_errno("cannot stat '%s'", ce
->name
);
2503 if (submodule_from_ce(ce
))
2504 return check_submodule_move_head(ce
, oid_to_hex(&ce
->oid
),
2507 return check_ok_to_remove(ce
->name
, ce_namelen(ce
),
2508 ce_to_dtype(ce
), ce
, &st
,
2509 error_type
, absent_type
, o
);
2513 static int verify_absent(const struct cache_entry
*ce
,
2514 enum unpack_trees_error_types error_type
,
2515 struct unpack_trees_options
*o
)
2517 if (!o
->skip_sparse_checkout
&& (ce
->ce_flags
& CE_NEW_SKIP_WORKTREE
))
2519 return verify_absent_1(ce
, error_type
, COMPLETELY_ABSENT
, o
);
2522 static int verify_absent_if_directory(const struct cache_entry
*ce
,
2523 enum unpack_trees_error_types error_type
,
2524 struct unpack_trees_options
*o
)
2526 if (!o
->skip_sparse_checkout
&& (ce
->ce_flags
& CE_NEW_SKIP_WORKTREE
))
2528 return verify_absent_1(ce
, error_type
, ABSENT_ANY_DIRECTORY
, o
);
2531 static int verify_absent_sparse(const struct cache_entry
*ce
,
2532 enum unpack_trees_error_types error_type
,
2533 struct unpack_trees_options
*o
)
2535 return verify_absent_1(ce
, error_type
, COMPLETELY_ABSENT
, o
);
2538 static int merged_entry(const struct cache_entry
*ce
,
2539 const struct cache_entry
*old
,
2540 struct unpack_trees_options
*o
)
2542 int update
= CE_UPDATE
;
2543 struct cache_entry
*merge
= dup_cache_entry(ce
, &o
->internal
.result
);
2547 * New index entries. In sparse checkout, the following
2548 * verify_absent() will be delayed until after
2549 * traverse_trees() finishes in unpack_trees(), then:
2551 * - CE_NEW_SKIP_WORKTREE will be computed correctly
2552 * - verify_absent() be called again, this time with
2553 * correct CE_NEW_SKIP_WORKTREE
2555 * verify_absent() call here does nothing in sparse
2556 * checkout (i.e. o->skip_sparse_checkout == 0)
2559 merge
->ce_flags
|= CE_NEW_SKIP_WORKTREE
;
2561 if (verify_absent(merge
,
2562 ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN
, o
)) {
2563 discard_cache_entry(merge
);
2566 invalidate_ce_path(merge
, o
);
2568 if (submodule_from_ce(ce
) && file_exists(ce
->name
)) {
2569 int ret
= check_submodule_move_head(ce
, NULL
,
2570 oid_to_hex(&ce
->oid
),
2576 } else if (!(old
->ce_flags
& CE_CONFLICTED
)) {
2578 * See if we can re-use the old CE directly?
2579 * That way we get the uptodate stat info.
2581 * This also removes the UPDATE flag on a match; otherwise
2582 * we will end up overwriting local changes in the work tree.
2584 if (same(old
, merge
)) {
2585 copy_cache_entry(merge
, old
);
2588 if (verify_uptodate(old
, o
)) {
2589 discard_cache_entry(merge
);
2592 /* Migrate old flags over */
2593 update
|= old
->ce_flags
& (CE_SKIP_WORKTREE
| CE_NEW_SKIP_WORKTREE
);
2594 invalidate_ce_path(old
, o
);
2597 if (submodule_from_ce(ce
) && file_exists(ce
->name
)) {
2598 int ret
= check_submodule_move_head(ce
, oid_to_hex(&old
->oid
),
2599 oid_to_hex(&ce
->oid
),
2606 * Previously unmerged entry left as an existence
2607 * marker by read_index_unmerged();
2609 if (verify_absent_if_directory(merge
,
2610 ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN
, o
)) {
2611 discard_cache_entry(merge
);
2615 invalidate_ce_path(old
, o
);
2618 if (do_add_entry(o
, merge
, update
, CE_STAGEMASK
) < 0)
2623 static int merged_sparse_dir(const struct cache_entry
* const *src
, int n
,
2624 struct unpack_trees_options
*o
)
2626 struct tree_desc t
[MAX_UNPACK_TREES
+ 1];
2627 void * tree_bufs
[MAX_UNPACK_TREES
+ 1];
2628 struct traverse_info info
;
2632 * Create the tree traversal information for traversing into *only* the
2635 setup_traverse_info(&info
, src
[0]->name
);
2636 info
.fn
= unpack_sparse_callback
;
2638 info
.show_all_errors
= o
->internal
.show_all_errors
;
2639 info
.pathspec
= o
->pathspec
;
2641 /* Get the tree descriptors of the sparse directory in each of the merging trees */
2642 for (i
= 0; i
< n
; i
++)
2643 tree_bufs
[i
] = fill_tree_descriptor(o
->src_index
->repo
, &t
[i
],
2644 src
[i
] && !is_null_oid(&src
[i
]->oid
) ? &src
[i
]->oid
: NULL
);
2646 ret
= traverse_trees(o
->src_index
, n
, t
, &info
);
2648 for (i
= 0; i
< n
; i
++)
2654 static int deleted_entry(const struct cache_entry
*ce
,
2655 const struct cache_entry
*old
,
2656 struct unpack_trees_options
*o
)
2658 /* Did it exist in the index? */
2660 if (verify_absent(ce
, ERROR_WOULD_LOSE_UNTRACKED_REMOVED
, o
))
2663 } else if (verify_absent_if_directory(ce
, ERROR_WOULD_LOSE_UNTRACKED_REMOVED
, o
)) {
2667 if (!(old
->ce_flags
& CE_CONFLICTED
) && verify_uptodate(old
, o
))
2669 add_entry(o
, ce
, CE_REMOVE
, 0);
2670 invalidate_ce_path(ce
, o
);
2674 static int keep_entry(const struct cache_entry
*ce
,
2675 struct unpack_trees_options
*o
)
2677 add_entry(o
, ce
, 0, 0);
2679 invalidate_ce_path(ce
, o
);
2684 static void show_stage_entry(FILE *o
,
2685 const char *label
, const struct cache_entry
*ce
)
2688 fprintf(o
, "%s (missing)\n", label
);
2690 fprintf(o
, "%s%06o %s %d\t%s\n",
2693 oid_to_hex(&ce
->oid
),
2699 int threeway_merge(const struct cache_entry
* const *stages
,
2700 struct unpack_trees_options
*o
)
2702 const struct cache_entry
*index
;
2703 const struct cache_entry
*head
;
2704 const struct cache_entry
*remote
= stages
[o
->head_idx
+ 1];
2707 int remote_match
= 0;
2709 int df_conflict_head
= 0;
2710 int df_conflict_remote
= 0;
2712 int any_anc_missing
= 0;
2713 int no_anc_exists
= 1;
2716 for (i
= 1; i
< o
->head_idx
; i
++) {
2717 if (!stages
[i
] || stages
[i
] == o
->df_conflict_entry
)
2718 any_anc_missing
= 1;
2724 head
= stages
[o
->head_idx
];
2726 if (head
== o
->df_conflict_entry
) {
2727 df_conflict_head
= 1;
2731 if (remote
== o
->df_conflict_entry
) {
2732 df_conflict_remote
= 1;
2737 * First, if there's a #16 situation, note that to prevent #13
2740 if (!same(remote
, head
)) {
2741 for (i
= 1; i
< o
->head_idx
; i
++) {
2742 if (same(stages
[i
], head
)) {
2745 if (same(stages
[i
], remote
)) {
2752 * We start with cases where the index is allowed to match
2753 * something other than the head: #14(ALT) and #2ALT, where it
2754 * is permitted to match the result instead.
2756 /* #14, #14ALT, #2ALT */
2757 if (remote
&& !df_conflict_head
&& head_match
&& !remote_match
) {
2758 if (index
&& !same(index
, remote
) && !same(index
, head
)) {
2759 if (S_ISSPARSEDIR(index
->ce_mode
))
2760 return merged_sparse_dir(stages
, 4, o
);
2762 return reject_merge(index
, o
);
2764 return merged_entry(remote
, index
, o
);
2767 * If we have an entry in the index cache, then we want to
2768 * make sure that it matches head.
2770 if (index
&& !same(index
, head
)) {
2771 if (S_ISSPARSEDIR(index
->ce_mode
))
2772 return merged_sparse_dir(stages
, 4, o
);
2774 return reject_merge(index
, o
);
2779 if (same(head
, remote
))
2780 return merged_entry(head
, index
, o
);
2782 if (!df_conflict_remote
&& remote_match
&& !head_match
)
2783 return merged_entry(head
, index
, o
);
2787 if (!head
&& !remote
&& any_anc_missing
)
2791 * Under the "aggressive" rule, we resolve mostly trivial
2792 * cases that we historically had git-merge-one-file resolve.
2794 if (o
->aggressive
) {
2795 int head_deleted
= !head
;
2796 int remote_deleted
= !remote
;
2797 const struct cache_entry
*ce
= NULL
;
2806 for (i
= 1; i
< o
->head_idx
; i
++) {
2807 if (stages
[i
] && stages
[i
] != o
->df_conflict_entry
) {
2816 * Deleted in one and unchanged in the other.
2818 if ((head_deleted
&& remote_deleted
) ||
2819 (head_deleted
&& remote
&& remote_match
) ||
2820 (remote_deleted
&& head
&& head_match
)) {
2822 return deleted_entry(index
, index
, o
);
2823 if (ce
&& !head_deleted
) {
2824 if (verify_absent(ce
, ERROR_WOULD_LOSE_UNTRACKED_REMOVED
, o
))
2830 * Added in both, identically.
2832 if (no_anc_exists
&& head
&& remote
&& same(head
, remote
))
2833 return merged_entry(head
, index
, o
);
2837 /* Handle "no merge" cases (see t/t1000-read-tree-m-3way.sh) */
2840 * If we've reached the "no merge" cases and we're merging
2841 * a sparse directory, we may have an "edit/edit" conflict that
2842 * can be resolved by individually merging directory contents.
2844 if (S_ISSPARSEDIR(index
->ce_mode
))
2845 return merged_sparse_dir(stages
, 4, o
);
2848 * If we're not merging a sparse directory, ensure the index is
2849 * up-to-date to avoid files getting overwritten with conflict
2852 if (verify_uptodate(index
, o
))
2856 o
->internal
.nontrivial_merge
= 1;
2858 /* #2, #3, #4, #6, #7, #9, #10, #11. */
2860 if (!head_match
|| !remote_match
) {
2861 for (i
= 1; i
< o
->head_idx
; i
++) {
2862 if (stages
[i
] && stages
[i
] != o
->df_conflict_entry
) {
2863 keep_entry(stages
[i
], o
);
2871 fprintf(stderr
, "read-tree: warning #16 detected\n");
2872 show_stage_entry(stderr
, "head ", stages
[head_match
]);
2873 show_stage_entry(stderr
, "remote ", stages
[remote_match
]);
2876 if (head
) { count
+= keep_entry(head
, o
); }
2877 if (remote
) { count
+= keep_entry(remote
, o
); }
2884 * The rule is to "carry forward" what is in the index without losing
2885 * information across a "fast-forward", favoring a successful merge
2886 * over a merge failure when it makes sense. For details of the
2887 * "carry forward" rule, please see <Documentation/git-read-tree.txt>.
2890 int twoway_merge(const struct cache_entry
* const *src
,
2891 struct unpack_trees_options
*o
)
2893 const struct cache_entry
*current
= src
[0];
2894 const struct cache_entry
*oldtree
= src
[1];
2895 const struct cache_entry
*newtree
= src
[2];
2897 if (o
->internal
.merge_size
!= 2)
2898 return error("Cannot do a twoway merge of %d trees",
2899 o
->internal
.merge_size
);
2901 if (oldtree
== o
->df_conflict_entry
)
2903 if (newtree
== o
->df_conflict_entry
)
2907 if (current
->ce_flags
& CE_CONFLICTED
) {
2908 if (same(oldtree
, newtree
) || o
->reset
) {
2910 return deleted_entry(current
, current
, o
);
2912 return merged_entry(newtree
, current
, o
);
2914 return reject_merge(current
, o
);
2915 } else if ((!oldtree
&& !newtree
) || /* 4 and 5 */
2916 (!oldtree
&& newtree
&&
2917 same(current
, newtree
)) || /* 6 and 7 */
2918 (oldtree
&& newtree
&&
2919 same(oldtree
, newtree
)) || /* 14 and 15 */
2920 (oldtree
&& newtree
&&
2921 !same(oldtree
, newtree
) && /* 18 and 19 */
2922 same(current
, newtree
))) {
2923 return keep_entry(current
, o
);
2924 } else if (oldtree
&& !newtree
&& same(current
, oldtree
)) {
2926 return deleted_entry(oldtree
, current
, o
);
2927 } else if (oldtree
&& newtree
&&
2928 same(current
, oldtree
) && !same(current
, newtree
)) {
2930 return merged_entry(newtree
, current
, o
);
2931 } else if (current
&& !oldtree
&& newtree
&&
2932 S_ISSPARSEDIR(current
->ce_mode
) != S_ISSPARSEDIR(newtree
->ce_mode
) &&
2933 ce_stage(current
) == 0) {
2935 * This case is a directory/file conflict across the sparse-index
2936 * boundary. When we are changing from one path to another via
2937 * 'git checkout', then we want to replace one entry with another
2938 * via merged_entry(). If there are staged changes, then we should
2939 * reject the merge instead.
2941 return merged_entry(newtree
, current
, o
);
2942 } else if (S_ISSPARSEDIR(current
->ce_mode
)) {
2944 * The sparse directories differ, but we don't know whether that's
2945 * because of two different files in the directory being modified
2946 * (can be trivially merged) or if there is a real file conflict.
2947 * Merge the sparse directory by OID to compare file-by-file.
2949 return merged_sparse_dir(src
, 3, o
);
2951 return reject_merge(current
, o
);
2954 if (oldtree
&& !o
->initial_checkout
) {
2956 * deletion of the path was staged;
2958 if (same(oldtree
, newtree
))
2960 return reject_merge(oldtree
, o
);
2962 return merged_entry(newtree
, current
, o
);
2964 return deleted_entry(oldtree
, current
, o
);
2970 * Keep the index entries at stage0, collapse stage1 but make sure
2971 * stage0 does not have anything there.
2973 int bind_merge(const struct cache_entry
* const *src
,
2974 struct unpack_trees_options
*o
)
2976 const struct cache_entry
*old
= src
[0];
2977 const struct cache_entry
*a
= src
[1];
2979 if (o
->internal
.merge_size
!= 1)
2980 return error("Cannot do a bind merge of %d trees",
2981 o
->internal
.merge_size
);
2983 return o
->quiet
? -1 :
2984 error(ERRORMSG(o
, ERROR_BIND_OVERLAP
),
2985 super_prefixed(a
->name
, o
->super_prefix
),
2986 super_prefixed(old
->name
, o
->super_prefix
));
2988 return keep_entry(old
, o
);
2990 return merged_entry(a
, NULL
, o
);
2997 * - take the stat information from stage0, take the data from stage1
2999 int oneway_merge(const struct cache_entry
* const *src
,
3000 struct unpack_trees_options
*o
)
3002 const struct cache_entry
*old
= src
[0];
3003 const struct cache_entry
*a
= src
[1];
3005 if (o
->internal
.merge_size
!= 1)
3006 return error("Cannot do a oneway merge of %d trees",
3007 o
->internal
.merge_size
);
3009 if (!a
|| a
== o
->df_conflict_entry
)
3010 return deleted_entry(old
, old
, o
);
3012 if (old
&& same(old
, a
)) {
3014 if (o
->reset
&& o
->update
&& !ce_uptodate(old
) && !ce_skip_worktree(old
) &&
3015 !(old
->ce_flags
& CE_FSMONITOR_VALID
)) {
3017 if (lstat(old
->name
, &st
) ||
3018 ie_match_stat(o
->src_index
, old
, &st
, CE_MATCH_IGNORE_VALID
|CE_MATCH_IGNORE_SKIP_WORKTREE
))
3019 update
|= CE_UPDATE
;
3021 if (o
->update
&& S_ISGITLINK(old
->ce_mode
) &&
3022 should_update_submodules() && !verify_uptodate(old
, o
))
3023 update
|= CE_UPDATE
;
3024 add_entry(o
, old
, update
, CE_STAGEMASK
);
3027 return merged_entry(a
, old
, o
);
3031 * Merge worktree and untracked entries in a stash entry.
3033 * Ignore all index entries. Collapse remaining trees but make sure that they
3034 * don't have any conflicting files.
3036 int stash_worktree_untracked_merge(const struct cache_entry
* const *src
,
3037 struct unpack_trees_options
*o
)
3039 const struct cache_entry
*worktree
= src
[1];
3040 const struct cache_entry
*untracked
= src
[2];
3042 if (o
->internal
.merge_size
!= 2)
3043 BUG("invalid merge_size: %d", o
->internal
.merge_size
);
3045 if (worktree
&& untracked
)
3046 return error(_("worktree and untracked commit have duplicate entries: %s"),
3047 super_prefixed(worktree
->name
, o
->super_prefix
));
3049 return merged_entry(worktree
? worktree
: untracked
, NULL
, o
);