2 * Copyright (C) the libgit2 contributors. All rights reserved.
4 * This file is part of libgit2, distributed under the GNU GPL v2 with
5 * a Linking Exception. For full terms see the included COPYING file.
12 #include "git2/repository.h"
13 #include "git2/refs.h"
14 #include "git2/tree.h"
15 #include "git2/blob.h"
16 #include "git2/config.h"
17 #include "git2/diff.h"
18 #include "git2/submodule.h"
19 #include "git2/sys/index.h"
20 #include "git2/sys/filter.h"
21 #include "git2/merge.h"
24 #include "repository.h"
31 #include "diff_xdiff.h"
39 /* See docs/checkout-internals.md for more information */
42 CHECKOUT_ACTION__NONE
= 0,
43 CHECKOUT_ACTION__REMOVE
= 1,
44 CHECKOUT_ACTION__UPDATE_BLOB
= 2,
45 CHECKOUT_ACTION__UPDATE_SUBMODULE
= 4,
46 CHECKOUT_ACTION__CONFLICT
= 8,
47 CHECKOUT_ACTION__REMOVE_CONFLICT
= 16,
48 CHECKOUT_ACTION__UPDATE_CONFLICT
= 32,
49 CHECKOUT_ACTION__MAX
= 32,
50 CHECKOUT_ACTION__DEFER_REMOVE
= 64,
51 CHECKOUT_ACTION__REMOVE_AND_UPDATE
=
52 (CHECKOUT_ACTION__UPDATE_BLOB
| CHECKOUT_ACTION__REMOVE
),
59 git_checkout_options opts
;
60 bool opts_free_baseline
;
65 git_vector remove_conflicts
;
66 git_vector update_conflicts
;
67 git_vector
*update_reuc
;
68 git_vector
*update_names
;
72 unsigned int strategy
;
74 bool reload_submodules
;
76 size_t completed_steps
;
77 git_checkout_perfdata perfdata
;
78 git_strmap
*mkdir_map
;
79 git_attr_session attr_session
;
83 const git_index_entry
*ancestor
;
84 const git_index_entry
*ours
;
85 const git_index_entry
*theirs
;
92 } checkout_conflictdata
;
94 static int checkout_notify(
96 git_checkout_notify_t why
,
97 const git_diff_delta
*delta
,
98 const git_index_entry
*wditem
)
100 git_diff_file wdfile
;
101 const git_diff_file
*baseline
= NULL
, *target
= NULL
, *workdir
= NULL
;
102 const char *path
= NULL
;
104 if (!data
->opts
.notify_cb
||
105 (why
& data
->opts
.notify_flags
) == 0)
109 memset(&wdfile
, 0, sizeof(wdfile
));
111 git_oid_cpy(&wdfile
.id
, &wditem
->id
);
112 wdfile
.path
= wditem
->path
;
113 wdfile
.size
= wditem
->file_size
;
114 wdfile
.flags
= GIT_DIFF_FLAG_VALID_ID
;
115 wdfile
.mode
= wditem
->mode
;
123 switch (delta
->status
) {
124 case GIT_DELTA_UNMODIFIED
:
125 case GIT_DELTA_MODIFIED
:
126 case GIT_DELTA_TYPECHANGE
:
128 baseline
= &delta
->old_file
;
129 target
= &delta
->new_file
;
131 case GIT_DELTA_ADDED
:
132 case GIT_DELTA_IGNORED
:
133 case GIT_DELTA_UNTRACKED
:
134 case GIT_DELTA_UNREADABLE
:
135 target
= &delta
->new_file
;
137 case GIT_DELTA_DELETED
:
138 baseline
= &delta
->old_file
;
142 path
= delta
->old_file
.path
;
146 int error
= data
->opts
.notify_cb(
147 why
, path
, baseline
, target
, workdir
, data
->opts
.notify_payload
);
149 return giterr_set_after_callback_function(
150 error
, "git_checkout notification");
154 GIT_INLINE(bool) is_workdir_base_or_new(
155 const git_oid
*workdir_id
,
156 const git_diff_file
*baseitem
,
157 const git_diff_file
*newitem
)
159 return (git_oid__cmp(&baseitem
->id
, workdir_id
) == 0 ||
160 git_oid__cmp(&newitem
->id
, workdir_id
) == 0);
163 static bool checkout_is_workdir_modified(
165 const git_diff_file
*baseitem
,
166 const git_diff_file
*newitem
,
167 const git_index_entry
*wditem
)
170 const git_index_entry
*ie
;
172 /* handle "modified" submodule */
173 if (wditem
->mode
== GIT_FILEMODE_COMMIT
) {
175 unsigned int sm_status
= 0;
176 const git_oid
*sm_oid
= NULL
;
179 if (git_submodule_lookup(&sm
, data
->repo
, wditem
->path
) < 0) {
184 if (git_submodule_status(&sm_status
, data
->repo
, wditem
->path
, GIT_SUBMODULE_IGNORE_UNSPECIFIED
) < 0 ||
185 GIT_SUBMODULE_STATUS_IS_WD_DIRTY(sm_status
))
187 else if ((sm_oid
= git_submodule_wd_id(sm
)) == NULL
)
190 rval
= (git_oid__cmp(&baseitem
->id
, sm_oid
) != 0);
192 git_submodule_free(sm
);
196 /* Look at the cache to decide if the workdir is modified. If not,
197 * we can simply compare the oid in the cache to the baseitem instead
198 * of hashing the file. If so, we allow the checkout to proceed if the
199 * oid is identical (ie, the staged item is what we're trying to check
202 if ((ie
= git_index_get_bypath(data
->index
, wditem
->path
, 0)) != NULL
) {
203 if (git_index_time_eq(&wditem
->mtime
, &ie
->mtime
) &&
204 wditem
->file_size
== ie
->file_size
)
205 return !is_workdir_base_or_new(&ie
->id
, baseitem
, newitem
);
208 /* depending on where base is coming from, we may or may not know
209 * the actual size of the data, so we can't rely on this shortcut.
211 if (baseitem
->size
&& wditem
->file_size
!= baseitem
->size
)
214 if (git_diff__oid_for_entry(&oid
, data
->diff
, wditem
, wditem
->mode
, NULL
) < 0)
217 /* Allow the checkout if the workdir is not modified *or* if the checkout
218 * target's contents are already in the working directory.
220 return !is_workdir_base_or_new(&oid
, baseitem
, newitem
);
223 #define CHECKOUT_ACTION_IF(FLAG,YES,NO) \
224 ((data->strategy & GIT_CHECKOUT_##FLAG) ? CHECKOUT_ACTION__##YES : CHECKOUT_ACTION__##NO)
226 static int checkout_action_common(
229 const git_diff_delta
*delta
,
230 const git_index_entry
*wd
)
232 git_checkout_notify_t notify
= GIT_CHECKOUT_NOTIFY_NONE
;
234 if ((data
->strategy
& GIT_CHECKOUT_UPDATE_ONLY
) != 0)
235 *action
= (*action
& ~CHECKOUT_ACTION__REMOVE
);
237 if ((*action
& CHECKOUT_ACTION__UPDATE_BLOB
) != 0) {
238 if (S_ISGITLINK(delta
->new_file
.mode
))
239 *action
= (*action
& ~CHECKOUT_ACTION__UPDATE_BLOB
) |
240 CHECKOUT_ACTION__UPDATE_SUBMODULE
;
242 /* to "update" a symlink, we must remove the old one first */
243 if (delta
->new_file
.mode
== GIT_FILEMODE_LINK
&& wd
!= NULL
)
244 *action
|= CHECKOUT_ACTION__REMOVE
;
246 /* if the file is on disk and doesn't match our mode, force update */
248 GIT_PERMS_IS_EXEC(wd
->mode
) !=
249 GIT_PERMS_IS_EXEC(delta
->new_file
.mode
))
250 *action
|= CHECKOUT_ACTION__REMOVE
;
252 notify
= GIT_CHECKOUT_NOTIFY_UPDATED
;
255 if ((*action
& CHECKOUT_ACTION__CONFLICT
) != 0)
256 notify
= GIT_CHECKOUT_NOTIFY_CONFLICT
;
258 return checkout_notify(data
, notify
, delta
, wd
);
261 static int checkout_action_no_wd(
264 const git_diff_delta
*delta
)
268 *action
= CHECKOUT_ACTION__NONE
;
270 switch (delta
->status
) {
271 case GIT_DELTA_UNMODIFIED
: /* case 12 */
272 error
= checkout_notify(data
, GIT_CHECKOUT_NOTIFY_DIRTY
, delta
, NULL
);
275 *action
= CHECKOUT_ACTION_IF(RECREATE_MISSING
, UPDATE_BLOB
, NONE
);
277 case GIT_DELTA_ADDED
: /* case 2 or 28 (and 5 but not really) */
278 *action
= CHECKOUT_ACTION_IF(SAFE
, UPDATE_BLOB
, NONE
);
280 case GIT_DELTA_MODIFIED
: /* case 13 (and 35 but not really) */
281 *action
= CHECKOUT_ACTION_IF(RECREATE_MISSING
, UPDATE_BLOB
, CONFLICT
);
283 case GIT_DELTA_TYPECHANGE
: /* case 21 (B->T) and 28 (T->B)*/
284 if (delta
->new_file
.mode
== GIT_FILEMODE_TREE
)
285 *action
= CHECKOUT_ACTION_IF(SAFE
, UPDATE_BLOB
, NONE
);
287 case GIT_DELTA_DELETED
: /* case 8 or 25 */
288 *action
= CHECKOUT_ACTION_IF(SAFE
, REMOVE
, NONE
);
290 default: /* impossible */
294 return checkout_action_common(action
, data
, delta
, NULL
);
297 static int checkout_target_fullpath(
298 git_buf
**out
, checkout_data
*data
, const char *path
)
300 git_buf_truncate(&data
->target_path
, data
->target_len
);
302 if (path
&& git_buf_puts(&data
->target_path
, path
) < 0)
305 *out
= &data
->target_path
;
310 static bool wd_item_is_removable(
311 checkout_data
*data
, const git_index_entry
*wd
)
315 if (wd
->mode
!= GIT_FILEMODE_TREE
)
318 if (checkout_target_fullpath(&full
, data
, wd
->path
) < 0)
321 return !full
|| !git_path_contains(full
, DOT_GIT
);
324 static int checkout_queue_remove(checkout_data
*data
, const char *path
)
326 char *copy
= git_pool_strdup(&data
->pool
, path
);
327 GITERR_CHECK_ALLOC(copy
);
328 return git_vector_insert(&data
->removes
, copy
);
331 /* note that this advances the iterator over the wd item */
332 static int checkout_action_wd_only(
334 git_iterator
*workdir
,
335 const git_index_entry
**wditem
,
336 git_vector
*pathspec
)
340 git_checkout_notify_t notify
= GIT_CHECKOUT_NOTIFY_NONE
;
341 const git_index_entry
*wd
= *wditem
;
343 if (!git_pathspec__match(
345 (data
->strategy
& GIT_CHECKOUT_DISABLE_PATHSPEC_MATCH
) != 0,
346 git_iterator_ignore_case(workdir
), NULL
, NULL
))
347 return git_iterator_advance(wditem
, workdir
);
349 /* check if item is tracked in the index but not in the checkout diff */
350 if (data
->index
!= NULL
) {
353 error
= git_index__find_pos(
354 &pos
, data
->index
, wd
->path
, 0, GIT_INDEX_STAGE_ANY
);
356 if (wd
->mode
!= GIT_FILEMODE_TREE
) {
357 if (!error
) { /* found by git_index__find_pos call */
358 notify
= GIT_CHECKOUT_NOTIFY_DIRTY
;
359 remove
= ((data
->strategy
& GIT_CHECKOUT_FORCE
) != 0);
360 } else if (error
!= GIT_ENOTFOUND
)
363 error
= 0; /* git_index__find_pos does not set error msg */
365 /* for tree entries, we have to see if there are any index
366 * entries that are contained inside that tree
368 const git_index_entry
*e
= git_index_get_byindex(data
->index
, pos
);
370 if (e
!= NULL
&& data
->diff
->pfxcomp(e
->path
, wd
->path
) == 0) {
371 notify
= GIT_CHECKOUT_NOTIFY_DIRTY
;
372 remove
= ((data
->strategy
& GIT_CHECKOUT_FORCE
) != 0);
377 if (notify
!= GIT_CHECKOUT_NOTIFY_NONE
) {
378 /* if we found something in the index, notify and advance */
379 if ((error
= checkout_notify(data
, notify
, NULL
, wd
)) != 0)
382 if (remove
&& wd_item_is_removable(data
, wd
))
383 error
= checkout_queue_remove(data
, wd
->path
);
386 error
= git_iterator_advance(wditem
, workdir
);
388 /* untracked or ignored - can't know which until we advance through */
389 bool over
= false, removable
= wd_item_is_removable(data
, wd
);
390 git_iterator_status_t untracked_state
;
392 /* copy the entry for issuing notification callback later */
393 git_index_entry saved_wd
= *wd
;
394 git_buf_sets(&data
->tmp
, wd
->path
);
395 saved_wd
.path
= data
->tmp
.ptr
;
397 error
= git_iterator_advance_over(
398 wditem
, &untracked_state
, workdir
);
399 if (error
== GIT_ITEROVER
)
404 if (untracked_state
== GIT_ITERATOR_STATUS_IGNORED
) {
405 notify
= GIT_CHECKOUT_NOTIFY_IGNORED
;
406 remove
= ((data
->strategy
& GIT_CHECKOUT_REMOVE_IGNORED
) != 0);
408 notify
= GIT_CHECKOUT_NOTIFY_UNTRACKED
;
409 remove
= ((data
->strategy
& GIT_CHECKOUT_REMOVE_UNTRACKED
) != 0);
412 if ((error
= checkout_notify(data
, notify
, NULL
, &saved_wd
)) != 0)
415 if (remove
&& removable
)
416 error
= checkout_queue_remove(data
, saved_wd
.path
);
418 if (!error
&& over
) /* restore ITEROVER if needed */
419 error
= GIT_ITEROVER
;
425 static bool submodule_is_config_only(
429 git_submodule
*sm
= NULL
;
430 unsigned int sm_loc
= 0;
433 if (git_submodule_lookup(&sm
, data
->repo
, path
) < 0)
436 if (git_submodule_location(&sm_loc
, sm
) < 0 ||
437 sm_loc
== GIT_SUBMODULE_STATUS_IN_CONFIG
)
440 git_submodule_free(sm
);
445 static bool checkout_is_empty_dir(checkout_data
*data
, const char *path
)
449 if (checkout_target_fullpath(&fullpath
, data
, path
) < 0)
452 return git_path_is_empty_dir(fullpath
->ptr
);
455 static int checkout_action_with_wd(
458 const git_diff_delta
*delta
,
459 git_iterator
*workdir
,
460 const git_index_entry
*wd
)
462 *action
= CHECKOUT_ACTION__NONE
;
464 switch (delta
->status
) {
465 case GIT_DELTA_UNMODIFIED
: /* case 14/15 or 33 */
466 if (checkout_is_workdir_modified(data
, &delta
->old_file
, &delta
->new_file
, wd
)) {
468 checkout_notify(data
, GIT_CHECKOUT_NOTIFY_DIRTY
, delta
, wd
) );
469 *action
= CHECKOUT_ACTION_IF(FORCE
, UPDATE_BLOB
, NONE
);
472 case GIT_DELTA_ADDED
: /* case 3, 4 or 6 */
473 if (git_iterator_current_is_ignored(workdir
))
474 *action
= CHECKOUT_ACTION_IF(DONT_OVERWRITE_IGNORED
, CONFLICT
, UPDATE_BLOB
);
476 *action
= CHECKOUT_ACTION_IF(FORCE
, UPDATE_BLOB
, CONFLICT
);
478 case GIT_DELTA_DELETED
: /* case 9 or 10 (or 26 but not really) */
479 if (checkout_is_workdir_modified(data
, &delta
->old_file
, &delta
->new_file
, wd
))
480 *action
= CHECKOUT_ACTION_IF(FORCE
, REMOVE
, CONFLICT
);
482 *action
= CHECKOUT_ACTION_IF(SAFE
, REMOVE
, NONE
);
484 case GIT_DELTA_MODIFIED
: /* case 16, 17, 18 (or 36 but not really) */
485 if (wd
->mode
!= GIT_FILEMODE_COMMIT
&&
486 checkout_is_workdir_modified(data
, &delta
->old_file
, &delta
->new_file
, wd
))
487 *action
= CHECKOUT_ACTION_IF(FORCE
, UPDATE_BLOB
, CONFLICT
);
489 *action
= CHECKOUT_ACTION_IF(SAFE
, UPDATE_BLOB
, NONE
);
491 case GIT_DELTA_TYPECHANGE
: /* case 22, 23, 29, 30 */
492 if (delta
->old_file
.mode
== GIT_FILEMODE_TREE
) {
493 if (wd
->mode
== GIT_FILEMODE_TREE
)
494 /* either deleting items in old tree will delete the wd dir,
495 * or we'll get a conflict when we attempt blob update...
497 *action
= CHECKOUT_ACTION_IF(SAFE
, UPDATE_BLOB
, NONE
);
498 else if (wd
->mode
== GIT_FILEMODE_COMMIT
) {
499 /* workdir is possibly a "phantom" submodule - treat as a
500 * tree if the only submodule info came from the config
502 if (submodule_is_config_only(data
, wd
->path
))
503 *action
= CHECKOUT_ACTION_IF(SAFE
, UPDATE_BLOB
, NONE
);
505 *action
= CHECKOUT_ACTION_IF(FORCE
, REMOVE_AND_UPDATE
, CONFLICT
);
507 *action
= CHECKOUT_ACTION_IF(FORCE
, REMOVE
, CONFLICT
);
509 else if (checkout_is_workdir_modified(data
, &delta
->old_file
, &delta
->new_file
, wd
))
510 *action
= CHECKOUT_ACTION_IF(FORCE
, REMOVE_AND_UPDATE
, CONFLICT
);
512 *action
= CHECKOUT_ACTION_IF(SAFE
, REMOVE_AND_UPDATE
, NONE
);
514 /* don't update if the typechange is to a tree */
515 if (delta
->new_file
.mode
== GIT_FILEMODE_TREE
)
516 *action
= (*action
& ~CHECKOUT_ACTION__UPDATE_BLOB
);
518 default: /* impossible */
522 return checkout_action_common(action
, data
, delta
, wd
);
525 static int checkout_action_with_wd_blocker(
528 const git_diff_delta
*delta
,
529 const git_index_entry
*wd
)
531 *action
= CHECKOUT_ACTION__NONE
;
533 switch (delta
->status
) {
534 case GIT_DELTA_UNMODIFIED
:
535 /* should show delta as dirty / deleted */
537 checkout_notify(data
, GIT_CHECKOUT_NOTIFY_DIRTY
, delta
, wd
) );
538 *action
= CHECKOUT_ACTION_IF(FORCE
, REMOVE_AND_UPDATE
, NONE
);
540 case GIT_DELTA_ADDED
:
541 case GIT_DELTA_MODIFIED
:
542 *action
= CHECKOUT_ACTION_IF(FORCE
, REMOVE_AND_UPDATE
, CONFLICT
);
544 case GIT_DELTA_DELETED
:
545 *action
= CHECKOUT_ACTION_IF(FORCE
, REMOVE
, CONFLICT
);
547 case GIT_DELTA_TYPECHANGE
:
548 /* not 100% certain about this... */
549 *action
= CHECKOUT_ACTION_IF(FORCE
, REMOVE_AND_UPDATE
, CONFLICT
);
551 default: /* impossible */
555 return checkout_action_common(action
, data
, delta
, wd
);
558 static int checkout_action_with_wd_dir(
561 const git_diff_delta
*delta
,
562 git_iterator
*workdir
,
563 const git_index_entry
*wd
)
565 *action
= CHECKOUT_ACTION__NONE
;
567 switch (delta
->status
) {
568 case GIT_DELTA_UNMODIFIED
: /* case 19 or 24 (or 34 but not really) */
570 checkout_notify(data
, GIT_CHECKOUT_NOTIFY_DIRTY
, delta
, NULL
));
572 checkout_notify(data
, GIT_CHECKOUT_NOTIFY_UNTRACKED
, NULL
, wd
));
573 *action
= CHECKOUT_ACTION_IF(FORCE
, REMOVE_AND_UPDATE
, NONE
);
575 case GIT_DELTA_ADDED
:/* case 4 (and 7 for dir) */
576 case GIT_DELTA_MODIFIED
: /* case 20 (or 37 but not really) */
577 if (delta
->old_file
.mode
== GIT_FILEMODE_COMMIT
)
578 /* expected submodule (and maybe found one) */;
579 else if (delta
->new_file
.mode
!= GIT_FILEMODE_TREE
)
580 *action
= git_iterator_current_is_ignored(workdir
) ?
581 CHECKOUT_ACTION_IF(DONT_OVERWRITE_IGNORED
, CONFLICT
, REMOVE_AND_UPDATE
) :
582 CHECKOUT_ACTION_IF(FORCE
, REMOVE_AND_UPDATE
, CONFLICT
);
584 case GIT_DELTA_DELETED
: /* case 11 (and 27 for dir) */
585 if (delta
->old_file
.mode
!= GIT_FILEMODE_TREE
)
587 checkout_notify(data
, GIT_CHECKOUT_NOTIFY_UNTRACKED
, NULL
, wd
));
589 case GIT_DELTA_TYPECHANGE
: /* case 24 or 31 */
590 if (delta
->old_file
.mode
== GIT_FILEMODE_TREE
) {
591 /* For typechange from dir, remove dir and add blob, but it is
592 * not safe to remove dir if it contains modified files.
593 * However, safely removing child files will remove the parent
594 * directory if is it left empty, so we can defer removing the
595 * dir and it will succeed if no children are left.
597 *action
= CHECKOUT_ACTION_IF(SAFE
, UPDATE_BLOB
, NONE
);
599 else if (delta
->new_file
.mode
!= GIT_FILEMODE_TREE
)
600 /* For typechange to dir, dir is already created so no action */
601 *action
= CHECKOUT_ACTION_IF(FORCE
, REMOVE_AND_UPDATE
, CONFLICT
);
603 default: /* impossible */
607 return checkout_action_common(action
, data
, delta
, wd
);
610 static int checkout_action_with_wd_dir_empty(
613 const git_diff_delta
*delta
)
615 int error
= checkout_action_no_wd(action
, data
, delta
);
617 /* We can always safely remove an empty directory. */
618 if (error
== 0 && *action
!= CHECKOUT_ACTION__NONE
)
619 *action
|= CHECKOUT_ACTION__REMOVE
;
624 static int checkout_action(
627 git_diff_delta
*delta
,
628 git_iterator
*workdir
,
629 const git_index_entry
**wditem
,
630 git_vector
*pathspec
)
633 int (*strcomp
)(const char *, const char *) = data
->diff
->strcomp
;
634 int (*pfxcomp
)(const char *str
, const char *pfx
) = data
->diff
->pfxcomp
;
635 int (*advance
)(const git_index_entry
**, git_iterator
*) = NULL
;
637 /* move workdir iterator to follow along with deltas */
640 const git_index_entry
*wd
= *wditem
;
643 return checkout_action_no_wd(action
, data
, delta
);
645 cmp
= strcomp(wd
->path
, delta
->old_file
.path
);
647 /* 1. wd before delta ("a/a" before "a/b")
648 * 2. wd prefixes delta & should expand ("a/" before "a/b")
649 * 3. wd prefixes delta & cannot expand ("a/b" before "a/b/c")
650 * 4. wd equals delta ("a/b" and "a/b")
651 * 5. wd after delta & delta prefixes wd ("a/b/c" after "a/b/" or "a/b")
652 * 6. wd after delta ("a/c" after "a/b")
656 cmp
= pfxcomp(delta
->old_file
.path
, wd
->path
);
659 if (wd
->mode
== GIT_FILEMODE_TREE
) {
660 /* case 2 - entry prefixed by workdir tree */
661 error
= git_iterator_advance_into(wditem
, workdir
);
662 if (error
< 0 && error
!= GIT_ITEROVER
)
667 /* case 3 maybe - wd contains non-dir where dir expected */
668 if (delta
->old_file
.path
[strlen(wd
->path
)] == '/') {
669 error
= checkout_action_with_wd_blocker(
670 action
, data
, delta
, wd
);
671 advance
= git_iterator_advance
;
676 /* case 1 - handle wd item (if it matches pathspec) */
677 error
= checkout_action_wd_only(data
, workdir
, wditem
, pathspec
);
678 if (error
&& error
!= GIT_ITEROVER
)
685 error
= checkout_action_with_wd(action
, data
, delta
, workdir
, wd
);
686 advance
= git_iterator_advance
;
690 cmp
= pfxcomp(wd
->path
, delta
->old_file
.path
);
692 if (cmp
== 0) { /* case 5 */
693 if (wd
->path
[strlen(delta
->old_file
.path
)] != '/')
694 return checkout_action_no_wd(action
, data
, delta
);
696 if (delta
->status
== GIT_DELTA_TYPECHANGE
) {
697 if (delta
->old_file
.mode
== GIT_FILEMODE_TREE
) {
698 error
= checkout_action_with_wd(action
, data
, delta
, workdir
, wd
);
699 advance
= git_iterator_advance_into
;
703 if (delta
->new_file
.mode
== GIT_FILEMODE_TREE
||
704 delta
->new_file
.mode
== GIT_FILEMODE_COMMIT
||
705 delta
->old_file
.mode
== GIT_FILEMODE_COMMIT
)
707 error
= checkout_action_with_wd(action
, data
, delta
, workdir
, wd
);
708 advance
= git_iterator_advance
;
713 return checkout_is_empty_dir(data
, wd
->path
) ?
714 checkout_action_with_wd_dir_empty(action
, data
, delta
) :
715 checkout_action_with_wd_dir(action
, data
, delta
, workdir
, wd
);
718 /* case 6 - wd is after delta */
719 return checkout_action_no_wd(action
, data
, delta
);
723 if (!error
&& advance
!= NULL
&&
724 (error
= advance(wditem
, workdir
)) < 0) {
726 if (error
== GIT_ITEROVER
)
733 static int checkout_remaining_wd_items(
735 git_iterator
*workdir
,
736 const git_index_entry
*wd
,
742 error
= checkout_action_wd_only(data
, workdir
, &wd
, spec
);
744 if (error
== GIT_ITEROVER
)
750 GIT_INLINE(int) checkout_idxentry_cmp(
751 const git_index_entry
*a
,
752 const git_index_entry
*b
)
761 return strcmp(a
->path
, b
->path
);
764 static int checkout_conflictdata_cmp(const void *a
, const void *b
)
766 const checkout_conflictdata
*ca
= a
;
767 const checkout_conflictdata
*cb
= b
;
770 if ((diff
= checkout_idxentry_cmp(ca
->ancestor
, cb
->ancestor
)) == 0 &&
771 (diff
= checkout_idxentry_cmp(ca
->ours
, cb
->theirs
)) == 0)
772 diff
= checkout_idxentry_cmp(ca
->theirs
, cb
->theirs
);
777 int checkout_conflictdata_empty(
778 const git_vector
*conflicts
, size_t idx
, void *payload
)
780 checkout_conflictdata
*conflict
;
784 if ((conflict
= git_vector_get(conflicts
, idx
)) == NULL
)
787 if (conflict
->ancestor
|| conflict
->ours
|| conflict
->theirs
)
794 GIT_INLINE(bool) conflict_pathspec_match(
796 git_iterator
*workdir
,
797 git_vector
*pathspec
,
798 const git_index_entry
*ancestor
,
799 const git_index_entry
*ours
,
800 const git_index_entry
*theirs
)
802 /* if the pathspec matches ours *or* theirs, proceed */
803 if (ours
&& git_pathspec__match(pathspec
, ours
->path
,
804 (data
->strategy
& GIT_CHECKOUT_DISABLE_PATHSPEC_MATCH
) != 0,
805 git_iterator_ignore_case(workdir
), NULL
, NULL
))
808 if (theirs
&& git_pathspec__match(pathspec
, theirs
->path
,
809 (data
->strategy
& GIT_CHECKOUT_DISABLE_PATHSPEC_MATCH
) != 0,
810 git_iterator_ignore_case(workdir
), NULL
, NULL
))
813 if (ancestor
&& git_pathspec__match(pathspec
, ancestor
->path
,
814 (data
->strategy
& GIT_CHECKOUT_DISABLE_PATHSPEC_MATCH
) != 0,
815 git_iterator_ignore_case(workdir
), NULL
, NULL
))
821 GIT_INLINE(int) checkout_conflict_detect_submodule(checkout_conflictdata
*conflict
)
823 conflict
->submodule
= ((conflict
->ancestor
&& S_ISGITLINK(conflict
->ancestor
->mode
)) ||
824 (conflict
->ours
&& S_ISGITLINK(conflict
->ours
->mode
)) ||
825 (conflict
->theirs
&& S_ISGITLINK(conflict
->theirs
->mode
)));
829 GIT_INLINE(int) checkout_conflict_detect_binary(git_repository
*repo
, checkout_conflictdata
*conflict
)
831 git_blob
*ancestor_blob
= NULL
, *our_blob
= NULL
, *their_blob
= NULL
;
834 if (conflict
->submodule
)
837 if (conflict
->ancestor
) {
838 if ((error
= git_blob_lookup(&ancestor_blob
, repo
, &conflict
->ancestor
->id
)) < 0)
841 conflict
->binary
= git_blob_is_binary(ancestor_blob
);
844 if (!conflict
->binary
&& conflict
->ours
) {
845 if ((error
= git_blob_lookup(&our_blob
, repo
, &conflict
->ours
->id
)) < 0)
848 conflict
->binary
= git_blob_is_binary(our_blob
);
851 if (!conflict
->binary
&& conflict
->theirs
) {
852 if ((error
= git_blob_lookup(&their_blob
, repo
, &conflict
->theirs
->id
)) < 0)
855 conflict
->binary
= git_blob_is_binary(their_blob
);
859 git_blob_free(ancestor_blob
);
860 git_blob_free(our_blob
);
861 git_blob_free(their_blob
);
866 static int checkout_conflict_append_update(
867 const git_index_entry
*ancestor
,
868 const git_index_entry
*ours
,
869 const git_index_entry
*theirs
,
872 checkout_data
*data
= payload
;
873 checkout_conflictdata
*conflict
;
876 conflict
= git__calloc(1, sizeof(checkout_conflictdata
));
877 GITERR_CHECK_ALLOC(conflict
);
879 conflict
->ancestor
= ancestor
;
880 conflict
->ours
= ours
;
881 conflict
->theirs
= theirs
;
883 if ((error
= checkout_conflict_detect_submodule(conflict
)) < 0 ||
884 (error
= checkout_conflict_detect_binary(data
->repo
, conflict
)) < 0)
890 if (git_vector_insert(&data
->update_conflicts
, conflict
))
896 static int checkout_conflicts_foreach(
899 git_iterator
*workdir
,
900 git_vector
*pathspec
,
901 int (*cb
)(const git_index_entry
*, const git_index_entry
*, const git_index_entry
*, void *),
904 git_index_conflict_iterator
*iterator
= NULL
;
905 const git_index_entry
*ancestor
, *ours
, *theirs
;
908 if ((error
= git_index_conflict_iterator_new(&iterator
, index
)) < 0)
911 /* Collect the conflicts */
912 while ((error
= git_index_conflict_next(&ancestor
, &ours
, &theirs
, iterator
)) == 0) {
913 if (!conflict_pathspec_match(data
, workdir
, pathspec
, ancestor
, ours
, theirs
))
916 if ((error
= cb(ancestor
, ours
, theirs
, payload
)) < 0)
920 if (error
== GIT_ITEROVER
)
924 git_index_conflict_iterator_free(iterator
);
929 static int checkout_conflicts_load(checkout_data
*data
, git_iterator
*workdir
, git_vector
*pathspec
)
933 /* Only write conficts from sources that have them: indexes. */
934 if ((index
= git_iterator_index(data
->target
)) == NULL
)
937 data
->update_conflicts
._cmp
= checkout_conflictdata_cmp
;
939 if (checkout_conflicts_foreach(data
, index
, workdir
, pathspec
, checkout_conflict_append_update
, data
) < 0)
942 /* Collect the REUC and NAME entries */
943 data
->update_reuc
= &index
->reuc
;
944 data
->update_names
= &index
->names
;
949 GIT_INLINE(int) checkout_conflicts_cmp_entry(
951 const git_index_entry
*entry
)
953 return strcmp((const char *)path
, entry
->path
);
956 static int checkout_conflicts_cmp_ancestor(const void *p
, const void *c
)
958 const char *path
= p
;
959 const checkout_conflictdata
*conflict
= c
;
961 if (!conflict
->ancestor
)
964 return checkout_conflicts_cmp_entry(path
, conflict
->ancestor
);
967 static checkout_conflictdata
*checkout_conflicts_search_ancestor(
973 if (git_vector_bsearch2(&pos
, &data
->update_conflicts
, checkout_conflicts_cmp_ancestor
, path
) < 0)
976 return git_vector_get(&data
->update_conflicts
, pos
);
979 static checkout_conflictdata
*checkout_conflicts_search_branch(
983 checkout_conflictdata
*conflict
;
986 git_vector_foreach(&data
->update_conflicts
, i
, conflict
) {
989 if (conflict
->ancestor
)
993 cmp
= checkout_conflicts_cmp_entry(path
, conflict
->ours
);
994 else if (conflict
->theirs
)
995 cmp
= checkout_conflicts_cmp_entry(path
, conflict
->theirs
);
1004 static int checkout_conflicts_load_byname_entry(
1005 checkout_conflictdata
**ancestor_out
,
1006 checkout_conflictdata
**ours_out
,
1007 checkout_conflictdata
**theirs_out
,
1008 checkout_data
*data
,
1009 const git_index_name_entry
*name_entry
)
1011 checkout_conflictdata
*ancestor
, *ours
= NULL
, *theirs
= NULL
;
1014 *ancestor_out
= NULL
;
1018 if (!name_entry
->ancestor
) {
1019 giterr_set(GITERR_INDEX
, "A NAME entry exists without an ancestor");
1024 if (!name_entry
->ours
&& !name_entry
->theirs
) {
1025 giterr_set(GITERR_INDEX
, "A NAME entry exists without an ours or theirs");
1030 if ((ancestor
= checkout_conflicts_search_ancestor(data
,
1031 name_entry
->ancestor
)) == NULL
) {
1032 giterr_set(GITERR_INDEX
,
1033 "A NAME entry referenced ancestor entry '%s' which does not exist in the main index",
1034 name_entry
->ancestor
);
1039 if (name_entry
->ours
) {
1040 if (strcmp(name_entry
->ancestor
, name_entry
->ours
) == 0)
1042 else if ((ours
= checkout_conflicts_search_branch(data
, name_entry
->ours
)) == NULL
||
1043 ours
->ours
== NULL
) {
1044 giterr_set(GITERR_INDEX
,
1045 "A NAME entry referenced our entry '%s' which does not exist in the main index",
1052 if (name_entry
->theirs
) {
1053 if (strcmp(name_entry
->ancestor
, name_entry
->theirs
) == 0)
1055 else if (name_entry
->ours
&& strcmp(name_entry
->ours
, name_entry
->theirs
) == 0)
1057 else if ((theirs
= checkout_conflicts_search_branch(data
, name_entry
->theirs
)) == NULL
||
1058 theirs
->theirs
== NULL
) {
1059 giterr_set(GITERR_INDEX
,
1060 "A NAME entry referenced their entry '%s' which does not exist in the main index",
1061 name_entry
->theirs
);
1067 *ancestor_out
= ancestor
;
1069 *theirs_out
= theirs
;
1075 static int checkout_conflicts_coalesce_renames(
1076 checkout_data
*data
)
1079 const git_index_name_entry
*name_entry
;
1080 checkout_conflictdata
*ancestor_conflict
, *our_conflict
, *their_conflict
;
1084 if ((index
= git_iterator_index(data
->target
)) == NULL
)
1087 /* Juggle entries based on renames */
1088 names
= git_index_name_entrycount(index
);
1090 for (i
= 0; i
< names
; i
++) {
1091 name_entry
= git_index_name_get_byindex(index
, i
);
1093 if ((error
= checkout_conflicts_load_byname_entry(
1094 &ancestor_conflict
, &our_conflict
, &their_conflict
,
1095 data
, name_entry
)) < 0)
1098 if (our_conflict
&& our_conflict
!= ancestor_conflict
) {
1099 ancestor_conflict
->ours
= our_conflict
->ours
;
1100 our_conflict
->ours
= NULL
;
1102 if (our_conflict
->theirs
)
1103 our_conflict
->name_collision
= 1;
1105 if (our_conflict
->name_collision
)
1106 ancestor_conflict
->name_collision
= 1;
1109 if (their_conflict
&& their_conflict
!= ancestor_conflict
) {
1110 ancestor_conflict
->theirs
= their_conflict
->theirs
;
1111 their_conflict
->theirs
= NULL
;
1113 if (their_conflict
->ours
)
1114 their_conflict
->name_collision
= 1;
1116 if (their_conflict
->name_collision
)
1117 ancestor_conflict
->name_collision
= 1;
1120 if (our_conflict
&& our_conflict
!= ancestor_conflict
&&
1121 their_conflict
&& their_conflict
!= ancestor_conflict
)
1122 ancestor_conflict
->one_to_two
= 1;
1125 git_vector_remove_matching(
1126 &data
->update_conflicts
, checkout_conflictdata_empty
, NULL
);
1132 static int checkout_conflicts_mark_directoryfile(
1133 checkout_data
*data
)
1136 checkout_conflictdata
*conflict
;
1137 const git_index_entry
*entry
;
1140 int prefixed
, error
= 0;
1142 if ((index
= git_iterator_index(data
->target
)) == NULL
)
1145 len
= git_index_entrycount(index
);
1147 /* Find d/f conflicts */
1148 git_vector_foreach(&data
->update_conflicts
, i
, conflict
) {
1149 if ((conflict
->ours
&& conflict
->theirs
) ||
1150 (!conflict
->ours
&& !conflict
->theirs
))
1153 path
= conflict
->ours
?
1154 conflict
->ours
->path
: conflict
->theirs
->path
;
1156 if ((error
= git_index_find(&j
, index
, path
)) < 0) {
1157 if (error
== GIT_ENOTFOUND
)
1158 giterr_set(GITERR_INDEX
,
1159 "Index inconsistency, could not find entry for expected conflict '%s'", path
);
1164 for (; j
< len
; j
++) {
1165 if ((entry
= git_index_get_byindex(index
, j
)) == NULL
) {
1166 giterr_set(GITERR_INDEX
,
1167 "Index inconsistency, truncated index while loading expected conflict '%s'", path
);
1172 prefixed
= git_path_equal_or_prefixed(path
, entry
->path
, NULL
);
1174 if (prefixed
== GIT_PATH_EQUAL
)
1177 if (prefixed
== GIT_PATH_PREFIX
)
1178 conflict
->directoryfile
= 1;
1188 static int checkout_get_update_conflicts(
1189 checkout_data
*data
,
1190 git_iterator
*workdir
,
1191 git_vector
*pathspec
)
1195 if (data
->strategy
& GIT_CHECKOUT_SKIP_UNMERGED
)
1198 if ((error
= checkout_conflicts_load(data
, workdir
, pathspec
)) < 0 ||
1199 (error
= checkout_conflicts_coalesce_renames(data
)) < 0 ||
1200 (error
= checkout_conflicts_mark_directoryfile(data
)) < 0)
1207 static int checkout_conflict_append_remove(
1208 const git_index_entry
*ancestor
,
1209 const git_index_entry
*ours
,
1210 const git_index_entry
*theirs
,
1213 checkout_data
*data
= payload
;
1216 assert(ancestor
|| ours
|| theirs
);
1219 name
= git__strdup(ancestor
->path
);
1221 name
= git__strdup(ours
->path
);
1223 name
= git__strdup(theirs
->path
);
1227 GITERR_CHECK_ALLOC(name
);
1229 return git_vector_insert(&data
->remove_conflicts
, (char *)name
);
1232 static int checkout_get_remove_conflicts(
1233 checkout_data
*data
,
1234 git_iterator
*workdir
,
1235 git_vector
*pathspec
)
1237 if ((data
->strategy
& GIT_CHECKOUT_DONT_UPDATE_INDEX
) != 0)
1240 return checkout_conflicts_foreach(data
, data
->index
, workdir
, pathspec
, checkout_conflict_append_remove
, data
);
1243 static int checkout_verify_paths(
1244 git_repository
*repo
,
1246 git_diff_delta
*delta
)
1248 unsigned int flags
= GIT_PATH_REJECT_WORKDIR_DEFAULTS
;
1250 if (action
& CHECKOUT_ACTION__REMOVE
) {
1251 if (!git_path_isvalid(repo
, delta
->old_file
.path
, flags
)) {
1252 giterr_set(GITERR_CHECKOUT
, "Cannot remove invalid path '%s'", delta
->old_file
.path
);
1257 if (action
& ~CHECKOUT_ACTION__REMOVE
) {
1258 if (!git_path_isvalid(repo
, delta
->new_file
.path
, flags
)) {
1259 giterr_set(GITERR_CHECKOUT
, "Cannot checkout to invalid path '%s'", delta
->new_file
.path
);
1267 static int checkout_get_actions(
1268 uint32_t **actions_ptr
,
1269 size_t **counts_ptr
,
1270 checkout_data
*data
,
1271 git_iterator
*workdir
)
1274 const git_index_entry
*wditem
;
1275 git_vector pathspec
= GIT_VECTOR_INIT
, *deltas
;
1277 git_diff_delta
*delta
;
1278 size_t i
, *counts
= NULL
;
1279 uint32_t *actions
= NULL
;
1281 git_pool_init(&pathpool
, 1);
1283 if (data
->opts
.paths
.count
> 0 &&
1284 git_pathspec__vinit(&pathspec
, &data
->opts
.paths
, &pathpool
) < 0)
1287 if ((error
= git_iterator_current(&wditem
, workdir
)) < 0 &&
1288 error
!= GIT_ITEROVER
)
1291 deltas
= &data
->diff
->deltas
;
1293 *counts_ptr
= counts
= git__calloc(CHECKOUT_ACTION__MAX
+1, sizeof(size_t));
1294 *actions_ptr
= actions
= git__calloc(
1295 deltas
->length
? deltas
->length
: 1, sizeof(uint32_t));
1296 if (!counts
|| !actions
) {
1301 git_vector_foreach(deltas
, i
, delta
) {
1302 if ((error
= checkout_action(&act
, data
, delta
, workdir
, &wditem
, &pathspec
)) == 0)
1303 error
= checkout_verify_paths(data
->repo
, act
, delta
);
1310 if (act
& CHECKOUT_ACTION__REMOVE
)
1311 counts
[CHECKOUT_ACTION__REMOVE
]++;
1312 if (act
& CHECKOUT_ACTION__UPDATE_BLOB
)
1313 counts
[CHECKOUT_ACTION__UPDATE_BLOB
]++;
1314 if (act
& CHECKOUT_ACTION__UPDATE_SUBMODULE
)
1315 counts
[CHECKOUT_ACTION__UPDATE_SUBMODULE
]++;
1316 if (act
& CHECKOUT_ACTION__CONFLICT
)
1317 counts
[CHECKOUT_ACTION__CONFLICT
]++;
1320 error
= checkout_remaining_wd_items(data
, workdir
, wditem
, &pathspec
);
1324 counts
[CHECKOUT_ACTION__REMOVE
] += data
->removes
.length
;
1326 if (counts
[CHECKOUT_ACTION__CONFLICT
] > 0 &&
1327 (data
->strategy
& GIT_CHECKOUT_ALLOW_CONFLICTS
) == 0)
1329 giterr_set(GITERR_CHECKOUT
, "%"PRIuZ
" %s checkout",
1330 counts
[CHECKOUT_ACTION__CONFLICT
],
1331 counts
[CHECKOUT_ACTION__CONFLICT
] == 1 ?
1332 "conflict prevents" : "conflicts prevent");
1333 error
= GIT_ECONFLICT
;
1338 if ((error
= checkout_get_remove_conflicts(data
, workdir
, &pathspec
)) < 0 ||
1339 (error
= checkout_get_update_conflicts(data
, workdir
, &pathspec
)) < 0)
1342 counts
[CHECKOUT_ACTION__REMOVE_CONFLICT
] = git_vector_length(&data
->remove_conflicts
);
1343 counts
[CHECKOUT_ACTION__UPDATE_CONFLICT
] = git_vector_length(&data
->update_conflicts
);
1345 git_pathspec__vfree(&pathspec
);
1346 git_pool_clear(&pathpool
);
1353 *actions_ptr
= NULL
;
1356 git_pathspec__vfree(&pathspec
);
1357 git_pool_clear(&pathpool
);
1362 static bool should_remove_existing(checkout_data
*data
)
1366 if (git_repository__cvar(&ignorecase
, data
->repo
, GIT_CVAR_IGNORECASE
) < 0) {
1370 return (ignorecase
&&
1371 (data
->strategy
& GIT_CHECKOUT_DONT_REMOVE_EXISTING
) == 0);
1374 #define MKDIR_NORMAL \
1375 GIT_MKDIR_PATH | GIT_MKDIR_VERIFY_DIR
1376 #define MKDIR_REMOVE_EXISTING \
1377 MKDIR_NORMAL | GIT_MKDIR_REMOVE_FILES | GIT_MKDIR_REMOVE_SYMLINKS
1379 static int checkout_mkdir(
1380 checkout_data
*data
,
1386 struct git_futils_mkdir_options mkdir_opts
= {0};
1389 mkdir_opts
.dir_map
= data
->mkdir_map
;
1390 mkdir_opts
.pool
= &data
->pool
;
1392 error
= git_futils_mkdir_relative(
1393 path
, base
, mode
, flags
, &mkdir_opts
);
1395 data
->perfdata
.mkdir_calls
+= mkdir_opts
.perfdata
.mkdir_calls
;
1396 data
->perfdata
.stat_calls
+= mkdir_opts
.perfdata
.stat_calls
;
1397 data
->perfdata
.chmod_calls
+= mkdir_opts
.perfdata
.chmod_calls
;
1402 static int mkpath2file(
1403 checkout_data
*data
, const char *path
, unsigned int mode
)
1406 bool remove_existing
= should_remove_existing(data
);
1407 unsigned int flags
=
1408 (remove_existing
? MKDIR_REMOVE_EXISTING
: MKDIR_NORMAL
) |
1409 GIT_MKDIR_SKIP_LAST
;
1412 if ((error
= checkout_mkdir(
1413 data
, path
, data
->opts
.target_directory
, mode
, flags
)) < 0)
1416 if (remove_existing
) {
1417 data
->perfdata
.stat_calls
++;
1419 if (p_lstat(path
, &st
) == 0) {
1421 /* Some file, symlink or folder already exists at this name.
1422 * We would have removed it in remove_the_old unless we're on
1423 * a case inensitive filesystem (or the user has asked us not
1424 * to). Remove the similarly named file to write the new.
1426 error
= git_futils_rmdir_r(path
, NULL
, GIT_RMDIR_REMOVE_FILES
);
1427 } else if (errno
!= ENOENT
) {
1428 giterr_set(GITERR_OS
, "Failed to stat file '%s'", path
);
1438 struct checkout_stream
{
1439 git_writestream base
;
1445 static int checkout_stream_write(
1446 git_writestream
*s
, const char *buffer
, size_t len
)
1448 struct checkout_stream
*stream
= (struct checkout_stream
*)s
;
1451 if ((ret
= p_write(stream
->fd
, buffer
, len
)) < 0)
1452 giterr_set(GITERR_OS
, "Could not write to '%s'", stream
->path
);
1457 static int checkout_stream_close(git_writestream
*s
)
1459 struct checkout_stream
*stream
= (struct checkout_stream
*)s
;
1460 assert(stream
&& stream
->open
);
1463 return p_close(stream
->fd
);
1466 static void checkout_stream_free(git_writestream
*s
)
1471 static int blob_content_to_file(
1472 checkout_data
*data
,
1476 const char *hint_path
,
1477 mode_t entry_filemode
)
1479 int flags
= data
->opts
.file_open_flags
;
1480 mode_t file_mode
= data
->opts
.file_mode
?
1481 data
->opts
.file_mode
: entry_filemode
;
1482 git_filter_options filter_opts
= GIT_FILTER_OPTIONS_INIT
;
1483 struct checkout_stream writer
;
1485 git_filter_list
*fl
= NULL
;
1489 if (hint_path
== NULL
)
1492 if ((error
= mkpath2file(data
, path
, data
->opts
.dir_mode
)) < 0)
1496 flags
= O_CREAT
| O_TRUNC
| O_WRONLY
;
1497 if (!(mode
= file_mode
))
1498 mode
= GIT_FILEMODE_BLOB
;
1500 if ((fd
= p_open(path
, flags
, mode
)) < 0) {
1501 giterr_set(GITERR_OS
, "Could not open '%s' for writing", path
);
1505 filter_opts
.attr_session
= &data
->attr_session
;
1506 filter_opts
.temp_buf
= &data
->tmp
;
1508 if (!data
->opts
.disable_filters
&&
1509 (error
= git_filter_list__load_ext(
1510 &fl
, data
->repo
, blob
, hint_path
,
1511 GIT_FILTER_TO_WORKTREE
, &filter_opts
))) {
1516 /* setup the writer */
1517 memset(&writer
, 0, sizeof(struct checkout_stream
));
1518 writer
.base
.write
= checkout_stream_write
;
1519 writer
.base
.close
= checkout_stream_close
;
1520 writer
.base
.free
= checkout_stream_free
;
1525 error
= git_filter_list_stream_blob(fl
, blob
, &writer
.base
);
1527 assert(writer
.open
== 0);
1529 git_filter_list_free(fl
);
1535 data
->perfdata
.stat_calls
++;
1537 if ((error
= p_stat(path
, st
)) < 0) {
1538 giterr_set(GITERR_OS
, "Error statting '%s'", path
);
1542 st
->st_mode
= entry_filemode
;
1548 static int blob_content_to_link(
1549 checkout_data
*data
,
1554 git_buf linktarget
= GIT_BUF_INIT
;
1557 if ((error
= mkpath2file(data
, path
, data
->opts
.dir_mode
)) < 0)
1560 if ((error
= git_blob__getbuf(&linktarget
, blob
)) < 0)
1563 if (data
->can_symlink
) {
1564 if ((error
= p_symlink(git_buf_cstr(&linktarget
), path
)) < 0)
1565 giterr_set(GITERR_OS
, "Could not create symlink %s", path
);
1567 error
= git_futils_fake_symlink(git_buf_cstr(&linktarget
), path
);
1571 data
->perfdata
.stat_calls
++;
1573 if ((error
= p_lstat(path
, st
)) < 0)
1574 giterr_set(GITERR_CHECKOUT
, "Could not stat symlink %s", path
);
1576 st
->st_mode
= GIT_FILEMODE_LINK
;
1579 git_buf_free(&linktarget
);
1584 static int checkout_update_index(
1585 checkout_data
*data
,
1586 const git_diff_file
*file
,
1589 git_index_entry entry
;
1594 memset(&entry
, 0, sizeof(entry
));
1595 entry
.path
= (char *)file
->path
; /* cast to prevent warning */
1596 git_index_entry__init_from_stat(&entry
, st
, true);
1597 git_oid_cpy(&entry
.id
, &file
->id
);
1599 return git_index_add(data
->index
, &entry
);
1602 static int checkout_submodule_update_index(
1603 checkout_data
*data
,
1604 const git_diff_file
*file
)
1609 /* update the index unless prevented */
1610 if ((data
->strategy
& GIT_CHECKOUT_DONT_UPDATE_INDEX
) != 0)
1613 if (checkout_target_fullpath(&fullpath
, data
, file
->path
) < 0)
1616 data
->perfdata
.stat_calls
++;
1617 if (p_stat(fullpath
->ptr
, &st
) < 0) {
1619 GITERR_CHECKOUT
, "Could not stat submodule %s\n", file
->path
);
1620 return GIT_ENOTFOUND
;
1623 st
.st_mode
= GIT_FILEMODE_COMMIT
;
1625 return checkout_update_index(data
, file
, &st
);
1628 static int checkout_submodule(
1629 checkout_data
*data
,
1630 const git_diff_file
*file
)
1632 bool remove_existing
= should_remove_existing(data
);
1635 /* Until submodules are supported, UPDATE_ONLY means do nothing here */
1636 if ((data
->strategy
& GIT_CHECKOUT_UPDATE_ONLY
) != 0)
1639 if ((error
= checkout_mkdir(
1641 file
->path
, data
->opts
.target_directory
, data
->opts
.dir_mode
,
1642 remove_existing
? MKDIR_REMOVE_EXISTING
: MKDIR_NORMAL
)) < 0)
1645 if ((error
= git_submodule_lookup(NULL
, data
->repo
, file
->path
)) < 0) {
1646 /* I've observed repos with submodules in the tree that do not
1647 * have a .gitmodules - core Git just makes an empty directory
1649 if (error
== GIT_ENOTFOUND
) {
1651 return checkout_submodule_update_index(data
, file
);
1657 /* TODO: Support checkout_strategy options. Two circumstances:
1658 * 1 - submodule already checked out, but we need to move the HEAD
1659 * to the new OID, or
1660 * 2 - submodule not checked out and we should recursively check it out
1662 * Checkout will not execute a pull on the submodule, but a clone
1663 * command should probably be able to. Do we need a submodule callback?
1666 return checkout_submodule_update_index(data
, file
);
1669 static void report_progress(
1670 checkout_data
*data
,
1673 if (data
->opts
.progress_cb
)
1674 data
->opts
.progress_cb(
1675 path
, data
->completed_steps
, data
->total_steps
,
1676 data
->opts
.progress_payload
);
1679 static int checkout_safe_for_update_only(
1680 checkout_data
*data
, const char *path
, mode_t expected_mode
)
1684 data
->perfdata
.stat_calls
++;
1686 if (p_lstat(path
, &st
) < 0) {
1687 /* if doesn't exist, then no error and no update */
1688 if (errno
== ENOENT
|| errno
== ENOTDIR
)
1691 /* otherwise, stat error and no update */
1692 giterr_set(GITERR_OS
, "Failed to stat file '%s'", path
);
1696 /* only safe for update if this is the same type of file */
1697 if ((st
.st_mode
& ~0777) == (expected_mode
& ~0777))
1703 static int checkout_write_content(
1704 checkout_data
*data
,
1706 const char *full_path
,
1707 const char *hint_path
,
1714 if ((error
= git_blob_lookup(&blob
, data
->repo
, oid
)) < 0)
1718 error
= blob_content_to_link(data
, st
, blob
, full_path
);
1720 error
= blob_content_to_file(data
, st
, blob
, full_path
, hint_path
, mode
);
1722 git_blob_free(blob
);
1724 /* if we try to create the blob and an existing directory blocks it from
1725 * being written, then there must have been a typechange conflict in a
1726 * parent directory - suppress the error and try to continue.
1728 if ((data
->strategy
& GIT_CHECKOUT_ALLOW_CONFLICTS
) != 0 &&
1729 (error
== GIT_ENOTFOUND
|| error
== GIT_EEXISTS
))
1738 static int checkout_blob(
1739 checkout_data
*data
,
1740 const git_diff_file
*file
)
1746 if (checkout_target_fullpath(&fullpath
, data
, file
->path
) < 0)
1749 if ((data
->strategy
& GIT_CHECKOUT_UPDATE_ONLY
) != 0) {
1750 int rval
= checkout_safe_for_update_only(
1751 data
, fullpath
->ptr
, file
->mode
);
1757 error
= checkout_write_content(
1758 data
, &file
->id
, fullpath
->ptr
, NULL
, file
->mode
, &st
);
1760 /* update the index unless prevented */
1761 if (!error
&& (data
->strategy
& GIT_CHECKOUT_DONT_UPDATE_INDEX
) == 0)
1762 error
= checkout_update_index(data
, file
, &st
);
1764 /* update the submodule data if this was a new .gitmodules file */
1765 if (!error
&& strcmp(file
->path
, ".gitmodules") == 0)
1766 data
->reload_submodules
= true;
1771 static int checkout_remove_the_old(
1772 unsigned int *actions
,
1773 checkout_data
*data
)
1776 git_diff_delta
*delta
;
1780 uint32_t flg
= GIT_RMDIR_EMPTY_PARENTS
|
1781 GIT_RMDIR_REMOVE_FILES
| GIT_RMDIR_REMOVE_BLOCKERS
;
1783 if (data
->opts
.checkout_strategy
& GIT_CHECKOUT_SKIP_LOCKED_DIRECTORIES
)
1784 flg
|= GIT_RMDIR_SKIP_NONEMPTY
;
1786 if (checkout_target_fullpath(&fullpath
, data
, NULL
) < 0)
1789 git_vector_foreach(&data
->diff
->deltas
, i
, delta
) {
1790 if (actions
[i
] & CHECKOUT_ACTION__REMOVE
) {
1791 error
= git_futils_rmdir_r(
1792 delta
->old_file
.path
, fullpath
->ptr
, flg
);
1797 data
->completed_steps
++;
1798 report_progress(data
, delta
->old_file
.path
);
1800 if ((actions
[i
] & CHECKOUT_ACTION__UPDATE_BLOB
) == 0 &&
1801 (data
->strategy
& GIT_CHECKOUT_DONT_UPDATE_INDEX
) == 0 &&
1802 data
->index
!= NULL
)
1804 (void)git_index_remove(data
->index
, delta
->old_file
.path
, 0);
1809 git_vector_foreach(&data
->removes
, i
, str
) {
1810 error
= git_futils_rmdir_r(str
, fullpath
->ptr
, flg
);
1814 data
->completed_steps
++;
1815 report_progress(data
, str
);
1817 if ((data
->strategy
& GIT_CHECKOUT_DONT_UPDATE_INDEX
) == 0 &&
1818 data
->index
!= NULL
)
1820 if (str
[strlen(str
) - 1] == '/')
1821 (void)git_index_remove_directory(data
->index
, str
, 0);
1823 (void)git_index_remove(data
->index
, str
, 0);
1830 static int checkout_deferred_remove(git_repository
*repo
, const char *path
)
1833 int error
= git_futils_rmdir_r(
1834 path
, data
->opts
.target_directory
, GIT_RMDIR_EMPTY_PARENTS
);
1836 if (error
== GIT_ENOTFOUND
) {
1850 static int checkout_create_the_new(
1851 unsigned int *actions
,
1852 checkout_data
*data
)
1855 git_diff_delta
*delta
;
1858 git_vector_foreach(&data
->diff
->deltas
, i
, delta
) {
1859 if (actions
[i
] & CHECKOUT_ACTION__DEFER_REMOVE
) {
1860 /* this had a blocker directory that should only be removed iff
1861 * all of the contents of the directory were safely removed
1863 if ((error
= checkout_deferred_remove(
1864 data
->repo
, delta
->old_file
.path
)) < 0)
1868 if (actions
[i
] & CHECKOUT_ACTION__UPDATE_BLOB
) {
1869 error
= checkout_blob(data
, &delta
->new_file
);
1873 data
->completed_steps
++;
1874 report_progress(data
, delta
->new_file
.path
);
1881 static int checkout_create_submodules(
1882 unsigned int *actions
,
1883 checkout_data
*data
)
1886 git_diff_delta
*delta
;
1889 git_vector_foreach(&data
->diff
->deltas
, i
, delta
) {
1890 if (actions
[i
] & CHECKOUT_ACTION__DEFER_REMOVE
) {
1891 /* this has a blocker directory that should only be removed iff
1892 * all of the contents of the directory were safely removed
1894 if ((error
= checkout_deferred_remove(
1895 data
->repo
, delta
->old_file
.path
)) < 0)
1899 if (actions
[i
] & CHECKOUT_ACTION__UPDATE_SUBMODULE
) {
1900 int error
= checkout_submodule(data
, &delta
->new_file
);
1904 data
->completed_steps
++;
1905 report_progress(data
, delta
->new_file
.path
);
1912 static int checkout_lookup_head_tree(git_tree
**out
, git_repository
*repo
)
1915 git_reference
*ref
= NULL
;
1918 if (!(error
= git_repository_head(&ref
, repo
)) &&
1919 !(error
= git_reference_peel(&head
, ref
, GIT_OBJ_TREE
)))
1920 *out
= (git_tree
*)head
;
1922 git_reference_free(ref
);
1928 static int conflict_entry_name(
1930 const char *side_name
,
1931 const char *filename
)
1933 if (git_buf_puts(out
, side_name
) < 0 ||
1934 git_buf_putc(out
, ':') < 0 ||
1935 git_buf_puts(out
, filename
) < 0)
1941 static int checkout_path_suffixed(git_buf
*path
, const char *suffix
)
1944 int i
= 0, error
= 0;
1946 if ((error
= git_buf_putc(path
, '~')) < 0 || (error
= git_buf_puts(path
, suffix
)) < 0)
1949 path_len
= git_buf_len(path
);
1951 while (git_path_exists(git_buf_cstr(path
)) && i
< INT_MAX
) {
1952 git_buf_truncate(path
, path_len
);
1954 if ((error
= git_buf_putc(path
, '_')) < 0 ||
1955 (error
= git_buf_printf(path
, "%d", i
)) < 0)
1962 git_buf_truncate(path
, path_len
);
1964 giterr_set(GITERR_CHECKOUT
, "Could not write '%s': working directory file exists", path
);
1971 static int checkout_write_entry(
1972 checkout_data
*data
,
1973 checkout_conflictdata
*conflict
,
1974 const git_index_entry
*side
)
1976 const char *hint_path
= NULL
, *suffix
;
1981 assert (side
== conflict
->ours
|| side
== conflict
->theirs
);
1983 if (checkout_target_fullpath(&fullpath
, data
, side
->path
) < 0)
1986 if ((conflict
->name_collision
|| conflict
->directoryfile
) &&
1987 (data
->strategy
& GIT_CHECKOUT_USE_OURS
) == 0 &&
1988 (data
->strategy
& GIT_CHECKOUT_USE_THEIRS
) == 0) {
1990 if (side
== conflict
->ours
)
1991 suffix
= data
->opts
.our_label
? data
->opts
.our_label
:
1994 suffix
= data
->opts
.their_label
? data
->opts
.their_label
:
1997 if (checkout_path_suffixed(fullpath
, suffix
) < 0)
2000 hint_path
= side
->path
;
2003 if ((data
->strategy
& GIT_CHECKOUT_UPDATE_ONLY
) != 0 &&
2004 (error
= checkout_safe_for_update_only(data
, fullpath
->ptr
, side
->mode
)) <= 0)
2007 return checkout_write_content(data
,
2008 &side
->id
, fullpath
->ptr
, hint_path
, side
->mode
, &st
);
2011 static int checkout_write_entries(
2012 checkout_data
*data
,
2013 checkout_conflictdata
*conflict
)
2017 if ((error
= checkout_write_entry(data
, conflict
, conflict
->ours
)) >= 0)
2018 error
= checkout_write_entry(data
, conflict
, conflict
->theirs
);
2023 static int checkout_merge_path(
2025 checkout_data
*data
,
2026 checkout_conflictdata
*conflict
,
2027 git_merge_file_result
*result
)
2029 const char *our_label_raw
, *their_label_raw
, *suffix
;
2032 if ((error
= git_buf_joinpath(out
, git_repository_workdir(data
->repo
), result
->path
)) < 0)
2035 /* Most conflicts simply use the filename in the index */
2036 if (!conflict
->name_collision
)
2039 /* Rename 2->1 conflicts need the branch name appended */
2040 our_label_raw
= data
->opts
.our_label
? data
->opts
.our_label
: "ours";
2041 their_label_raw
= data
->opts
.their_label
? data
->opts
.their_label
: "theirs";
2042 suffix
= strcmp(result
->path
, conflict
->ours
->path
) == 0 ? our_label_raw
: their_label_raw
;
2044 if ((error
= checkout_path_suffixed(out
, suffix
)) < 0)
2050 static int checkout_write_merge(
2051 checkout_data
*data
,
2052 checkout_conflictdata
*conflict
)
2054 git_buf our_label
= GIT_BUF_INIT
, their_label
= GIT_BUF_INIT
,
2055 path_suffixed
= GIT_BUF_INIT
, path_workdir
= GIT_BUF_INIT
,
2056 in_data
= GIT_BUF_INIT
, out_data
= GIT_BUF_INIT
;
2057 git_merge_file_options opts
= GIT_MERGE_FILE_OPTIONS_INIT
;
2058 git_merge_file_result result
= {0};
2059 git_filebuf output
= GIT_FILEBUF_INIT
;
2060 git_filter_list
*fl
= NULL
;
2061 git_filter_options filter_opts
= GIT_FILTER_OPTIONS_INIT
;
2064 if (data
->opts
.checkout_strategy
& GIT_CHECKOUT_CONFLICT_STYLE_DIFF3
)
2065 opts
.flags
|= GIT_MERGE_FILE_STYLE_DIFF3
;
2067 opts
.ancestor_label
= data
->opts
.ancestor_label
?
2068 data
->opts
.ancestor_label
: "ancestor";
2069 opts
.our_label
= data
->opts
.our_label
?
2070 data
->opts
.our_label
: "ours";
2071 opts
.their_label
= data
->opts
.their_label
?
2072 data
->opts
.their_label
: "theirs";
2074 /* If all the paths are identical, decorate the diff3 file with the branch
2075 * names. Otherwise, append branch_name:path.
2077 if (conflict
->ours
&& conflict
->theirs
&&
2078 strcmp(conflict
->ours
->path
, conflict
->theirs
->path
) != 0) {
2080 if ((error
= conflict_entry_name(
2081 &our_label
, opts
.our_label
, conflict
->ours
->path
)) < 0 ||
2082 (error
= conflict_entry_name(
2083 &their_label
, opts
.their_label
, conflict
->theirs
->path
)) < 0)
2086 opts
.our_label
= git_buf_cstr(&our_label
);
2087 opts
.their_label
= git_buf_cstr(&their_label
);
2090 if ((error
= git_merge_file_from_index(&result
, data
->repo
,
2091 conflict
->ancestor
, conflict
->ours
, conflict
->theirs
, &opts
)) < 0)
2094 if (result
.path
== NULL
|| result
.mode
== 0) {
2095 giterr_set(GITERR_CHECKOUT
, "Could not merge contents of file");
2096 error
= GIT_ECONFLICT
;
2100 if ((error
= checkout_merge_path(&path_workdir
, data
, conflict
, &result
)) < 0)
2103 if ((data
->strategy
& GIT_CHECKOUT_UPDATE_ONLY
) != 0 &&
2104 (error
= checkout_safe_for_update_only(data
, git_buf_cstr(&path_workdir
), result
.mode
)) <= 0)
2107 if (!data
->opts
.disable_filters
) {
2108 in_data
.ptr
= (char *)result
.ptr
;
2109 in_data
.size
= result
.len
;
2111 filter_opts
.attr_session
= &data
->attr_session
;
2112 filter_opts
.temp_buf
= &data
->tmp
;
2114 if ((error
= git_filter_list__load_ext(
2115 &fl
, data
->repo
, NULL
, git_buf_cstr(&path_workdir
),
2116 GIT_FILTER_TO_WORKTREE
, &filter_opts
)) < 0 ||
2117 (error
= git_filter_list_apply_to_data(&out_data
, fl
, &in_data
)) < 0)
2120 out_data
.ptr
= (char *)result
.ptr
;
2121 out_data
.size
= result
.len
;
2124 if ((error
= mkpath2file(data
, path_workdir
.ptr
, data
->opts
.dir_mode
)) < 0 ||
2125 (error
= git_filebuf_open(&output
, git_buf_cstr(&path_workdir
), GIT_FILEBUF_DO_NOT_BUFFER
, result
.mode
)) < 0 ||
2126 (error
= git_filebuf_write(&output
, out_data
.ptr
, out_data
.size
)) < 0 ||
2127 (error
= git_filebuf_commit(&output
)) < 0)
2131 git_filter_list_free(fl
);
2133 git_buf_free(&out_data
);
2134 git_buf_free(&our_label
);
2135 git_buf_free(&their_label
);
2137 git_merge_file_result_free(&result
);
2138 git_buf_free(&path_workdir
);
2139 git_buf_free(&path_suffixed
);
2144 static int checkout_conflict_add(
2145 checkout_data
*data
,
2146 const git_index_entry
*conflict
)
2148 int error
= git_index_remove(data
->index
, conflict
->path
, 0);
2150 if (error
== GIT_ENOTFOUND
)
2155 return git_index_add(data
->index
, conflict
);
2158 static int checkout_conflict_update_index(
2159 checkout_data
*data
,
2160 checkout_conflictdata
*conflict
)
2164 if (conflict
->ancestor
)
2165 error
= checkout_conflict_add(data
, conflict
->ancestor
);
2167 if (!error
&& conflict
->ours
)
2168 error
= checkout_conflict_add(data
, conflict
->ours
);
2170 if (!error
&& conflict
->theirs
)
2171 error
= checkout_conflict_add(data
, conflict
->theirs
);
2176 static int checkout_create_conflicts(checkout_data
*data
)
2178 checkout_conflictdata
*conflict
;
2182 git_vector_foreach(&data
->update_conflicts
, i
, conflict
) {
2184 /* Both deleted: nothing to do */
2185 if (conflict
->ours
== NULL
&& conflict
->theirs
== NULL
)
2188 else if ((data
->strategy
& GIT_CHECKOUT_USE_OURS
) &&
2190 error
= checkout_write_entry(data
, conflict
, conflict
->ours
);
2191 else if ((data
->strategy
& GIT_CHECKOUT_USE_THEIRS
) &&
2193 error
= checkout_write_entry(data
, conflict
, conflict
->theirs
);
2195 /* Ignore the other side of name collisions. */
2196 else if ((data
->strategy
& GIT_CHECKOUT_USE_OURS
) &&
2197 !conflict
->ours
&& conflict
->name_collision
)
2199 else if ((data
->strategy
& GIT_CHECKOUT_USE_THEIRS
) &&
2200 !conflict
->theirs
&& conflict
->name_collision
)
2203 /* For modify/delete, name collisions and d/f conflicts, write
2204 * the file (potentially with the name mangled.
2206 else if (conflict
->ours
!= NULL
&& conflict
->theirs
== NULL
)
2207 error
= checkout_write_entry(data
, conflict
, conflict
->ours
);
2208 else if (conflict
->ours
== NULL
&& conflict
->theirs
!= NULL
)
2209 error
= checkout_write_entry(data
, conflict
, conflict
->theirs
);
2211 /* Add/add conflicts and rename 1->2 conflicts, write the
2212 * ours/theirs sides (potentially name mangled).
2214 else if (conflict
->one_to_two
)
2215 error
= checkout_write_entries(data
, conflict
);
2217 /* If all sides are links, write the ours side */
2218 else if (S_ISLNK(conflict
->ours
->mode
) &&
2219 S_ISLNK(conflict
->theirs
->mode
))
2220 error
= checkout_write_entry(data
, conflict
, conflict
->ours
);
2221 /* Link/file conflicts, write the file side */
2222 else if (S_ISLNK(conflict
->ours
->mode
))
2223 error
= checkout_write_entry(data
, conflict
, conflict
->theirs
);
2224 else if (S_ISLNK(conflict
->theirs
->mode
))
2225 error
= checkout_write_entry(data
, conflict
, conflict
->ours
);
2227 /* If any side is a gitlink, do nothing. */
2228 else if (conflict
->submodule
)
2231 /* If any side is binary, write the ours side */
2232 else if (conflict
->binary
)
2233 error
= checkout_write_entry(data
, conflict
, conflict
->ours
);
2236 error
= checkout_write_merge(data
, conflict
);
2238 /* Update the index extensions (REUC and NAME) if we're checking
2239 * out a different index. (Otherwise just leave them there.)
2241 if (!error
&& (data
->strategy
& GIT_CHECKOUT_DONT_UPDATE_INDEX
) == 0)
2242 error
= checkout_conflict_update_index(data
, conflict
);
2247 data
->completed_steps
++;
2248 report_progress(data
,
2249 conflict
->ours
? conflict
->ours
->path
:
2250 (conflict
->theirs
? conflict
->theirs
->path
: conflict
->ancestor
->path
));
2256 static int checkout_remove_conflicts(checkout_data
*data
)
2258 const char *conflict
;
2261 git_vector_foreach(&data
->remove_conflicts
, i
, conflict
) {
2262 if (git_index_conflict_remove(data
->index
, conflict
) < 0)
2265 data
->completed_steps
++;
2271 static int checkout_extensions_update_index(checkout_data
*data
)
2273 const git_index_reuc_entry
*reuc_entry
;
2274 const git_index_name_entry
*name_entry
;
2278 if ((data
->strategy
& GIT_CHECKOUT_UPDATE_ONLY
) != 0)
2281 if (data
->update_reuc
) {
2282 git_vector_foreach(data
->update_reuc
, i
, reuc_entry
) {
2283 if ((error
= git_index_reuc_add(data
->index
, reuc_entry
->path
,
2284 reuc_entry
->mode
[0], &reuc_entry
->oid
[0],
2285 reuc_entry
->mode
[1], &reuc_entry
->oid
[1],
2286 reuc_entry
->mode
[2], &reuc_entry
->oid
[2])) < 0)
2291 if (data
->update_names
) {
2292 git_vector_foreach(data
->update_names
, i
, name_entry
) {
2293 if ((error
= git_index_name_add(data
->index
, name_entry
->ancestor
,
2294 name_entry
->ours
, name_entry
->theirs
)) < 0)
2303 static void checkout_data_clear(checkout_data
*data
)
2305 if (data
->opts_free_baseline
) {
2306 git_tree_free(data
->opts
.baseline
);
2307 data
->opts
.baseline
= NULL
;
2310 git_vector_free(&data
->removes
);
2311 git_pool_clear(&data
->pool
);
2313 git_vector_free_deep(&data
->remove_conflicts
);
2314 git_vector_free_deep(&data
->update_conflicts
);
2316 git__free(data
->pfx
);
2319 git_strmap_free(data
->mkdir_map
);
2321 git_buf_free(&data
->target_path
);
2322 git_buf_free(&data
->tmp
);
2324 git_index_free(data
->index
);
2327 git_strmap_free(data
->mkdir_map
);
2329 git_attr_session__free(&data
->attr_session
);
2332 static int checkout_data_init(
2333 checkout_data
*data
,
2334 git_iterator
*target
,
2335 const git_checkout_options
*proposed
)
2338 git_repository
*repo
= git_iterator_owner(target
);
2340 memset(data
, 0, sizeof(*data
));
2343 giterr_set(GITERR_CHECKOUT
, "Cannot checkout nothing");
2347 if ((!proposed
|| !proposed
->target_directory
) &&
2348 (error
= git_repository__ensure_not_bare(repo
, "checkout")) < 0)
2352 data
->target
= target
;
2354 GITERR_CHECK_VERSION(
2355 proposed
, GIT_CHECKOUT_OPTIONS_VERSION
, "git_checkout_options");
2358 GIT_INIT_STRUCTURE(&data
->opts
, GIT_CHECKOUT_OPTIONS_VERSION
);
2360 memmove(&data
->opts
, proposed
, sizeof(git_checkout_options
));
2362 if (!data
->opts
.target_directory
)
2363 data
->opts
.target_directory
= git_repository_workdir(repo
);
2364 else if (!git_path_isdir(data
->opts
.target_directory
) &&
2365 (error
= checkout_mkdir(data
,
2366 data
->opts
.target_directory
, NULL
,
2367 GIT_DIR_MODE
, GIT_MKDIR_VERIFY_DIR
)) < 0)
2370 /* refresh config and index content unless NO_REFRESH is given */
2371 if ((data
->opts
.checkout_strategy
& GIT_CHECKOUT_NO_REFRESH
) == 0) {
2374 if ((error
= git_repository_config__weakptr(&cfg
, repo
)) < 0)
2377 /* Get the repository index and reload it (unless we're checking
2378 * out the index; then it has the changes we're trying to check
2379 * out and those should not be overwritten.)
2381 if ((error
= git_repository_index(&data
->index
, data
->repo
)) < 0)
2384 if (data
->index
!= git_iterator_index(target
)) {
2385 if ((error
= git_index_read(data
->index
, true)) < 0)
2388 /* cannot checkout if unresolved conflicts exist */
2389 if ((data
->opts
.checkout_strategy
& GIT_CHECKOUT_FORCE
) == 0 &&
2390 git_index_has_conflicts(data
->index
)) {
2391 error
= GIT_ECONFLICT
;
2392 giterr_set(GITERR_CHECKOUT
,
2393 "unresolved conflicts exist in the index");
2397 /* clean conflict data in the current index */
2398 git_index_name_clear(data
->index
);
2399 git_index_reuc_clear(data
->index
);
2403 /* if you are forcing, allow all safe updates, plus recreate missing */
2404 if ((data
->opts
.checkout_strategy
& GIT_CHECKOUT_FORCE
) != 0)
2405 data
->opts
.checkout_strategy
|= GIT_CHECKOUT_SAFE
|
2406 GIT_CHECKOUT_RECREATE_MISSING
;
2408 /* if the repository does not actually have an index file, then this
2409 * is an initial checkout (perhaps from clone), so we allow safe updates
2411 if (!data
->index
->on_disk
&&
2412 (data
->opts
.checkout_strategy
& GIT_CHECKOUT_SAFE
) != 0)
2413 data
->opts
.checkout_strategy
|= GIT_CHECKOUT_RECREATE_MISSING
;
2415 data
->strategy
= data
->opts
.checkout_strategy
;
2417 /* opts->disable_filters is false by default */
2419 if (!data
->opts
.dir_mode
)
2420 data
->opts
.dir_mode
= GIT_DIR_MODE
;
2422 if (!data
->opts
.file_open_flags
)
2423 data
->opts
.file_open_flags
= O_CREAT
| O_TRUNC
| O_WRONLY
;
2425 data
->pfx
= git_pathspec_prefix(&data
->opts
.paths
);
2427 if ((error
= git_repository__cvar(
2428 &data
->can_symlink
, repo
, GIT_CVAR_SYMLINKS
)) < 0)
2431 if (!data
->opts
.baseline
&& !data
->opts
.baseline_index
) {
2432 data
->opts_free_baseline
= true;
2434 error
= checkout_lookup_head_tree(&data
->opts
.baseline
, repo
);
2436 if (error
== GIT_EUNBORNBRANCH
) {
2445 if ((data
->opts
.checkout_strategy
&
2446 (GIT_CHECKOUT_CONFLICT_STYLE_MERGE
| GIT_CHECKOUT_CONFLICT_STYLE_DIFF3
)) == 0) {
2447 git_config_entry
*conflict_style
= NULL
;
2448 git_config
*cfg
= NULL
;
2450 if ((error
= git_repository_config__weakptr(&cfg
, repo
)) < 0 ||
2451 (error
= git_config_get_entry(&conflict_style
, cfg
, "merge.conflictstyle")) < 0 ||
2452 error
== GIT_ENOTFOUND
)
2456 else if (strcmp(conflict_style
->value
, "merge") == 0)
2457 data
->opts
.checkout_strategy
|= GIT_CHECKOUT_CONFLICT_STYLE_MERGE
;
2458 else if (strcmp(conflict_style
->value
, "diff3") == 0)
2459 data
->opts
.checkout_strategy
|= GIT_CHECKOUT_CONFLICT_STYLE_DIFF3
;
2461 giterr_set(GITERR_CHECKOUT
, "unknown style '%s' given for 'merge.conflictstyle'",
2464 git_config_entry_free(conflict_style
);
2467 git_config_entry_free(conflict_style
);
2470 git_pool_init(&data
->pool
, 1);
2472 if ((error
= git_vector_init(&data
->removes
, 0, git__strcmp_cb
)) < 0 ||
2473 (error
= git_vector_init(&data
->remove_conflicts
, 0, NULL
)) < 0 ||
2474 (error
= git_vector_init(&data
->update_conflicts
, 0, NULL
)) < 0 ||
2475 (error
= git_buf_puts(&data
->target_path
, data
->opts
.target_directory
)) < 0 ||
2476 (error
= git_path_to_dir(&data
->target_path
)) < 0 ||
2477 (error
= git_strmap_alloc(&data
->mkdir_map
)) < 0)
2480 data
->target_len
= git_buf_len(&data
->target_path
);
2482 git_attr_session__init(&data
->attr_session
, data
->repo
);
2486 checkout_data_clear(data
);
2491 #define CHECKOUT_INDEX_DONT_WRITE_MASK \
2492 (GIT_CHECKOUT_DONT_UPDATE_INDEX | GIT_CHECKOUT_DONT_WRITE_INDEX)
2494 int git_checkout_iterator(
2495 git_iterator
*target
,
2497 const git_checkout_options
*opts
)
2500 git_iterator
*baseline
= NULL
, *workdir
= NULL
;
2501 git_iterator_options baseline_opts
= GIT_ITERATOR_OPTIONS_INIT
,
2502 workdir_opts
= GIT_ITERATOR_OPTIONS_INIT
;
2503 checkout_data data
= {0};
2504 git_diff_options diff_opts
= GIT_DIFF_OPTIONS_INIT
;
2505 uint32_t *actions
= NULL
;
2506 size_t *counts
= NULL
;
2508 /* initialize structures and options */
2509 error
= checkout_data_init(&data
, target
, opts
);
2514 GIT_DIFF_INCLUDE_UNMODIFIED
|
2515 GIT_DIFF_INCLUDE_UNREADABLE
|
2516 GIT_DIFF_INCLUDE_UNTRACKED
|
2517 GIT_DIFF_RECURSE_UNTRACKED_DIRS
| /* needed to match baseline */
2518 GIT_DIFF_INCLUDE_IGNORED
|
2519 GIT_DIFF_INCLUDE_TYPECHANGE
|
2520 GIT_DIFF_INCLUDE_TYPECHANGE_TREES
|
2521 GIT_DIFF_SKIP_BINARY_CHECK
|
2522 GIT_DIFF_INCLUDE_CASECHANGE
;
2523 if (data
.opts
.checkout_strategy
& GIT_CHECKOUT_DISABLE_PATHSPEC_MATCH
)
2524 diff_opts
.flags
|= GIT_DIFF_DISABLE_PATHSPEC_MATCH
;
2525 if (data
.opts
.paths
.count
> 0)
2526 diff_opts
.pathspec
= data
.opts
.paths
;
2528 /* set up iterators */
2530 workdir_opts
.flags
= git_iterator_ignore_case(target
) ?
2531 GIT_ITERATOR_IGNORE_CASE
: GIT_ITERATOR_DONT_IGNORE_CASE
;
2532 workdir_opts
.flags
|= GIT_ITERATOR_DONT_AUTOEXPAND
;
2533 workdir_opts
.start
= data
.pfx
;
2534 workdir_opts
.end
= data
.pfx
;
2536 if ((error
= git_iterator_reset_range(target
, data
.pfx
, data
.pfx
)) < 0 ||
2537 (error
= git_iterator_for_workdir_ext(
2538 &workdir
, data
.repo
, data
.opts
.target_directory
, index
, NULL
,
2539 &workdir_opts
)) < 0)
2542 baseline_opts
.flags
= git_iterator_ignore_case(target
) ?
2543 GIT_ITERATOR_IGNORE_CASE
: GIT_ITERATOR_DONT_IGNORE_CASE
;
2544 baseline_opts
.start
= data
.pfx
;
2545 baseline_opts
.end
= data
.pfx
;
2547 if (data
.opts
.baseline_index
) {
2548 if ((error
= git_iterator_for_index(
2549 &baseline
, git_index_owner(data
.opts
.baseline_index
),
2550 data
.opts
.baseline_index
, &baseline_opts
)) < 0)
2553 if ((error
= git_iterator_for_tree(
2554 &baseline
, data
.opts
.baseline
, &baseline_opts
)) < 0)
2558 /* Should not have case insensitivity mismatch */
2559 assert(git_iterator_ignore_case(workdir
) == git_iterator_ignore_case(baseline
));
2561 /* Generate baseline-to-target diff which will include an entry for
2562 * every possible update that might need to be made.
2564 if ((error
= git_diff__from_iterators(
2565 &data
.diff
, data
.repo
, baseline
, target
, &diff_opts
)) < 0)
2568 /* Loop through diff (and working directory iterator) building a list of
2569 * actions to be taken, plus look for conflicts and send notifications,
2570 * then loop through conflicts.
2572 if ((error
= checkout_get_actions(&actions
, &counts
, &data
, workdir
)) != 0)
2575 data
.total_steps
= counts
[CHECKOUT_ACTION__REMOVE
] +
2576 counts
[CHECKOUT_ACTION__REMOVE_CONFLICT
] +
2577 counts
[CHECKOUT_ACTION__UPDATE_BLOB
] +
2578 counts
[CHECKOUT_ACTION__UPDATE_SUBMODULE
] +
2579 counts
[CHECKOUT_ACTION__UPDATE_CONFLICT
];
2581 report_progress(&data
, NULL
); /* establish 0 baseline */
2583 /* To deal with some order dependencies, perform remaining checkout
2584 * in three passes: removes, then update blobs, then update submodules.
2586 if (counts
[CHECKOUT_ACTION__REMOVE
] > 0 &&
2587 (error
= checkout_remove_the_old(actions
, &data
)) < 0)
2590 if (counts
[CHECKOUT_ACTION__REMOVE_CONFLICT
] > 0 &&
2591 (error
= checkout_remove_conflicts(&data
)) < 0)
2594 if (counts
[CHECKOUT_ACTION__UPDATE_BLOB
] > 0 &&
2595 (error
= checkout_create_the_new(actions
, &data
)) < 0)
2598 if (counts
[CHECKOUT_ACTION__UPDATE_SUBMODULE
] > 0 &&
2599 (error
= checkout_create_submodules(actions
, &data
)) < 0)
2602 if (counts
[CHECKOUT_ACTION__UPDATE_CONFLICT
] > 0 &&
2603 (error
= checkout_create_conflicts(&data
)) < 0)
2606 if (data
.index
!= git_iterator_index(target
) &&
2607 (error
= checkout_extensions_update_index(&data
)) < 0)
2610 assert(data
.completed_steps
== data
.total_steps
);
2612 if (data
.opts
.perfdata_cb
)
2613 data
.opts
.perfdata_cb(&data
.perfdata
, data
.opts
.perfdata_payload
);
2616 if (!error
&& data
.index
!= NULL
&&
2617 (data
.strategy
& CHECKOUT_INDEX_DONT_WRITE_MASK
) == 0)
2618 error
= git_index_write(data
.index
);
2620 git_diff_free(data
.diff
);
2621 git_iterator_free(workdir
);
2622 git_iterator_free(baseline
);
2625 checkout_data_clear(&data
);
2630 int git_checkout_index(
2631 git_repository
*repo
,
2633 const git_checkout_options
*opts
)
2635 int error
, owned
= 0;
2636 git_iterator
*index_i
;
2638 if (!index
&& !repo
) {
2639 giterr_set(GITERR_CHECKOUT
,
2640 "Must provide either repository or index to checkout");
2644 if (index
&& repo
&&
2645 git_index_owner(index
) &&
2646 git_index_owner(index
) != repo
) {
2647 giterr_set(GITERR_CHECKOUT
,
2648 "Index to checkout does not match repository");
2650 } else if(index
&& repo
&& !git_index_owner(index
)) {
2651 GIT_REFCOUNT_OWN(index
, repo
);
2656 repo
= git_index_owner(index
);
2658 if (!index
&& (error
= git_repository_index__weakptr(&index
, repo
)) < 0)
2660 GIT_REFCOUNT_INC(index
);
2662 if (!(error
= git_iterator_for_index(&index_i
, repo
, index
, NULL
)))
2663 error
= git_checkout_iterator(index_i
, index
, opts
);
2666 GIT_REFCOUNT_OWN(index
, NULL
);
2668 git_iterator_free(index_i
);
2669 git_index_free(index
);
2674 int git_checkout_tree(
2675 git_repository
*repo
,
2676 const git_object
*treeish
,
2677 const git_checkout_options
*opts
)
2681 git_tree
*tree
= NULL
;
2682 git_iterator
*tree_i
= NULL
;
2683 git_iterator_options iter_opts
= GIT_ITERATOR_OPTIONS_INIT
;
2685 if (!treeish
&& !repo
) {
2686 giterr_set(GITERR_CHECKOUT
,
2687 "Must provide either repository or tree to checkout");
2690 if (treeish
&& repo
&& git_object_owner(treeish
) != repo
) {
2691 giterr_set(GITERR_CHECKOUT
,
2692 "Object to checkout does not match repository");
2697 repo
= git_object_owner(treeish
);
2700 if (git_object_peel((git_object
**)&tree
, treeish
, GIT_OBJ_TREE
) < 0) {
2702 GITERR_CHECKOUT
, "Provided object cannot be peeled to a tree");
2707 if ((error
= checkout_lookup_head_tree(&tree
, repo
)) < 0) {
2708 if (error
!= GIT_EUNBORNBRANCH
)
2711 "HEAD could not be peeled to a tree and no treeish given");
2716 if ((error
= git_repository_index(&index
, repo
)) < 0)
2719 if ((opts
->checkout_strategy
& GIT_CHECKOUT_DISABLE_PATHSPEC_MATCH
)) {
2720 iter_opts
.pathlist
.count
= opts
->paths
.count
;
2721 iter_opts
.pathlist
.strings
= opts
->paths
.strings
;
2724 if (!(error
= git_iterator_for_tree(&tree_i
, tree
, &iter_opts
)))
2725 error
= git_checkout_iterator(tree_i
, index
, opts
);
2727 git_iterator_free(tree_i
);
2728 git_index_free(index
);
2729 git_tree_free(tree
);
2734 int git_checkout_head(
2735 git_repository
*repo
,
2736 const git_checkout_options
*opts
)
2739 return git_checkout_tree(repo
, NULL
, opts
);
2742 int git_checkout_init_options(git_checkout_options
*opts
, unsigned int version
)
2744 GIT_INIT_STRUCTURE_FROM_TEMPLATE(
2745 opts
, version
, git_checkout_options
, GIT_CHECKOUT_OPTIONS_INIT
);