1 #define USE_THE_REPOSITORY_VARIABLE
3 #include "git-compat-util.h"
6 #include "repository.h"
9 #include "environment.h"
12 #include "name-hash.h"
14 #include "tree-walk.h"
15 #include "cache-tree.h"
16 #include "unpack-trees.h"
20 #include "read-cache.h"
21 #include "split-index.h"
22 #include "sparse-index.h"
23 #include "submodule.h"
24 #include "submodule-config.h"
27 #include "fsmonitor.h"
28 #include "object-store-ll.h"
29 #include "promisor-remote.h"
31 #include "parallel-checkout.h"
35 * Error messages expected by scripts out of plumbing commands such as
36 * read-tree. Non-scripted Porcelain is not required to use these messages
37 * and in fact are encouraged to reword them to better suit their particular
38 * situation better. See how "git checkout" and "git merge" replaces
39 * them using setup_unpack_trees_porcelain(), for example.
41 static const char *unpack_plumbing_errors
[NB_UNPACK_TREES_WARNING_TYPES
] = {
42 /* ERROR_WOULD_OVERWRITE */
43 "Entry '%s' would be overwritten by merge. Cannot merge.",
45 /* ERROR_NOT_UPTODATE_FILE */
46 "Entry '%s' not uptodate. Cannot merge.",
48 /* ERROR_NOT_UPTODATE_DIR */
49 "Updating '%s' would lose untracked files in it",
51 /* ERROR_CWD_IN_THE_WAY */
52 "Refusing to remove '%s' since it is the current working directory.",
54 /* ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN */
55 "Untracked working tree file '%s' would be overwritten by merge.",
57 /* ERROR_WOULD_LOSE_UNTRACKED_REMOVED */
58 "Untracked working tree file '%s' would be removed by merge.",
60 /* ERROR_BIND_OVERLAP */
61 "Entry '%s' overlaps with '%s'. Cannot bind.",
63 /* ERROR_WOULD_LOSE_SUBMODULE */
64 "Submodule '%s' cannot checkout new HEAD.",
66 /* NB_UNPACK_TREES_ERROR_TYPES; just a meta value */
69 /* WARNING_SPARSE_NOT_UPTODATE_FILE */
70 "Path '%s' not uptodate; will not remove from working tree.",
72 /* WARNING_SPARSE_UNMERGED_FILE */
73 "Path '%s' unmerged; will not remove from working tree.",
75 /* WARNING_SPARSE_ORPHANED_NOT_OVERWRITTEN */
76 "Path '%s' already present; will not overwrite with sparse update.",
79 #define ERRORMSG(o,type) \
80 ( ((o) && (o)->internal.msgs[(type)]) \
81 ? ((o)->internal.msgs[(type)]) \
82 : (unpack_plumbing_errors[(type)]) )
84 static const char *super_prefixed(const char *path
, const char *super_prefix
)
87 * It is necessary and sufficient to have two static buffers
88 * here, as the return value of this function is fed to
89 * error() using the unpack_*_errors[] templates we see above.
91 static struct strbuf buf
[2] = {STRBUF_INIT
, STRBUF_INIT
};
92 static int super_prefix_len
= -1;
93 static unsigned idx
= ARRAY_SIZE(buf
) - 1;
95 if (super_prefix_len
< 0) {
100 for (i
= 0; i
< ARRAY_SIZE(buf
); i
++)
101 strbuf_addstr(&buf
[i
], super_prefix
);
102 super_prefix_len
= buf
[0].len
;
106 if (!super_prefix_len
)
109 if (++idx
>= ARRAY_SIZE(buf
))
112 strbuf_setlen(&buf
[idx
], super_prefix_len
);
113 strbuf_addstr(&buf
[idx
], path
);
118 void setup_unpack_trees_porcelain(struct unpack_trees_options
*opts
,
122 const char **msgs
= opts
->internal
.msgs
;
125 strvec_init(&opts
->internal
.msgs_to_free
);
127 if (!strcmp(cmd
, "checkout"))
128 msg
= advice_enabled(ADVICE_COMMIT_BEFORE_MERGE
)
129 ? _("Your local changes to the following files would be overwritten by checkout:\n%%s"
130 "Please commit your changes or stash them before you switch branches.")
131 : _("Your local changes to the following files would be overwritten by checkout:\n%%s");
132 else if (!strcmp(cmd
, "merge"))
133 msg
= advice_enabled(ADVICE_COMMIT_BEFORE_MERGE
)
134 ? _("Your local changes to the following files would be overwritten by merge:\n%%s"
135 "Please commit your changes or stash them before you merge.")
136 : _("Your local changes to the following files would be overwritten by merge:\n%%s");
138 msg
= advice_enabled(ADVICE_COMMIT_BEFORE_MERGE
)
139 ? _("Your local changes to the following files would be overwritten by %s:\n%%s"
140 "Please commit your changes or stash them before you %s.")
141 : _("Your local changes to the following files would be overwritten by %s:\n%%s");
142 msgs
[ERROR_WOULD_OVERWRITE
] = msgs
[ERROR_NOT_UPTODATE_FILE
] =
143 strvec_pushf(&opts
->internal
.msgs_to_free
, msg
, cmd
, cmd
);
145 msgs
[ERROR_NOT_UPTODATE_DIR
] =
146 _("Updating the following directories would lose untracked files in them:\n%s");
148 msgs
[ERROR_CWD_IN_THE_WAY
] =
149 _("Refusing to remove the current working directory:\n%s");
151 if (!strcmp(cmd
, "checkout"))
152 msg
= advice_enabled(ADVICE_COMMIT_BEFORE_MERGE
)
153 ? _("The following untracked working tree files would be removed by checkout:\n%%s"
154 "Please move or remove them before you switch branches.")
155 : _("The following untracked working tree files would be removed by checkout:\n%%s");
156 else if (!strcmp(cmd
, "merge"))
157 msg
= advice_enabled(ADVICE_COMMIT_BEFORE_MERGE
)
158 ? _("The following untracked working tree files would be removed by merge:\n%%s"
159 "Please move or remove them before you merge.")
160 : _("The following untracked working tree files would be removed by merge:\n%%s");
162 msg
= advice_enabled(ADVICE_COMMIT_BEFORE_MERGE
)
163 ? _("The following untracked working tree files would be removed by %s:\n%%s"
164 "Please move or remove them before you %s.")
165 : _("The following untracked working tree files would be removed by %s:\n%%s");
166 msgs
[ERROR_WOULD_LOSE_UNTRACKED_REMOVED
] =
167 strvec_pushf(&opts
->internal
.msgs_to_free
, msg
, cmd
, cmd
);
169 if (!strcmp(cmd
, "checkout"))
170 msg
= advice_enabled(ADVICE_COMMIT_BEFORE_MERGE
)
171 ? _("The following untracked working tree files would be overwritten by checkout:\n%%s"
172 "Please move or remove them before you switch branches.")
173 : _("The following untracked working tree files would be overwritten by checkout:\n%%s");
174 else if (!strcmp(cmd
, "merge"))
175 msg
= advice_enabled(ADVICE_COMMIT_BEFORE_MERGE
)
176 ? _("The following untracked working tree files would be overwritten by merge:\n%%s"
177 "Please move or remove them before you merge.")
178 : _("The following untracked working tree files would be overwritten by merge:\n%%s");
180 msg
= advice_enabled(ADVICE_COMMIT_BEFORE_MERGE
)
181 ? _("The following untracked working tree files would be overwritten by %s:\n%%s"
182 "Please move or remove them before you %s.")
183 : _("The following untracked working tree files would be overwritten by %s:\n%%s");
184 msgs
[ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN
] =
185 strvec_pushf(&opts
->internal
.msgs_to_free
, msg
, cmd
, cmd
);
188 * Special case: ERROR_BIND_OVERLAP refers to a pair of paths, we
189 * cannot easily display it as a list.
191 msgs
[ERROR_BIND_OVERLAP
] = _("Entry '%s' overlaps with '%s'. Cannot bind.");
193 msgs
[ERROR_WOULD_LOSE_SUBMODULE
] =
194 _("Cannot update submodule:\n%s");
196 msgs
[WARNING_SPARSE_NOT_UPTODATE_FILE
] =
197 _("The following paths are not up to date and were left despite sparse patterns:\n%s");
198 msgs
[WARNING_SPARSE_UNMERGED_FILE
] =
199 _("The following paths are unmerged and were left despite sparse patterns:\n%s");
200 msgs
[WARNING_SPARSE_ORPHANED_NOT_OVERWRITTEN
] =
201 _("The following paths were already present and thus not updated despite sparse patterns:\n%s");
203 opts
->internal
.show_all_errors
= 1;
204 /* rejected paths may not have a static buffer */
205 for (i
= 0; i
< ARRAY_SIZE(opts
->internal
.unpack_rejects
); i
++)
206 opts
->internal
.unpack_rejects
[i
].strdup_strings
= 1;
209 void clear_unpack_trees_porcelain(struct unpack_trees_options
*opts
)
211 strvec_clear(&opts
->internal
.msgs_to_free
);
212 memset(opts
->internal
.msgs
, 0, sizeof(opts
->internal
.msgs
));
215 static int do_add_entry(struct unpack_trees_options
*o
, struct cache_entry
*ce
,
216 unsigned int set
, unsigned int clear
)
223 ce
->ce_flags
= (ce
->ce_flags
& ~clear
) | set
;
224 return add_index_entry(&o
->internal
.result
, ce
,
225 ADD_CACHE_OK_TO_ADD
| ADD_CACHE_OK_TO_REPLACE
);
228 static void add_entry(struct unpack_trees_options
*o
,
229 const struct cache_entry
*ce
,
230 unsigned int set
, unsigned int clear
)
232 do_add_entry(o
, dup_cache_entry(ce
, &o
->internal
.result
), set
, clear
);
236 * add error messages on path <path>
237 * corresponding to the type <e> with the message <msg>
238 * indicating if it should be display in porcelain or not
240 static int add_rejected_path(struct unpack_trees_options
*o
,
241 enum unpack_trees_error_types e
,
247 if (!o
->internal
.show_all_errors
)
248 return error(ERRORMSG(o
, e
), super_prefixed(path
,
252 * Otherwise, insert in a list for future display by
253 * display_(error|warning)_msgs()
255 string_list_append(&o
->internal
.unpack_rejects
[e
], path
);
260 * display all the error messages stored in a nice way
262 static void display_error_msgs(struct unpack_trees_options
*o
)
265 unsigned error_displayed
= 0;
266 for (e
= 0; e
< NB_UNPACK_TREES_ERROR_TYPES
; e
++) {
267 struct string_list
*rejects
= &o
->internal
.unpack_rejects
[e
];
269 if (rejects
->nr
> 0) {
271 struct strbuf path
= STRBUF_INIT
;
274 for (i
= 0; i
< rejects
->nr
; i
++)
275 strbuf_addf(&path
, "\t%s\n", rejects
->items
[i
].string
);
276 error(ERRORMSG(o
, e
), super_prefixed(path
.buf
,
278 strbuf_release(&path
);
280 string_list_clear(rejects
, 0);
283 fprintf(stderr
, _("Aborting\n"));
287 * display all the warning messages stored in a nice way
289 static void display_warning_msgs(struct unpack_trees_options
*o
)
292 unsigned warning_displayed
= 0;
293 for (e
= NB_UNPACK_TREES_ERROR_TYPES
+ 1;
294 e
< NB_UNPACK_TREES_WARNING_TYPES
; e
++) {
295 struct string_list
*rejects
= &o
->internal
.unpack_rejects
[e
];
297 if (rejects
->nr
> 0) {
299 struct strbuf path
= STRBUF_INIT
;
301 warning_displayed
= 1;
302 for (i
= 0; i
< rejects
->nr
; i
++)
303 strbuf_addf(&path
, "\t%s\n", rejects
->items
[i
].string
);
304 warning(ERRORMSG(o
, e
), super_prefixed(path
.buf
,
306 strbuf_release(&path
);
308 string_list_clear(rejects
, 0);
310 if (warning_displayed
)
311 fprintf(stderr
, _("After fixing the above paths, you may want to run `git sparse-checkout reapply`.\n"));
313 static int check_submodule_move_head(const struct cache_entry
*ce
,
316 struct unpack_trees_options
*o
)
318 unsigned flags
= SUBMODULE_MOVE_HEAD_DRY_RUN
;
319 const struct submodule
*sub
= submodule_from_ce(ce
);
325 flags
|= SUBMODULE_MOVE_HEAD_FORCE
;
327 if (submodule_move_head(ce
->name
, o
->super_prefix
, old_id
, new_id
,
329 return add_rejected_path(o
, ERROR_WOULD_LOSE_SUBMODULE
, ce
->name
);
334 * Perform the loading of the repository's gitmodules file. This function is
335 * used by 'check_update()' to perform loading of the gitmodules file in two
336 * different situations:
337 * (1) before removing entries from the working tree if the gitmodules file has
338 * been marked for removal. This situation is specified by 'state' == NULL.
339 * (2) before checking out entries to the working tree if the gitmodules file
340 * has been marked for update. This situation is specified by 'state' != NULL.
342 static void load_gitmodules_file(struct index_state
*index
,
343 struct checkout
*state
)
345 int pos
= index_name_pos(index
, GITMODULES_FILE
, strlen(GITMODULES_FILE
));
348 struct cache_entry
*ce
= index
->cache
[pos
];
349 if (!state
&& ce
->ce_flags
& CE_WT_REMOVE
) {
350 repo_read_gitmodules(the_repository
, 0);
351 } else if (state
&& (ce
->ce_flags
& CE_UPDATE
)) {
352 submodule_free(the_repository
);
353 checkout_entry(ce
, state
, NULL
, NULL
);
354 repo_read_gitmodules(the_repository
, 0);
359 static struct progress
*get_progress(struct unpack_trees_options
*o
,
360 struct index_state
*index
)
362 unsigned cnt
= 0, total
= 0;
364 if (!o
->update
|| !o
->verbose_update
)
367 for (; cnt
< index
->cache_nr
; cnt
++) {
368 const struct cache_entry
*ce
= index
->cache
[cnt
];
369 if (ce
->ce_flags
& (CE_UPDATE
| CE_WT_REMOVE
))
373 return start_delayed_progress(_("Updating files"), total
);
376 static void setup_collided_checkout_detection(struct checkout
*state
,
377 struct index_state
*index
)
382 for (i
= 0; i
< index
->cache_nr
; i
++)
383 index
->cache
[i
]->ce_flags
&= ~CE_MATCHED
;
386 static void report_collided_checkout(struct index_state
*index
)
388 struct string_list list
= STRING_LIST_INIT_NODUP
;
391 for (i
= 0; i
< index
->cache_nr
; i
++) {
392 struct cache_entry
*ce
= index
->cache
[i
];
394 if (!(ce
->ce_flags
& CE_MATCHED
))
397 string_list_append(&list
, ce
->name
);
398 ce
->ce_flags
&= ~CE_MATCHED
;
401 list
.cmp
= fspathcmp
;
402 string_list_sort(&list
);
405 warning(_("the following paths have collided (e.g. case-sensitive paths\n"
406 "on a case-insensitive filesystem) and only one from the same\n"
407 "colliding group is in the working tree:\n"));
409 for (i
= 0; i
< list
.nr
; i
++)
410 fprintf(stderr
, " '%s'\n", list
.items
[i
].string
);
413 string_list_clear(&list
, 0);
416 static int must_checkout(const struct cache_entry
*ce
)
418 return ce
->ce_flags
& CE_UPDATE
;
421 static int check_updates(struct unpack_trees_options
*o
,
422 struct index_state
*index
)
426 struct progress
*progress
;
427 struct checkout state
= CHECKOUT_INIT
;
428 int i
, pc_workers
, pc_threshold
;
430 trace_performance_enter();
431 state
.super_prefix
= o
->super_prefix
;
434 state
.refresh_cache
= 1;
435 state
.istate
= index
;
436 clone_checkout_metadata(&state
.meta
, &o
->meta
, NULL
);
438 if (!o
->update
|| o
->dry_run
) {
439 remove_marked_cache_entries(index
, 0);
440 trace_performance_leave("check_updates");
445 setup_collided_checkout_detection(&state
, index
);
447 progress
= get_progress(o
, index
);
449 /* Start with clean cache to avoid using any possibly outdated info. */
450 invalidate_lstat_cache();
452 git_attr_set_direction(GIT_ATTR_CHECKOUT
);
454 if (should_update_submodules())
455 load_gitmodules_file(index
, NULL
);
457 for (i
= 0; i
< index
->cache_nr
; i
++) {
458 const struct cache_entry
*ce
= index
->cache
[i
];
460 if (ce
->ce_flags
& CE_WT_REMOVE
) {
461 display_progress(progress
, ++cnt
);
462 unlink_entry(ce
, o
->super_prefix
);
466 remove_marked_cache_entries(index
, 0);
467 remove_scheduled_dirs();
469 if (should_update_submodules())
470 load_gitmodules_file(index
, &state
);
472 if (repo_has_promisor_remote(the_repository
))
474 * Prefetch the objects that are to be checked out in the loop
477 prefetch_cache_entries(index
, must_checkout
);
479 get_parallel_checkout_configs(&pc_workers
, &pc_threshold
);
481 enable_delayed_checkout(&state
);
483 init_parallel_checkout();
484 for (i
= 0; i
< index
->cache_nr
; i
++) {
485 struct cache_entry
*ce
= index
->cache
[i
];
487 if (must_checkout(ce
)) {
488 size_t last_pc_queue_size
= pc_queue_size();
490 if (ce
->ce_flags
& CE_WT_REMOVE
)
491 BUG("both update and delete flags are set on %s",
493 ce
->ce_flags
&= ~CE_UPDATE
;
494 errs
|= checkout_entry(ce
, &state
, NULL
, NULL
);
496 if (last_pc_queue_size
== pc_queue_size())
497 display_progress(progress
, ++cnt
);
501 errs
|= run_parallel_checkout(&state
, pc_workers
, pc_threshold
,
503 stop_progress(&progress
);
504 errs
|= finish_delayed_checkout(&state
, o
->verbose_update
);
505 git_attr_set_direction(GIT_ATTR_CHECKIN
);
508 report_collided_checkout(index
);
510 trace_performance_leave("check_updates");
514 static int verify_uptodate_sparse(const struct cache_entry
*ce
,
515 struct unpack_trees_options
*o
);
516 static int verify_absent_sparse(const struct cache_entry
*ce
,
517 enum unpack_trees_error_types
,
518 struct unpack_trees_options
*o
);
520 static int apply_sparse_checkout(struct index_state
*istate
,
521 struct cache_entry
*ce
,
522 struct unpack_trees_options
*o
)
524 int was_skip_worktree
= ce_skip_worktree(ce
);
526 if (ce
->ce_flags
& CE_NEW_SKIP_WORKTREE
)
527 ce
->ce_flags
|= CE_SKIP_WORKTREE
;
529 ce
->ce_flags
&= ~CE_SKIP_WORKTREE
;
530 if (was_skip_worktree
!= ce_skip_worktree(ce
)) {
531 ce
->ce_flags
|= CE_UPDATE_IN_BASE
;
532 mark_fsmonitor_invalid(istate
, ce
);
533 istate
->cache_changed
|= CE_ENTRY_CHANGED
;
537 * if (!was_skip_worktree && !ce_skip_worktree()) {
538 * This is perfectly normal. Move on;
543 * Merge strategies may set CE_UPDATE|CE_REMOVE outside checkout
544 * area as a result of ce_skip_worktree() shortcuts in
545 * verify_absent() and verify_uptodate().
546 * Make sure they don't modify worktree if they are already
547 * outside checkout area
549 if (was_skip_worktree
&& ce_skip_worktree(ce
)) {
550 ce
->ce_flags
&= ~CE_UPDATE
;
553 * By default, when CE_REMOVE is on, CE_WT_REMOVE is also
554 * on to get that file removed from both index and worktree.
555 * If that file is already outside worktree area, don't
558 if (ce
->ce_flags
& CE_REMOVE
)
559 ce
->ce_flags
&= ~CE_WT_REMOVE
;
562 if (!was_skip_worktree
&& ce_skip_worktree(ce
)) {
564 * If CE_UPDATE is set, verify_uptodate() must be called already
565 * also stat info may have lost after merged_entry() so calling
566 * verify_uptodate() again may fail
568 if (!(ce
->ce_flags
& CE_UPDATE
) &&
569 verify_uptodate_sparse(ce
, o
)) {
570 ce
->ce_flags
&= ~CE_SKIP_WORKTREE
;
573 ce
->ce_flags
|= CE_WT_REMOVE
;
574 ce
->ce_flags
&= ~CE_UPDATE
;
576 if (was_skip_worktree
&& !ce_skip_worktree(ce
)) {
577 if (verify_absent_sparse(ce
, WARNING_SPARSE_ORPHANED_NOT_OVERWRITTEN
, o
))
579 ce
->ce_flags
|= CE_UPDATE
;
584 static int warn_conflicted_path(struct index_state
*istate
,
586 struct unpack_trees_options
*o
)
588 char *conflicting_path
= istate
->cache
[i
]->name
;
591 add_rejected_path(o
, WARNING_SPARSE_UNMERGED_FILE
, conflicting_path
);
593 /* Find out how many higher stage entries are at same path */
594 while ((++count
) + i
< istate
->cache_nr
&&
595 !strcmp(conflicting_path
, istate
->cache
[count
+ i
]->name
))
601 static inline int call_unpack_fn(const struct cache_entry
* const *src
,
602 struct unpack_trees_options
*o
)
604 int ret
= o
->fn(src
, o
);
610 static void mark_ce_used(struct cache_entry
*ce
, struct unpack_trees_options
*o
)
612 ce
->ce_flags
|= CE_UNPACKED
;
614 if (o
->internal
.cache_bottom
< o
->src_index
->cache_nr
&&
615 o
->src_index
->cache
[o
->internal
.cache_bottom
] == ce
) {
616 int bottom
= o
->internal
.cache_bottom
;
618 while (bottom
< o
->src_index
->cache_nr
&&
619 o
->src_index
->cache
[bottom
]->ce_flags
& CE_UNPACKED
)
621 o
->internal
.cache_bottom
= bottom
;
625 static void mark_all_ce_unused(struct index_state
*index
)
628 for (i
= 0; i
< index
->cache_nr
; i
++)
629 index
->cache
[i
]->ce_flags
&= ~(CE_UNPACKED
| CE_ADDED
| CE_NEW_SKIP_WORKTREE
);
632 static int locate_in_src_index(const struct cache_entry
*ce
,
633 struct unpack_trees_options
*o
)
635 struct index_state
*index
= o
->src_index
;
636 int len
= ce_namelen(ce
);
637 int pos
= index_name_pos(index
, ce
->name
, len
);
644 * We call unpack_index_entry() with an unmerged cache entry
645 * only in diff-index, and it wants a single callback. Skip
646 * the other unmerged entry with the same name.
648 static void mark_ce_used_same_name(struct cache_entry
*ce
,
649 struct unpack_trees_options
*o
)
651 struct index_state
*index
= o
->src_index
;
652 int len
= ce_namelen(ce
);
655 for (pos
= locate_in_src_index(ce
, o
); pos
< index
->cache_nr
; pos
++) {
656 struct cache_entry
*next
= index
->cache
[pos
];
657 if (len
!= ce_namelen(next
) ||
658 memcmp(ce
->name
, next
->name
, len
))
660 mark_ce_used(next
, o
);
664 static struct cache_entry
*next_cache_entry(struct unpack_trees_options
*o
)
666 const struct index_state
*index
= o
->src_index
;
667 int pos
= o
->internal
.cache_bottom
;
669 while (pos
< index
->cache_nr
) {
670 struct cache_entry
*ce
= index
->cache
[pos
];
671 if (!(ce
->ce_flags
& CE_UNPACKED
))
678 static void add_same_unmerged(const struct cache_entry
*ce
,
679 struct unpack_trees_options
*o
)
681 struct index_state
*index
= o
->src_index
;
682 int len
= ce_namelen(ce
);
683 int pos
= index_name_pos(index
, ce
->name
, len
);
686 die("programming error in a caller of mark_ce_used_same_name");
687 for (pos
= -pos
- 1; pos
< index
->cache_nr
; pos
++) {
688 struct cache_entry
*next
= index
->cache
[pos
];
689 if (len
!= ce_namelen(next
) ||
690 memcmp(ce
->name
, next
->name
, len
))
692 add_entry(o
, next
, 0, 0);
693 mark_ce_used(next
, o
);
697 static int unpack_index_entry(struct cache_entry
*ce
,
698 struct unpack_trees_options
*o
)
700 const struct cache_entry
*src
[MAX_UNPACK_TREES
+ 1] = { NULL
, };
707 if (o
->skip_unmerged
) {
708 add_entry(o
, ce
, 0, 0);
712 ret
= call_unpack_fn(src
, o
);
714 mark_ce_used_same_name(ce
, o
);
718 static int find_cache_pos(struct traverse_info
*, const char *p
, size_t len
);
720 static void restore_cache_bottom(struct traverse_info
*info
, int bottom
)
722 struct unpack_trees_options
*o
= info
->data
;
724 if (o
->diff_index_cached
)
726 o
->internal
.cache_bottom
= bottom
;
729 static int switch_cache_bottom(struct traverse_info
*info
)
731 struct unpack_trees_options
*o
= info
->data
;
734 if (o
->diff_index_cached
)
736 ret
= o
->internal
.cache_bottom
;
737 pos
= find_cache_pos(info
->prev
, info
->name
, info
->namelen
);
740 o
->internal
.cache_bottom
= -2 - pos
;
742 o
->internal
.cache_bottom
= o
->src_index
->cache_nr
;
746 static inline int are_same_oid(struct name_entry
*name_j
, struct name_entry
*name_k
)
748 return !is_null_oid(&name_j
->oid
) && !is_null_oid(&name_k
->oid
) && oideq(&name_j
->oid
, &name_k
->oid
);
751 static int all_trees_same_as_cache_tree(int n
, unsigned long dirmask
,
752 struct name_entry
*names
,
753 struct traverse_info
*info
)
755 struct unpack_trees_options
*o
= info
->data
;
758 if (!o
->merge
|| dirmask
!= ((1 << n
) - 1))
761 for (i
= 1; i
< n
; i
++)
762 if (!are_same_oid(names
, names
+ i
))
765 return cache_tree_matches_traversal(o
->src_index
->cache_tree
, names
, info
);
768 static int index_pos_by_traverse_info(struct name_entry
*names
,
769 struct traverse_info
*info
)
771 struct unpack_trees_options
*o
= info
->data
;
772 struct strbuf name
= STRBUF_INIT
;
775 strbuf_make_traverse_path(&name
, info
, names
->path
, names
->pathlen
);
776 strbuf_addch(&name
, '/');
777 pos
= index_name_pos(o
->src_index
, name
.buf
, name
.len
);
779 if (!o
->src_index
->sparse_index
||
780 !(o
->src_index
->cache
[pos
]->ce_flags
& CE_SKIP_WORKTREE
))
781 BUG("This is a directory and should not exist in index");
785 if (pos
>= o
->src_index
->cache_nr
||
786 !starts_with(o
->src_index
->cache
[pos
]->name
, name
.buf
) ||
787 (pos
> 0 && starts_with(o
->src_index
->cache
[pos
-1]->name
, name
.buf
)))
788 BUG("pos %d doesn't point to the first entry of %s in index",
790 strbuf_release(&name
);
795 * Fast path if we detect that all trees are the same as cache-tree at this
796 * path. We'll walk these trees in an iterative loop using cache-tree/index
797 * instead of ODB since we already know what these trees contain.
799 static int traverse_by_cache_tree(int pos
, int nr_entries
, int nr_names
,
800 struct traverse_info
*info
)
802 struct cache_entry
*src
[MAX_UNPACK_TREES
+ 1] = { NULL
, };
803 struct unpack_trees_options
*o
= info
->data
;
804 struct cache_entry
*tree_ce
= NULL
;
809 BUG("We need cache-tree to do this optimization");
812 * Do what unpack_callback() and unpack_single_entry() normally
813 * do. But we walk all paths in an iterative loop instead.
815 * D/F conflicts and higher stage entries are not a concern
816 * because cache-tree would be invalidated and we would never
817 * get here in the first place.
819 for (i
= 0; i
< nr_entries
; i
++) {
820 int new_ce_len
, len
, rc
;
822 src
[0] = o
->src_index
->cache
[pos
+ i
];
824 len
= ce_namelen(src
[0]);
825 new_ce_len
= cache_entry_size(len
);
827 if (new_ce_len
> ce_len
) {
829 tree_ce
= xrealloc(tree_ce
, new_ce_len
);
830 memset(tree_ce
, 0, new_ce_len
);
833 tree_ce
->ce_flags
= create_ce_flags(0);
835 for (d
= 1; d
<= nr_names
; d
++)
839 tree_ce
->ce_mode
= src
[0]->ce_mode
;
840 tree_ce
->ce_namelen
= len
;
841 oidcpy(&tree_ce
->oid
, &src
[0]->oid
);
842 memcpy(tree_ce
->name
, src
[0]->name
, len
+ 1);
844 rc
= call_unpack_fn((const struct cache_entry
* const *)src
, o
);
850 mark_ce_used(src
[0], o
);
853 if (o
->internal
.debug_unpack
)
854 printf("Unpacked %d entries from %s to %s using cache-tree\n",
856 o
->src_index
->cache
[pos
]->name
,
857 o
->src_index
->cache
[pos
+ nr_entries
- 1]->name
);
861 static int traverse_trees_recursive(int n
, unsigned long dirmask
,
862 unsigned long df_conflicts
,
863 struct name_entry
*names
,
864 struct traverse_info
*info
)
866 struct unpack_trees_options
*o
= info
->data
;
871 struct traverse_info newinfo
;
872 struct name_entry
*p
;
875 nr_entries
= all_trees_same_as_cache_tree(n
, dirmask
, names
, info
);
876 if (nr_entries
> 0) {
877 int pos
= index_pos_by_traverse_info(names
, info
);
879 if (!o
->merge
|| df_conflicts
)
880 BUG("Wrong condition to get here buddy");
883 * All entries up to 'pos' must have been processed
884 * (i.e. marked CE_UNPACKED) at this point. But to be safe,
885 * save and restore cache_bottom anyway to not miss
886 * unprocessed entries before 'pos'.
888 bottom
= o
->internal
.cache_bottom
;
889 ret
= traverse_by_cache_tree(pos
, nr_entries
, n
, info
);
890 o
->internal
.cache_bottom
= bottom
;
900 newinfo
.pathspec
= info
->pathspec
;
901 newinfo
.name
= p
->path
;
902 newinfo
.namelen
= p
->pathlen
;
903 newinfo
.mode
= p
->mode
;
904 newinfo
.pathlen
= st_add3(newinfo
.pathlen
, tree_entry_len(p
), 1);
905 newinfo
.df_conflicts
|= df_conflicts
;
911 * Fetch the tree from the ODB for each peer directory in the
914 * For 2- and 3-way traversals, we try to avoid hitting the
915 * ODB twice for the same OID. This should yield a nice speed
916 * up in checkouts and merges when the commits are similar.
918 * We don't bother doing the full O(n^2) search for larger n,
919 * because wider traversals don't happen that often and we
920 * avoid the search setup.
922 * When 2 peer OIDs are the same, we just copy the tree
923 * descriptor data. This implicitly borrows the buffer
924 * data from the earlier cell.
926 for (i
= 0; i
< n
; i
++, dirmask
>>= 1) {
927 if (i
> 0 && are_same_oid(&names
[i
], &names
[i
- 1]))
929 else if (i
> 1 && are_same_oid(&names
[i
], &names
[i
- 2]))
932 const struct object_id
*oid
= NULL
;
935 buf
[nr_buf
++] = fill_tree_descriptor(the_repository
, t
+ i
, oid
);
939 bottom
= switch_cache_bottom(&newinfo
);
940 ret
= traverse_trees(o
->src_index
, n
, t
, &newinfo
);
941 restore_cache_bottom(&newinfo
, bottom
);
943 for (i
= 0; i
< nr_buf
; i
++)
952 * Compare the traverse-path to the cache entry without actually
953 * having to generate the textual representation of the traverse
956 * NOTE! This *only* compares up to the size of the traverse path
957 * itself - the caller needs to do the final check for the cache
958 * entry having more data at the end!
960 static int do_compare_entry_piecewise(const struct cache_entry
*ce
,
961 const struct traverse_info
*info
,
962 const char *name
, size_t namelen
,
969 int cmp
= do_compare_entry_piecewise(ce
, info
->prev
,
970 info
->name
, info
->namelen
,
975 pathlen
= info
->pathlen
;
976 ce_len
= ce_namelen(ce
);
978 /* If ce_len < pathlen then we must have previously hit "name == directory" entry */
979 if (ce_len
< pathlen
)
983 ce_name
= ce
->name
+ pathlen
;
985 return df_name_compare(ce_name
, ce_len
, S_IFREG
, name
, namelen
, mode
);
988 static int do_compare_entry(const struct cache_entry
*ce
,
989 const struct traverse_info
*info
,
990 const char *name
, size_t namelen
,
999 * If we have not precomputed the traverse path, it is quicker
1000 * to avoid doing so. But if we have precomputed it,
1001 * it is quicker to use the precomputed version.
1003 if (!info
->traverse_path
)
1004 return do_compare_entry_piecewise(ce
, info
, name
, namelen
, mode
);
1006 cmp
= strncmp(ce
->name
, info
->traverse_path
, info
->pathlen
);
1010 pathlen
= info
->pathlen
;
1011 ce_len
= ce_namelen(ce
);
1013 if (ce_len
< pathlen
)
1017 ce_name
= ce
->name
+ pathlen
;
1019 ce_mode
= S_ISSPARSEDIR(ce
->ce_mode
) ? S_IFDIR
: S_IFREG
;
1020 return df_name_compare(ce_name
, ce_len
, ce_mode
, name
, namelen
, mode
);
1023 static int compare_entry(const struct cache_entry
*ce
, const struct traverse_info
*info
, const struct name_entry
*n
)
1025 int cmp
= do_compare_entry(ce
, info
, n
->path
, n
->pathlen
, n
->mode
);
1030 * At this point, we know that we have a prefix match. If ce
1031 * is a sparse directory, then allow an exact match. This only
1032 * works when the input name is a directory, since ce->name
1033 * ends in a directory separator.
1035 if (S_ISSPARSEDIR(ce
->ce_mode
) &&
1036 ce
->ce_namelen
== traverse_path_len(info
, tree_entry_len(n
)) + 1)
1040 * Even if the beginning compared identically, the ce should
1041 * compare as bigger than a directory leading up to it!
1043 return ce_namelen(ce
) > traverse_path_len(info
, tree_entry_len(n
));
1046 static int ce_in_traverse_path(const struct cache_entry
*ce
,
1047 const struct traverse_info
*info
)
1051 if (do_compare_entry(ce
, info
->prev
,
1052 info
->name
, info
->namelen
, info
->mode
))
1055 * If ce (blob) is the same name as the path (which is a tree
1056 * we will be descending into), it won't be inside it.
1058 return (info
->pathlen
< ce_namelen(ce
));
1061 static struct cache_entry
*create_ce_entry(const struct traverse_info
*info
,
1062 const struct name_entry
*n
,
1064 struct index_state
*istate
,
1066 int is_sparse_directory
)
1068 size_t len
= traverse_path_len(info
, tree_entry_len(n
));
1069 size_t alloc_len
= is_sparse_directory
? len
+ 1 : len
;
1070 struct cache_entry
*ce
=
1072 make_empty_transient_cache_entry(alloc_len
, NULL
) :
1073 make_empty_cache_entry(istate
, alloc_len
);
1075 ce
->ce_mode
= create_ce_mode(n
->mode
);
1076 ce
->ce_flags
= create_ce_flags(stage
);
1077 ce
->ce_namelen
= len
;
1078 oidcpy(&ce
->oid
, &n
->oid
);
1079 /* len+1 because the cache_entry allocates space for NUL */
1080 make_traverse_path(ce
->name
, len
+ 1, info
, n
->path
, n
->pathlen
);
1082 if (is_sparse_directory
) {
1083 ce
->name
[len
] = '/';
1084 ce
->name
[len
+ 1] = '\0';
1086 ce
->ce_flags
|= CE_SKIP_WORKTREE
;
1093 * Determine whether the path specified by 'p' should be unpacked as a new
1094 * sparse directory in a sparse index. A new sparse directory 'A/':
1095 * - must be outside the sparse cone.
1096 * - must not already be in the index (i.e., no index entry with name 'A/'
1098 * - must not have any child entries in the index (i.e., no index entry
1099 * 'A/<something>' exists).
1100 * If 'p' meets the above requirements, return 1; otherwise, return 0.
1102 static int entry_is_new_sparse_dir(const struct traverse_info
*info
,
1103 const struct name_entry
*p
)
1106 struct strbuf dirpath
= STRBUF_INIT
;
1107 struct unpack_trees_options
*o
= info
->data
;
1109 if (!S_ISDIR(p
->mode
))
1113 * If the path is inside the sparse cone, it can't be a sparse directory.
1115 strbuf_add(&dirpath
, info
->traverse_path
, info
->pathlen
);
1116 strbuf_add(&dirpath
, p
->path
, p
->pathlen
);
1117 strbuf_addch(&dirpath
, '/');
1118 if (path_in_cone_mode_sparse_checkout(dirpath
.buf
, o
->src_index
)) {
1123 pos
= index_name_pos_sparse(o
->src_index
, dirpath
.buf
, dirpath
.len
);
1125 /* Path is already in the index, not a new sparse dir */
1130 /* Where would this sparse dir be inserted into the index? */
1132 if (pos
>= o
->src_index
->cache_nr
) {
1134 * Sparse dir would be inserted at the end of the index, so we
1135 * know it has no child entries.
1142 * If the dir has child entries in the index, the first would be at the
1143 * position the sparse directory would be inserted. If the entry at this
1144 * position is inside the dir, not a new sparse dir.
1146 res
= strncmp(o
->src_index
->cache
[pos
]->name
, dirpath
.buf
, dirpath
.len
);
1149 strbuf_release(&dirpath
);
1154 * Note that traverse_by_cache_tree() duplicates some logic in this function
1155 * without actually calling it. If you change the logic here you may need to
1156 * check and change there as well.
1158 static int unpack_single_entry(int n
, unsigned long mask
,
1159 unsigned long dirmask
,
1160 struct cache_entry
**src
,
1161 const struct name_entry
*names
,
1162 const struct traverse_info
*info
,
1163 int *is_new_sparse_dir
)
1166 struct unpack_trees_options
*o
= info
->data
;
1167 unsigned long conflicts
= info
->df_conflicts
| dirmask
;
1168 const struct name_entry
*p
= names
;
1170 *is_new_sparse_dir
= 0;
1171 if (mask
== dirmask
&& !src
[0]) {
1173 * If we're not in a sparse index, we can't unpack a directory
1174 * without recursing into it, so we return.
1176 if (!o
->src_index
->sparse_index
)
1179 /* Find first entry with a real name (we could use "mask" too) */
1184 * If the directory is completely missing from the index but
1185 * would otherwise be a sparse directory, we should unpack it.
1186 * If not, we'll return and continue recursively traversing the
1189 *is_new_sparse_dir
= entry_is_new_sparse_dir(info
, p
);
1190 if (!*is_new_sparse_dir
)
1195 * When we are unpacking a sparse directory, then this isn't necessarily
1196 * a directory-file conflict.
1198 if (mask
== dirmask
&&
1199 (*is_new_sparse_dir
|| (src
[0] && S_ISSPARSEDIR(src
[0]->ce_mode
))))
1203 * Ok, we've filled in up to any potential index entry in src[0],
1206 for (i
= 0; i
< n
; i
++) {
1208 unsigned int bit
= 1ul << i
;
1209 if (conflicts
& bit
) {
1210 src
[i
+ o
->merge
] = o
->df_conflict_entry
;
1217 else if (i
+ 1 < o
->head_idx
)
1219 else if (i
+ 1 > o
->head_idx
)
1225 * If the merge bit is set, then the cache entries are
1226 * discarded in the following block. In this case,
1227 * construct "transient" cache_entries, as they are
1228 * not stored in the index. otherwise construct the
1229 * cache entry from the index aware logic.
1231 src
[i
+ o
->merge
] = create_ce_entry(info
, names
+ i
, stage
,
1232 &o
->internal
.result
,
1233 o
->merge
, bit
& dirmask
);
1237 int rc
= call_unpack_fn((const struct cache_entry
* const *)src
,
1239 for (i
= 0; i
< n
; i
++) {
1240 struct cache_entry
*ce
= src
[i
+ o
->merge
];
1241 if (ce
!= o
->df_conflict_entry
)
1242 discard_cache_entry(ce
);
1247 for (i
= 0; i
< n
; i
++)
1248 if (src
[i
] && src
[i
] != o
->df_conflict_entry
)
1249 if (do_add_entry(o
, src
[i
], 0, 0))
1255 static int unpack_failed(struct unpack_trees_options
*o
, const char *message
)
1257 discard_index(&o
->internal
.result
);
1258 if (!o
->quiet
&& !o
->exiting_early
) {
1260 return error("%s", message
);
1267 * The tree traversal is looking at name p. If we have a matching entry,
1268 * return it. If name p is a directory in the index, do not return
1269 * anything, as we will want to match it when the traversal descends into
1272 static int find_cache_pos(struct traverse_info
*info
,
1273 const char *p
, size_t p_len
)
1276 struct unpack_trees_options
*o
= info
->data
;
1277 struct index_state
*index
= o
->src_index
;
1278 int pfxlen
= info
->pathlen
;
1280 for (pos
= o
->internal
.cache_bottom
; pos
< index
->cache_nr
; pos
++) {
1281 const struct cache_entry
*ce
= index
->cache
[pos
];
1282 const char *ce_name
, *ce_slash
;
1285 if (ce
->ce_flags
& CE_UNPACKED
) {
1287 * cache_bottom entry is already unpacked, so
1288 * we can never match it; don't check it
1291 if (pos
== o
->internal
.cache_bottom
)
1292 ++o
->internal
.cache_bottom
;
1295 if (!ce_in_traverse_path(ce
, info
)) {
1297 * Check if we can skip future cache checks
1298 * (because we're already past all possible
1299 * entries in the traverse path).
1301 if (info
->traverse_path
) {
1302 if (strncmp(ce
->name
, info
->traverse_path
,
1308 ce_name
= ce
->name
+ pfxlen
;
1309 ce_slash
= strchr(ce_name
, '/');
1311 ce_len
= ce_slash
- ce_name
;
1313 ce_len
= ce_namelen(ce
) - pfxlen
;
1314 cmp
= name_compare(p
, p_len
, ce_name
, ce_len
);
1316 * Exact match; if we have a directory we need to
1317 * delay returning it.
1320 return ce_slash
? -2 - pos
: pos
;
1322 continue; /* keep looking */
1324 * ce_name sorts after p->path; could it be that we
1325 * have files under p->path directory in the index?
1326 * E.g. ce_name == "t-i", and p->path == "t"; we may
1327 * have "t/a" in the index.
1329 if (p_len
< ce_len
&& !memcmp(ce_name
, p
, p_len
) &&
1330 ce_name
[p_len
] < '/')
1331 continue; /* keep looking */
1338 * Given a sparse directory entry 'ce', compare ce->name to
1339 * info->traverse_path + p->path + '/' if info->traverse_path
1342 * Compare ce->name to p->path + '/' otherwise. Note that
1343 * ce->name must end in a trailing '/' because it is a sparse
1346 static int sparse_dir_matches_path(const struct cache_entry
*ce
,
1347 struct traverse_info
*info
,
1348 const struct name_entry
*p
)
1350 assert(S_ISSPARSEDIR(ce
->ce_mode
));
1351 assert(ce
->name
[ce
->ce_namelen
- 1] == '/');
1354 return ce
->ce_namelen
== info
->pathlen
+ p
->pathlen
+ 1 &&
1355 ce
->name
[info
->pathlen
- 1] == '/' &&
1356 !strncmp(ce
->name
, info
->traverse_path
, info
->pathlen
) &&
1357 !strncmp(ce
->name
+ info
->pathlen
, p
->path
, p
->pathlen
);
1358 return ce
->ce_namelen
== p
->pathlen
+ 1 &&
1359 !strncmp(ce
->name
, p
->path
, p
->pathlen
);
1362 static struct cache_entry
*find_cache_entry(struct traverse_info
*info
,
1363 const struct name_entry
*p
)
1366 int pos
= find_cache_pos(info
, p
->path
, p
->pathlen
);
1367 struct unpack_trees_options
*o
= info
->data
;
1370 return o
->src_index
->cache
[pos
];
1373 * Check for a sparse-directory entry named "path/".
1374 * Due to the input p->path not having a trailing
1375 * slash, the negative 'pos' value overshoots the
1376 * expected position, hence "-2" instead of "-1".
1380 if (pos
< 0 || pos
>= o
->src_index
->cache_nr
)
1384 * Due to lexicographic sorting and sparse directory
1385 * entries ending with a trailing slash, our path as a
1386 * sparse directory (e.g "subdir/") and our path as a
1387 * file (e.g. "subdir") might be separated by other
1388 * paths (e.g. "subdir-").
1391 struct cache_entry
*ce
= o
->src_index
->cache
[pos
];
1393 if (!skip_prefix(ce
->name
, info
->traverse_path
, &path
) ||
1394 strncmp(path
, p
->path
, p
->pathlen
) ||
1395 path
[p
->pathlen
] != '/')
1398 if (S_ISSPARSEDIR(ce
->ce_mode
) &&
1399 sparse_dir_matches_path(ce
, info
, p
))
1408 static void debug_path(struct traverse_info
*info
)
1411 debug_path(info
->prev
);
1412 if (*info
->prev
->name
)
1415 printf("%s", info
->name
);
1418 static void debug_name_entry(int i
, struct name_entry
*n
)
1420 printf("ent#%d %06o %s\n", i
,
1421 n
->path
? n
->mode
: 0,
1422 n
->path
? n
->path
: "(missing)");
1425 static void debug_unpack_callback(int n
,
1427 unsigned long dirmask
,
1428 struct name_entry
*names
,
1429 struct traverse_info
*info
)
1432 printf("* unpack mask %lu, dirmask %lu, cnt %d ",
1436 for (i
= 0; i
< n
; i
++)
1437 debug_name_entry(i
, names
+ i
);
1441 * Returns true if and only if the given cache_entry is a
1442 * sparse-directory entry that matches the given name_entry
1443 * from the tree walk at the given traverse_info.
1445 static int is_sparse_directory_entry(struct cache_entry
*ce
,
1446 const struct name_entry
*name
,
1447 struct traverse_info
*info
)
1449 if (!ce
|| !name
|| !S_ISSPARSEDIR(ce
->ce_mode
))
1452 return sparse_dir_matches_path(ce
, info
, name
);
1455 static int unpack_sparse_callback(int n
, unsigned long mask
, unsigned long dirmask
, struct name_entry
*names
, struct traverse_info
*info
)
1457 struct cache_entry
*src
[MAX_UNPACK_TREES
+ 1] = { NULL
, };
1458 struct unpack_trees_options
*o
= info
->data
;
1459 int ret
, is_new_sparse_dir
;
1464 * Unlike in 'unpack_callback', where src[0] is derived from the index when
1465 * merging, src[0] is a transient cache entry derived from the first tree
1466 * provided. Create the temporary entry as if it came from a non-sparse index.
1468 if (!is_null_oid(&names
[0].oid
)) {
1469 src
[0] = create_ce_entry(info
, &names
[0], 0,
1470 &o
->internal
.result
, 1,
1471 dirmask
& (1ul << 0));
1472 src
[0]->ce_flags
|= (CE_SKIP_WORKTREE
| CE_NEW_SKIP_WORKTREE
);
1476 * 'unpack_single_entry' assumes that src[0] is derived directly from
1477 * the index, rather than from an entry in 'names'. This is *not* true when
1478 * merging a sparse directory, in which case names[0] is the "index" source
1479 * entry. To match the expectations of 'unpack_single_entry', shift past the
1480 * "index" tree (i.e., names[0]) and adjust 'names', 'n', 'mask', and
1481 * 'dirmask' accordingly.
1483 ret
= unpack_single_entry(n
- 1, mask
>> 1, dirmask
>> 1, src
, names
+ 1, info
, &is_new_sparse_dir
);
1486 discard_cache_entry(src
[0]);
1488 return ret
>= 0 ? mask
: -1;
1492 * Note that traverse_by_cache_tree() duplicates some logic in this function
1493 * without actually calling it. If you change the logic here you may need to
1494 * check and change there as well.
1496 static int unpack_callback(int n
, unsigned long mask
, unsigned long dirmask
, struct name_entry
*names
, struct traverse_info
*info
)
1498 struct cache_entry
*src
[MAX_UNPACK_TREES
+ 1] = { NULL
, };
1499 struct unpack_trees_options
*o
= info
->data
;
1500 const struct name_entry
*p
= names
;
1501 int is_new_sparse_dir
;
1503 /* Find first entry with a real name (we could use "mask" too) */
1507 if (o
->internal
.debug_unpack
)
1508 debug_unpack_callback(n
, mask
, dirmask
, names
, info
);
1510 /* Are we supposed to look at the index too? */
1514 struct cache_entry
*ce
;
1516 if (o
->diff_index_cached
)
1517 ce
= next_cache_entry(o
);
1519 ce
= find_cache_entry(info
, p
);
1523 cmp
= compare_entry(ce
, info
, p
);
1525 if (unpack_index_entry(ce
, o
) < 0)
1526 return unpack_failed(o
, NULL
);
1532 * If we skip unmerged index
1533 * entries, we'll skip this
1534 * entry *and* the tree
1535 * entries associated with it!
1537 if (o
->skip_unmerged
) {
1538 add_same_unmerged(ce
, o
);
1548 if (unpack_single_entry(n
, mask
, dirmask
, src
, names
, info
, &is_new_sparse_dir
))
1551 if (o
->merge
&& src
[0]) {
1552 if (ce_stage(src
[0]))
1553 mark_ce_used_same_name(src
[0], o
);
1555 mark_ce_used(src
[0], o
);
1558 /* Now handle any directories.. */
1560 /* special case: "diff-index --cached" looking at a tree */
1561 if (o
->diff_index_cached
&&
1562 n
== 1 && dirmask
== 1 && S_ISDIR(names
->mode
)) {
1564 matches
= cache_tree_matches_traversal(o
->src_index
->cache_tree
,
1567 * Everything under the name matches; skip the
1568 * entire hierarchy. diff_index_cached codepath
1569 * special cases D/F conflicts in such a way that
1570 * it does not do any look-ahead, so this is safe.
1574 * Only increment the cache_bottom if the
1575 * directory isn't a sparse directory index
1576 * entry (if it is, it was already incremented)
1577 * in 'mark_ce_used()'
1579 if (!src
[0] || !S_ISSPARSEDIR(src
[0]->ce_mode
))
1580 o
->internal
.cache_bottom
+= matches
;
1585 if (!is_sparse_directory_entry(src
[0], p
, info
) &&
1586 !is_new_sparse_dir
&&
1587 traverse_trees_recursive(n
, dirmask
, mask
& ~dirmask
,
1598 static int clear_ce_flags_1(struct index_state
*istate
,
1599 struct cache_entry
**cache
, int nr
,
1600 struct strbuf
*prefix
,
1601 int select_mask
, int clear_mask
,
1602 struct pattern_list
*pl
,
1603 enum pattern_match_result default_match
,
1606 /* Whole directory matching */
1607 static int clear_ce_flags_dir(struct index_state
*istate
,
1608 struct cache_entry
**cache
, int nr
,
1609 struct strbuf
*prefix
,
1611 int select_mask
, int clear_mask
,
1612 struct pattern_list
*pl
,
1613 enum pattern_match_result default_match
,
1616 struct cache_entry
**cache_end
;
1619 enum pattern_match_result ret
, orig_ret
;
1620 orig_ret
= path_matches_pattern_list(prefix
->buf
, prefix
->len
,
1621 basename
, &dtype
, pl
, istate
);
1623 strbuf_addch(prefix
, '/');
1625 /* If undecided, use matching result of parent dir in defval */
1626 if (orig_ret
== UNDECIDED
)
1627 ret
= default_match
;
1631 for (cache_end
= cache
; cache_end
!= cache
+ nr
; cache_end
++) {
1632 struct cache_entry
*ce
= *cache_end
;
1633 if (strncmp(ce
->name
, prefix
->buf
, prefix
->len
))
1637 if (pl
->use_cone_patterns
&& orig_ret
== MATCHED_RECURSIVE
) {
1638 struct cache_entry
**ce
= cache
;
1639 rc
= cache_end
- cache
;
1641 while (ce
< cache_end
) {
1642 (*ce
)->ce_flags
&= ~clear_mask
;
1645 } else if (pl
->use_cone_patterns
&& orig_ret
== NOT_MATCHED
) {
1646 rc
= cache_end
- cache
;
1648 rc
= clear_ce_flags_1(istate
, cache
, cache_end
- cache
,
1650 select_mask
, clear_mask
,
1655 strbuf_setlen(prefix
, prefix
->len
- 1);
1660 * Traverse the index, find every entry that matches according to
1661 * o->pl. Do "ce_flags &= ~clear_mask" on those entries. Return the
1662 * number of traversed entries.
1664 * If select_mask is non-zero, only entries whose ce_flags has on of
1665 * those bits enabled are traversed.
1667 * cache : pointer to an index entry
1668 * prefix_len : an offset to its path
1670 * The current path ("prefix") including the trailing '/' is
1671 * cache[0]->name[0..(prefix_len-1)]
1672 * Top level path has prefix_len zero.
1674 static int clear_ce_flags_1(struct index_state
*istate
,
1675 struct cache_entry
**cache
, int nr
,
1676 struct strbuf
*prefix
,
1677 int select_mask
, int clear_mask
,
1678 struct pattern_list
*pl
,
1679 enum pattern_match_result default_match
,
1682 struct cache_entry
**cache_end
= nr
? cache
+ nr
: cache
;
1685 * Process all entries that have the given prefix and meet
1686 * select_mask condition
1688 while(cache
!= cache_end
) {
1689 struct cache_entry
*ce
= *cache
;
1690 const char *name
, *slash
;
1692 enum pattern_match_result ret
;
1694 display_progress(istate
->progress
, progress_nr
);
1696 if (select_mask
&& !(ce
->ce_flags
& select_mask
)) {
1702 if (prefix
->len
&& strncmp(ce
->name
, prefix
->buf
, prefix
->len
))
1705 name
= ce
->name
+ prefix
->len
;
1706 slash
= strchr(name
, '/');
1708 /* If it's a directory, try whole directory match first */
1713 strbuf_add(prefix
, name
, len
);
1715 processed
= clear_ce_flags_dir(istate
, cache
, cache_end
- cache
,
1717 prefix
->buf
+ prefix
->len
- len
,
1718 select_mask
, clear_mask
,
1722 /* clear_c_f_dir eats a whole dir already? */
1725 progress_nr
+= processed
;
1726 strbuf_setlen(prefix
, prefix
->len
- len
);
1730 strbuf_addch(prefix
, '/');
1731 processed
= clear_ce_flags_1(istate
, cache
, cache_end
- cache
,
1733 select_mask
, clear_mask
, pl
,
1734 default_match
, progress_nr
);
1737 progress_nr
+= processed
;
1739 strbuf_setlen(prefix
, prefix
->len
- len
- 1);
1744 dtype
= ce_to_dtype(ce
);
1745 ret
= path_matches_pattern_list(ce
->name
,
1747 name
, &dtype
, pl
, istate
);
1748 if (ret
== UNDECIDED
)
1749 ret
= default_match
;
1750 if (ret
== MATCHED
|| ret
== MATCHED_RECURSIVE
)
1751 ce
->ce_flags
&= ~clear_mask
;
1756 display_progress(istate
->progress
, progress_nr
);
1757 return nr
- (cache_end
- cache
);
1760 static int clear_ce_flags(struct index_state
*istate
,
1761 int select_mask
, int clear_mask
,
1762 struct pattern_list
*pl
,
1765 static struct strbuf prefix
= STRBUF_INIT
;
1769 strbuf_reset(&prefix
);
1771 istate
->progress
= start_delayed_progress(
1772 _("Updating index flags"),
1775 xsnprintf(label
, sizeof(label
), "clear_ce_flags(0x%08lx,0x%08lx)",
1776 (unsigned long)select_mask
, (unsigned long)clear_mask
);
1777 trace2_region_enter("unpack_trees", label
, the_repository
);
1778 rval
= clear_ce_flags_1(istate
,
1782 select_mask
, clear_mask
,
1784 trace2_region_leave("unpack_trees", label
, the_repository
);
1786 stop_progress(&istate
->progress
);
1791 * Set/Clear CE_NEW_SKIP_WORKTREE according to $GIT_DIR/info/sparse-checkout
1793 static void mark_new_skip_worktree(struct pattern_list
*pl
,
1794 struct index_state
*istate
,
1795 int select_flag
, int skip_wt_flag
,
1801 * 1. Pretend the narrowest worktree: only unmerged entries
1804 for (i
= 0; i
< istate
->cache_nr
; i
++) {
1805 struct cache_entry
*ce
= istate
->cache
[i
];
1807 if (select_flag
&& !(ce
->ce_flags
& select_flag
))
1810 if (!ce_stage(ce
) && !(ce
->ce_flags
& CE_CONFLICTED
))
1811 ce
->ce_flags
|= skip_wt_flag
;
1813 ce
->ce_flags
&= ~skip_wt_flag
;
1817 * 2. Widen worktree according to sparse-checkout file.
1818 * Matched entries will have skip_wt_flag cleared (i.e. "in")
1820 clear_ce_flags(istate
, select_flag
, skip_wt_flag
, pl
, show_progress
);
1823 static void populate_from_existing_patterns(struct unpack_trees_options
*o
,
1824 struct pattern_list
*pl
)
1826 if (get_sparse_checkout_patterns(pl
) < 0)
1827 o
->skip_sparse_checkout
= 1;
1829 o
->internal
.pl
= pl
;
1832 static void update_sparsity_for_prefix(const char *prefix
,
1833 struct index_state
*istate
)
1835 int prefix_len
= strlen(prefix
);
1836 struct strbuf ce_prefix
= STRBUF_INIT
;
1838 if (!istate
->sparse_index
)
1841 while (prefix_len
> 0 && prefix
[prefix_len
- 1] == '/')
1844 if (prefix_len
<= 0)
1845 BUG("Invalid prefix passed to update_sparsity_for_prefix");
1847 strbuf_grow(&ce_prefix
, prefix_len
+ 1);
1848 strbuf_add(&ce_prefix
, prefix
, prefix_len
);
1849 strbuf_addch(&ce_prefix
, '/');
1852 * If the prefix points to a sparse directory or a path inside a sparse
1853 * directory, the index should be expanded. This is accomplished in one
1855 * - if the prefix is inside a sparse directory, it will be expanded by
1856 * the 'ensure_full_index(...)' call in 'index_name_pos(...)'.
1857 * - if the prefix matches an existing sparse directory entry,
1858 * 'index_name_pos(...)' will return its index position, triggering
1859 * the 'ensure_full_index(...)' below.
1861 if (!path_in_cone_mode_sparse_checkout(ce_prefix
.buf
, istate
) &&
1862 index_name_pos(istate
, ce_prefix
.buf
, ce_prefix
.len
) >= 0)
1863 ensure_full_index(istate
);
1865 strbuf_release(&ce_prefix
);
1868 static int verify_absent(const struct cache_entry
*,
1869 enum unpack_trees_error_types
,
1870 struct unpack_trees_options
*);
1872 * N-way merge "len" trees. Returns 0 on success, -1 on failure to manipulate the
1873 * resulting index, -2 on failure to reflect the changes to the work tree.
1875 * CE_ADDED, CE_UNPACKED and CE_NEW_SKIP_WORKTREE are used internally
1877 int unpack_trees(unsigned len
, struct tree_desc
*t
, struct unpack_trees_options
*o
)
1879 struct repository
*repo
= the_repository
;
1881 static struct cache_entry
*dfc
;
1882 struct pattern_list pl
;
1883 int free_pattern_list
= 0;
1884 struct dir_struct dir
= DIR_INIT
;
1886 if (o
->reset
== UNPACK_RESET_INVALID
)
1887 BUG("o->reset had a value of 1; should be UNPACK_TREES_*_UNTRACKED");
1889 if (len
> MAX_UNPACK_TREES
)
1890 die("unpack_trees takes at most %d trees", MAX_UNPACK_TREES
);
1891 if (o
->internal
.dir
)
1892 BUG("o->internal.dir is for internal use only");
1894 BUG("o->internal.pl is for internal use only");
1895 if (o
->df_conflict_entry
)
1896 BUG("o->df_conflict_entry is an output only field");
1898 trace_performance_enter();
1899 trace2_region_enter("unpack_trees", "unpack_trees", the_repository
);
1901 prepare_repo_settings(repo
);
1902 if (repo
->settings
.command_requires_full_index
) {
1903 ensure_full_index(o
->src_index
);
1905 ensure_full_index(o
->dst_index
);
1908 if (o
->reset
== UNPACK_RESET_OVERWRITE_UNTRACKED
&&
1909 o
->preserve_ignored
)
1910 BUG("UNPACK_RESET_OVERWRITE_UNTRACKED incompatible with preserved ignored files");
1912 if (!o
->preserve_ignored
) {
1913 o
->internal
.dir
= &dir
;
1914 o
->internal
.dir
->flags
|= DIR_SHOW_IGNORED
;
1915 setup_standard_excludes(o
->internal
.dir
);
1919 update_sparsity_for_prefix(o
->prefix
, o
->src_index
);
1921 if (!core_apply_sparse_checkout
|| !o
->update
)
1922 o
->skip_sparse_checkout
= 1;
1923 if (!o
->skip_sparse_checkout
) {
1924 memset(&pl
, 0, sizeof(pl
));
1925 free_pattern_list
= 1;
1926 populate_from_existing_patterns(o
, &pl
);
1929 index_state_init(&o
->internal
.result
, o
->src_index
->repo
);
1930 o
->internal
.result
.initialized
= 1;
1931 o
->internal
.result
.timestamp
.sec
= o
->src_index
->timestamp
.sec
;
1932 o
->internal
.result
.timestamp
.nsec
= o
->src_index
->timestamp
.nsec
;
1933 o
->internal
.result
.version
= o
->src_index
->version
;
1934 if (!o
->src_index
->split_index
) {
1935 o
->internal
.result
.split_index
= NULL
;
1936 } else if (o
->src_index
== o
->dst_index
) {
1938 * o->dst_index (and thus o->src_index) will be discarded
1939 * and overwritten with o->internal.result at the end of
1940 * this function, so just use src_index's split_index to
1941 * avoid having to create a new one.
1943 o
->internal
.result
.split_index
= o
->src_index
->split_index
;
1944 if (o
->src_index
->cache_changed
& SPLIT_INDEX_ORDERED
)
1945 o
->internal
.result
.cache_changed
|= SPLIT_INDEX_ORDERED
;
1946 o
->internal
.result
.split_index
->refcount
++;
1948 o
->internal
.result
.split_index
=
1949 init_split_index(&o
->internal
.result
);
1951 oidcpy(&o
->internal
.result
.oid
, &o
->src_index
->oid
);
1952 o
->internal
.merge_size
= len
;
1953 mark_all_ce_unused(o
->src_index
);
1955 o
->internal
.result
.fsmonitor_last_update
=
1956 xstrdup_or_null(o
->src_index
->fsmonitor_last_update
);
1957 o
->internal
.result
.fsmonitor_has_run_once
= o
->src_index
->fsmonitor_has_run_once
;
1959 if (!o
->src_index
->initialized
&&
1960 !repo
->settings
.command_requires_full_index
&&
1961 is_sparse_index_allowed(&o
->internal
.result
, 0))
1962 o
->internal
.result
.sparse_index
= 1;
1965 * Sparse checkout loop #1: set NEW_SKIP_WORKTREE on existing entries
1967 if (!o
->skip_sparse_checkout
)
1968 mark_new_skip_worktree(o
->internal
.pl
, o
->src_index
, 0,
1969 CE_NEW_SKIP_WORKTREE
, o
->verbose_update
);
1972 dfc
= xcalloc(1, cache_entry_size(0));
1973 o
->df_conflict_entry
= dfc
;
1976 const char *prefix
= o
->prefix
? o
->prefix
: "";
1977 struct traverse_info info
;
1979 setup_traverse_info(&info
, prefix
);
1980 info
.fn
= unpack_callback
;
1982 info
.show_all_errors
= o
->internal
.show_all_errors
;
1983 info
.pathspec
= o
->pathspec
;
1987 * Unpack existing index entries that sort before the
1988 * prefix the tree is spliced into. Note that o->merge
1989 * is always true in this case.
1992 struct cache_entry
*ce
= next_cache_entry(o
);
1995 if (ce_in_traverse_path(ce
, &info
))
1997 if (unpack_index_entry(ce
, o
) < 0)
2002 trace_performance_enter();
2003 trace2_region_enter("unpack_trees", "traverse_trees", the_repository
);
2004 ret
= traverse_trees(o
->src_index
, len
, t
, &info
);
2005 trace2_region_leave("unpack_trees", "traverse_trees", the_repository
);
2006 trace_performance_leave("traverse_trees");
2011 /* Any left-over entries in the index? */
2014 struct cache_entry
*ce
= next_cache_entry(o
);
2017 if (unpack_index_entry(ce
, o
) < 0)
2021 mark_all_ce_unused(o
->src_index
);
2023 if (o
->trivial_merges_only
&& o
->internal
.nontrivial_merge
) {
2024 ret
= unpack_failed(o
, "Merge requires file-level merging");
2028 if (!o
->skip_sparse_checkout
) {
2030 * Sparse checkout loop #2: set NEW_SKIP_WORKTREE on entries not in loop #1
2031 * If they will have NEW_SKIP_WORKTREE, also set CE_SKIP_WORKTREE
2032 * so apply_sparse_checkout() won't attempt to remove it from worktree
2034 mark_new_skip_worktree(o
->internal
.pl
, &o
->internal
.result
,
2035 CE_ADDED
, CE_SKIP_WORKTREE
| CE_NEW_SKIP_WORKTREE
,
2039 for (i
= 0; i
< o
->internal
.result
.cache_nr
; i
++) {
2040 struct cache_entry
*ce
= o
->internal
.result
.cache
[i
];
2043 * Entries marked with CE_ADDED in merged_entry() do not have
2044 * verify_absent() check (the check is effectively disabled
2045 * because CE_NEW_SKIP_WORKTREE is set unconditionally).
2047 * Do the real check now because we have had
2048 * correct CE_NEW_SKIP_WORKTREE
2050 if (ce
->ce_flags
& CE_ADDED
&&
2051 verify_absent(ce
, WARNING_SPARSE_ORPHANED_NOT_OVERWRITTEN
, o
))
2054 if (apply_sparse_checkout(&o
->internal
.result
, ce
, o
))
2059 * Inability to sparsify or de-sparsify individual
2060 * paths is not an error, but just a warning.
2062 if (o
->internal
.show_all_errors
)
2063 display_warning_msgs(o
);
2068 ret
= check_updates(o
, &o
->internal
.result
) ? (-2) : 0;
2070 move_index_extensions(&o
->internal
.result
, o
->src_index
);
2072 if (git_env_bool("GIT_TEST_CHECK_CACHE_TREE", 0))
2073 cache_tree_verify(the_repository
,
2074 &o
->internal
.result
);
2075 if (!o
->skip_cache_tree_update
&&
2076 !cache_tree_fully_valid(o
->internal
.result
.cache_tree
))
2077 cache_tree_update(&o
->internal
.result
,
2082 o
->internal
.result
.updated_workdir
= 1;
2083 discard_index(o
->dst_index
);
2084 *o
->dst_index
= o
->internal
.result
;
2086 discard_index(&o
->internal
.result
);
2088 o
->src_index
= NULL
;
2091 if (free_pattern_list
)
2092 clear_pattern_list(&pl
);
2093 if (o
->internal
.dir
) {
2094 dir_clear(o
->internal
.dir
);
2095 o
->internal
.dir
= NULL
;
2097 trace2_region_leave("unpack_trees", "unpack_trees", the_repository
);
2098 trace_performance_leave("unpack_trees");
2102 if (o
->internal
.show_all_errors
)
2103 display_error_msgs(o
);
2104 mark_all_ce_unused(o
->src_index
);
2105 ret
= unpack_failed(o
, NULL
);
2106 if (o
->exiting_early
)
2112 * Update SKIP_WORKTREE bits according to sparsity patterns, and update
2113 * working directory to match.
2115 * CE_NEW_SKIP_WORKTREE is used internally.
2117 enum update_sparsity_result
update_sparsity(struct unpack_trees_options
*o
,
2118 struct pattern_list
*pl
)
2120 enum update_sparsity_result ret
= UPDATE_SPARSITY_SUCCESS
;
2122 unsigned old_show_all_errors
;
2123 int free_pattern_list
= 0;
2125 old_show_all_errors
= o
->internal
.show_all_errors
;
2126 o
->internal
.show_all_errors
= 1;
2127 index_state_init(&o
->internal
.result
, o
->src_index
->repo
);
2130 if (!o
->update
|| o
->index_only
|| o
->skip_sparse_checkout
)
2131 BUG("update_sparsity() is for reflecting sparsity patterns in working directory");
2132 if (o
->src_index
!= o
->dst_index
|| o
->fn
)
2133 BUG("update_sparsity() called wrong");
2135 trace_performance_enter();
2137 /* If we weren't given patterns, use the recorded ones */
2139 free_pattern_list
= 1;
2140 pl
= xcalloc(1, sizeof(*pl
));
2141 populate_from_existing_patterns(o
, pl
);
2143 o
->internal
.pl
= pl
;
2145 /* Expand sparse directories as needed */
2146 expand_index(o
->src_index
, o
->internal
.pl
);
2148 /* Set NEW_SKIP_WORKTREE on existing entries. */
2149 mark_all_ce_unused(o
->src_index
);
2150 mark_new_skip_worktree(o
->internal
.pl
, o
->src_index
, 0,
2151 CE_NEW_SKIP_WORKTREE
, o
->verbose_update
);
2153 /* Then loop over entries and update/remove as needed */
2154 ret
= UPDATE_SPARSITY_SUCCESS
;
2155 for (i
= 0; i
< o
->src_index
->cache_nr
; i
++) {
2156 struct cache_entry
*ce
= o
->src_index
->cache
[i
];
2160 /* -1 because for loop will increment by 1 */
2161 i
+= warn_conflicted_path(o
->src_index
, i
, o
) - 1;
2162 ret
= UPDATE_SPARSITY_WARNINGS
;
2166 if (apply_sparse_checkout(o
->src_index
, ce
, o
))
2167 ret
= UPDATE_SPARSITY_WARNINGS
;
2170 if (check_updates(o
, o
->src_index
))
2171 ret
= UPDATE_SPARSITY_WORKTREE_UPDATE_FAILURES
;
2173 display_warning_msgs(o
);
2174 o
->internal
.show_all_errors
= old_show_all_errors
;
2175 if (free_pattern_list
) {
2176 clear_pattern_list(pl
);
2178 o
->internal
.pl
= NULL
;
2180 trace_performance_leave("update_sparsity");
2184 /* Here come the merge functions */
2186 static int reject_merge(const struct cache_entry
*ce
,
2187 struct unpack_trees_options
*o
)
2189 return add_rejected_path(o
, ERROR_WOULD_OVERWRITE
, ce
->name
);
2192 static int same(const struct cache_entry
*a
, const struct cache_entry
*b
)
2198 if ((a
->ce_flags
| b
->ce_flags
) & CE_CONFLICTED
)
2200 return a
->ce_mode
== b
->ce_mode
&&
2201 oideq(&a
->oid
, &b
->oid
);
2206 * When a CE gets turned into an unmerged entry, we
2207 * want it to be up-to-date
2209 static int verify_uptodate_1(const struct cache_entry
*ce
,
2210 struct unpack_trees_options
*o
,
2211 enum unpack_trees_error_types error_type
)
2219 * CE_VALID and CE_SKIP_WORKTREE cheat, we better check again
2220 * if this entry is truly up-to-date because this file may be
2223 if ((ce
->ce_flags
& CE_VALID
) || ce_skip_worktree(ce
))
2224 ; /* keep checking */
2225 else if (o
->reset
|| ce_uptodate(ce
))
2228 if (!lstat(ce
->name
, &st
)) {
2229 int flags
= CE_MATCH_IGNORE_VALID
|CE_MATCH_IGNORE_SKIP_WORKTREE
;
2230 unsigned changed
= ie_match_stat(o
->src_index
, ce
, &st
, flags
);
2232 if (submodule_from_ce(ce
)) {
2233 int r
= check_submodule_move_head(ce
,
2234 "HEAD", oid_to_hex(&ce
->oid
), o
);
2236 return add_rejected_path(o
, error_type
, ce
->name
);
2243 * Historic default policy was to allow submodule to be out
2244 * of sync wrt the superproject index. If the submodule was
2245 * not considered interesting above, we don't care here.
2247 if (S_ISGITLINK(ce
->ce_mode
))
2252 if (errno
== ENOENT
)
2254 return add_rejected_path(o
, error_type
, ce
->name
);
2257 int verify_uptodate(const struct cache_entry
*ce
,
2258 struct unpack_trees_options
*o
)
2260 if (!o
->skip_sparse_checkout
&&
2261 (ce
->ce_flags
& CE_SKIP_WORKTREE
) &&
2262 (ce
->ce_flags
& CE_NEW_SKIP_WORKTREE
))
2264 return verify_uptodate_1(ce
, o
, ERROR_NOT_UPTODATE_FILE
);
2267 static int verify_uptodate_sparse(const struct cache_entry
*ce
,
2268 struct unpack_trees_options
*o
)
2270 return verify_uptodate_1(ce
, o
, WARNING_SPARSE_NOT_UPTODATE_FILE
);
2274 * TODO: We should actually invalidate o->internal.result, not src_index [1].
2275 * But since cache tree and untracked cache both are not copied to
2276 * o->internal.result until unpacking is complete, we invalidate them on
2277 * src_index instead with the assumption that they will be copied to
2278 * dst_index at the end.
2280 * [1] src_index->cache_tree is also used in unpack_callback() so if
2281 * we invalidate o->internal.result, we need to update it to use
2282 * o->internal.result.cache_tree as well.
2284 static void invalidate_ce_path(const struct cache_entry
*ce
,
2285 struct unpack_trees_options
*o
)
2289 cache_tree_invalidate_path(o
->src_index
, ce
->name
);
2290 untracked_cache_invalidate_path(o
->src_index
, ce
->name
, 1);
2294 * Check that checking out ce->sha1 in subdir ce->name is not
2295 * going to overwrite any working files.
2297 static int verify_clean_submodule(const char *old_sha1
,
2298 const struct cache_entry
*ce
,
2299 struct unpack_trees_options
*o
)
2301 if (!submodule_from_ce(ce
))
2304 return check_submodule_move_head(ce
, old_sha1
,
2305 oid_to_hex(&ce
->oid
), o
);
2308 static int verify_clean_subdirectory(const struct cache_entry
*ce
,
2309 struct unpack_trees_options
*o
)
2312 * we are about to extract "ce->name"; we would not want to lose
2313 * anything in the existing directory there.
2317 struct dir_struct d
;
2321 if (S_ISGITLINK(ce
->ce_mode
)) {
2322 struct object_id oid
;
2323 int sub_head
= repo_resolve_gitlink_ref(the_repository
, ce
->name
,
2326 * If we are not going to update the submodule, then
2329 if (!sub_head
&& oideq(&oid
, &ce
->oid
))
2331 return verify_clean_submodule(sub_head
? NULL
: oid_to_hex(&oid
),
2336 * First let's make sure we do not have a local modification
2337 * in that directory.
2339 namelen
= ce_namelen(ce
);
2340 for (i
= locate_in_src_index(ce
, o
);
2341 i
< o
->src_index
->cache_nr
;
2343 struct cache_entry
*ce2
= o
->src_index
->cache
[i
];
2344 int len
= ce_namelen(ce2
);
2345 if (len
< namelen
||
2346 strncmp(ce
->name
, ce2
->name
, namelen
) ||
2347 ce2
->name
[namelen
] != '/')
2350 * ce2->name is an entry in the subdirectory to be
2353 if (!ce_stage(ce2
)) {
2354 if (verify_uptodate(ce2
, o
))
2356 add_entry(o
, ce2
, CE_REMOVE
, 0);
2357 invalidate_ce_path(ce
, o
);
2358 mark_ce_used(ce2
, o
);
2363 /* Do not lose a locally present file that is not ignored. */
2364 pathbuf
= xstrfmt("%.*s/", namelen
, ce
->name
);
2366 memset(&d
, 0, sizeof(d
));
2367 if (o
->internal
.dir
)
2368 setup_standard_excludes(&d
);
2369 i
= read_directory(&d
, o
->src_index
, pathbuf
, namelen
+1, NULL
);
2373 return add_rejected_path(o
, ERROR_NOT_UPTODATE_DIR
, ce
->name
);
2375 /* Do not lose startup_info->original_cwd */
2376 if (startup_info
->original_cwd
&&
2377 !strcmp(startup_info
->original_cwd
, ce
->name
))
2378 return add_rejected_path(o
, ERROR_CWD_IN_THE_WAY
, ce
->name
);
2384 * This gets called when there was no index entry for the tree entry 'dst',
2385 * but we found a file in the working tree that 'lstat()' said was fine,
2386 * and we're on a case-insensitive filesystem.
2388 * See if we can find a case-insensitive match in the index that also
2389 * matches the stat information, and assume it's that other file!
2391 static int icase_exists(struct unpack_trees_options
*o
, const char *name
, int len
, struct stat
*st
)
2393 const struct cache_entry
*src
;
2395 src
= index_file_exists(o
->src_index
, name
, len
, 1);
2396 return src
&& !ie_match_stat(o
->src_index
, src
, st
, CE_MATCH_IGNORE_VALID
|CE_MATCH_IGNORE_SKIP_WORKTREE
);
2399 enum absent_checking_type
{
2401 ABSENT_ANY_DIRECTORY
2404 static int check_ok_to_remove(const char *name
, int len
, int dtype
,
2405 const struct cache_entry
*ce
, struct stat
*st
,
2406 enum unpack_trees_error_types error_type
,
2407 enum absent_checking_type absent_type
,
2408 struct unpack_trees_options
*o
)
2410 const struct cache_entry
*result
;
2413 * It may be that the 'lstat()' succeeded even though
2414 * target 'ce' was absent, because there is an old
2415 * entry that is different only in case..
2417 * Ignore that lstat() if it matches.
2419 if (ignore_case
&& icase_exists(o
, name
, len
, st
))
2422 if (o
->internal
.dir
&&
2423 is_excluded(o
->internal
.dir
, o
->src_index
, name
, &dtype
))
2425 * ce->name is explicitly excluded, so it is Ok to
2429 if (S_ISDIR(st
->st_mode
)) {
2431 * We are checking out path "foo" and
2432 * found "foo/." in the working tree.
2433 * This is tricky -- if we have modified
2434 * files that are in "foo/" we would lose
2437 if (verify_clean_subdirectory(ce
, o
) < 0)
2442 /* If we only care about directories, then we can remove */
2443 if (absent_type
== ABSENT_ANY_DIRECTORY
)
2447 * The previous round may already have decided to
2448 * delete this path, which is in a subdirectory that
2449 * is being replaced with a blob.
2451 result
= index_file_exists(&o
->internal
.result
, name
, len
, 0);
2453 if (result
->ce_flags
& CE_REMOVE
)
2457 return add_rejected_path(o
, error_type
, name
);
2461 * We do not want to remove or overwrite a working tree file that
2462 * is not tracked, unless it is ignored.
2464 static int verify_absent_1(const struct cache_entry
*ce
,
2465 enum unpack_trees_error_types error_type
,
2466 enum absent_checking_type absent_type
,
2467 struct unpack_trees_options
*o
)
2472 if (o
->index_only
|| !o
->update
)
2475 if (o
->reset
== UNPACK_RESET_OVERWRITE_UNTRACKED
) {
2476 /* Avoid nuking startup_info->original_cwd... */
2477 if (startup_info
->original_cwd
&&
2478 !strcmp(startup_info
->original_cwd
, ce
->name
))
2479 return add_rejected_path(o
, ERROR_CWD_IN_THE_WAY
,
2481 /* ...but nuke anything else. */
2485 len
= check_leading_path(ce
->name
, ce_namelen(ce
), 0);
2492 path
= xmemdupz(ce
->name
, len
);
2493 if (lstat(path
, &st
))
2494 ret
= error_errno("cannot stat '%s'", path
);
2496 if (submodule_from_ce(ce
))
2497 ret
= check_submodule_move_head(ce
,
2498 oid_to_hex(&ce
->oid
),
2501 ret
= check_ok_to_remove(path
, len
, DT_UNKNOWN
, NULL
,
2507 } else if (lstat(ce
->name
, &st
)) {
2508 if (errno
!= ENOENT
)
2509 return error_errno("cannot stat '%s'", ce
->name
);
2512 if (submodule_from_ce(ce
))
2513 return check_submodule_move_head(ce
, oid_to_hex(&ce
->oid
),
2516 return check_ok_to_remove(ce
->name
, ce_namelen(ce
),
2517 ce_to_dtype(ce
), ce
, &st
,
2518 error_type
, absent_type
, o
);
2522 static int verify_absent(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
, COMPLETELY_ABSENT
, o
);
2531 static int verify_absent_if_directory(const struct cache_entry
*ce
,
2532 enum unpack_trees_error_types error_type
,
2533 struct unpack_trees_options
*o
)
2535 if (!o
->skip_sparse_checkout
&& (ce
->ce_flags
& CE_NEW_SKIP_WORKTREE
))
2537 return verify_absent_1(ce
, error_type
, ABSENT_ANY_DIRECTORY
, o
);
2540 static int verify_absent_sparse(const struct cache_entry
*ce
,
2541 enum unpack_trees_error_types error_type
,
2542 struct unpack_trees_options
*o
)
2544 return verify_absent_1(ce
, error_type
, COMPLETELY_ABSENT
, o
);
2547 static int merged_entry(const struct cache_entry
*ce
,
2548 const struct cache_entry
*old
,
2549 struct unpack_trees_options
*o
)
2551 int update
= CE_UPDATE
;
2552 struct cache_entry
*merge
= dup_cache_entry(ce
, &o
->internal
.result
);
2556 * New index entries. In sparse checkout, the following
2557 * verify_absent() will be delayed until after
2558 * traverse_trees() finishes in unpack_trees(), then:
2560 * - CE_NEW_SKIP_WORKTREE will be computed correctly
2561 * - verify_absent() be called again, this time with
2562 * correct CE_NEW_SKIP_WORKTREE
2564 * verify_absent() call here does nothing in sparse
2565 * checkout (i.e. o->skip_sparse_checkout == 0)
2568 merge
->ce_flags
|= CE_NEW_SKIP_WORKTREE
;
2570 if (verify_absent(merge
,
2571 ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN
, o
)) {
2572 discard_cache_entry(merge
);
2575 invalidate_ce_path(merge
, o
);
2577 if (submodule_from_ce(ce
) && file_exists(ce
->name
)) {
2578 int ret
= check_submodule_move_head(ce
, NULL
,
2579 oid_to_hex(&ce
->oid
),
2585 } else if (!(old
->ce_flags
& CE_CONFLICTED
)) {
2587 * See if we can re-use the old CE directly?
2588 * That way we get the uptodate stat info.
2590 * This also removes the UPDATE flag on a match; otherwise
2591 * we will end up overwriting local changes in the work tree.
2593 if (same(old
, merge
)) {
2594 copy_cache_entry(merge
, old
);
2597 if (verify_uptodate(old
, o
)) {
2598 discard_cache_entry(merge
);
2601 /* Migrate old flags over */
2602 update
|= old
->ce_flags
& (CE_SKIP_WORKTREE
| CE_NEW_SKIP_WORKTREE
);
2603 invalidate_ce_path(old
, o
);
2606 if (submodule_from_ce(ce
) && file_exists(ce
->name
)) {
2607 int ret
= check_submodule_move_head(ce
, oid_to_hex(&old
->oid
),
2608 oid_to_hex(&ce
->oid
),
2615 * Previously unmerged entry left as an existence
2616 * marker by read_index_unmerged();
2618 if (verify_absent_if_directory(merge
,
2619 ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN
, o
)) {
2620 discard_cache_entry(merge
);
2624 invalidate_ce_path(old
, o
);
2627 if (do_add_entry(o
, merge
, update
, CE_STAGEMASK
) < 0)
2632 static int merged_sparse_dir(const struct cache_entry
* const *src
, int n
,
2633 struct unpack_trees_options
*o
)
2635 struct tree_desc t
[MAX_UNPACK_TREES
+ 1];
2636 void * tree_bufs
[MAX_UNPACK_TREES
+ 1];
2637 struct traverse_info info
;
2641 * Create the tree traversal information for traversing into *only* the
2644 setup_traverse_info(&info
, src
[0]->name
);
2645 info
.fn
= unpack_sparse_callback
;
2647 info
.show_all_errors
= o
->internal
.show_all_errors
;
2648 info
.pathspec
= o
->pathspec
;
2650 /* Get the tree descriptors of the sparse directory in each of the merging trees */
2651 for (i
= 0; i
< n
; i
++)
2652 tree_bufs
[i
] = fill_tree_descriptor(o
->src_index
->repo
, &t
[i
],
2653 src
[i
] && !is_null_oid(&src
[i
]->oid
) ? &src
[i
]->oid
: NULL
);
2655 ret
= traverse_trees(o
->src_index
, n
, t
, &info
);
2657 for (i
= 0; i
< n
; i
++)
2663 static int deleted_entry(const struct cache_entry
*ce
,
2664 const struct cache_entry
*old
,
2665 struct unpack_trees_options
*o
)
2667 /* Did it exist in the index? */
2669 if (verify_absent(ce
, ERROR_WOULD_LOSE_UNTRACKED_REMOVED
, o
))
2672 } else if (verify_absent_if_directory(ce
, ERROR_WOULD_LOSE_UNTRACKED_REMOVED
, o
)) {
2676 if (!(old
->ce_flags
& CE_CONFLICTED
) && verify_uptodate(old
, o
))
2678 add_entry(o
, ce
, CE_REMOVE
, 0);
2679 invalidate_ce_path(ce
, o
);
2683 static int keep_entry(const struct cache_entry
*ce
,
2684 struct unpack_trees_options
*o
)
2686 add_entry(o
, ce
, 0, 0);
2688 invalidate_ce_path(ce
, o
);
2693 static void show_stage_entry(FILE *o
,
2694 const char *label
, const struct cache_entry
*ce
)
2697 fprintf(o
, "%s (missing)\n", label
);
2699 fprintf(o
, "%s%06o %s %d\t%s\n",
2702 oid_to_hex(&ce
->oid
),
2708 int threeway_merge(const struct cache_entry
* const *stages
,
2709 struct unpack_trees_options
*o
)
2711 const struct cache_entry
*index
;
2712 const struct cache_entry
*head
;
2713 const struct cache_entry
*remote
= stages
[o
->head_idx
+ 1];
2716 int remote_match
= 0;
2718 int df_conflict_head
= 0;
2719 int df_conflict_remote
= 0;
2721 int any_anc_missing
= 0;
2722 int no_anc_exists
= 1;
2725 for (i
= 1; i
< o
->head_idx
; i
++) {
2726 if (!stages
[i
] || stages
[i
] == o
->df_conflict_entry
)
2727 any_anc_missing
= 1;
2733 head
= stages
[o
->head_idx
];
2735 if (head
== o
->df_conflict_entry
) {
2736 df_conflict_head
= 1;
2740 if (remote
== o
->df_conflict_entry
) {
2741 df_conflict_remote
= 1;
2746 * First, if there's a #16 situation, note that to prevent #13
2749 if (!same(remote
, head
)) {
2750 for (i
= 1; i
< o
->head_idx
; i
++) {
2751 if (same(stages
[i
], head
)) {
2754 if (same(stages
[i
], remote
)) {
2761 * We start with cases where the index is allowed to match
2762 * something other than the head: #14(ALT) and #2ALT, where it
2763 * is permitted to match the result instead.
2765 /* #14, #14ALT, #2ALT */
2766 if (remote
&& !df_conflict_head
&& head_match
&& !remote_match
) {
2767 if (index
&& !same(index
, remote
) && !same(index
, head
)) {
2768 if (S_ISSPARSEDIR(index
->ce_mode
))
2769 return merged_sparse_dir(stages
, 4, o
);
2771 return reject_merge(index
, o
);
2773 return merged_entry(remote
, index
, o
);
2776 * If we have an entry in the index cache, then we want to
2777 * make sure that it matches head.
2779 if (index
&& !same(index
, head
)) {
2780 if (S_ISSPARSEDIR(index
->ce_mode
))
2781 return merged_sparse_dir(stages
, 4, o
);
2783 return reject_merge(index
, o
);
2788 if (same(head
, remote
))
2789 return merged_entry(head
, index
, o
);
2791 if (!df_conflict_remote
&& remote_match
&& !head_match
)
2792 return merged_entry(head
, index
, o
);
2796 if (!head
&& !remote
&& any_anc_missing
)
2800 * Under the "aggressive" rule, we resolve mostly trivial
2801 * cases that we historically had git-merge-one-file resolve.
2803 if (o
->aggressive
) {
2804 int head_deleted
= !head
;
2805 int remote_deleted
= !remote
;
2806 const struct cache_entry
*ce
= NULL
;
2815 for (i
= 1; i
< o
->head_idx
; i
++) {
2816 if (stages
[i
] && stages
[i
] != o
->df_conflict_entry
) {
2825 * Deleted in one and unchanged in the other.
2827 if ((head_deleted
&& remote_deleted
) ||
2828 (head_deleted
&& remote
&& remote_match
) ||
2829 (remote_deleted
&& head
&& head_match
)) {
2831 return deleted_entry(index
, index
, o
);
2832 if (ce
&& !head_deleted
) {
2833 if (verify_absent(ce
, ERROR_WOULD_LOSE_UNTRACKED_REMOVED
, o
))
2839 * Added in both, identically.
2841 if (no_anc_exists
&& head
&& remote
&& same(head
, remote
))
2842 return merged_entry(head
, index
, o
);
2846 /* Handle "no merge" cases (see t/t1000-read-tree-m-3way.sh) */
2849 * If we've reached the "no merge" cases and we're merging
2850 * a sparse directory, we may have an "edit/edit" conflict that
2851 * can be resolved by individually merging directory contents.
2853 if (S_ISSPARSEDIR(index
->ce_mode
))
2854 return merged_sparse_dir(stages
, 4, o
);
2857 * If we're not merging a sparse directory, ensure the index is
2858 * up-to-date to avoid files getting overwritten with conflict
2861 if (verify_uptodate(index
, o
))
2865 o
->internal
.nontrivial_merge
= 1;
2867 /* #2, #3, #4, #6, #7, #9, #10, #11. */
2869 if (!head_match
|| !remote_match
) {
2870 for (i
= 1; i
< o
->head_idx
; i
++) {
2871 if (stages
[i
] && stages
[i
] != o
->df_conflict_entry
) {
2872 keep_entry(stages
[i
], o
);
2880 fprintf(stderr
, "read-tree: warning #16 detected\n");
2881 show_stage_entry(stderr
, "head ", stages
[head_match
]);
2882 show_stage_entry(stderr
, "remote ", stages
[remote_match
]);
2885 if (head
) { count
+= keep_entry(head
, o
); }
2886 if (remote
) { count
+= keep_entry(remote
, o
); }
2893 * The rule is to "carry forward" what is in the index without losing
2894 * information across a "fast-forward", favoring a successful merge
2895 * over a merge failure when it makes sense. For details of the
2896 * "carry forward" rule, please see <Documentation/git-read-tree.txt>.
2899 int twoway_merge(const struct cache_entry
* const *src
,
2900 struct unpack_trees_options
*o
)
2902 const struct cache_entry
*current
= src
[0];
2903 const struct cache_entry
*oldtree
= src
[1];
2904 const struct cache_entry
*newtree
= src
[2];
2906 if (o
->internal
.merge_size
!= 2)
2907 return error("Cannot do a twoway merge of %d trees",
2908 o
->internal
.merge_size
);
2910 if (oldtree
== o
->df_conflict_entry
)
2912 if (newtree
== o
->df_conflict_entry
)
2916 if (current
->ce_flags
& CE_CONFLICTED
) {
2917 if (same(oldtree
, newtree
) || o
->reset
) {
2919 return deleted_entry(current
, current
, o
);
2921 return merged_entry(newtree
, current
, o
);
2923 return reject_merge(current
, o
);
2924 } else if ((!oldtree
&& !newtree
) || /* 4 and 5 */
2925 (!oldtree
&& newtree
&&
2926 same(current
, newtree
)) || /* 6 and 7 */
2927 (oldtree
&& newtree
&&
2928 same(oldtree
, newtree
)) || /* 14 and 15 */
2929 (oldtree
&& newtree
&&
2930 !same(oldtree
, newtree
) && /* 18 and 19 */
2931 same(current
, newtree
))) {
2932 return keep_entry(current
, o
);
2933 } else if (oldtree
&& !newtree
&& same(current
, oldtree
)) {
2935 return deleted_entry(oldtree
, current
, o
);
2936 } else if (oldtree
&& newtree
&&
2937 same(current
, oldtree
) && !same(current
, newtree
)) {
2939 return merged_entry(newtree
, current
, o
);
2940 } else if (current
&& !oldtree
&& newtree
&&
2941 S_ISSPARSEDIR(current
->ce_mode
) != S_ISSPARSEDIR(newtree
->ce_mode
) &&
2942 ce_stage(current
) == 0) {
2944 * This case is a directory/file conflict across the sparse-index
2945 * boundary. When we are changing from one path to another via
2946 * 'git checkout', then we want to replace one entry with another
2947 * via merged_entry(). If there are staged changes, then we should
2948 * reject the merge instead.
2950 return merged_entry(newtree
, current
, o
);
2951 } else if (S_ISSPARSEDIR(current
->ce_mode
)) {
2953 * The sparse directories differ, but we don't know whether that's
2954 * because of two different files in the directory being modified
2955 * (can be trivially merged) or if there is a real file conflict.
2956 * Merge the sparse directory by OID to compare file-by-file.
2958 return merged_sparse_dir(src
, 3, o
);
2960 return reject_merge(current
, o
);
2963 if (oldtree
&& !o
->initial_checkout
) {
2965 * deletion of the path was staged;
2967 if (same(oldtree
, newtree
))
2969 return reject_merge(oldtree
, o
);
2971 return merged_entry(newtree
, current
, o
);
2973 return deleted_entry(oldtree
, current
, o
);
2979 * Keep the index entries at stage0, collapse stage1 but make sure
2980 * stage0 does not have anything there.
2982 int bind_merge(const struct cache_entry
* const *src
,
2983 struct unpack_trees_options
*o
)
2985 const struct cache_entry
*old
= src
[0];
2986 const struct cache_entry
*a
= src
[1];
2988 if (o
->internal
.merge_size
!= 1)
2989 return error("Cannot do a bind merge of %d trees",
2990 o
->internal
.merge_size
);
2992 return o
->quiet
? -1 :
2993 error(ERRORMSG(o
, ERROR_BIND_OVERLAP
),
2994 super_prefixed(a
->name
, o
->super_prefix
),
2995 super_prefixed(old
->name
, o
->super_prefix
));
2997 return keep_entry(old
, o
);
2999 return merged_entry(a
, NULL
, o
);
3006 * - take the stat information from stage0, take the data from stage1
3008 int oneway_merge(const struct cache_entry
* const *src
,
3009 struct unpack_trees_options
*o
)
3011 const struct cache_entry
*old
= src
[0];
3012 const struct cache_entry
*a
= src
[1];
3014 if (o
->internal
.merge_size
!= 1)
3015 return error("Cannot do a oneway merge of %d trees",
3016 o
->internal
.merge_size
);
3018 if (!a
|| a
== o
->df_conflict_entry
)
3019 return deleted_entry(old
, old
, o
);
3021 if (old
&& same(old
, a
)) {
3023 if (o
->reset
&& o
->update
&& !ce_uptodate(old
) && !ce_skip_worktree(old
) &&
3024 !(old
->ce_flags
& CE_FSMONITOR_VALID
)) {
3026 if (lstat(old
->name
, &st
) ||
3027 ie_match_stat(o
->src_index
, old
, &st
, CE_MATCH_IGNORE_VALID
|CE_MATCH_IGNORE_SKIP_WORKTREE
))
3028 update
|= CE_UPDATE
;
3030 if (o
->update
&& S_ISGITLINK(old
->ce_mode
) &&
3031 should_update_submodules() && !verify_uptodate(old
, o
))
3032 update
|= CE_UPDATE
;
3033 add_entry(o
, old
, update
, CE_STAGEMASK
);
3036 return merged_entry(a
, old
, o
);
3040 * Merge worktree and untracked entries in a stash entry.
3042 * Ignore all index entries. Collapse remaining trees but make sure that they
3043 * don't have any conflicting files.
3045 int stash_worktree_untracked_merge(const struct cache_entry
* const *src
,
3046 struct unpack_trees_options
*o
)
3048 const struct cache_entry
*worktree
= src
[1];
3049 const struct cache_entry
*untracked
= src
[2];
3051 if (o
->internal
.merge_size
!= 2)
3052 BUG("invalid merge_size: %d", o
->internal
.merge_size
);
3054 if (worktree
&& untracked
)
3055 return error(_("worktree and untracked commit have duplicate entries: %s"),
3056 super_prefixed(worktree
->name
, o
->super_prefix
));
3058 return merged_entry(worktree
? worktree
: untracked
, NULL
, o
);