4 #include "repository.h"
7 #include "environment.h"
11 #include "tree-walk.h"
12 #include "cache-tree.h"
13 #include "unpack-trees.h"
17 #include "split-index.h"
18 #include "sparse-index.h"
19 #include "submodule.h"
20 #include "submodule-config.h"
22 #include "fsmonitor.h"
23 #include "object-store.h"
24 #include "promisor-remote.h"
26 #include "parallel-checkout.h"
30 * Error messages expected by scripts out of plumbing commands such as
31 * read-tree. Non-scripted Porcelain is not required to use these messages
32 * and in fact are encouraged to reword them to better suit their particular
33 * situation better. See how "git checkout" and "git merge" replaces
34 * them using setup_unpack_trees_porcelain(), for example.
36 static const char *unpack_plumbing_errors
[NB_UNPACK_TREES_WARNING_TYPES
] = {
37 /* ERROR_WOULD_OVERWRITE */
38 "Entry '%s' would be overwritten by merge. Cannot merge.",
40 /* ERROR_NOT_UPTODATE_FILE */
41 "Entry '%s' not uptodate. Cannot merge.",
43 /* ERROR_NOT_UPTODATE_DIR */
44 "Updating '%s' would lose untracked files in it",
46 /* ERROR_CWD_IN_THE_WAY */
47 "Refusing to remove '%s' since it is the current working directory.",
49 /* ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN */
50 "Untracked working tree file '%s' would be overwritten by merge.",
52 /* ERROR_WOULD_LOSE_UNTRACKED_REMOVED */
53 "Untracked working tree file '%s' would be removed by merge.",
55 /* ERROR_BIND_OVERLAP */
56 "Entry '%s' overlaps with '%s'. Cannot bind.",
58 /* ERROR_WOULD_LOSE_SUBMODULE */
59 "Submodule '%s' cannot checkout new HEAD.",
61 /* NB_UNPACK_TREES_ERROR_TYPES; just a meta value */
64 /* WARNING_SPARSE_NOT_UPTODATE_FILE */
65 "Path '%s' not uptodate; will not remove from working tree.",
67 /* WARNING_SPARSE_UNMERGED_FILE */
68 "Path '%s' unmerged; will not remove from working tree.",
70 /* WARNING_SPARSE_ORPHANED_NOT_OVERWRITTEN */
71 "Path '%s' already present; will not overwrite with sparse update.",
74 #define ERRORMSG(o,type) \
75 ( ((o) && (o)->internal.msgs[(type)]) \
76 ? ((o)->internal.msgs[(type)]) \
77 : (unpack_plumbing_errors[(type)]) )
79 static const char *super_prefixed(const char *path
, const char *super_prefix
)
82 * It is necessary and sufficient to have two static buffers
83 * here, as the return value of this function is fed to
84 * error() using the unpack_*_errors[] templates we see above.
86 static struct strbuf buf
[2] = {STRBUF_INIT
, STRBUF_INIT
};
87 static int super_prefix_len
= -1;
88 static unsigned idx
= ARRAY_SIZE(buf
) - 1;
90 if (super_prefix_len
< 0) {
95 for (i
= 0; i
< ARRAY_SIZE(buf
); i
++)
96 strbuf_addstr(&buf
[i
], super_prefix
);
97 super_prefix_len
= buf
[0].len
;
101 if (!super_prefix_len
)
104 if (++idx
>= ARRAY_SIZE(buf
))
107 strbuf_setlen(&buf
[idx
], super_prefix_len
);
108 strbuf_addstr(&buf
[idx
], path
);
113 void setup_unpack_trees_porcelain(struct unpack_trees_options
*opts
,
117 const char **msgs
= opts
->internal
.msgs
;
120 strvec_init(&opts
->internal
.msgs_to_free
);
122 if (!strcmp(cmd
, "checkout"))
123 msg
= advice_enabled(ADVICE_COMMIT_BEFORE_MERGE
)
124 ? _("Your local changes to the following files would be overwritten by checkout:\n%%s"
125 "Please commit your changes or stash them before you switch branches.")
126 : _("Your local changes to the following files would be overwritten by checkout:\n%%s");
127 else if (!strcmp(cmd
, "merge"))
128 msg
= advice_enabled(ADVICE_COMMIT_BEFORE_MERGE
)
129 ? _("Your local changes to the following files would be overwritten by merge:\n%%s"
130 "Please commit your changes or stash them before you merge.")
131 : _("Your local changes to the following files would be overwritten by merge:\n%%s");
133 msg
= advice_enabled(ADVICE_COMMIT_BEFORE_MERGE
)
134 ? _("Your local changes to the following files would be overwritten by %s:\n%%s"
135 "Please commit your changes or stash them before you %s.")
136 : _("Your local changes to the following files would be overwritten by %s:\n%%s");
137 msgs
[ERROR_WOULD_OVERWRITE
] = msgs
[ERROR_NOT_UPTODATE_FILE
] =
138 strvec_pushf(&opts
->internal
.msgs_to_free
, msg
, cmd
, cmd
);
140 msgs
[ERROR_NOT_UPTODATE_DIR
] =
141 _("Updating the following directories would lose untracked files in them:\n%s");
143 msgs
[ERROR_CWD_IN_THE_WAY
] =
144 _("Refusing to remove the current working directory:\n%s");
146 if (!strcmp(cmd
, "checkout"))
147 msg
= advice_enabled(ADVICE_COMMIT_BEFORE_MERGE
)
148 ? _("The following untracked working tree files would be removed by checkout:\n%%s"
149 "Please move or remove them before you switch branches.")
150 : _("The following untracked working tree files would be removed by checkout:\n%%s");
151 else if (!strcmp(cmd
, "merge"))
152 msg
= advice_enabled(ADVICE_COMMIT_BEFORE_MERGE
)
153 ? _("The following untracked working tree files would be removed by merge:\n%%s"
154 "Please move or remove them before you merge.")
155 : _("The following untracked working tree files would be removed by merge:\n%%s");
157 msg
= advice_enabled(ADVICE_COMMIT_BEFORE_MERGE
)
158 ? _("The following untracked working tree files would be removed by %s:\n%%s"
159 "Please move or remove them before you %s.")
160 : _("The following untracked working tree files would be removed by %s:\n%%s");
161 msgs
[ERROR_WOULD_LOSE_UNTRACKED_REMOVED
] =
162 strvec_pushf(&opts
->internal
.msgs_to_free
, msg
, cmd
, cmd
);
164 if (!strcmp(cmd
, "checkout"))
165 msg
= advice_enabled(ADVICE_COMMIT_BEFORE_MERGE
)
166 ? _("The following untracked working tree files would be overwritten by checkout:\n%%s"
167 "Please move or remove them before you switch branches.")
168 : _("The following untracked working tree files would be overwritten by checkout:\n%%s");
169 else if (!strcmp(cmd
, "merge"))
170 msg
= advice_enabled(ADVICE_COMMIT_BEFORE_MERGE
)
171 ? _("The following untracked working tree files would be overwritten by merge:\n%%s"
172 "Please move or remove them before you merge.")
173 : _("The following untracked working tree files would be overwritten by merge:\n%%s");
175 msg
= advice_enabled(ADVICE_COMMIT_BEFORE_MERGE
)
176 ? _("The following untracked working tree files would be overwritten by %s:\n%%s"
177 "Please move or remove them before you %s.")
178 : _("The following untracked working tree files would be overwritten by %s:\n%%s");
179 msgs
[ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN
] =
180 strvec_pushf(&opts
->internal
.msgs_to_free
, msg
, cmd
, cmd
);
183 * Special case: ERROR_BIND_OVERLAP refers to a pair of paths, we
184 * cannot easily display it as a list.
186 msgs
[ERROR_BIND_OVERLAP
] = _("Entry '%s' overlaps with '%s'. Cannot bind.");
188 msgs
[ERROR_WOULD_LOSE_SUBMODULE
] =
189 _("Cannot update submodule:\n%s");
191 msgs
[WARNING_SPARSE_NOT_UPTODATE_FILE
] =
192 _("The following paths are not up to date and were left despite sparse patterns:\n%s");
193 msgs
[WARNING_SPARSE_UNMERGED_FILE
] =
194 _("The following paths are unmerged and were left despite sparse patterns:\n%s");
195 msgs
[WARNING_SPARSE_ORPHANED_NOT_OVERWRITTEN
] =
196 _("The following paths were already present and thus not updated despite sparse patterns:\n%s");
198 opts
->internal
.show_all_errors
= 1;
199 /* rejected paths may not have a static buffer */
200 for (i
= 0; i
< ARRAY_SIZE(opts
->internal
.unpack_rejects
); i
++)
201 opts
->internal
.unpack_rejects
[i
].strdup_strings
= 1;
204 void clear_unpack_trees_porcelain(struct unpack_trees_options
*opts
)
206 strvec_clear(&opts
->internal
.msgs_to_free
);
207 memset(opts
->internal
.msgs
, 0, sizeof(opts
->internal
.msgs
));
210 static int do_add_entry(struct unpack_trees_options
*o
, struct cache_entry
*ce
,
211 unsigned int set
, unsigned int clear
)
218 ce
->ce_flags
= (ce
->ce_flags
& ~clear
) | set
;
219 return add_index_entry(&o
->internal
.result
, ce
,
220 ADD_CACHE_OK_TO_ADD
| ADD_CACHE_OK_TO_REPLACE
);
223 static void add_entry(struct unpack_trees_options
*o
,
224 const struct cache_entry
*ce
,
225 unsigned int set
, unsigned int clear
)
227 do_add_entry(o
, dup_cache_entry(ce
, &o
->internal
.result
), set
, clear
);
231 * add error messages on path <path>
232 * corresponding to the type <e> with the message <msg>
233 * indicating if it should be display in porcelain or not
235 static int add_rejected_path(struct unpack_trees_options
*o
,
236 enum unpack_trees_error_types e
,
242 if (!o
->internal
.show_all_errors
)
243 return error(ERRORMSG(o
, e
), super_prefixed(path
,
247 * Otherwise, insert in a list for future display by
248 * display_(error|warning)_msgs()
250 string_list_append(&o
->internal
.unpack_rejects
[e
], path
);
255 * display all the error messages stored in a nice way
257 static void display_error_msgs(struct unpack_trees_options
*o
)
260 unsigned error_displayed
= 0;
261 for (e
= 0; e
< NB_UNPACK_TREES_ERROR_TYPES
; e
++) {
262 struct string_list
*rejects
= &o
->internal
.unpack_rejects
[e
];
264 if (rejects
->nr
> 0) {
266 struct strbuf path
= STRBUF_INIT
;
269 for (i
= 0; i
< rejects
->nr
; i
++)
270 strbuf_addf(&path
, "\t%s\n", rejects
->items
[i
].string
);
271 error(ERRORMSG(o
, e
), super_prefixed(path
.buf
,
273 strbuf_release(&path
);
275 string_list_clear(rejects
, 0);
278 fprintf(stderr
, _("Aborting\n"));
282 * display all the warning messages stored in a nice way
284 static void display_warning_msgs(struct unpack_trees_options
*o
)
287 unsigned warning_displayed
= 0;
288 for (e
= NB_UNPACK_TREES_ERROR_TYPES
+ 1;
289 e
< NB_UNPACK_TREES_WARNING_TYPES
; e
++) {
290 struct string_list
*rejects
= &o
->internal
.unpack_rejects
[e
];
292 if (rejects
->nr
> 0) {
294 struct strbuf path
= STRBUF_INIT
;
296 warning_displayed
= 1;
297 for (i
= 0; i
< rejects
->nr
; i
++)
298 strbuf_addf(&path
, "\t%s\n", rejects
->items
[i
].string
);
299 warning(ERRORMSG(o
, e
), super_prefixed(path
.buf
,
301 strbuf_release(&path
);
303 string_list_clear(rejects
, 0);
305 if (warning_displayed
)
306 fprintf(stderr
, _("After fixing the above paths, you may want to run `git sparse-checkout reapply`.\n"));
308 static int check_submodule_move_head(const struct cache_entry
*ce
,
311 struct unpack_trees_options
*o
)
313 unsigned flags
= SUBMODULE_MOVE_HEAD_DRY_RUN
;
314 const struct submodule
*sub
= submodule_from_ce(ce
);
320 flags
|= SUBMODULE_MOVE_HEAD_FORCE
;
322 if (submodule_move_head(ce
->name
, o
->super_prefix
, old_id
, new_id
,
324 return add_rejected_path(o
, ERROR_WOULD_LOSE_SUBMODULE
, ce
->name
);
329 * Perform the loading of the repository's gitmodules file. This function is
330 * used by 'check_update()' to perform loading of the gitmodules file in two
331 * different situations:
332 * (1) before removing entries from the working tree if the gitmodules file has
333 * been marked for removal. This situation is specified by 'state' == NULL.
334 * (2) before checking out entries to the working tree if the gitmodules file
335 * has been marked for update. This situation is specified by 'state' != NULL.
337 static void load_gitmodules_file(struct index_state
*index
,
338 struct checkout
*state
)
340 int pos
= index_name_pos(index
, GITMODULES_FILE
, strlen(GITMODULES_FILE
));
343 struct cache_entry
*ce
= index
->cache
[pos
];
344 if (!state
&& ce
->ce_flags
& CE_WT_REMOVE
) {
345 repo_read_gitmodules(the_repository
, 0);
346 } else if (state
&& (ce
->ce_flags
& CE_UPDATE
)) {
347 submodule_free(the_repository
);
348 checkout_entry(ce
, state
, NULL
, NULL
);
349 repo_read_gitmodules(the_repository
, 0);
354 static struct progress
*get_progress(struct unpack_trees_options
*o
,
355 struct index_state
*index
)
357 unsigned cnt
= 0, total
= 0;
359 if (!o
->update
|| !o
->verbose_update
)
362 for (; cnt
< index
->cache_nr
; cnt
++) {
363 const struct cache_entry
*ce
= index
->cache
[cnt
];
364 if (ce
->ce_flags
& (CE_UPDATE
| CE_WT_REMOVE
))
368 return start_delayed_progress(_("Updating files"), total
);
371 static void setup_collided_checkout_detection(struct checkout
*state
,
372 struct index_state
*index
)
377 for (i
= 0; i
< index
->cache_nr
; i
++)
378 index
->cache
[i
]->ce_flags
&= ~CE_MATCHED
;
381 static void report_collided_checkout(struct index_state
*index
)
383 struct string_list list
= STRING_LIST_INIT_NODUP
;
386 for (i
= 0; i
< index
->cache_nr
; i
++) {
387 struct cache_entry
*ce
= index
->cache
[i
];
389 if (!(ce
->ce_flags
& CE_MATCHED
))
392 string_list_append(&list
, ce
->name
);
393 ce
->ce_flags
&= ~CE_MATCHED
;
396 list
.cmp
= fspathcmp
;
397 string_list_sort(&list
);
400 warning(_("the following paths have collided (e.g. case-sensitive paths\n"
401 "on a case-insensitive filesystem) and only one from the same\n"
402 "colliding group is in the working tree:\n"));
404 for (i
= 0; i
< list
.nr
; i
++)
405 fprintf(stderr
, " '%s'\n", list
.items
[i
].string
);
408 string_list_clear(&list
, 0);
411 static int must_checkout(const struct cache_entry
*ce
)
413 return ce
->ce_flags
& CE_UPDATE
;
416 static int check_updates(struct unpack_trees_options
*o
,
417 struct index_state
*index
)
421 struct progress
*progress
;
422 struct checkout state
= CHECKOUT_INIT
;
423 int i
, pc_workers
, pc_threshold
;
425 trace_performance_enter();
426 state
.super_prefix
= o
->super_prefix
;
429 state
.refresh_cache
= 1;
430 state
.istate
= index
;
431 clone_checkout_metadata(&state
.meta
, &o
->meta
, NULL
);
433 if (!o
->update
|| o
->dry_run
) {
434 remove_marked_cache_entries(index
, 0);
435 trace_performance_leave("check_updates");
440 setup_collided_checkout_detection(&state
, index
);
442 progress
= get_progress(o
, index
);
444 /* Start with clean cache to avoid using any possibly outdated info. */
445 invalidate_lstat_cache();
447 git_attr_set_direction(GIT_ATTR_CHECKOUT
);
449 if (should_update_submodules())
450 load_gitmodules_file(index
, NULL
);
452 for (i
= 0; i
< index
->cache_nr
; i
++) {
453 const struct cache_entry
*ce
= index
->cache
[i
];
455 if (ce
->ce_flags
& CE_WT_REMOVE
) {
456 display_progress(progress
, ++cnt
);
457 unlink_entry(ce
, o
->super_prefix
);
461 remove_marked_cache_entries(index
, 0);
462 remove_scheduled_dirs();
464 if (should_update_submodules())
465 load_gitmodules_file(index
, &state
);
467 if (repo_has_promisor_remote(the_repository
))
469 * Prefetch the objects that are to be checked out in the loop
472 prefetch_cache_entries(index
, must_checkout
);
474 get_parallel_checkout_configs(&pc_workers
, &pc_threshold
);
476 enable_delayed_checkout(&state
);
478 init_parallel_checkout();
479 for (i
= 0; i
< index
->cache_nr
; i
++) {
480 struct cache_entry
*ce
= index
->cache
[i
];
482 if (must_checkout(ce
)) {
483 size_t last_pc_queue_size
= pc_queue_size();
485 if (ce
->ce_flags
& CE_WT_REMOVE
)
486 BUG("both update and delete flags are set on %s",
488 ce
->ce_flags
&= ~CE_UPDATE
;
489 errs
|= checkout_entry(ce
, &state
, NULL
, NULL
);
491 if (last_pc_queue_size
== pc_queue_size())
492 display_progress(progress
, ++cnt
);
496 errs
|= run_parallel_checkout(&state
, pc_workers
, pc_threshold
,
498 stop_progress(&progress
);
499 errs
|= finish_delayed_checkout(&state
, o
->verbose_update
);
500 git_attr_set_direction(GIT_ATTR_CHECKIN
);
503 report_collided_checkout(index
);
505 trace_performance_leave("check_updates");
509 static int verify_uptodate_sparse(const struct cache_entry
*ce
,
510 struct unpack_trees_options
*o
);
511 static int verify_absent_sparse(const struct cache_entry
*ce
,
512 enum unpack_trees_error_types
,
513 struct unpack_trees_options
*o
);
515 static int apply_sparse_checkout(struct index_state
*istate
,
516 struct cache_entry
*ce
,
517 struct unpack_trees_options
*o
)
519 int was_skip_worktree
= ce_skip_worktree(ce
);
521 if (ce
->ce_flags
& CE_NEW_SKIP_WORKTREE
)
522 ce
->ce_flags
|= CE_SKIP_WORKTREE
;
524 ce
->ce_flags
&= ~CE_SKIP_WORKTREE
;
525 if (was_skip_worktree
!= ce_skip_worktree(ce
)) {
526 ce
->ce_flags
|= CE_UPDATE_IN_BASE
;
527 mark_fsmonitor_invalid(istate
, ce
);
528 istate
->cache_changed
|= CE_ENTRY_CHANGED
;
532 * if (!was_skip_worktree && !ce_skip_worktree()) {
533 * This is perfectly normal. Move on;
538 * Merge strategies may set CE_UPDATE|CE_REMOVE outside checkout
539 * area as a result of ce_skip_worktree() shortcuts in
540 * verify_absent() and verify_uptodate().
541 * Make sure they don't modify worktree if they are already
542 * outside checkout area
544 if (was_skip_worktree
&& ce_skip_worktree(ce
)) {
545 ce
->ce_flags
&= ~CE_UPDATE
;
548 * By default, when CE_REMOVE is on, CE_WT_REMOVE is also
549 * on to get that file removed from both index and worktree.
550 * If that file is already outside worktree area, don't
553 if (ce
->ce_flags
& CE_REMOVE
)
554 ce
->ce_flags
&= ~CE_WT_REMOVE
;
557 if (!was_skip_worktree
&& ce_skip_worktree(ce
)) {
559 * If CE_UPDATE is set, verify_uptodate() must be called already
560 * also stat info may have lost after merged_entry() so calling
561 * verify_uptodate() again may fail
563 if (!(ce
->ce_flags
& CE_UPDATE
) &&
564 verify_uptodate_sparse(ce
, o
)) {
565 ce
->ce_flags
&= ~CE_SKIP_WORKTREE
;
568 ce
->ce_flags
|= CE_WT_REMOVE
;
569 ce
->ce_flags
&= ~CE_UPDATE
;
571 if (was_skip_worktree
&& !ce_skip_worktree(ce
)) {
572 if (verify_absent_sparse(ce
, WARNING_SPARSE_ORPHANED_NOT_OVERWRITTEN
, o
))
574 ce
->ce_flags
|= CE_UPDATE
;
579 static int warn_conflicted_path(struct index_state
*istate
,
581 struct unpack_trees_options
*o
)
583 char *conflicting_path
= istate
->cache
[i
]->name
;
586 add_rejected_path(o
, WARNING_SPARSE_UNMERGED_FILE
, conflicting_path
);
588 /* Find out how many higher stage entries are at same path */
589 while ((++count
) + i
< istate
->cache_nr
&&
590 !strcmp(conflicting_path
, istate
->cache
[count
+ i
]->name
))
596 static inline int call_unpack_fn(const struct cache_entry
* const *src
,
597 struct unpack_trees_options
*o
)
599 int ret
= o
->fn(src
, o
);
605 static void mark_ce_used(struct cache_entry
*ce
, struct unpack_trees_options
*o
)
607 ce
->ce_flags
|= CE_UNPACKED
;
609 if (o
->internal
.cache_bottom
< o
->src_index
->cache_nr
&&
610 o
->src_index
->cache
[o
->internal
.cache_bottom
] == ce
) {
611 int bottom
= o
->internal
.cache_bottom
;
613 while (bottom
< o
->src_index
->cache_nr
&&
614 o
->src_index
->cache
[bottom
]->ce_flags
& CE_UNPACKED
)
616 o
->internal
.cache_bottom
= bottom
;
620 static void mark_all_ce_unused(struct index_state
*index
)
623 for (i
= 0; i
< index
->cache_nr
; i
++)
624 index
->cache
[i
]->ce_flags
&= ~(CE_UNPACKED
| CE_ADDED
| CE_NEW_SKIP_WORKTREE
);
627 static int locate_in_src_index(const struct cache_entry
*ce
,
628 struct unpack_trees_options
*o
)
630 struct index_state
*index
= o
->src_index
;
631 int len
= ce_namelen(ce
);
632 int pos
= index_name_pos(index
, ce
->name
, len
);
639 * We call unpack_index_entry() with an unmerged cache entry
640 * only in diff-index, and it wants a single callback. Skip
641 * the other unmerged entry with the same name.
643 static void mark_ce_used_same_name(struct cache_entry
*ce
,
644 struct unpack_trees_options
*o
)
646 struct index_state
*index
= o
->src_index
;
647 int len
= ce_namelen(ce
);
650 for (pos
= locate_in_src_index(ce
, o
); pos
< index
->cache_nr
; pos
++) {
651 struct cache_entry
*next
= index
->cache
[pos
];
652 if (len
!= ce_namelen(next
) ||
653 memcmp(ce
->name
, next
->name
, len
))
655 mark_ce_used(next
, o
);
659 static struct cache_entry
*next_cache_entry(struct unpack_trees_options
*o
)
661 const struct index_state
*index
= o
->src_index
;
662 int pos
= o
->internal
.cache_bottom
;
664 while (pos
< index
->cache_nr
) {
665 struct cache_entry
*ce
= index
->cache
[pos
];
666 if (!(ce
->ce_flags
& CE_UNPACKED
))
673 static void add_same_unmerged(const struct cache_entry
*ce
,
674 struct unpack_trees_options
*o
)
676 struct index_state
*index
= o
->src_index
;
677 int len
= ce_namelen(ce
);
678 int pos
= index_name_pos(index
, ce
->name
, len
);
681 die("programming error in a caller of mark_ce_used_same_name");
682 for (pos
= -pos
- 1; pos
< index
->cache_nr
; pos
++) {
683 struct cache_entry
*next
= index
->cache
[pos
];
684 if (len
!= ce_namelen(next
) ||
685 memcmp(ce
->name
, next
->name
, len
))
687 add_entry(o
, next
, 0, 0);
688 mark_ce_used(next
, o
);
692 static int unpack_index_entry(struct cache_entry
*ce
,
693 struct unpack_trees_options
*o
)
695 const struct cache_entry
*src
[MAX_UNPACK_TREES
+ 1] = { NULL
, };
702 if (o
->skip_unmerged
) {
703 add_entry(o
, ce
, 0, 0);
707 ret
= call_unpack_fn(src
, o
);
709 mark_ce_used_same_name(ce
, o
);
713 static int find_cache_pos(struct traverse_info
*, const char *p
, size_t len
);
715 static void restore_cache_bottom(struct traverse_info
*info
, int bottom
)
717 struct unpack_trees_options
*o
= info
->data
;
719 if (o
->diff_index_cached
)
721 o
->internal
.cache_bottom
= bottom
;
724 static int switch_cache_bottom(struct traverse_info
*info
)
726 struct unpack_trees_options
*o
= info
->data
;
729 if (o
->diff_index_cached
)
731 ret
= o
->internal
.cache_bottom
;
732 pos
= find_cache_pos(info
->prev
, info
->name
, info
->namelen
);
735 o
->internal
.cache_bottom
= -2 - pos
;
737 o
->internal
.cache_bottom
= o
->src_index
->cache_nr
;
741 static inline int are_same_oid(struct name_entry
*name_j
, struct name_entry
*name_k
)
743 return !is_null_oid(&name_j
->oid
) && !is_null_oid(&name_k
->oid
) && oideq(&name_j
->oid
, &name_k
->oid
);
746 static int all_trees_same_as_cache_tree(int n
, unsigned long dirmask
,
747 struct name_entry
*names
,
748 struct traverse_info
*info
)
750 struct unpack_trees_options
*o
= info
->data
;
753 if (!o
->merge
|| dirmask
!= ((1 << n
) - 1))
756 for (i
= 1; i
< n
; i
++)
757 if (!are_same_oid(names
, names
+ i
))
760 return cache_tree_matches_traversal(o
->src_index
->cache_tree
, names
, info
);
763 static int index_pos_by_traverse_info(struct name_entry
*names
,
764 struct traverse_info
*info
)
766 struct unpack_trees_options
*o
= info
->data
;
767 struct strbuf name
= STRBUF_INIT
;
770 strbuf_make_traverse_path(&name
, info
, names
->path
, names
->pathlen
);
771 strbuf_addch(&name
, '/');
772 pos
= index_name_pos(o
->src_index
, name
.buf
, name
.len
);
774 if (!o
->src_index
->sparse_index
||
775 !(o
->src_index
->cache
[pos
]->ce_flags
& CE_SKIP_WORKTREE
))
776 BUG("This is a directory and should not exist in index");
780 if (pos
>= o
->src_index
->cache_nr
||
781 !starts_with(o
->src_index
->cache
[pos
]->name
, name
.buf
) ||
782 (pos
> 0 && starts_with(o
->src_index
->cache
[pos
-1]->name
, name
.buf
)))
783 BUG("pos %d doesn't point to the first entry of %s in index",
785 strbuf_release(&name
);
790 * Fast path if we detect that all trees are the same as cache-tree at this
791 * path. We'll walk these trees in an iterative loop using cache-tree/index
792 * instead of ODB since we already know what these trees contain.
794 static int traverse_by_cache_tree(int pos
, int nr_entries
, int nr_names
,
795 struct traverse_info
*info
)
797 struct cache_entry
*src
[MAX_UNPACK_TREES
+ 1] = { NULL
, };
798 struct unpack_trees_options
*o
= info
->data
;
799 struct cache_entry
*tree_ce
= NULL
;
804 BUG("We need cache-tree to do this optimization");
807 * Do what unpack_callback() and unpack_single_entry() normally
808 * do. But we walk all paths in an iterative loop instead.
810 * D/F conflicts and higher stage entries are not a concern
811 * because cache-tree would be invalidated and we would never
812 * get here in the first place.
814 for (i
= 0; i
< nr_entries
; i
++) {
815 int new_ce_len
, len
, rc
;
817 src
[0] = o
->src_index
->cache
[pos
+ i
];
819 len
= ce_namelen(src
[0]);
820 new_ce_len
= cache_entry_size(len
);
822 if (new_ce_len
> ce_len
) {
824 tree_ce
= xrealloc(tree_ce
, new_ce_len
);
825 memset(tree_ce
, 0, new_ce_len
);
828 tree_ce
->ce_flags
= create_ce_flags(0);
830 for (d
= 1; d
<= nr_names
; d
++)
834 tree_ce
->ce_mode
= src
[0]->ce_mode
;
835 tree_ce
->ce_namelen
= len
;
836 oidcpy(&tree_ce
->oid
, &src
[0]->oid
);
837 memcpy(tree_ce
->name
, src
[0]->name
, len
+ 1);
839 rc
= call_unpack_fn((const struct cache_entry
* const *)src
, o
);
845 mark_ce_used(src
[0], o
);
848 if (o
->internal
.debug_unpack
)
849 printf("Unpacked %d entries from %s to %s using cache-tree\n",
851 o
->src_index
->cache
[pos
]->name
,
852 o
->src_index
->cache
[pos
+ nr_entries
- 1]->name
);
856 static int traverse_trees_recursive(int n
, unsigned long dirmask
,
857 unsigned long df_conflicts
,
858 struct name_entry
*names
,
859 struct traverse_info
*info
)
861 struct unpack_trees_options
*o
= info
->data
;
864 struct tree_desc t
[MAX_UNPACK_TREES
];
865 void *buf
[MAX_UNPACK_TREES
];
866 struct traverse_info newinfo
;
867 struct name_entry
*p
;
870 nr_entries
= all_trees_same_as_cache_tree(n
, dirmask
, names
, info
);
871 if (nr_entries
> 0) {
872 int pos
= index_pos_by_traverse_info(names
, info
);
874 if (!o
->merge
|| df_conflicts
)
875 BUG("Wrong condition to get here buddy");
878 * All entries up to 'pos' must have been processed
879 * (i.e. marked CE_UNPACKED) at this point. But to be safe,
880 * save and restore cache_bottom anyway to not miss
881 * unprocessed entries before 'pos'.
883 bottom
= o
->internal
.cache_bottom
;
884 ret
= traverse_by_cache_tree(pos
, nr_entries
, n
, info
);
885 o
->internal
.cache_bottom
= bottom
;
895 newinfo
.pathspec
= info
->pathspec
;
896 newinfo
.name
= p
->path
;
897 newinfo
.namelen
= p
->pathlen
;
898 newinfo
.mode
= p
->mode
;
899 newinfo
.pathlen
= st_add3(newinfo
.pathlen
, tree_entry_len(p
), 1);
900 newinfo
.df_conflicts
|= df_conflicts
;
903 * Fetch the tree from the ODB for each peer directory in the
906 * For 2- and 3-way traversals, we try to avoid hitting the
907 * ODB twice for the same OID. This should yield a nice speed
908 * up in checkouts and merges when the commits are similar.
910 * We don't bother doing the full O(n^2) search for larger n,
911 * because wider traversals don't happen that often and we
912 * avoid the search setup.
914 * When 2 peer OIDs are the same, we just copy the tree
915 * descriptor data. This implicitly borrows the buffer
916 * data from the earlier cell.
918 for (i
= 0; i
< n
; i
++, dirmask
>>= 1) {
919 if (i
> 0 && are_same_oid(&names
[i
], &names
[i
- 1]))
921 else if (i
> 1 && are_same_oid(&names
[i
], &names
[i
- 2]))
924 const struct object_id
*oid
= NULL
;
927 buf
[nr_buf
++] = fill_tree_descriptor(the_repository
, t
+ i
, oid
);
931 bottom
= switch_cache_bottom(&newinfo
);
932 ret
= traverse_trees(o
->src_index
, n
, t
, &newinfo
);
933 restore_cache_bottom(&newinfo
, bottom
);
935 for (i
= 0; i
< nr_buf
; i
++)
942 * Compare the traverse-path to the cache entry without actually
943 * having to generate the textual representation of the traverse
946 * NOTE! This *only* compares up to the size of the traverse path
947 * itself - the caller needs to do the final check for the cache
948 * entry having more data at the end!
950 static int do_compare_entry_piecewise(const struct cache_entry
*ce
,
951 const struct traverse_info
*info
,
952 const char *name
, size_t namelen
,
959 int cmp
= do_compare_entry_piecewise(ce
, info
->prev
,
960 info
->name
, info
->namelen
,
965 pathlen
= info
->pathlen
;
966 ce_len
= ce_namelen(ce
);
968 /* If ce_len < pathlen then we must have previously hit "name == directory" entry */
969 if (ce_len
< pathlen
)
973 ce_name
= ce
->name
+ pathlen
;
975 return df_name_compare(ce_name
, ce_len
, S_IFREG
, name
, namelen
, mode
);
978 static int do_compare_entry(const struct cache_entry
*ce
,
979 const struct traverse_info
*info
,
980 const char *name
, size_t namelen
,
989 * If we have not precomputed the traverse path, it is quicker
990 * to avoid doing so. But if we have precomputed it,
991 * it is quicker to use the precomputed version.
993 if (!info
->traverse_path
)
994 return do_compare_entry_piecewise(ce
, info
, name
, namelen
, mode
);
996 cmp
= strncmp(ce
->name
, info
->traverse_path
, info
->pathlen
);
1000 pathlen
= info
->pathlen
;
1001 ce_len
= ce_namelen(ce
);
1003 if (ce_len
< pathlen
)
1007 ce_name
= ce
->name
+ pathlen
;
1009 ce_mode
= S_ISSPARSEDIR(ce
->ce_mode
) ? S_IFDIR
: S_IFREG
;
1010 return df_name_compare(ce_name
, ce_len
, ce_mode
, name
, namelen
, mode
);
1013 static int compare_entry(const struct cache_entry
*ce
, const struct traverse_info
*info
, const struct name_entry
*n
)
1015 int cmp
= do_compare_entry(ce
, info
, n
->path
, n
->pathlen
, n
->mode
);
1020 * At this point, we know that we have a prefix match. If ce
1021 * is a sparse directory, then allow an exact match. This only
1022 * works when the input name is a directory, since ce->name
1023 * ends in a directory separator.
1025 if (S_ISSPARSEDIR(ce
->ce_mode
) &&
1026 ce
->ce_namelen
== traverse_path_len(info
, tree_entry_len(n
)) + 1)
1030 * Even if the beginning compared identically, the ce should
1031 * compare as bigger than a directory leading up to it!
1033 return ce_namelen(ce
) > traverse_path_len(info
, tree_entry_len(n
));
1036 static int ce_in_traverse_path(const struct cache_entry
*ce
,
1037 const struct traverse_info
*info
)
1041 if (do_compare_entry(ce
, info
->prev
,
1042 info
->name
, info
->namelen
, info
->mode
))
1045 * If ce (blob) is the same name as the path (which is a tree
1046 * we will be descending into), it won't be inside it.
1048 return (info
->pathlen
< ce_namelen(ce
));
1051 static struct cache_entry
*create_ce_entry(const struct traverse_info
*info
,
1052 const struct name_entry
*n
,
1054 struct index_state
*istate
,
1056 int is_sparse_directory
)
1058 size_t len
= traverse_path_len(info
, tree_entry_len(n
));
1059 size_t alloc_len
= is_sparse_directory
? len
+ 1 : len
;
1060 struct cache_entry
*ce
=
1062 make_empty_transient_cache_entry(alloc_len
, NULL
) :
1063 make_empty_cache_entry(istate
, alloc_len
);
1065 ce
->ce_mode
= create_ce_mode(n
->mode
);
1066 ce
->ce_flags
= create_ce_flags(stage
);
1067 ce
->ce_namelen
= len
;
1068 oidcpy(&ce
->oid
, &n
->oid
);
1069 /* len+1 because the cache_entry allocates space for NUL */
1070 make_traverse_path(ce
->name
, len
+ 1, info
, n
->path
, n
->pathlen
);
1072 if (is_sparse_directory
) {
1073 ce
->name
[len
] = '/';
1074 ce
->name
[len
+ 1] = '\0';
1076 ce
->ce_flags
|= CE_SKIP_WORKTREE
;
1083 * Determine whether the path specified by 'p' should be unpacked as a new
1084 * sparse directory in a sparse index. A new sparse directory 'A/':
1085 * - must be outside the sparse cone.
1086 * - must not already be in the index (i.e., no index entry with name 'A/'
1088 * - must not have any child entries in the index (i.e., no index entry
1089 * 'A/<something>' exists).
1090 * If 'p' meets the above requirements, return 1; otherwise, return 0.
1092 static int entry_is_new_sparse_dir(const struct traverse_info
*info
,
1093 const struct name_entry
*p
)
1096 struct strbuf dirpath
= STRBUF_INIT
;
1097 struct unpack_trees_options
*o
= info
->data
;
1099 if (!S_ISDIR(p
->mode
))
1103 * If the path is inside the sparse cone, it can't be a sparse directory.
1105 strbuf_add(&dirpath
, info
->traverse_path
, info
->pathlen
);
1106 strbuf_add(&dirpath
, p
->path
, p
->pathlen
);
1107 strbuf_addch(&dirpath
, '/');
1108 if (path_in_cone_mode_sparse_checkout(dirpath
.buf
, o
->src_index
)) {
1113 pos
= index_name_pos_sparse(o
->src_index
, dirpath
.buf
, dirpath
.len
);
1115 /* Path is already in the index, not a new sparse dir */
1120 /* Where would this sparse dir be inserted into the index? */
1122 if (pos
>= o
->src_index
->cache_nr
) {
1124 * Sparse dir would be inserted at the end of the index, so we
1125 * know it has no child entries.
1132 * If the dir has child entries in the index, the first would be at the
1133 * position the sparse directory would be inserted. If the entry at this
1134 * position is inside the dir, not a new sparse dir.
1136 res
= strncmp(o
->src_index
->cache
[pos
]->name
, dirpath
.buf
, dirpath
.len
);
1139 strbuf_release(&dirpath
);
1144 * Note that traverse_by_cache_tree() duplicates some logic in this function
1145 * without actually calling it. If you change the logic here you may need to
1146 * check and change there as well.
1148 static int unpack_single_entry(int n
, unsigned long mask
,
1149 unsigned long dirmask
,
1150 struct cache_entry
**src
,
1151 const struct name_entry
*names
,
1152 const struct traverse_info
*info
,
1153 int *is_new_sparse_dir
)
1156 struct unpack_trees_options
*o
= info
->data
;
1157 unsigned long conflicts
= info
->df_conflicts
| dirmask
;
1158 const struct name_entry
*p
= names
;
1160 *is_new_sparse_dir
= 0;
1161 if (mask
== dirmask
&& !src
[0]) {
1163 * If we're not in a sparse index, we can't unpack a directory
1164 * without recursing into it, so we return.
1166 if (!o
->src_index
->sparse_index
)
1169 /* Find first entry with a real name (we could use "mask" too) */
1174 * If the directory is completely missing from the index but
1175 * would otherwise be a sparse directory, we should unpack it.
1176 * If not, we'll return and continue recursively traversing the
1179 *is_new_sparse_dir
= entry_is_new_sparse_dir(info
, p
);
1180 if (!*is_new_sparse_dir
)
1185 * When we are unpacking a sparse directory, then this isn't necessarily
1186 * a directory-file conflict.
1188 if (mask
== dirmask
&&
1189 (*is_new_sparse_dir
|| (src
[0] && S_ISSPARSEDIR(src
[0]->ce_mode
))))
1193 * Ok, we've filled in up to any potential index entry in src[0],
1196 for (i
= 0; i
< n
; i
++) {
1198 unsigned int bit
= 1ul << i
;
1199 if (conflicts
& bit
) {
1200 src
[i
+ o
->merge
] = o
->df_conflict_entry
;
1207 else if (i
+ 1 < o
->head_idx
)
1209 else if (i
+ 1 > o
->head_idx
)
1215 * If the merge bit is set, then the cache entries are
1216 * discarded in the following block. In this case,
1217 * construct "transient" cache_entries, as they are
1218 * not stored in the index. otherwise construct the
1219 * cache entry from the index aware logic.
1221 src
[i
+ o
->merge
] = create_ce_entry(info
, names
+ i
, stage
,
1222 &o
->internal
.result
,
1223 o
->merge
, bit
& dirmask
);
1227 int rc
= call_unpack_fn((const struct cache_entry
* const *)src
,
1229 for (i
= 0; i
< n
; i
++) {
1230 struct cache_entry
*ce
= src
[i
+ o
->merge
];
1231 if (ce
!= o
->df_conflict_entry
)
1232 discard_cache_entry(ce
);
1237 for (i
= 0; i
< n
; i
++)
1238 if (src
[i
] && src
[i
] != o
->df_conflict_entry
)
1239 if (do_add_entry(o
, src
[i
], 0, 0))
1245 static int unpack_failed(struct unpack_trees_options
*o
, const char *message
)
1247 discard_index(&o
->internal
.result
);
1248 if (!o
->quiet
&& !o
->exiting_early
) {
1250 return error("%s", message
);
1257 * The tree traversal is looking at name p. If we have a matching entry,
1258 * return it. If name p is a directory in the index, do not return
1259 * anything, as we will want to match it when the traversal descends into
1262 static int find_cache_pos(struct traverse_info
*info
,
1263 const char *p
, size_t p_len
)
1266 struct unpack_trees_options
*o
= info
->data
;
1267 struct index_state
*index
= o
->src_index
;
1268 int pfxlen
= info
->pathlen
;
1270 for (pos
= o
->internal
.cache_bottom
; pos
< index
->cache_nr
; pos
++) {
1271 const struct cache_entry
*ce
= index
->cache
[pos
];
1272 const char *ce_name
, *ce_slash
;
1275 if (ce
->ce_flags
& CE_UNPACKED
) {
1277 * cache_bottom entry is already unpacked, so
1278 * we can never match it; don't check it
1281 if (pos
== o
->internal
.cache_bottom
)
1282 ++o
->internal
.cache_bottom
;
1285 if (!ce_in_traverse_path(ce
, info
)) {
1287 * Check if we can skip future cache checks
1288 * (because we're already past all possible
1289 * entries in the traverse path).
1291 if (info
->traverse_path
) {
1292 if (strncmp(ce
->name
, info
->traverse_path
,
1298 ce_name
= ce
->name
+ pfxlen
;
1299 ce_slash
= strchr(ce_name
, '/');
1301 ce_len
= ce_slash
- ce_name
;
1303 ce_len
= ce_namelen(ce
) - pfxlen
;
1304 cmp
= name_compare(p
, p_len
, ce_name
, ce_len
);
1306 * Exact match; if we have a directory we need to
1307 * delay returning it.
1310 return ce_slash
? -2 - pos
: pos
;
1312 continue; /* keep looking */
1314 * ce_name sorts after p->path; could it be that we
1315 * have files under p->path directory in the index?
1316 * E.g. ce_name == "t-i", and p->path == "t"; we may
1317 * have "t/a" in the index.
1319 if (p_len
< ce_len
&& !memcmp(ce_name
, p
, p_len
) &&
1320 ce_name
[p_len
] < '/')
1321 continue; /* keep looking */
1328 * Given a sparse directory entry 'ce', compare ce->name to
1329 * info->traverse_path + p->path + '/' if info->traverse_path
1332 * Compare ce->name to p->path + '/' otherwise. Note that
1333 * ce->name must end in a trailing '/' because it is a sparse
1336 static int sparse_dir_matches_path(const struct cache_entry
*ce
,
1337 struct traverse_info
*info
,
1338 const struct name_entry
*p
)
1340 assert(S_ISSPARSEDIR(ce
->ce_mode
));
1341 assert(ce
->name
[ce
->ce_namelen
- 1] == '/');
1344 return ce
->ce_namelen
== info
->pathlen
+ p
->pathlen
+ 1 &&
1345 ce
->name
[info
->pathlen
- 1] == '/' &&
1346 !strncmp(ce
->name
, info
->traverse_path
, info
->pathlen
) &&
1347 !strncmp(ce
->name
+ info
->pathlen
, p
->path
, p
->pathlen
);
1348 return ce
->ce_namelen
== p
->pathlen
+ 1 &&
1349 !strncmp(ce
->name
, p
->path
, p
->pathlen
);
1352 static struct cache_entry
*find_cache_entry(struct traverse_info
*info
,
1353 const struct name_entry
*p
)
1356 int pos
= find_cache_pos(info
, p
->path
, p
->pathlen
);
1357 struct unpack_trees_options
*o
= info
->data
;
1360 return o
->src_index
->cache
[pos
];
1363 * Check for a sparse-directory entry named "path/".
1364 * Due to the input p->path not having a trailing
1365 * slash, the negative 'pos' value overshoots the
1366 * expected position, hence "-2" instead of "-1".
1370 if (pos
< 0 || pos
>= o
->src_index
->cache_nr
)
1374 * Due to lexicographic sorting and sparse directory
1375 * entries ending with a trailing slash, our path as a
1376 * sparse directory (e.g "subdir/") and our path as a
1377 * file (e.g. "subdir") might be separated by other
1378 * paths (e.g. "subdir-").
1381 struct cache_entry
*ce
= o
->src_index
->cache
[pos
];
1383 if (!skip_prefix(ce
->name
, info
->traverse_path
, &path
) ||
1384 strncmp(path
, p
->path
, p
->pathlen
) ||
1385 path
[p
->pathlen
] != '/')
1388 if (S_ISSPARSEDIR(ce
->ce_mode
) &&
1389 sparse_dir_matches_path(ce
, info
, p
))
1398 static void debug_path(struct traverse_info
*info
)
1401 debug_path(info
->prev
);
1402 if (*info
->prev
->name
)
1405 printf("%s", info
->name
);
1408 static void debug_name_entry(int i
, struct name_entry
*n
)
1410 printf("ent#%d %06o %s\n", i
,
1411 n
->path
? n
->mode
: 0,
1412 n
->path
? n
->path
: "(missing)");
1415 static void debug_unpack_callback(int n
,
1417 unsigned long dirmask
,
1418 struct name_entry
*names
,
1419 struct traverse_info
*info
)
1422 printf("* unpack mask %lu, dirmask %lu, cnt %d ",
1426 for (i
= 0; i
< n
; i
++)
1427 debug_name_entry(i
, names
+ i
);
1431 * Returns true if and only if the given cache_entry is a
1432 * sparse-directory entry that matches the given name_entry
1433 * from the tree walk at the given traverse_info.
1435 static int is_sparse_directory_entry(struct cache_entry
*ce
,
1436 const struct name_entry
*name
,
1437 struct traverse_info
*info
)
1439 if (!ce
|| !name
|| !S_ISSPARSEDIR(ce
->ce_mode
))
1442 return sparse_dir_matches_path(ce
, info
, name
);
1445 static int unpack_sparse_callback(int n
, unsigned long mask
, unsigned long dirmask
, struct name_entry
*names
, struct traverse_info
*info
)
1447 struct cache_entry
*src
[MAX_UNPACK_TREES
+ 1] = { NULL
, };
1448 struct unpack_trees_options
*o
= info
->data
;
1449 int ret
, is_new_sparse_dir
;
1454 * Unlike in 'unpack_callback', where src[0] is derived from the index when
1455 * merging, src[0] is a transient cache entry derived from the first tree
1456 * provided. Create the temporary entry as if it came from a non-sparse index.
1458 if (!is_null_oid(&names
[0].oid
)) {
1459 src
[0] = create_ce_entry(info
, &names
[0], 0,
1460 &o
->internal
.result
, 1,
1461 dirmask
& (1ul << 0));
1462 src
[0]->ce_flags
|= (CE_SKIP_WORKTREE
| CE_NEW_SKIP_WORKTREE
);
1466 * 'unpack_single_entry' assumes that src[0] is derived directly from
1467 * the index, rather than from an entry in 'names'. This is *not* true when
1468 * merging a sparse directory, in which case names[0] is the "index" source
1469 * entry. To match the expectations of 'unpack_single_entry', shift past the
1470 * "index" tree (i.e., names[0]) and adjust 'names', 'n', 'mask', and
1471 * 'dirmask' accordingly.
1473 ret
= unpack_single_entry(n
- 1, mask
>> 1, dirmask
>> 1, src
, names
+ 1, info
, &is_new_sparse_dir
);
1476 discard_cache_entry(src
[0]);
1478 return ret
>= 0 ? mask
: -1;
1482 * Note that traverse_by_cache_tree() duplicates some logic in this function
1483 * without actually calling it. If you change the logic here you may need to
1484 * check and change there as well.
1486 static int unpack_callback(int n
, unsigned long mask
, unsigned long dirmask
, struct name_entry
*names
, struct traverse_info
*info
)
1488 struct cache_entry
*src
[MAX_UNPACK_TREES
+ 1] = { NULL
, };
1489 struct unpack_trees_options
*o
= info
->data
;
1490 const struct name_entry
*p
= names
;
1491 int is_new_sparse_dir
;
1493 /* Find first entry with a real name (we could use "mask" too) */
1497 if (o
->internal
.debug_unpack
)
1498 debug_unpack_callback(n
, mask
, dirmask
, names
, info
);
1500 /* Are we supposed to look at the index too? */
1504 struct cache_entry
*ce
;
1506 if (o
->diff_index_cached
)
1507 ce
= next_cache_entry(o
);
1509 ce
= find_cache_entry(info
, p
);
1513 cmp
= compare_entry(ce
, info
, p
);
1515 if (unpack_index_entry(ce
, o
) < 0)
1516 return unpack_failed(o
, NULL
);
1522 * If we skip unmerged index
1523 * entries, we'll skip this
1524 * entry *and* the tree
1525 * entries associated with it!
1527 if (o
->skip_unmerged
) {
1528 add_same_unmerged(ce
, o
);
1538 if (unpack_single_entry(n
, mask
, dirmask
, src
, names
, info
, &is_new_sparse_dir
))
1541 if (o
->merge
&& src
[0]) {
1542 if (ce_stage(src
[0]))
1543 mark_ce_used_same_name(src
[0], o
);
1545 mark_ce_used(src
[0], o
);
1548 /* Now handle any directories.. */
1550 /* special case: "diff-index --cached" looking at a tree */
1551 if (o
->diff_index_cached
&&
1552 n
== 1 && dirmask
== 1 && S_ISDIR(names
->mode
)) {
1554 matches
= cache_tree_matches_traversal(o
->src_index
->cache_tree
,
1557 * Everything under the name matches; skip the
1558 * entire hierarchy. diff_index_cached codepath
1559 * special cases D/F conflicts in such a way that
1560 * it does not do any look-ahead, so this is safe.
1564 * Only increment the cache_bottom if the
1565 * directory isn't a sparse directory index
1566 * entry (if it is, it was already incremented)
1567 * in 'mark_ce_used()'
1569 if (!src
[0] || !S_ISSPARSEDIR(src
[0]->ce_mode
))
1570 o
->internal
.cache_bottom
+= matches
;
1575 if (!is_sparse_directory_entry(src
[0], p
, info
) &&
1576 !is_new_sparse_dir
&&
1577 traverse_trees_recursive(n
, dirmask
, mask
& ~dirmask
,
1588 static int clear_ce_flags_1(struct index_state
*istate
,
1589 struct cache_entry
**cache
, int nr
,
1590 struct strbuf
*prefix
,
1591 int select_mask
, int clear_mask
,
1592 struct pattern_list
*pl
,
1593 enum pattern_match_result default_match
,
1596 /* Whole directory matching */
1597 static int clear_ce_flags_dir(struct index_state
*istate
,
1598 struct cache_entry
**cache
, int nr
,
1599 struct strbuf
*prefix
,
1601 int select_mask
, int clear_mask
,
1602 struct pattern_list
*pl
,
1603 enum pattern_match_result default_match
,
1606 struct cache_entry
**cache_end
;
1609 enum pattern_match_result ret
, orig_ret
;
1610 orig_ret
= path_matches_pattern_list(prefix
->buf
, prefix
->len
,
1611 basename
, &dtype
, pl
, istate
);
1613 strbuf_addch(prefix
, '/');
1615 /* If undecided, use matching result of parent dir in defval */
1616 if (orig_ret
== UNDECIDED
)
1617 ret
= default_match
;
1621 for (cache_end
= cache
; cache_end
!= cache
+ nr
; cache_end
++) {
1622 struct cache_entry
*ce
= *cache_end
;
1623 if (strncmp(ce
->name
, prefix
->buf
, prefix
->len
))
1627 if (pl
->use_cone_patterns
&& orig_ret
== MATCHED_RECURSIVE
) {
1628 struct cache_entry
**ce
= cache
;
1629 rc
= cache_end
- cache
;
1631 while (ce
< cache_end
) {
1632 (*ce
)->ce_flags
&= ~clear_mask
;
1635 } else if (pl
->use_cone_patterns
&& orig_ret
== NOT_MATCHED
) {
1636 rc
= cache_end
- cache
;
1638 rc
= clear_ce_flags_1(istate
, cache
, cache_end
- cache
,
1640 select_mask
, clear_mask
,
1645 strbuf_setlen(prefix
, prefix
->len
- 1);
1650 * Traverse the index, find every entry that matches according to
1651 * o->pl. Do "ce_flags &= ~clear_mask" on those entries. Return the
1652 * number of traversed entries.
1654 * If select_mask is non-zero, only entries whose ce_flags has on of
1655 * those bits enabled are traversed.
1657 * cache : pointer to an index entry
1658 * prefix_len : an offset to its path
1660 * The current path ("prefix") including the trailing '/' is
1661 * cache[0]->name[0..(prefix_len-1)]
1662 * Top level path has prefix_len zero.
1664 static int clear_ce_flags_1(struct index_state
*istate
,
1665 struct cache_entry
**cache
, int nr
,
1666 struct strbuf
*prefix
,
1667 int select_mask
, int clear_mask
,
1668 struct pattern_list
*pl
,
1669 enum pattern_match_result default_match
,
1672 struct cache_entry
**cache_end
= nr
? cache
+ nr
: cache
;
1675 * Process all entries that have the given prefix and meet
1676 * select_mask condition
1678 while(cache
!= cache_end
) {
1679 struct cache_entry
*ce
= *cache
;
1680 const char *name
, *slash
;
1682 enum pattern_match_result ret
;
1684 display_progress(istate
->progress
, progress_nr
);
1686 if (select_mask
&& !(ce
->ce_flags
& select_mask
)) {
1692 if (prefix
->len
&& strncmp(ce
->name
, prefix
->buf
, prefix
->len
))
1695 name
= ce
->name
+ prefix
->len
;
1696 slash
= strchr(name
, '/');
1698 /* If it's a directory, try whole directory match first */
1703 strbuf_add(prefix
, name
, len
);
1705 processed
= clear_ce_flags_dir(istate
, cache
, cache_end
- cache
,
1707 prefix
->buf
+ prefix
->len
- len
,
1708 select_mask
, clear_mask
,
1712 /* clear_c_f_dir eats a whole dir already? */
1715 progress_nr
+= processed
;
1716 strbuf_setlen(prefix
, prefix
->len
- len
);
1720 strbuf_addch(prefix
, '/');
1721 processed
= clear_ce_flags_1(istate
, cache
, cache_end
- cache
,
1723 select_mask
, clear_mask
, pl
,
1724 default_match
, progress_nr
);
1727 progress_nr
+= processed
;
1729 strbuf_setlen(prefix
, prefix
->len
- len
- 1);
1734 dtype
= ce_to_dtype(ce
);
1735 ret
= path_matches_pattern_list(ce
->name
,
1737 name
, &dtype
, pl
, istate
);
1738 if (ret
== UNDECIDED
)
1739 ret
= default_match
;
1740 if (ret
== MATCHED
|| ret
== MATCHED_RECURSIVE
)
1741 ce
->ce_flags
&= ~clear_mask
;
1746 display_progress(istate
->progress
, progress_nr
);
1747 return nr
- (cache_end
- cache
);
1750 static int clear_ce_flags(struct index_state
*istate
,
1751 int select_mask
, int clear_mask
,
1752 struct pattern_list
*pl
,
1755 static struct strbuf prefix
= STRBUF_INIT
;
1759 strbuf_reset(&prefix
);
1761 istate
->progress
= start_delayed_progress(
1762 _("Updating index flags"),
1765 xsnprintf(label
, sizeof(label
), "clear_ce_flags(0x%08lx,0x%08lx)",
1766 (unsigned long)select_mask
, (unsigned long)clear_mask
);
1767 trace2_region_enter("unpack_trees", label
, the_repository
);
1768 rval
= clear_ce_flags_1(istate
,
1772 select_mask
, clear_mask
,
1774 trace2_region_leave("unpack_trees", label
, the_repository
);
1776 stop_progress(&istate
->progress
);
1781 * Set/Clear CE_NEW_SKIP_WORKTREE according to $GIT_DIR/info/sparse-checkout
1783 static void mark_new_skip_worktree(struct pattern_list
*pl
,
1784 struct index_state
*istate
,
1785 int select_flag
, int skip_wt_flag
,
1791 * 1. Pretend the narrowest worktree: only unmerged entries
1794 for (i
= 0; i
< istate
->cache_nr
; i
++) {
1795 struct cache_entry
*ce
= istate
->cache
[i
];
1797 if (select_flag
&& !(ce
->ce_flags
& select_flag
))
1800 if (!ce_stage(ce
) && !(ce
->ce_flags
& CE_CONFLICTED
))
1801 ce
->ce_flags
|= skip_wt_flag
;
1803 ce
->ce_flags
&= ~skip_wt_flag
;
1807 * 2. Widen worktree according to sparse-checkout file.
1808 * Matched entries will have skip_wt_flag cleared (i.e. "in")
1810 clear_ce_flags(istate
, select_flag
, skip_wt_flag
, pl
, show_progress
);
1813 static void populate_from_existing_patterns(struct unpack_trees_options
*o
,
1814 struct pattern_list
*pl
)
1816 if (get_sparse_checkout_patterns(pl
) < 0)
1817 o
->skip_sparse_checkout
= 1;
1819 o
->internal
.pl
= pl
;
1822 static void update_sparsity_for_prefix(const char *prefix
,
1823 struct index_state
*istate
)
1825 int prefix_len
= strlen(prefix
);
1826 struct strbuf ce_prefix
= STRBUF_INIT
;
1828 if (!istate
->sparse_index
)
1831 while (prefix_len
> 0 && prefix
[prefix_len
- 1] == '/')
1834 if (prefix_len
<= 0)
1835 BUG("Invalid prefix passed to update_sparsity_for_prefix");
1837 strbuf_grow(&ce_prefix
, prefix_len
+ 1);
1838 strbuf_add(&ce_prefix
, prefix
, prefix_len
);
1839 strbuf_addch(&ce_prefix
, '/');
1842 * If the prefix points to a sparse directory or a path inside a sparse
1843 * directory, the index should be expanded. This is accomplished in one
1845 * - if the prefix is inside a sparse directory, it will be expanded by
1846 * the 'ensure_full_index(...)' call in 'index_name_pos(...)'.
1847 * - if the prefix matches an existing sparse directory entry,
1848 * 'index_name_pos(...)' will return its index position, triggering
1849 * the 'ensure_full_index(...)' below.
1851 if (!path_in_cone_mode_sparse_checkout(ce_prefix
.buf
, istate
) &&
1852 index_name_pos(istate
, ce_prefix
.buf
, ce_prefix
.len
) >= 0)
1853 ensure_full_index(istate
);
1855 strbuf_release(&ce_prefix
);
1858 static int verify_absent(const struct cache_entry
*,
1859 enum unpack_trees_error_types
,
1860 struct unpack_trees_options
*);
1862 * N-way merge "len" trees. Returns 0 on success, -1 on failure to manipulate the
1863 * resulting index, -2 on failure to reflect the changes to the work tree.
1865 * CE_ADDED, CE_UNPACKED and CE_NEW_SKIP_WORKTREE are used internally
1867 int unpack_trees(unsigned len
, struct tree_desc
*t
, struct unpack_trees_options
*o
)
1869 struct repository
*repo
= the_repository
;
1871 static struct cache_entry
*dfc
;
1872 struct pattern_list pl
;
1873 int free_pattern_list
= 0;
1874 struct dir_struct dir
= DIR_INIT
;
1876 if (o
->reset
== UNPACK_RESET_INVALID
)
1877 BUG("o->reset had a value of 1; should be UNPACK_TREES_*_UNTRACKED");
1879 if (len
> MAX_UNPACK_TREES
)
1880 die("unpack_trees takes at most %d trees", MAX_UNPACK_TREES
);
1881 if (o
->internal
.dir
)
1882 BUG("o->internal.dir is for internal use only");
1884 BUG("o->internal.pl is for internal use only");
1885 if (o
->df_conflict_entry
)
1886 BUG("o->df_conflict_entry is an output only field");
1888 trace_performance_enter();
1889 trace2_region_enter("unpack_trees", "unpack_trees", the_repository
);
1891 prepare_repo_settings(repo
);
1892 if (repo
->settings
.command_requires_full_index
) {
1893 ensure_full_index(o
->src_index
);
1895 ensure_full_index(o
->dst_index
);
1898 if (o
->reset
== UNPACK_RESET_OVERWRITE_UNTRACKED
&&
1899 o
->preserve_ignored
)
1900 BUG("UNPACK_RESET_OVERWRITE_UNTRACKED incompatible with preserved ignored files");
1902 if (!o
->preserve_ignored
) {
1903 o
->internal
.dir
= &dir
;
1904 o
->internal
.dir
->flags
|= DIR_SHOW_IGNORED
;
1905 setup_standard_excludes(o
->internal
.dir
);
1909 update_sparsity_for_prefix(o
->prefix
, o
->src_index
);
1911 if (!core_apply_sparse_checkout
|| !o
->update
)
1912 o
->skip_sparse_checkout
= 1;
1913 if (!o
->skip_sparse_checkout
) {
1914 memset(&pl
, 0, sizeof(pl
));
1915 free_pattern_list
= 1;
1916 populate_from_existing_patterns(o
, &pl
);
1919 index_state_init(&o
->internal
.result
, o
->src_index
->repo
);
1920 o
->internal
.result
.initialized
= 1;
1921 o
->internal
.result
.timestamp
.sec
= o
->src_index
->timestamp
.sec
;
1922 o
->internal
.result
.timestamp
.nsec
= o
->src_index
->timestamp
.nsec
;
1923 o
->internal
.result
.version
= o
->src_index
->version
;
1924 if (!o
->src_index
->split_index
) {
1925 o
->internal
.result
.split_index
= NULL
;
1926 } else if (o
->src_index
== o
->dst_index
) {
1928 * o->dst_index (and thus o->src_index) will be discarded
1929 * and overwritten with o->internal.result at the end of
1930 * this function, so just use src_index's split_index to
1931 * avoid having to create a new one.
1933 o
->internal
.result
.split_index
= o
->src_index
->split_index
;
1934 o
->internal
.result
.split_index
->refcount
++;
1936 o
->internal
.result
.split_index
=
1937 init_split_index(&o
->internal
.result
);
1939 oidcpy(&o
->internal
.result
.oid
, &o
->src_index
->oid
);
1940 o
->internal
.merge_size
= len
;
1941 mark_all_ce_unused(o
->src_index
);
1943 o
->internal
.result
.fsmonitor_last_update
=
1944 xstrdup_or_null(o
->src_index
->fsmonitor_last_update
);
1945 o
->internal
.result
.fsmonitor_has_run_once
= o
->src_index
->fsmonitor_has_run_once
;
1947 if (!o
->src_index
->initialized
&&
1948 !repo
->settings
.command_requires_full_index
&&
1949 is_sparse_index_allowed(&o
->internal
.result
, 0))
1950 o
->internal
.result
.sparse_index
= 1;
1953 * Sparse checkout loop #1: set NEW_SKIP_WORKTREE on existing entries
1955 if (!o
->skip_sparse_checkout
)
1956 mark_new_skip_worktree(o
->internal
.pl
, o
->src_index
, 0,
1957 CE_NEW_SKIP_WORKTREE
, o
->verbose_update
);
1960 dfc
= xcalloc(1, cache_entry_size(0));
1961 o
->df_conflict_entry
= dfc
;
1964 const char *prefix
= o
->prefix
? o
->prefix
: "";
1965 struct traverse_info info
;
1967 setup_traverse_info(&info
, prefix
);
1968 info
.fn
= unpack_callback
;
1970 info
.show_all_errors
= o
->internal
.show_all_errors
;
1971 info
.pathspec
= o
->pathspec
;
1975 * Unpack existing index entries that sort before the
1976 * prefix the tree is spliced into. Note that o->merge
1977 * is always true in this case.
1980 struct cache_entry
*ce
= next_cache_entry(o
);
1983 if (ce_in_traverse_path(ce
, &info
))
1985 if (unpack_index_entry(ce
, o
) < 0)
1990 trace_performance_enter();
1991 trace2_region_enter("unpack_trees", "traverse_trees", the_repository
);
1992 ret
= traverse_trees(o
->src_index
, len
, t
, &info
);
1993 trace2_region_leave("unpack_trees", "traverse_trees", the_repository
);
1994 trace_performance_leave("traverse_trees");
1999 /* Any left-over entries in the index? */
2002 struct cache_entry
*ce
= next_cache_entry(o
);
2005 if (unpack_index_entry(ce
, o
) < 0)
2009 mark_all_ce_unused(o
->src_index
);
2011 if (o
->trivial_merges_only
&& o
->internal
.nontrivial_merge
) {
2012 ret
= unpack_failed(o
, "Merge requires file-level merging");
2016 if (!o
->skip_sparse_checkout
) {
2018 * Sparse checkout loop #2: set NEW_SKIP_WORKTREE on entries not in loop #1
2019 * If they will have NEW_SKIP_WORKTREE, also set CE_SKIP_WORKTREE
2020 * so apply_sparse_checkout() won't attempt to remove it from worktree
2022 mark_new_skip_worktree(o
->internal
.pl
, &o
->internal
.result
,
2023 CE_ADDED
, CE_SKIP_WORKTREE
| CE_NEW_SKIP_WORKTREE
,
2027 for (i
= 0; i
< o
->internal
.result
.cache_nr
; i
++) {
2028 struct cache_entry
*ce
= o
->internal
.result
.cache
[i
];
2031 * Entries marked with CE_ADDED in merged_entry() do not have
2032 * verify_absent() check (the check is effectively disabled
2033 * because CE_NEW_SKIP_WORKTREE is set unconditionally).
2035 * Do the real check now because we have had
2036 * correct CE_NEW_SKIP_WORKTREE
2038 if (ce
->ce_flags
& CE_ADDED
&&
2039 verify_absent(ce
, WARNING_SPARSE_ORPHANED_NOT_OVERWRITTEN
, o
))
2042 if (apply_sparse_checkout(&o
->internal
.result
, ce
, o
))
2047 * Inability to sparsify or de-sparsify individual
2048 * paths is not an error, but just a warning.
2050 if (o
->internal
.show_all_errors
)
2051 display_warning_msgs(o
);
2056 ret
= check_updates(o
, &o
->internal
.result
) ? (-2) : 0;
2058 move_index_extensions(&o
->internal
.result
, o
->src_index
);
2060 if (git_env_bool("GIT_TEST_CHECK_CACHE_TREE", 0))
2061 cache_tree_verify(the_repository
,
2062 &o
->internal
.result
);
2063 if (!o
->skip_cache_tree_update
&&
2064 !cache_tree_fully_valid(o
->internal
.result
.cache_tree
))
2065 cache_tree_update(&o
->internal
.result
,
2070 o
->internal
.result
.updated_workdir
= 1;
2071 discard_index(o
->dst_index
);
2072 *o
->dst_index
= o
->internal
.result
;
2074 discard_index(&o
->internal
.result
);
2076 o
->src_index
= NULL
;
2079 if (free_pattern_list
)
2080 clear_pattern_list(&pl
);
2081 if (o
->internal
.dir
) {
2082 dir_clear(o
->internal
.dir
);
2083 o
->internal
.dir
= NULL
;
2085 trace2_region_leave("unpack_trees", "unpack_trees", the_repository
);
2086 trace_performance_leave("unpack_trees");
2090 if (o
->internal
.show_all_errors
)
2091 display_error_msgs(o
);
2092 mark_all_ce_unused(o
->src_index
);
2093 ret
= unpack_failed(o
, NULL
);
2094 if (o
->exiting_early
)
2100 * Update SKIP_WORKTREE bits according to sparsity patterns, and update
2101 * working directory to match.
2103 * CE_NEW_SKIP_WORKTREE is used internally.
2105 enum update_sparsity_result
update_sparsity(struct unpack_trees_options
*o
,
2106 struct pattern_list
*pl
)
2108 enum update_sparsity_result ret
= UPDATE_SPARSITY_SUCCESS
;
2110 unsigned old_show_all_errors
;
2111 int free_pattern_list
= 0;
2113 old_show_all_errors
= o
->internal
.show_all_errors
;
2114 o
->internal
.show_all_errors
= 1;
2115 index_state_init(&o
->internal
.result
, o
->src_index
->repo
);
2118 if (!o
->update
|| o
->index_only
|| o
->skip_sparse_checkout
)
2119 BUG("update_sparsity() is for reflecting sparsity patterns in working directory");
2120 if (o
->src_index
!= o
->dst_index
|| o
->fn
)
2121 BUG("update_sparsity() called wrong");
2123 trace_performance_enter();
2125 /* If we weren't given patterns, use the recorded ones */
2127 free_pattern_list
= 1;
2128 pl
= xcalloc(1, sizeof(*pl
));
2129 populate_from_existing_patterns(o
, pl
);
2131 o
->internal
.pl
= pl
;
2133 /* Expand sparse directories as needed */
2134 expand_index(o
->src_index
, o
->internal
.pl
);
2136 /* Set NEW_SKIP_WORKTREE on existing entries. */
2137 mark_all_ce_unused(o
->src_index
);
2138 mark_new_skip_worktree(o
->internal
.pl
, o
->src_index
, 0,
2139 CE_NEW_SKIP_WORKTREE
, o
->verbose_update
);
2141 /* Then loop over entries and update/remove as needed */
2142 ret
= UPDATE_SPARSITY_SUCCESS
;
2143 for (i
= 0; i
< o
->src_index
->cache_nr
; i
++) {
2144 struct cache_entry
*ce
= o
->src_index
->cache
[i
];
2148 /* -1 because for loop will increment by 1 */
2149 i
+= warn_conflicted_path(o
->src_index
, i
, o
) - 1;
2150 ret
= UPDATE_SPARSITY_WARNINGS
;
2154 if (apply_sparse_checkout(o
->src_index
, ce
, o
))
2155 ret
= UPDATE_SPARSITY_WARNINGS
;
2158 if (check_updates(o
, o
->src_index
))
2159 ret
= UPDATE_SPARSITY_WORKTREE_UPDATE_FAILURES
;
2161 display_warning_msgs(o
);
2162 o
->internal
.show_all_errors
= old_show_all_errors
;
2163 if (free_pattern_list
) {
2164 clear_pattern_list(pl
);
2166 o
->internal
.pl
= NULL
;
2168 trace_performance_leave("update_sparsity");
2172 /* Here come the merge functions */
2174 static int reject_merge(const struct cache_entry
*ce
,
2175 struct unpack_trees_options
*o
)
2177 return add_rejected_path(o
, ERROR_WOULD_OVERWRITE
, ce
->name
);
2180 static int same(const struct cache_entry
*a
, const struct cache_entry
*b
)
2186 if ((a
->ce_flags
| b
->ce_flags
) & CE_CONFLICTED
)
2188 return a
->ce_mode
== b
->ce_mode
&&
2189 oideq(&a
->oid
, &b
->oid
);
2194 * When a CE gets turned into an unmerged entry, we
2195 * want it to be up-to-date
2197 static int verify_uptodate_1(const struct cache_entry
*ce
,
2198 struct unpack_trees_options
*o
,
2199 enum unpack_trees_error_types error_type
)
2207 * CE_VALID and CE_SKIP_WORKTREE cheat, we better check again
2208 * if this entry is truly up-to-date because this file may be
2211 if ((ce
->ce_flags
& CE_VALID
) || ce_skip_worktree(ce
))
2212 ; /* keep checking */
2213 else if (o
->reset
|| ce_uptodate(ce
))
2216 if (!lstat(ce
->name
, &st
)) {
2217 int flags
= CE_MATCH_IGNORE_VALID
|CE_MATCH_IGNORE_SKIP_WORKTREE
;
2218 unsigned changed
= ie_match_stat(o
->src_index
, ce
, &st
, flags
);
2220 if (submodule_from_ce(ce
)) {
2221 int r
= check_submodule_move_head(ce
,
2222 "HEAD", oid_to_hex(&ce
->oid
), o
);
2224 return add_rejected_path(o
, error_type
, ce
->name
);
2231 * Historic default policy was to allow submodule to be out
2232 * of sync wrt the superproject index. If the submodule was
2233 * not considered interesting above, we don't care here.
2235 if (S_ISGITLINK(ce
->ce_mode
))
2240 if (errno
== ENOENT
)
2242 return add_rejected_path(o
, error_type
, ce
->name
);
2245 int verify_uptodate(const struct cache_entry
*ce
,
2246 struct unpack_trees_options
*o
)
2248 if (!o
->skip_sparse_checkout
&&
2249 (ce
->ce_flags
& CE_SKIP_WORKTREE
) &&
2250 (ce
->ce_flags
& CE_NEW_SKIP_WORKTREE
))
2252 return verify_uptodate_1(ce
, o
, ERROR_NOT_UPTODATE_FILE
);
2255 static int verify_uptodate_sparse(const struct cache_entry
*ce
,
2256 struct unpack_trees_options
*o
)
2258 return verify_uptodate_1(ce
, o
, WARNING_SPARSE_NOT_UPTODATE_FILE
);
2262 * TODO: We should actually invalidate o->internal.result, not src_index [1].
2263 * But since cache tree and untracked cache both are not copied to
2264 * o->internal.result until unpacking is complete, we invalidate them on
2265 * src_index instead with the assumption that they will be copied to
2266 * dst_index at the end.
2268 * [1] src_index->cache_tree is also used in unpack_callback() so if
2269 * we invalidate o->internal.result, we need to update it to use
2270 * o->internal.result.cache_tree as well.
2272 static void invalidate_ce_path(const struct cache_entry
*ce
,
2273 struct unpack_trees_options
*o
)
2277 cache_tree_invalidate_path(o
->src_index
, ce
->name
);
2278 untracked_cache_invalidate_path(o
->src_index
, ce
->name
, 1);
2282 * Check that checking out ce->sha1 in subdir ce->name is not
2283 * going to overwrite any working files.
2285 static int verify_clean_submodule(const char *old_sha1
,
2286 const struct cache_entry
*ce
,
2287 struct unpack_trees_options
*o
)
2289 if (!submodule_from_ce(ce
))
2292 return check_submodule_move_head(ce
, old_sha1
,
2293 oid_to_hex(&ce
->oid
), o
);
2296 static int verify_clean_subdirectory(const struct cache_entry
*ce
,
2297 struct unpack_trees_options
*o
)
2300 * we are about to extract "ce->name"; we would not want to lose
2301 * anything in the existing directory there.
2305 struct dir_struct d
;
2309 if (S_ISGITLINK(ce
->ce_mode
)) {
2310 struct object_id oid
;
2311 int sub_head
= resolve_gitlink_ref(ce
->name
, "HEAD", &oid
);
2313 * If we are not going to update the submodule, then
2316 if (!sub_head
&& oideq(&oid
, &ce
->oid
))
2318 return verify_clean_submodule(sub_head
? NULL
: oid_to_hex(&oid
),
2323 * First let's make sure we do not have a local modification
2324 * in that directory.
2326 namelen
= ce_namelen(ce
);
2327 for (i
= locate_in_src_index(ce
, o
);
2328 i
< o
->src_index
->cache_nr
;
2330 struct cache_entry
*ce2
= o
->src_index
->cache
[i
];
2331 int len
= ce_namelen(ce2
);
2332 if (len
< namelen
||
2333 strncmp(ce
->name
, ce2
->name
, namelen
) ||
2334 ce2
->name
[namelen
] != '/')
2337 * ce2->name is an entry in the subdirectory to be
2340 if (!ce_stage(ce2
)) {
2341 if (verify_uptodate(ce2
, o
))
2343 add_entry(o
, ce2
, CE_REMOVE
, 0);
2344 invalidate_ce_path(ce
, o
);
2345 mark_ce_used(ce2
, o
);
2350 /* Do not lose a locally present file that is not ignored. */
2351 pathbuf
= xstrfmt("%.*s/", namelen
, ce
->name
);
2353 memset(&d
, 0, sizeof(d
));
2354 if (o
->internal
.dir
)
2355 setup_standard_excludes(&d
);
2356 i
= read_directory(&d
, o
->src_index
, pathbuf
, namelen
+1, NULL
);
2360 return add_rejected_path(o
, ERROR_NOT_UPTODATE_DIR
, ce
->name
);
2362 /* Do not lose startup_info->original_cwd */
2363 if (startup_info
->original_cwd
&&
2364 !strcmp(startup_info
->original_cwd
, ce
->name
))
2365 return add_rejected_path(o
, ERROR_CWD_IN_THE_WAY
, ce
->name
);
2371 * This gets called when there was no index entry for the tree entry 'dst',
2372 * but we found a file in the working tree that 'lstat()' said was fine,
2373 * and we're on a case-insensitive filesystem.
2375 * See if we can find a case-insensitive match in the index that also
2376 * matches the stat information, and assume it's that other file!
2378 static int icase_exists(struct unpack_trees_options
*o
, const char *name
, int len
, struct stat
*st
)
2380 const struct cache_entry
*src
;
2382 src
= index_file_exists(o
->src_index
, name
, len
, 1);
2383 return src
&& !ie_match_stat(o
->src_index
, src
, st
, CE_MATCH_IGNORE_VALID
|CE_MATCH_IGNORE_SKIP_WORKTREE
);
2386 enum absent_checking_type
{
2388 ABSENT_ANY_DIRECTORY
2391 static int check_ok_to_remove(const char *name
, int len
, int dtype
,
2392 const struct cache_entry
*ce
, struct stat
*st
,
2393 enum unpack_trees_error_types error_type
,
2394 enum absent_checking_type absent_type
,
2395 struct unpack_trees_options
*o
)
2397 const struct cache_entry
*result
;
2400 * It may be that the 'lstat()' succeeded even though
2401 * target 'ce' was absent, because there is an old
2402 * entry that is different only in case..
2404 * Ignore that lstat() if it matches.
2406 if (ignore_case
&& icase_exists(o
, name
, len
, st
))
2409 if (o
->internal
.dir
&&
2410 is_excluded(o
->internal
.dir
, o
->src_index
, name
, &dtype
))
2412 * ce->name is explicitly excluded, so it is Ok to
2416 if (S_ISDIR(st
->st_mode
)) {
2418 * We are checking out path "foo" and
2419 * found "foo/." in the working tree.
2420 * This is tricky -- if we have modified
2421 * files that are in "foo/" we would lose
2424 if (verify_clean_subdirectory(ce
, o
) < 0)
2429 /* If we only care about directories, then we can remove */
2430 if (absent_type
== ABSENT_ANY_DIRECTORY
)
2434 * The previous round may already have decided to
2435 * delete this path, which is in a subdirectory that
2436 * is being replaced with a blob.
2438 result
= index_file_exists(&o
->internal
.result
, name
, len
, 0);
2440 if (result
->ce_flags
& CE_REMOVE
)
2444 return add_rejected_path(o
, error_type
, name
);
2448 * We do not want to remove or overwrite a working tree file that
2449 * is not tracked, unless it is ignored.
2451 static int verify_absent_1(const struct cache_entry
*ce
,
2452 enum unpack_trees_error_types error_type
,
2453 enum absent_checking_type absent_type
,
2454 struct unpack_trees_options
*o
)
2459 if (o
->index_only
|| !o
->update
)
2462 if (o
->reset
== UNPACK_RESET_OVERWRITE_UNTRACKED
) {
2463 /* Avoid nuking startup_info->original_cwd... */
2464 if (startup_info
->original_cwd
&&
2465 !strcmp(startup_info
->original_cwd
, ce
->name
))
2466 return add_rejected_path(o
, ERROR_CWD_IN_THE_WAY
,
2468 /* ...but nuke anything else. */
2472 len
= check_leading_path(ce
->name
, ce_namelen(ce
), 0);
2479 path
= xmemdupz(ce
->name
, len
);
2480 if (lstat(path
, &st
))
2481 ret
= error_errno("cannot stat '%s'", path
);
2483 if (submodule_from_ce(ce
))
2484 ret
= check_submodule_move_head(ce
,
2485 oid_to_hex(&ce
->oid
),
2488 ret
= check_ok_to_remove(path
, len
, DT_UNKNOWN
, NULL
,
2494 } else if (lstat(ce
->name
, &st
)) {
2495 if (errno
!= ENOENT
)
2496 return error_errno("cannot stat '%s'", ce
->name
);
2499 if (submodule_from_ce(ce
))
2500 return check_submodule_move_head(ce
, oid_to_hex(&ce
->oid
),
2503 return check_ok_to_remove(ce
->name
, ce_namelen(ce
),
2504 ce_to_dtype(ce
), ce
, &st
,
2505 error_type
, absent_type
, o
);
2509 static int verify_absent(const struct cache_entry
*ce
,
2510 enum unpack_trees_error_types error_type
,
2511 struct unpack_trees_options
*o
)
2513 if (!o
->skip_sparse_checkout
&& (ce
->ce_flags
& CE_NEW_SKIP_WORKTREE
))
2515 return verify_absent_1(ce
, error_type
, COMPLETELY_ABSENT
, o
);
2518 static int verify_absent_if_directory(const struct cache_entry
*ce
,
2519 enum unpack_trees_error_types error_type
,
2520 struct unpack_trees_options
*o
)
2522 if (!o
->skip_sparse_checkout
&& (ce
->ce_flags
& CE_NEW_SKIP_WORKTREE
))
2524 return verify_absent_1(ce
, error_type
, ABSENT_ANY_DIRECTORY
, o
);
2527 static int verify_absent_sparse(const struct cache_entry
*ce
,
2528 enum unpack_trees_error_types error_type
,
2529 struct unpack_trees_options
*o
)
2531 return verify_absent_1(ce
, error_type
, COMPLETELY_ABSENT
, o
);
2534 static int merged_entry(const struct cache_entry
*ce
,
2535 const struct cache_entry
*old
,
2536 struct unpack_trees_options
*o
)
2538 int update
= CE_UPDATE
;
2539 struct cache_entry
*merge
= dup_cache_entry(ce
, &o
->internal
.result
);
2543 * New index entries. In sparse checkout, the following
2544 * verify_absent() will be delayed until after
2545 * traverse_trees() finishes in unpack_trees(), then:
2547 * - CE_NEW_SKIP_WORKTREE will be computed correctly
2548 * - verify_absent() be called again, this time with
2549 * correct CE_NEW_SKIP_WORKTREE
2551 * verify_absent() call here does nothing in sparse
2552 * checkout (i.e. o->skip_sparse_checkout == 0)
2555 merge
->ce_flags
|= CE_NEW_SKIP_WORKTREE
;
2557 if (verify_absent(merge
,
2558 ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN
, o
)) {
2559 discard_cache_entry(merge
);
2562 invalidate_ce_path(merge
, o
);
2564 if (submodule_from_ce(ce
) && file_exists(ce
->name
)) {
2565 int ret
= check_submodule_move_head(ce
, NULL
,
2566 oid_to_hex(&ce
->oid
),
2572 } else if (!(old
->ce_flags
& CE_CONFLICTED
)) {
2574 * See if we can re-use the old CE directly?
2575 * That way we get the uptodate stat info.
2577 * This also removes the UPDATE flag on a match; otherwise
2578 * we will end up overwriting local changes in the work tree.
2580 if (same(old
, merge
)) {
2581 copy_cache_entry(merge
, old
);
2584 if (verify_uptodate(old
, o
)) {
2585 discard_cache_entry(merge
);
2588 /* Migrate old flags over */
2589 update
|= old
->ce_flags
& (CE_SKIP_WORKTREE
| CE_NEW_SKIP_WORKTREE
);
2590 invalidate_ce_path(old
, o
);
2593 if (submodule_from_ce(ce
) && file_exists(ce
->name
)) {
2594 int ret
= check_submodule_move_head(ce
, oid_to_hex(&old
->oid
),
2595 oid_to_hex(&ce
->oid
),
2602 * Previously unmerged entry left as an existence
2603 * marker by read_index_unmerged();
2605 if (verify_absent_if_directory(merge
,
2606 ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN
, o
)) {
2607 discard_cache_entry(merge
);
2611 invalidate_ce_path(old
, o
);
2614 if (do_add_entry(o
, merge
, update
, CE_STAGEMASK
) < 0)
2619 static int merged_sparse_dir(const struct cache_entry
* const *src
, int n
,
2620 struct unpack_trees_options
*o
)
2622 struct tree_desc t
[MAX_UNPACK_TREES
+ 1];
2623 void * tree_bufs
[MAX_UNPACK_TREES
+ 1];
2624 struct traverse_info info
;
2628 * Create the tree traversal information for traversing into *only* the
2631 setup_traverse_info(&info
, src
[0]->name
);
2632 info
.fn
= unpack_sparse_callback
;
2634 info
.show_all_errors
= o
->internal
.show_all_errors
;
2635 info
.pathspec
= o
->pathspec
;
2637 /* Get the tree descriptors of the sparse directory in each of the merging trees */
2638 for (i
= 0; i
< n
; i
++)
2639 tree_bufs
[i
] = fill_tree_descriptor(o
->src_index
->repo
, &t
[i
],
2640 src
[i
] && !is_null_oid(&src
[i
]->oid
) ? &src
[i
]->oid
: NULL
);
2642 ret
= traverse_trees(o
->src_index
, n
, t
, &info
);
2644 for (i
= 0; i
< n
; i
++)
2650 static int deleted_entry(const struct cache_entry
*ce
,
2651 const struct cache_entry
*old
,
2652 struct unpack_trees_options
*o
)
2654 /* Did it exist in the index? */
2656 if (verify_absent(ce
, ERROR_WOULD_LOSE_UNTRACKED_REMOVED
, o
))
2659 } else if (verify_absent_if_directory(ce
, ERROR_WOULD_LOSE_UNTRACKED_REMOVED
, o
)) {
2663 if (!(old
->ce_flags
& CE_CONFLICTED
) && verify_uptodate(old
, o
))
2665 add_entry(o
, ce
, CE_REMOVE
, 0);
2666 invalidate_ce_path(ce
, o
);
2670 static int keep_entry(const struct cache_entry
*ce
,
2671 struct unpack_trees_options
*o
)
2673 add_entry(o
, ce
, 0, 0);
2675 invalidate_ce_path(ce
, o
);
2680 static void show_stage_entry(FILE *o
,
2681 const char *label
, const struct cache_entry
*ce
)
2684 fprintf(o
, "%s (missing)\n", label
);
2686 fprintf(o
, "%s%06o %s %d\t%s\n",
2689 oid_to_hex(&ce
->oid
),
2695 int threeway_merge(const struct cache_entry
* const *stages
,
2696 struct unpack_trees_options
*o
)
2698 const struct cache_entry
*index
;
2699 const struct cache_entry
*head
;
2700 const struct cache_entry
*remote
= stages
[o
->head_idx
+ 1];
2703 int remote_match
= 0;
2705 int df_conflict_head
= 0;
2706 int df_conflict_remote
= 0;
2708 int any_anc_missing
= 0;
2709 int no_anc_exists
= 1;
2712 for (i
= 1; i
< o
->head_idx
; i
++) {
2713 if (!stages
[i
] || stages
[i
] == o
->df_conflict_entry
)
2714 any_anc_missing
= 1;
2720 head
= stages
[o
->head_idx
];
2722 if (head
== o
->df_conflict_entry
) {
2723 df_conflict_head
= 1;
2727 if (remote
== o
->df_conflict_entry
) {
2728 df_conflict_remote
= 1;
2733 * First, if there's a #16 situation, note that to prevent #13
2736 if (!same(remote
, head
)) {
2737 for (i
= 1; i
< o
->head_idx
; i
++) {
2738 if (same(stages
[i
], head
)) {
2741 if (same(stages
[i
], remote
)) {
2748 * We start with cases where the index is allowed to match
2749 * something other than the head: #14(ALT) and #2ALT, where it
2750 * is permitted to match the result instead.
2752 /* #14, #14ALT, #2ALT */
2753 if (remote
&& !df_conflict_head
&& head_match
&& !remote_match
) {
2754 if (index
&& !same(index
, remote
) && !same(index
, head
)) {
2755 if (S_ISSPARSEDIR(index
->ce_mode
))
2756 return merged_sparse_dir(stages
, 4, o
);
2758 return reject_merge(index
, o
);
2760 return merged_entry(remote
, index
, o
);
2763 * If we have an entry in the index cache, then we want to
2764 * make sure that it matches head.
2766 if (index
&& !same(index
, head
)) {
2767 if (S_ISSPARSEDIR(index
->ce_mode
))
2768 return merged_sparse_dir(stages
, 4, o
);
2770 return reject_merge(index
, o
);
2775 if (same(head
, remote
))
2776 return merged_entry(head
, index
, o
);
2778 if (!df_conflict_remote
&& remote_match
&& !head_match
)
2779 return merged_entry(head
, index
, o
);
2783 if (!head
&& !remote
&& any_anc_missing
)
2787 * Under the "aggressive" rule, we resolve mostly trivial
2788 * cases that we historically had git-merge-one-file resolve.
2790 if (o
->aggressive
) {
2791 int head_deleted
= !head
;
2792 int remote_deleted
= !remote
;
2793 const struct cache_entry
*ce
= NULL
;
2802 for (i
= 1; i
< o
->head_idx
; i
++) {
2803 if (stages
[i
] && stages
[i
] != o
->df_conflict_entry
) {
2812 * Deleted in one and unchanged in the other.
2814 if ((head_deleted
&& remote_deleted
) ||
2815 (head_deleted
&& remote
&& remote_match
) ||
2816 (remote_deleted
&& head
&& head_match
)) {
2818 return deleted_entry(index
, index
, o
);
2819 if (ce
&& !head_deleted
) {
2820 if (verify_absent(ce
, ERROR_WOULD_LOSE_UNTRACKED_REMOVED
, o
))
2826 * Added in both, identically.
2828 if (no_anc_exists
&& head
&& remote
&& same(head
, remote
))
2829 return merged_entry(head
, index
, o
);
2833 /* Handle "no merge" cases (see t/t1000-read-tree-m-3way.sh) */
2836 * If we've reached the "no merge" cases and we're merging
2837 * a sparse directory, we may have an "edit/edit" conflict that
2838 * can be resolved by individually merging directory contents.
2840 if (S_ISSPARSEDIR(index
->ce_mode
))
2841 return merged_sparse_dir(stages
, 4, o
);
2844 * If we're not merging a sparse directory, ensure the index is
2845 * up-to-date to avoid files getting overwritten with conflict
2848 if (verify_uptodate(index
, o
))
2852 o
->internal
.nontrivial_merge
= 1;
2854 /* #2, #3, #4, #6, #7, #9, #10, #11. */
2856 if (!head_match
|| !remote_match
) {
2857 for (i
= 1; i
< o
->head_idx
; i
++) {
2858 if (stages
[i
] && stages
[i
] != o
->df_conflict_entry
) {
2859 keep_entry(stages
[i
], o
);
2867 fprintf(stderr
, "read-tree: warning #16 detected\n");
2868 show_stage_entry(stderr
, "head ", stages
[head_match
]);
2869 show_stage_entry(stderr
, "remote ", stages
[remote_match
]);
2872 if (head
) { count
+= keep_entry(head
, o
); }
2873 if (remote
) { count
+= keep_entry(remote
, o
); }
2880 * The rule is to "carry forward" what is in the index without losing
2881 * information across a "fast-forward", favoring a successful merge
2882 * over a merge failure when it makes sense. For details of the
2883 * "carry forward" rule, please see <Documentation/git-read-tree.txt>.
2886 int twoway_merge(const struct cache_entry
* const *src
,
2887 struct unpack_trees_options
*o
)
2889 const struct cache_entry
*current
= src
[0];
2890 const struct cache_entry
*oldtree
= src
[1];
2891 const struct cache_entry
*newtree
= src
[2];
2893 if (o
->internal
.merge_size
!= 2)
2894 return error("Cannot do a twoway merge of %d trees",
2895 o
->internal
.merge_size
);
2897 if (oldtree
== o
->df_conflict_entry
)
2899 if (newtree
== o
->df_conflict_entry
)
2903 if (current
->ce_flags
& CE_CONFLICTED
) {
2904 if (same(oldtree
, newtree
) || o
->reset
) {
2906 return deleted_entry(current
, current
, o
);
2908 return merged_entry(newtree
, current
, o
);
2910 return reject_merge(current
, o
);
2911 } else if ((!oldtree
&& !newtree
) || /* 4 and 5 */
2912 (!oldtree
&& newtree
&&
2913 same(current
, newtree
)) || /* 6 and 7 */
2914 (oldtree
&& newtree
&&
2915 same(oldtree
, newtree
)) || /* 14 and 15 */
2916 (oldtree
&& newtree
&&
2917 !same(oldtree
, newtree
) && /* 18 and 19 */
2918 same(current
, newtree
))) {
2919 return keep_entry(current
, o
);
2920 } else if (oldtree
&& !newtree
&& same(current
, oldtree
)) {
2922 return deleted_entry(oldtree
, current
, o
);
2923 } else if (oldtree
&& newtree
&&
2924 same(current
, oldtree
) && !same(current
, newtree
)) {
2926 return merged_entry(newtree
, current
, o
);
2927 } else if (current
&& !oldtree
&& newtree
&&
2928 S_ISSPARSEDIR(current
->ce_mode
) != S_ISSPARSEDIR(newtree
->ce_mode
) &&
2929 ce_stage(current
) == 0) {
2931 * This case is a directory/file conflict across the sparse-index
2932 * boundary. When we are changing from one path to another via
2933 * 'git checkout', then we want to replace one entry with another
2934 * via merged_entry(). If there are staged changes, then we should
2935 * reject the merge instead.
2937 return merged_entry(newtree
, current
, o
);
2938 } else if (S_ISSPARSEDIR(current
->ce_mode
)) {
2940 * The sparse directories differ, but we don't know whether that's
2941 * because of two different files in the directory being modified
2942 * (can be trivially merged) or if there is a real file conflict.
2943 * Merge the sparse directory by OID to compare file-by-file.
2945 return merged_sparse_dir(src
, 3, o
);
2947 return reject_merge(current
, o
);
2950 if (oldtree
&& !o
->initial_checkout
) {
2952 * deletion of the path was staged;
2954 if (same(oldtree
, newtree
))
2956 return reject_merge(oldtree
, o
);
2958 return merged_entry(newtree
, current
, o
);
2960 return deleted_entry(oldtree
, current
, o
);
2966 * Keep the index entries at stage0, collapse stage1 but make sure
2967 * stage0 does not have anything there.
2969 int bind_merge(const struct cache_entry
* const *src
,
2970 struct unpack_trees_options
*o
)
2972 const struct cache_entry
*old
= src
[0];
2973 const struct cache_entry
*a
= src
[1];
2975 if (o
->internal
.merge_size
!= 1)
2976 return error("Cannot do a bind merge of %d trees",
2977 o
->internal
.merge_size
);
2979 return o
->quiet
? -1 :
2980 error(ERRORMSG(o
, ERROR_BIND_OVERLAP
),
2981 super_prefixed(a
->name
, o
->super_prefix
),
2982 super_prefixed(old
->name
, o
->super_prefix
));
2984 return keep_entry(old
, o
);
2986 return merged_entry(a
, NULL
, o
);
2993 * - take the stat information from stage0, take the data from stage1
2995 int oneway_merge(const struct cache_entry
* const *src
,
2996 struct unpack_trees_options
*o
)
2998 const struct cache_entry
*old
= src
[0];
2999 const struct cache_entry
*a
= src
[1];
3001 if (o
->internal
.merge_size
!= 1)
3002 return error("Cannot do a oneway merge of %d trees",
3003 o
->internal
.merge_size
);
3005 if (!a
|| a
== o
->df_conflict_entry
)
3006 return deleted_entry(old
, old
, o
);
3008 if (old
&& same(old
, a
)) {
3010 if (o
->reset
&& o
->update
&& !ce_uptodate(old
) && !ce_skip_worktree(old
) &&
3011 !(old
->ce_flags
& CE_FSMONITOR_VALID
)) {
3013 if (lstat(old
->name
, &st
) ||
3014 ie_match_stat(o
->src_index
, old
, &st
, CE_MATCH_IGNORE_VALID
|CE_MATCH_IGNORE_SKIP_WORKTREE
))
3015 update
|= CE_UPDATE
;
3017 if (o
->update
&& S_ISGITLINK(old
->ce_mode
) &&
3018 should_update_submodules() && !verify_uptodate(old
, o
))
3019 update
|= CE_UPDATE
;
3020 add_entry(o
, old
, update
, CE_STAGEMASK
);
3023 return merged_entry(a
, old
, o
);
3027 * Merge worktree and untracked entries in a stash entry.
3029 * Ignore all index entries. Collapse remaining trees but make sure that they
3030 * don't have any conflicting files.
3032 int stash_worktree_untracked_merge(const struct cache_entry
* const *src
,
3033 struct unpack_trees_options
*o
)
3035 const struct cache_entry
*worktree
= src
[1];
3036 const struct cache_entry
*untracked
= src
[2];
3038 if (o
->internal
.merge_size
!= 2)
3039 BUG("invalid merge_size: %d", o
->internal
.merge_size
);
3041 if (worktree
&& untracked
)
3042 return error(_("worktree and untracked commit have duplicate entries: %s"),
3043 super_prefixed(worktree
->name
, o
->super_prefix
));
3045 return merged_entry(worktree
? worktree
: untracked
, NULL
, o
);