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 bool wd_item_is_removable(git_iterator
*iter
, const git_index_entry
*wd
)
299 git_buf
*full
= NULL
;
301 if (wd
->mode
!= GIT_FILEMODE_TREE
)
303 if (git_iterator_current_workdir_path(&full
, iter
) < 0)
305 return !full
|| !git_path_contains(full
, DOT_GIT
);
308 static int checkout_queue_remove(checkout_data
*data
, const char *path
)
310 char *copy
= git_pool_strdup(&data
->pool
, path
);
311 GITERR_CHECK_ALLOC(copy
);
312 return git_vector_insert(&data
->removes
, copy
);
315 /* note that this advances the iterator over the wd item */
316 static int checkout_action_wd_only(
318 git_iterator
*workdir
,
319 const git_index_entry
**wditem
,
320 git_vector
*pathspec
)
324 git_checkout_notify_t notify
= GIT_CHECKOUT_NOTIFY_NONE
;
325 const git_index_entry
*wd
= *wditem
;
327 if (!git_pathspec__match(
329 (data
->strategy
& GIT_CHECKOUT_DISABLE_PATHSPEC_MATCH
) != 0,
330 git_iterator_ignore_case(workdir
), NULL
, NULL
))
331 return git_iterator_advance(wditem
, workdir
);
333 /* check if item is tracked in the index but not in the checkout diff */
334 if (data
->index
!= NULL
) {
337 error
= git_index__find_pos(
338 &pos
, data
->index
, wd
->path
, 0, GIT_INDEX_STAGE_ANY
);
340 if (wd
->mode
!= GIT_FILEMODE_TREE
) {
341 if (!error
) { /* found by git_index__find_pos call */
342 notify
= GIT_CHECKOUT_NOTIFY_DIRTY
;
343 remove
= ((data
->strategy
& GIT_CHECKOUT_FORCE
) != 0);
344 } else if (error
!= GIT_ENOTFOUND
)
347 error
= 0; /* git_index__find_pos does not set error msg */
349 /* for tree entries, we have to see if there are any index
350 * entries that are contained inside that tree
352 const git_index_entry
*e
= git_index_get_byindex(data
->index
, pos
);
354 if (e
!= NULL
&& data
->diff
->pfxcomp(e
->path
, wd
->path
) == 0) {
355 notify
= GIT_CHECKOUT_NOTIFY_DIRTY
;
356 remove
= ((data
->strategy
& GIT_CHECKOUT_FORCE
) != 0);
361 if (notify
!= GIT_CHECKOUT_NOTIFY_NONE
) {
362 /* if we found something in the index, notify and advance */
363 if ((error
= checkout_notify(data
, notify
, NULL
, wd
)) != 0)
366 if (remove
&& wd_item_is_removable(workdir
, wd
))
367 error
= checkout_queue_remove(data
, wd
->path
);
370 error
= git_iterator_advance(wditem
, workdir
);
372 /* untracked or ignored - can't know which until we advance through */
373 bool over
= false, removable
= wd_item_is_removable(workdir
, wd
);
374 git_iterator_status_t untracked_state
;
376 /* copy the entry for issuing notification callback later */
377 git_index_entry saved_wd
= *wd
;
378 git_buf_sets(&data
->tmp
, wd
->path
);
379 saved_wd
.path
= data
->tmp
.ptr
;
381 error
= git_iterator_advance_over_with_status(
382 wditem
, &untracked_state
, workdir
);
383 if (error
== GIT_ITEROVER
)
388 if (untracked_state
== GIT_ITERATOR_STATUS_IGNORED
) {
389 notify
= GIT_CHECKOUT_NOTIFY_IGNORED
;
390 remove
= ((data
->strategy
& GIT_CHECKOUT_REMOVE_IGNORED
) != 0);
392 notify
= GIT_CHECKOUT_NOTIFY_UNTRACKED
;
393 remove
= ((data
->strategy
& GIT_CHECKOUT_REMOVE_UNTRACKED
) != 0);
396 if ((error
= checkout_notify(data
, notify
, NULL
, &saved_wd
)) != 0)
399 if (remove
&& removable
)
400 error
= checkout_queue_remove(data
, saved_wd
.path
);
402 if (!error
&& over
) /* restore ITEROVER if needed */
403 error
= GIT_ITEROVER
;
409 static bool submodule_is_config_only(
413 git_submodule
*sm
= NULL
;
414 unsigned int sm_loc
= 0;
417 if (git_submodule_lookup(&sm
, data
->repo
, path
) < 0)
420 if (git_submodule_location(&sm_loc
, sm
) < 0 ||
421 sm_loc
== GIT_SUBMODULE_STATUS_IN_CONFIG
)
424 git_submodule_free(sm
);
429 static bool checkout_is_empty_dir(checkout_data
*data
, const char *path
)
431 git_buf_truncate(&data
->path
, data
->workdir_len
);
432 if (git_buf_puts(&data
->path
, path
) < 0)
434 return git_path_is_empty_dir(data
->path
.ptr
);
437 static int checkout_action_with_wd(
440 const git_diff_delta
*delta
,
441 git_iterator
*workdir
,
442 const git_index_entry
*wd
)
444 *action
= CHECKOUT_ACTION__NONE
;
446 switch (delta
->status
) {
447 case GIT_DELTA_UNMODIFIED
: /* case 14/15 or 33 */
448 if (checkout_is_workdir_modified(data
, &delta
->old_file
, &delta
->new_file
, wd
)) {
450 checkout_notify(data
, GIT_CHECKOUT_NOTIFY_DIRTY
, delta
, wd
) );
451 *action
= CHECKOUT_ACTION_IF(FORCE
, UPDATE_BLOB
, NONE
);
454 case GIT_DELTA_ADDED
: /* case 3, 4 or 6 */
455 if (git_iterator_current_is_ignored(workdir
))
456 *action
= CHECKOUT_ACTION_IF(DONT_OVERWRITE_IGNORED
, CONFLICT
, UPDATE_BLOB
);
458 *action
= CHECKOUT_ACTION_IF(FORCE
, UPDATE_BLOB
, CONFLICT
);
460 case GIT_DELTA_DELETED
: /* case 9 or 10 (or 26 but not really) */
461 if (checkout_is_workdir_modified(data
, &delta
->old_file
, &delta
->new_file
, wd
))
462 *action
= CHECKOUT_ACTION_IF(FORCE
, REMOVE
, CONFLICT
);
464 *action
= CHECKOUT_ACTION_IF(SAFE
, REMOVE
, NONE
);
466 case GIT_DELTA_MODIFIED
: /* case 16, 17, 18 (or 36 but not really) */
467 if (checkout_is_workdir_modified(data
, &delta
->old_file
, &delta
->new_file
, wd
))
468 *action
= CHECKOUT_ACTION_IF(FORCE
, UPDATE_BLOB
, CONFLICT
);
470 *action
= CHECKOUT_ACTION_IF(SAFE
, UPDATE_BLOB
, NONE
);
472 case GIT_DELTA_TYPECHANGE
: /* case 22, 23, 29, 30 */
473 if (delta
->old_file
.mode
== GIT_FILEMODE_TREE
) {
474 if (wd
->mode
== GIT_FILEMODE_TREE
)
475 /* either deleting items in old tree will delete the wd dir,
476 * or we'll get a conflict when we attempt blob update...
478 *action
= CHECKOUT_ACTION_IF(SAFE
, UPDATE_BLOB
, NONE
);
479 else if (wd
->mode
== GIT_FILEMODE_COMMIT
) {
480 /* workdir is possibly a "phantom" submodule - treat as a
481 * tree if the only submodule info came from the config
483 if (submodule_is_config_only(data
, wd
->path
))
484 *action
= CHECKOUT_ACTION_IF(SAFE
, UPDATE_BLOB
, NONE
);
486 *action
= CHECKOUT_ACTION_IF(FORCE
, REMOVE_AND_UPDATE
, CONFLICT
);
488 *action
= CHECKOUT_ACTION_IF(FORCE
, REMOVE
, CONFLICT
);
490 else if (checkout_is_workdir_modified(data
, &delta
->old_file
, &delta
->new_file
, wd
))
491 *action
= CHECKOUT_ACTION_IF(FORCE
, REMOVE_AND_UPDATE
, CONFLICT
);
493 *action
= CHECKOUT_ACTION_IF(SAFE
, REMOVE_AND_UPDATE
, NONE
);
495 /* don't update if the typechange is to a tree */
496 if (delta
->new_file
.mode
== GIT_FILEMODE_TREE
)
497 *action
= (*action
& ~CHECKOUT_ACTION__UPDATE_BLOB
);
499 default: /* impossible */
503 return checkout_action_common(action
, data
, delta
, wd
);
506 static int checkout_action_with_wd_blocker(
509 const git_diff_delta
*delta
,
510 const git_index_entry
*wd
)
512 *action
= CHECKOUT_ACTION__NONE
;
514 switch (delta
->status
) {
515 case GIT_DELTA_UNMODIFIED
:
516 /* should show delta as dirty / deleted */
518 checkout_notify(data
, GIT_CHECKOUT_NOTIFY_DIRTY
, delta
, wd
) );
519 *action
= CHECKOUT_ACTION_IF(FORCE
, REMOVE_AND_UPDATE
, NONE
);
521 case GIT_DELTA_ADDED
:
522 case GIT_DELTA_MODIFIED
:
523 *action
= CHECKOUT_ACTION_IF(FORCE
, REMOVE_AND_UPDATE
, CONFLICT
);
525 case GIT_DELTA_DELETED
:
526 *action
= CHECKOUT_ACTION_IF(FORCE
, REMOVE
, CONFLICT
);
528 case GIT_DELTA_TYPECHANGE
:
529 /* not 100% certain about this... */
530 *action
= CHECKOUT_ACTION_IF(FORCE
, REMOVE_AND_UPDATE
, CONFLICT
);
532 default: /* impossible */
536 return checkout_action_common(action
, data
, delta
, wd
);
539 static int checkout_action_with_wd_dir(
542 const git_diff_delta
*delta
,
543 git_iterator
*workdir
,
544 const git_index_entry
*wd
)
546 *action
= CHECKOUT_ACTION__NONE
;
548 switch (delta
->status
) {
549 case GIT_DELTA_UNMODIFIED
: /* case 19 or 24 (or 34 but not really) */
551 checkout_notify(data
, GIT_CHECKOUT_NOTIFY_DIRTY
, delta
, NULL
));
553 checkout_notify(data
, GIT_CHECKOUT_NOTIFY_UNTRACKED
, NULL
, wd
));
554 *action
= CHECKOUT_ACTION_IF(FORCE
, REMOVE_AND_UPDATE
, NONE
);
556 case GIT_DELTA_ADDED
:/* case 4 (and 7 for dir) */
557 case GIT_DELTA_MODIFIED
: /* case 20 (or 37 but not really) */
558 if (delta
->old_file
.mode
== GIT_FILEMODE_COMMIT
)
559 /* expected submodule (and maybe found one) */;
560 else if (delta
->new_file
.mode
!= GIT_FILEMODE_TREE
)
561 *action
= git_iterator_current_is_ignored(workdir
) ?
562 CHECKOUT_ACTION_IF(DONT_OVERWRITE_IGNORED
, CONFLICT
, REMOVE_AND_UPDATE
) :
563 CHECKOUT_ACTION_IF(FORCE
, REMOVE_AND_UPDATE
, CONFLICT
);
565 case GIT_DELTA_DELETED
: /* case 11 (and 27 for dir) */
566 if (delta
->old_file
.mode
!= GIT_FILEMODE_TREE
)
568 checkout_notify(data
, GIT_CHECKOUT_NOTIFY_UNTRACKED
, NULL
, wd
));
570 case GIT_DELTA_TYPECHANGE
: /* case 24 or 31 */
571 if (delta
->old_file
.mode
== GIT_FILEMODE_TREE
) {
572 /* For typechange from dir, remove dir and add blob, but it is
573 * not safe to remove dir if it contains modified files.
574 * However, safely removing child files will remove the parent
575 * directory if is it left empty, so we can defer removing the
576 * dir and it will succeed if no children are left.
578 *action
= CHECKOUT_ACTION_IF(SAFE
, UPDATE_BLOB
, NONE
);
580 else if (delta
->new_file
.mode
!= GIT_FILEMODE_TREE
)
581 /* For typechange to dir, dir is already created so no action */
582 *action
= CHECKOUT_ACTION_IF(FORCE
, REMOVE_AND_UPDATE
, CONFLICT
);
584 default: /* impossible */
588 return checkout_action_common(action
, data
, delta
, wd
);
591 static int checkout_action_with_wd_dir_empty(
594 const git_diff_delta
*delta
)
596 int error
= checkout_action_no_wd(action
, data
, delta
);
598 /* We can always safely remove an empty directory. */
599 if (error
== 0 && *action
!= CHECKOUT_ACTION__NONE
)
600 *action
|= CHECKOUT_ACTION__REMOVE
;
605 static int checkout_action(
608 git_diff_delta
*delta
,
609 git_iterator
*workdir
,
610 const git_index_entry
**wditem
,
611 git_vector
*pathspec
)
614 int (*strcomp
)(const char *, const char *) = data
->diff
->strcomp
;
615 int (*pfxcomp
)(const char *str
, const char *pfx
) = data
->diff
->pfxcomp
;
616 int (*advance
)(const git_index_entry
**, git_iterator
*) = NULL
;
618 /* move workdir iterator to follow along with deltas */
621 const git_index_entry
*wd
= *wditem
;
624 return checkout_action_no_wd(action
, data
, delta
);
626 cmp
= strcomp(wd
->path
, delta
->old_file
.path
);
628 /* 1. wd before delta ("a/a" before "a/b")
629 * 2. wd prefixes delta & should expand ("a/" before "a/b")
630 * 3. wd prefixes delta & cannot expand ("a/b" before "a/b/c")
631 * 4. wd equals delta ("a/b" and "a/b")
632 * 5. wd after delta & delta prefixes wd ("a/b/c" after "a/b/" or "a/b")
633 * 6. wd after delta ("a/c" after "a/b")
637 cmp
= pfxcomp(delta
->old_file
.path
, wd
->path
);
640 if (wd
->mode
== GIT_FILEMODE_TREE
) {
641 /* case 2 - entry prefixed by workdir tree */
642 error
= git_iterator_advance_into_or_over(wditem
, workdir
);
643 if (error
< 0 && error
!= GIT_ITEROVER
)
648 /* case 3 maybe - wd contains non-dir where dir expected */
649 if (delta
->old_file
.path
[strlen(wd
->path
)] == '/') {
650 error
= checkout_action_with_wd_blocker(
651 action
, data
, delta
, wd
);
652 advance
= git_iterator_advance
;
657 /* case 1 - handle wd item (if it matches pathspec) */
658 error
= checkout_action_wd_only(data
, workdir
, wditem
, pathspec
);
659 if (error
&& error
!= GIT_ITEROVER
)
666 error
= checkout_action_with_wd(action
, data
, delta
, workdir
, wd
);
667 advance
= git_iterator_advance
;
671 cmp
= pfxcomp(wd
->path
, delta
->old_file
.path
);
673 if (cmp
== 0) { /* case 5 */
674 if (wd
->path
[strlen(delta
->old_file
.path
)] != '/')
675 return checkout_action_no_wd(action
, data
, delta
);
677 if (delta
->status
== GIT_DELTA_TYPECHANGE
) {
678 if (delta
->old_file
.mode
== GIT_FILEMODE_TREE
) {
679 error
= checkout_action_with_wd(action
, data
, delta
, workdir
, wd
);
680 advance
= git_iterator_advance_into
;
684 if (delta
->new_file
.mode
== GIT_FILEMODE_TREE
||
685 delta
->new_file
.mode
== GIT_FILEMODE_COMMIT
||
686 delta
->old_file
.mode
== GIT_FILEMODE_COMMIT
)
688 error
= checkout_action_with_wd(action
, data
, delta
, workdir
, wd
);
689 advance
= git_iterator_advance
;
694 return checkout_is_empty_dir(data
, wd
->path
) ?
695 checkout_action_with_wd_dir_empty(action
, data
, delta
) :
696 checkout_action_with_wd_dir(action
, data
, delta
, workdir
, wd
);
699 /* case 6 - wd is after delta */
700 return checkout_action_no_wd(action
, data
, delta
);
704 if (!error
&& advance
!= NULL
&&
705 (error
= advance(wditem
, workdir
)) < 0) {
707 if (error
== GIT_ITEROVER
)
714 static int checkout_remaining_wd_items(
716 git_iterator
*workdir
,
717 const git_index_entry
*wd
,
723 error
= checkout_action_wd_only(data
, workdir
, &wd
, spec
);
725 if (error
== GIT_ITEROVER
)
731 GIT_INLINE(int) checkout_idxentry_cmp(
732 const git_index_entry
*a
,
733 const git_index_entry
*b
)
742 return strcmp(a
->path
, b
->path
);
745 static int checkout_conflictdata_cmp(const void *a
, const void *b
)
747 const checkout_conflictdata
*ca
= a
;
748 const checkout_conflictdata
*cb
= b
;
751 if ((diff
= checkout_idxentry_cmp(ca
->ancestor
, cb
->ancestor
)) == 0 &&
752 (diff
= checkout_idxentry_cmp(ca
->ours
, cb
->theirs
)) == 0)
753 diff
= checkout_idxentry_cmp(ca
->theirs
, cb
->theirs
);
758 int checkout_conflictdata_empty(
759 const git_vector
*conflicts
, size_t idx
, void *payload
)
761 checkout_conflictdata
*conflict
;
765 if ((conflict
= git_vector_get(conflicts
, idx
)) == NULL
)
768 if (conflict
->ancestor
|| conflict
->ours
|| conflict
->theirs
)
775 GIT_INLINE(bool) conflict_pathspec_match(
777 git_iterator
*workdir
,
778 git_vector
*pathspec
,
779 const git_index_entry
*ancestor
,
780 const git_index_entry
*ours
,
781 const git_index_entry
*theirs
)
783 /* if the pathspec matches ours *or* theirs, proceed */
784 if (ours
&& git_pathspec__match(pathspec
, ours
->path
,
785 (data
->strategy
& GIT_CHECKOUT_DISABLE_PATHSPEC_MATCH
) != 0,
786 git_iterator_ignore_case(workdir
), NULL
, NULL
))
789 if (theirs
&& git_pathspec__match(pathspec
, theirs
->path
,
790 (data
->strategy
& GIT_CHECKOUT_DISABLE_PATHSPEC_MATCH
) != 0,
791 git_iterator_ignore_case(workdir
), NULL
, NULL
))
794 if (ancestor
&& git_pathspec__match(pathspec
, ancestor
->path
,
795 (data
->strategy
& GIT_CHECKOUT_DISABLE_PATHSPEC_MATCH
) != 0,
796 git_iterator_ignore_case(workdir
), NULL
, NULL
))
802 GIT_INLINE(int) checkout_conflict_detect_submodule(checkout_conflictdata
*conflict
)
804 conflict
->submodule
= ((conflict
->ancestor
&& S_ISGITLINK(conflict
->ancestor
->mode
)) ||
805 (conflict
->ours
&& S_ISGITLINK(conflict
->ours
->mode
)) ||
806 (conflict
->theirs
&& S_ISGITLINK(conflict
->theirs
->mode
)));
810 GIT_INLINE(int) checkout_conflict_detect_binary(git_repository
*repo
, checkout_conflictdata
*conflict
)
812 git_blob
*ancestor_blob
= NULL
, *our_blob
= NULL
, *their_blob
= NULL
;
815 if (conflict
->submodule
)
818 if (conflict
->ancestor
) {
819 if ((error
= git_blob_lookup(&ancestor_blob
, repo
, &conflict
->ancestor
->id
)) < 0)
822 conflict
->binary
= git_blob_is_binary(ancestor_blob
);
825 if (!conflict
->binary
&& conflict
->ours
) {
826 if ((error
= git_blob_lookup(&our_blob
, repo
, &conflict
->ours
->id
)) < 0)
829 conflict
->binary
= git_blob_is_binary(our_blob
);
832 if (!conflict
->binary
&& conflict
->theirs
) {
833 if ((error
= git_blob_lookup(&their_blob
, repo
, &conflict
->theirs
->id
)) < 0)
836 conflict
->binary
= git_blob_is_binary(their_blob
);
840 git_blob_free(ancestor_blob
);
841 git_blob_free(our_blob
);
842 git_blob_free(their_blob
);
847 static int checkout_conflict_append_update(
848 const git_index_entry
*ancestor
,
849 const git_index_entry
*ours
,
850 const git_index_entry
*theirs
,
853 checkout_data
*data
= payload
;
854 checkout_conflictdata
*conflict
;
857 conflict
= git__calloc(1, sizeof(checkout_conflictdata
));
858 GITERR_CHECK_ALLOC(conflict
);
860 conflict
->ancestor
= ancestor
;
861 conflict
->ours
= ours
;
862 conflict
->theirs
= theirs
;
864 if ((error
= checkout_conflict_detect_submodule(conflict
)) < 0 ||
865 (error
= checkout_conflict_detect_binary(data
->repo
, conflict
)) < 0)
871 if (git_vector_insert(&data
->update_conflicts
, conflict
))
877 static int checkout_conflicts_foreach(
880 git_iterator
*workdir
,
881 git_vector
*pathspec
,
882 int (*cb
)(const git_index_entry
*, const git_index_entry
*, const git_index_entry
*, void *),
885 git_index_conflict_iterator
*iterator
= NULL
;
886 const git_index_entry
*ancestor
, *ours
, *theirs
;
889 if ((error
= git_index_conflict_iterator_new(&iterator
, index
)) < 0)
892 /* Collect the conflicts */
893 while ((error
= git_index_conflict_next(&ancestor
, &ours
, &theirs
, iterator
)) == 0) {
894 if (!conflict_pathspec_match(data
, workdir
, pathspec
, ancestor
, ours
, theirs
))
897 if ((error
= cb(ancestor
, ours
, theirs
, payload
)) < 0)
901 if (error
== GIT_ITEROVER
)
905 git_index_conflict_iterator_free(iterator
);
910 static int checkout_conflicts_load(checkout_data
*data
, git_iterator
*workdir
, git_vector
*pathspec
)
914 /* Only write conficts from sources that have them: indexes. */
915 if ((index
= git_iterator_get_index(data
->target
)) == NULL
)
918 data
->update_conflicts
._cmp
= checkout_conflictdata_cmp
;
920 if (checkout_conflicts_foreach(data
, index
, workdir
, pathspec
, checkout_conflict_append_update
, data
) < 0)
923 /* Collect the REUC and NAME entries */
924 data
->update_reuc
= &index
->reuc
;
925 data
->update_names
= &index
->names
;
930 GIT_INLINE(int) checkout_conflicts_cmp_entry(
932 const git_index_entry
*entry
)
934 return strcmp((const char *)path
, entry
->path
);
937 static int checkout_conflicts_cmp_ancestor(const void *p
, const void *c
)
939 const char *path
= p
;
940 const checkout_conflictdata
*conflict
= c
;
942 if (!conflict
->ancestor
)
945 return checkout_conflicts_cmp_entry(path
, conflict
->ancestor
);
948 static checkout_conflictdata
*checkout_conflicts_search_ancestor(
954 if (git_vector_bsearch2(&pos
, &data
->update_conflicts
, checkout_conflicts_cmp_ancestor
, path
) < 0)
957 return git_vector_get(&data
->update_conflicts
, pos
);
960 static checkout_conflictdata
*checkout_conflicts_search_branch(
964 checkout_conflictdata
*conflict
;
967 git_vector_foreach(&data
->update_conflicts
, i
, conflict
) {
970 if (conflict
->ancestor
)
974 cmp
= checkout_conflicts_cmp_entry(path
, conflict
->ours
);
975 else if (conflict
->theirs
)
976 cmp
= checkout_conflicts_cmp_entry(path
, conflict
->theirs
);
985 static int checkout_conflicts_load_byname_entry(
986 checkout_conflictdata
**ancestor_out
,
987 checkout_conflictdata
**ours_out
,
988 checkout_conflictdata
**theirs_out
,
990 const git_index_name_entry
*name_entry
)
992 checkout_conflictdata
*ancestor
, *ours
= NULL
, *theirs
= NULL
;
995 *ancestor_out
= NULL
;
999 if (!name_entry
->ancestor
) {
1000 giterr_set(GITERR_INDEX
, "A NAME entry exists without an ancestor");
1005 if (!name_entry
->ours
&& !name_entry
->theirs
) {
1006 giterr_set(GITERR_INDEX
, "A NAME entry exists without an ours or theirs");
1011 if ((ancestor
= checkout_conflicts_search_ancestor(data
,
1012 name_entry
->ancestor
)) == NULL
) {
1013 giterr_set(GITERR_INDEX
,
1014 "A NAME entry referenced ancestor entry '%s' which does not exist in the main index",
1015 name_entry
->ancestor
);
1020 if (name_entry
->ours
) {
1021 if (strcmp(name_entry
->ancestor
, name_entry
->ours
) == 0)
1023 else if ((ours
= checkout_conflicts_search_branch(data
, name_entry
->ours
)) == NULL
||
1024 ours
->ours
== NULL
) {
1025 giterr_set(GITERR_INDEX
,
1026 "A NAME entry referenced our entry '%s' which does not exist in the main index",
1033 if (name_entry
->theirs
) {
1034 if (strcmp(name_entry
->ancestor
, name_entry
->theirs
) == 0)
1036 else if (name_entry
->ours
&& strcmp(name_entry
->ours
, name_entry
->theirs
) == 0)
1038 else if ((theirs
= checkout_conflicts_search_branch(data
, name_entry
->theirs
)) == NULL
||
1039 theirs
->theirs
== NULL
) {
1040 giterr_set(GITERR_INDEX
,
1041 "A NAME entry referenced their entry '%s' which does not exist in the main index",
1042 name_entry
->theirs
);
1048 *ancestor_out
= ancestor
;
1050 *theirs_out
= theirs
;
1056 static int checkout_conflicts_coalesce_renames(
1057 checkout_data
*data
)
1060 const git_index_name_entry
*name_entry
;
1061 checkout_conflictdata
*ancestor_conflict
, *our_conflict
, *their_conflict
;
1065 if ((index
= git_iterator_get_index(data
->target
)) == NULL
)
1068 /* Juggle entries based on renames */
1069 names
= git_index_name_entrycount(index
);
1071 for (i
= 0; i
< names
; i
++) {
1072 name_entry
= git_index_name_get_byindex(index
, i
);
1074 if ((error
= checkout_conflicts_load_byname_entry(
1075 &ancestor_conflict
, &our_conflict
, &their_conflict
,
1076 data
, name_entry
)) < 0)
1079 if (our_conflict
&& our_conflict
!= ancestor_conflict
) {
1080 ancestor_conflict
->ours
= our_conflict
->ours
;
1081 our_conflict
->ours
= NULL
;
1083 if (our_conflict
->theirs
)
1084 our_conflict
->name_collision
= 1;
1086 if (our_conflict
->name_collision
)
1087 ancestor_conflict
->name_collision
= 1;
1090 if (their_conflict
&& their_conflict
!= ancestor_conflict
) {
1091 ancestor_conflict
->theirs
= their_conflict
->theirs
;
1092 their_conflict
->theirs
= NULL
;
1094 if (their_conflict
->ours
)
1095 their_conflict
->name_collision
= 1;
1097 if (their_conflict
->name_collision
)
1098 ancestor_conflict
->name_collision
= 1;
1101 if (our_conflict
&& our_conflict
!= ancestor_conflict
&&
1102 their_conflict
&& their_conflict
!= ancestor_conflict
)
1103 ancestor_conflict
->one_to_two
= 1;
1106 git_vector_remove_matching(
1107 &data
->update_conflicts
, checkout_conflictdata_empty
, NULL
);
1113 static int checkout_conflicts_mark_directoryfile(
1114 checkout_data
*data
)
1117 checkout_conflictdata
*conflict
;
1118 const git_index_entry
*entry
;
1121 int prefixed
, error
= 0;
1123 if ((index
= git_iterator_get_index(data
->target
)) == NULL
)
1126 len
= git_index_entrycount(index
);
1128 /* Find d/f conflicts */
1129 git_vector_foreach(&data
->update_conflicts
, i
, conflict
) {
1130 if ((conflict
->ours
&& conflict
->theirs
) ||
1131 (!conflict
->ours
&& !conflict
->theirs
))
1134 path
= conflict
->ours
?
1135 conflict
->ours
->path
: conflict
->theirs
->path
;
1137 if ((error
= git_index_find(&j
, index
, path
)) < 0) {
1138 if (error
== GIT_ENOTFOUND
)
1139 giterr_set(GITERR_INDEX
,
1140 "Index inconsistency, could not find entry for expected conflict '%s'", path
);
1145 for (; j
< len
; j
++) {
1146 if ((entry
= git_index_get_byindex(index
, j
)) == NULL
) {
1147 giterr_set(GITERR_INDEX
,
1148 "Index inconsistency, truncated index while loading expected conflict '%s'", path
);
1153 prefixed
= git_path_equal_or_prefixed(path
, entry
->path
, NULL
);
1155 if (prefixed
== GIT_PATH_EQUAL
)
1158 if (prefixed
== GIT_PATH_PREFIX
)
1159 conflict
->directoryfile
= 1;
1169 static int checkout_get_update_conflicts(
1170 checkout_data
*data
,
1171 git_iterator
*workdir
,
1172 git_vector
*pathspec
)
1176 if (data
->strategy
& GIT_CHECKOUT_SKIP_UNMERGED
)
1179 if ((error
= checkout_conflicts_load(data
, workdir
, pathspec
)) < 0 ||
1180 (error
= checkout_conflicts_coalesce_renames(data
)) < 0 ||
1181 (error
= checkout_conflicts_mark_directoryfile(data
)) < 0)
1188 static int checkout_conflict_append_remove(
1189 const git_index_entry
*ancestor
,
1190 const git_index_entry
*ours
,
1191 const git_index_entry
*theirs
,
1194 checkout_data
*data
= payload
;
1197 assert(ancestor
|| ours
|| theirs
);
1200 name
= git__strdup(ancestor
->path
);
1202 name
= git__strdup(ours
->path
);
1204 name
= git__strdup(theirs
->path
);
1208 GITERR_CHECK_ALLOC(name
);
1210 return git_vector_insert(&data
->remove_conflicts
, (char *)name
);
1213 static int checkout_get_remove_conflicts(
1214 checkout_data
*data
,
1215 git_iterator
*workdir
,
1216 git_vector
*pathspec
)
1218 if ((data
->strategy
& GIT_CHECKOUT_DONT_UPDATE_INDEX
) != 0)
1221 return checkout_conflicts_foreach(data
, data
->index
, workdir
, pathspec
, checkout_conflict_append_remove
, data
);
1224 static int checkout_verify_paths(
1225 git_repository
*repo
,
1227 git_diff_delta
*delta
)
1229 unsigned int flags
= GIT_PATH_REJECT_DEFAULTS
| GIT_PATH_REJECT_DOT_GIT
;
1231 if (action
& CHECKOUT_ACTION__REMOVE
) {
1232 if (!git_path_isvalid(repo
, delta
->old_file
.path
, flags
)) {
1233 giterr_set(GITERR_CHECKOUT
, "Cannot remove invalid path '%s'", delta
->old_file
.path
);
1238 if (action
& ~CHECKOUT_ACTION__REMOVE
) {
1239 if (!git_path_isvalid(repo
, delta
->new_file
.path
, flags
)) {
1240 giterr_set(GITERR_CHECKOUT
, "Cannot checkout to invalid path '%s'", delta
->new_file
.path
);
1248 static int checkout_get_actions(
1249 uint32_t **actions_ptr
,
1250 size_t **counts_ptr
,
1251 checkout_data
*data
,
1252 git_iterator
*workdir
)
1255 const git_index_entry
*wditem
;
1256 git_vector pathspec
= GIT_VECTOR_INIT
, *deltas
;
1258 git_diff_delta
*delta
;
1259 size_t i
, *counts
= NULL
;
1260 uint32_t *actions
= NULL
;
1262 git_pool_init(&pathpool
, 1);
1264 if (data
->opts
.paths
.count
> 0 &&
1265 git_pathspec__vinit(&pathspec
, &data
->opts
.paths
, &pathpool
) < 0)
1268 if ((error
= git_iterator_current(&wditem
, workdir
)) < 0 &&
1269 error
!= GIT_ITEROVER
)
1272 deltas
= &data
->diff
->deltas
;
1274 *counts_ptr
= counts
= git__calloc(CHECKOUT_ACTION__MAX
+1, sizeof(size_t));
1275 *actions_ptr
= actions
= git__calloc(
1276 deltas
->length
? deltas
->length
: 1, sizeof(uint32_t));
1277 if (!counts
|| !actions
) {
1282 git_vector_foreach(deltas
, i
, delta
) {
1283 if ((error
= checkout_action(&act
, data
, delta
, workdir
, &wditem
, &pathspec
)) == 0)
1284 error
= checkout_verify_paths(data
->repo
, act
, delta
);
1291 if (act
& CHECKOUT_ACTION__REMOVE
)
1292 counts
[CHECKOUT_ACTION__REMOVE
]++;
1293 if (act
& CHECKOUT_ACTION__UPDATE_BLOB
)
1294 counts
[CHECKOUT_ACTION__UPDATE_BLOB
]++;
1295 if (act
& CHECKOUT_ACTION__UPDATE_SUBMODULE
)
1296 counts
[CHECKOUT_ACTION__UPDATE_SUBMODULE
]++;
1297 if (act
& CHECKOUT_ACTION__CONFLICT
)
1298 counts
[CHECKOUT_ACTION__CONFLICT
]++;
1301 error
= checkout_remaining_wd_items(data
, workdir
, wditem
, &pathspec
);
1305 counts
[CHECKOUT_ACTION__REMOVE
] += data
->removes
.length
;
1307 if (counts
[CHECKOUT_ACTION__CONFLICT
] > 0 &&
1308 (data
->strategy
& GIT_CHECKOUT_ALLOW_CONFLICTS
) == 0)
1310 giterr_set(GITERR_CHECKOUT
, "%"PRIuZ
" %s checkout",
1311 counts
[CHECKOUT_ACTION__CONFLICT
],
1312 counts
[CHECKOUT_ACTION__CONFLICT
] == 1 ?
1313 "conflict prevents" : "conflicts prevent");
1314 error
= GIT_ECONFLICT
;
1319 if ((error
= checkout_get_remove_conflicts(data
, workdir
, &pathspec
)) < 0 ||
1320 (error
= checkout_get_update_conflicts(data
, workdir
, &pathspec
)) < 0)
1323 counts
[CHECKOUT_ACTION__REMOVE_CONFLICT
] = git_vector_length(&data
->remove_conflicts
);
1324 counts
[CHECKOUT_ACTION__UPDATE_CONFLICT
] = git_vector_length(&data
->update_conflicts
);
1326 git_pathspec__vfree(&pathspec
);
1327 git_pool_clear(&pathpool
);
1334 *actions_ptr
= NULL
;
1337 git_pathspec__vfree(&pathspec
);
1338 git_pool_clear(&pathpool
);
1343 static bool should_remove_existing(checkout_data
*data
)
1347 git_repository__cvar(&ignorecase
, data
->repo
, GIT_CVAR_IGNORECASE
);
1349 return (ignorecase
&&
1350 (data
->strategy
& GIT_CHECKOUT_DONT_REMOVE_EXISTING
) == 0);
1353 #define MKDIR_NORMAL \
1354 GIT_MKDIR_PATH | GIT_MKDIR_VERIFY_DIR
1355 #define MKDIR_REMOVE_EXISTING \
1356 MKDIR_NORMAL | GIT_MKDIR_REMOVE_FILES | GIT_MKDIR_REMOVE_SYMLINKS
1358 static int checkout_mkdir(
1359 checkout_data
*data
,
1365 struct git_futils_mkdir_options mkdir_opts
= {0};
1368 mkdir_opts
.dir_map
= data
->mkdir_map
;
1369 mkdir_opts
.pool
= &data
->pool
;
1371 error
= git_futils_mkdir_relative(
1372 path
, base
, mode
, flags
, &mkdir_opts
);
1374 data
->perfdata
.mkdir_calls
+= mkdir_opts
.perfdata
.mkdir_calls
;
1375 data
->perfdata
.stat_calls
+= mkdir_opts
.perfdata
.stat_calls
;
1376 data
->perfdata
.chmod_calls
+= mkdir_opts
.perfdata
.chmod_calls
;
1381 static int mkpath2file(
1382 checkout_data
*data
, const char *path
, unsigned int mode
)
1385 bool remove_existing
= should_remove_existing(data
);
1386 unsigned int flags
=
1387 (remove_existing
? MKDIR_REMOVE_EXISTING
: MKDIR_NORMAL
) |
1388 GIT_MKDIR_SKIP_LAST
;
1391 if ((error
= checkout_mkdir(
1392 data
, path
, data
->opts
.target_directory
, mode
, flags
)) < 0)
1395 if (remove_existing
) {
1396 data
->perfdata
.stat_calls
++;
1398 if (p_lstat(path
, &st
) == 0) {
1400 /* Some file, symlink or folder already exists at this name.
1401 * We would have removed it in remove_the_old unless we're on
1402 * a case inensitive filesystem (or the user has asked us not
1403 * to). Remove the similarly named file to write the new.
1405 error
= git_futils_rmdir_r(path
, NULL
, GIT_RMDIR_REMOVE_FILES
);
1406 } else if (errno
!= ENOENT
) {
1407 giterr_set(GITERR_OS
, "Failed to stat file '%s'", path
);
1417 struct checkout_stream
{
1418 git_writestream base
;
1424 static int checkout_stream_write(
1425 git_writestream
*s
, const char *buffer
, size_t len
)
1427 struct checkout_stream
*stream
= (struct checkout_stream
*)s
;
1430 if ((ret
= p_write(stream
->fd
, buffer
, len
)) < 0)
1431 giterr_set(GITERR_OS
, "Could not write to '%s'", stream
->path
);
1436 static int checkout_stream_close(git_writestream
*s
)
1438 struct checkout_stream
*stream
= (struct checkout_stream
*)s
;
1439 assert(stream
&& stream
->open
);
1442 return p_close(stream
->fd
);
1445 static void checkout_stream_free(git_writestream
*s
)
1450 static int blob_content_to_file(
1451 checkout_data
*data
,
1455 const char *hint_path
,
1456 mode_t entry_filemode
)
1458 int flags
= data
->opts
.file_open_flags
;
1459 mode_t file_mode
= data
->opts
.file_mode
?
1460 data
->opts
.file_mode
: entry_filemode
;
1461 git_filter_options filter_opts
= GIT_FILTER_OPTIONS_INIT
;
1462 struct checkout_stream writer
;
1464 git_filter_list
*fl
= NULL
;
1468 if (hint_path
== NULL
)
1471 if ((error
= mkpath2file(data
, path
, data
->opts
.dir_mode
)) < 0)
1475 flags
= O_CREAT
| O_TRUNC
| O_WRONLY
;
1476 if (!(mode
= file_mode
))
1477 mode
= GIT_FILEMODE_BLOB
;
1479 if ((fd
= p_open(path
, flags
, mode
)) < 0) {
1480 giterr_set(GITERR_OS
, "Could not open '%s' for writing", path
);
1484 filter_opts
.attr_session
= &data
->attr_session
;
1485 filter_opts
.temp_buf
= &data
->tmp
;
1487 if (!data
->opts
.disable_filters
&&
1488 (error
= git_filter_list__load_ext(
1489 &fl
, data
->repo
, blob
, hint_path
,
1490 GIT_FILTER_TO_WORKTREE
, &filter_opts
)))
1493 /* setup the writer */
1494 memset(&writer
, 0, sizeof(struct checkout_stream
));
1495 writer
.base
.write
= checkout_stream_write
;
1496 writer
.base
.close
= checkout_stream_close
;
1497 writer
.base
.free
= checkout_stream_free
;
1502 error
= git_filter_list_stream_blob(fl
, blob
, &writer
.base
);
1504 assert(writer
.open
== 0);
1506 git_filter_list_free(fl
);
1512 data
->perfdata
.stat_calls
++;
1514 if ((error
= p_stat(path
, st
)) < 0) {
1515 giterr_set(GITERR_OS
, "Error statting '%s'", path
);
1519 st
->st_mode
= entry_filemode
;
1525 static int blob_content_to_link(
1526 checkout_data
*data
,
1531 git_buf linktarget
= GIT_BUF_INIT
;
1534 if ((error
= mkpath2file(data
, path
, data
->opts
.dir_mode
)) < 0)
1537 if ((error
= git_blob__getbuf(&linktarget
, blob
)) < 0)
1540 if (data
->can_symlink
) {
1541 if ((error
= p_symlink(git_buf_cstr(&linktarget
), path
)) < 0)
1542 giterr_set(GITERR_OS
, "Could not create symlink %s", path
);
1544 error
= git_futils_fake_symlink(git_buf_cstr(&linktarget
), path
);
1548 data
->perfdata
.stat_calls
++;
1550 if ((error
= p_lstat(path
, st
)) < 0)
1551 giterr_set(GITERR_CHECKOUT
, "Could not stat symlink %s", path
);
1553 st
->st_mode
= GIT_FILEMODE_LINK
;
1556 git_buf_free(&linktarget
);
1561 static int checkout_update_index(
1562 checkout_data
*data
,
1563 const git_diff_file
*file
,
1566 git_index_entry entry
;
1571 memset(&entry
, 0, sizeof(entry
));
1572 entry
.path
= (char *)file
->path
; /* cast to prevent warning */
1573 git_index_entry__init_from_stat(&entry
, st
, true);
1574 git_oid_cpy(&entry
.id
, &file
->id
);
1576 return git_index_add(data
->index
, &entry
);
1579 static int checkout_submodule_update_index(
1580 checkout_data
*data
,
1581 const git_diff_file
*file
)
1585 /* update the index unless prevented */
1586 if ((data
->strategy
& GIT_CHECKOUT_DONT_UPDATE_INDEX
) != 0)
1589 git_buf_truncate(&data
->path
, data
->workdir_len
);
1590 if (git_buf_puts(&data
->path
, file
->path
) < 0)
1593 data
->perfdata
.stat_calls
++;
1594 if (p_stat(git_buf_cstr(&data
->path
), &st
) < 0) {
1596 GITERR_CHECKOUT
, "Could not stat submodule %s\n", file
->path
);
1597 return GIT_ENOTFOUND
;
1600 st
.st_mode
= GIT_FILEMODE_COMMIT
;
1602 return checkout_update_index(data
, file
, &st
);
1605 static int checkout_submodule(
1606 checkout_data
*data
,
1607 const git_diff_file
*file
)
1609 bool remove_existing
= should_remove_existing(data
);
1612 /* Until submodules are supported, UPDATE_ONLY means do nothing here */
1613 if ((data
->strategy
& GIT_CHECKOUT_UPDATE_ONLY
) != 0)
1616 if ((error
= checkout_mkdir(
1618 file
->path
, data
->opts
.target_directory
, data
->opts
.dir_mode
,
1619 remove_existing
? MKDIR_REMOVE_EXISTING
: MKDIR_NORMAL
)) < 0)
1622 if ((error
= git_submodule_lookup(NULL
, data
->repo
, file
->path
)) < 0) {
1623 /* I've observed repos with submodules in the tree that do not
1624 * have a .gitmodules - core Git just makes an empty directory
1626 if (error
== GIT_ENOTFOUND
) {
1628 return checkout_submodule_update_index(data
, file
);
1634 /* TODO: Support checkout_strategy options. Two circumstances:
1635 * 1 - submodule already checked out, but we need to move the HEAD
1636 * to the new OID, or
1637 * 2 - submodule not checked out and we should recursively check it out
1639 * Checkout will not execute a pull on the submodule, but a clone
1640 * command should probably be able to. Do we need a submodule callback?
1643 return checkout_submodule_update_index(data
, file
);
1646 static void report_progress(
1647 checkout_data
*data
,
1650 if (data
->opts
.progress_cb
)
1651 data
->opts
.progress_cb(
1652 path
, data
->completed_steps
, data
->total_steps
,
1653 data
->opts
.progress_payload
);
1656 static int checkout_safe_for_update_only(
1657 checkout_data
*data
, const char *path
, mode_t expected_mode
)
1661 data
->perfdata
.stat_calls
++;
1663 if (p_lstat(path
, &st
) < 0) {
1664 /* if doesn't exist, then no error and no update */
1665 if (errno
== ENOENT
|| errno
== ENOTDIR
)
1668 /* otherwise, stat error and no update */
1669 giterr_set(GITERR_OS
, "Failed to stat file '%s'", path
);
1673 /* only safe for update if this is the same type of file */
1674 if ((st
.st_mode
& ~0777) == (expected_mode
& ~0777))
1680 static int checkout_write_content(
1681 checkout_data
*data
,
1683 const char *full_path
,
1684 const char *hint_path
,
1691 if ((error
= git_blob_lookup(&blob
, data
->repo
, oid
)) < 0)
1695 error
= blob_content_to_link(data
, st
, blob
, full_path
);
1697 error
= blob_content_to_file(data
, st
, blob
, full_path
, hint_path
, mode
);
1699 git_blob_free(blob
);
1701 /* if we try to create the blob and an existing directory blocks it from
1702 * being written, then there must have been a typechange conflict in a
1703 * parent directory - suppress the error and try to continue.
1705 if ((data
->strategy
& GIT_CHECKOUT_ALLOW_CONFLICTS
) != 0 &&
1706 (error
== GIT_ENOTFOUND
|| error
== GIT_EEXISTS
))
1715 static int checkout_blob(
1716 checkout_data
*data
,
1717 const git_diff_file
*file
)
1722 git_buf_truncate(&data
->path
, data
->workdir_len
);
1723 if (git_buf_puts(&data
->path
, file
->path
) < 0)
1726 if ((data
->strategy
& GIT_CHECKOUT_UPDATE_ONLY
) != 0) {
1727 int rval
= checkout_safe_for_update_only(
1728 data
, git_buf_cstr(&data
->path
), file
->mode
);
1733 error
= checkout_write_content(
1734 data
, &file
->id
, git_buf_cstr(&data
->path
), NULL
, file
->mode
, &st
);
1736 /* update the index unless prevented */
1737 if (!error
&& (data
->strategy
& GIT_CHECKOUT_DONT_UPDATE_INDEX
) == 0)
1738 error
= checkout_update_index(data
, file
, &st
);
1740 /* update the submodule data if this was a new .gitmodules file */
1741 if (!error
&& strcmp(file
->path
, ".gitmodules") == 0)
1742 data
->reload_submodules
= true;
1747 static int checkout_remove_the_old(
1748 unsigned int *actions
,
1749 checkout_data
*data
)
1752 git_diff_delta
*delta
;
1755 const char *workdir
= git_buf_cstr(&data
->path
);
1756 uint32_t flg
= GIT_RMDIR_EMPTY_PARENTS
|
1757 GIT_RMDIR_REMOVE_FILES
| GIT_RMDIR_REMOVE_BLOCKERS
;
1759 if (data
->opts
.checkout_strategy
& GIT_CHECKOUT_SKIP_LOCKED_DIRECTORIES
)
1760 flg
|= GIT_RMDIR_SKIP_NONEMPTY
;
1762 git_buf_truncate(&data
->path
, data
->workdir_len
);
1764 git_vector_foreach(&data
->diff
->deltas
, i
, delta
) {
1765 if (actions
[i
] & CHECKOUT_ACTION__REMOVE
) {
1766 error
= git_futils_rmdir_r(delta
->old_file
.path
, workdir
, flg
);
1770 data
->completed_steps
++;
1771 report_progress(data
, delta
->old_file
.path
);
1773 if ((actions
[i
] & CHECKOUT_ACTION__UPDATE_BLOB
) == 0 &&
1774 (data
->strategy
& GIT_CHECKOUT_DONT_UPDATE_INDEX
) == 0 &&
1775 data
->index
!= NULL
)
1777 (void)git_index_remove(data
->index
, delta
->old_file
.path
, 0);
1782 git_vector_foreach(&data
->removes
, i
, str
) {
1783 error
= git_futils_rmdir_r(str
, workdir
, flg
);
1787 data
->completed_steps
++;
1788 report_progress(data
, str
);
1790 if ((data
->strategy
& GIT_CHECKOUT_DONT_UPDATE_INDEX
) == 0 &&
1791 data
->index
!= NULL
)
1793 if (str
[strlen(str
) - 1] == '/')
1794 (void)git_index_remove_directory(data
->index
, str
, 0);
1796 (void)git_index_remove(data
->index
, str
, 0);
1803 static int checkout_deferred_remove(git_repository
*repo
, const char *path
)
1806 int error
= git_futils_rmdir_r(
1807 path
, data
->opts
.target_directory
, GIT_RMDIR_EMPTY_PARENTS
);
1809 if (error
== GIT_ENOTFOUND
) {
1823 static int checkout_create_the_new(
1824 unsigned int *actions
,
1825 checkout_data
*data
)
1828 git_diff_delta
*delta
;
1831 git_vector_foreach(&data
->diff
->deltas
, i
, delta
) {
1832 if (actions
[i
] & CHECKOUT_ACTION__DEFER_REMOVE
) {
1833 /* this had a blocker directory that should only be removed iff
1834 * all of the contents of the directory were safely removed
1836 if ((error
= checkout_deferred_remove(
1837 data
->repo
, delta
->old_file
.path
)) < 0)
1841 if (actions
[i
] & CHECKOUT_ACTION__UPDATE_BLOB
) {
1842 error
= checkout_blob(data
, &delta
->new_file
);
1846 data
->completed_steps
++;
1847 report_progress(data
, delta
->new_file
.path
);
1854 static int checkout_create_submodules(
1855 unsigned int *actions
,
1856 checkout_data
*data
)
1859 git_diff_delta
*delta
;
1862 git_vector_foreach(&data
->diff
->deltas
, i
, delta
) {
1863 if (actions
[i
] & CHECKOUT_ACTION__DEFER_REMOVE
) {
1864 /* this has a blocker directory that should only be removed iff
1865 * all of the contents of the directory were safely removed
1867 if ((error
= checkout_deferred_remove(
1868 data
->repo
, delta
->old_file
.path
)) < 0)
1872 if (actions
[i
] & CHECKOUT_ACTION__UPDATE_SUBMODULE
) {
1873 int error
= checkout_submodule(data
, &delta
->new_file
);
1877 data
->completed_steps
++;
1878 report_progress(data
, delta
->new_file
.path
);
1885 static int checkout_lookup_head_tree(git_tree
**out
, git_repository
*repo
)
1888 git_reference
*ref
= NULL
;
1891 if (!(error
= git_repository_head(&ref
, repo
)) &&
1892 !(error
= git_reference_peel(&head
, ref
, GIT_OBJ_TREE
)))
1893 *out
= (git_tree
*)head
;
1895 git_reference_free(ref
);
1901 static int conflict_entry_name(
1903 const char *side_name
,
1904 const char *filename
)
1906 if (git_buf_puts(out
, side_name
) < 0 ||
1907 git_buf_putc(out
, ':') < 0 ||
1908 git_buf_puts(out
, filename
) < 0)
1914 static int checkout_path_suffixed(git_buf
*path
, const char *suffix
)
1917 int i
= 0, error
= 0;
1919 if ((error
= git_buf_putc(path
, '~')) < 0 || (error
= git_buf_puts(path
, suffix
)) < 0)
1922 path_len
= git_buf_len(path
);
1924 while (git_path_exists(git_buf_cstr(path
)) && i
< INT_MAX
) {
1925 git_buf_truncate(path
, path_len
);
1927 if ((error
= git_buf_putc(path
, '_')) < 0 ||
1928 (error
= git_buf_printf(path
, "%d", i
)) < 0)
1935 git_buf_truncate(path
, path_len
);
1937 giterr_set(GITERR_CHECKOUT
, "Could not write '%s': working directory file exists", path
);
1944 static int checkout_write_entry(
1945 checkout_data
*data
,
1946 checkout_conflictdata
*conflict
,
1947 const git_index_entry
*side
)
1949 const char *hint_path
= NULL
, *suffix
;
1953 assert (side
== conflict
->ours
|| side
== conflict
->theirs
);
1955 git_buf_truncate(&data
->path
, data
->workdir_len
);
1956 if (git_buf_puts(&data
->path
, side
->path
) < 0)
1959 if ((conflict
->name_collision
|| conflict
->directoryfile
) &&
1960 (data
->strategy
& GIT_CHECKOUT_USE_OURS
) == 0 &&
1961 (data
->strategy
& GIT_CHECKOUT_USE_THEIRS
) == 0) {
1963 if (side
== conflict
->ours
)
1964 suffix
= data
->opts
.our_label
? data
->opts
.our_label
:
1967 suffix
= data
->opts
.their_label
? data
->opts
.their_label
:
1970 if (checkout_path_suffixed(&data
->path
, suffix
) < 0)
1973 hint_path
= side
->path
;
1976 if ((data
->strategy
& GIT_CHECKOUT_UPDATE_ONLY
) != 0 &&
1977 (error
= checkout_safe_for_update_only(data
, git_buf_cstr(&data
->path
), side
->mode
)) <= 0)
1980 return checkout_write_content(data
,
1981 &side
->id
, git_buf_cstr(&data
->path
), hint_path
, side
->mode
, &st
);
1984 static int checkout_write_entries(
1985 checkout_data
*data
,
1986 checkout_conflictdata
*conflict
)
1990 if ((error
= checkout_write_entry(data
, conflict
, conflict
->ours
)) >= 0)
1991 error
= checkout_write_entry(data
, conflict
, conflict
->theirs
);
1996 static int checkout_merge_path(
1998 checkout_data
*data
,
1999 checkout_conflictdata
*conflict
,
2000 git_merge_file_result
*result
)
2002 const char *our_label_raw
, *their_label_raw
, *suffix
;
2005 if ((error
= git_buf_joinpath(out
, git_repository_workdir(data
->repo
), result
->path
)) < 0)
2008 /* Most conflicts simply use the filename in the index */
2009 if (!conflict
->name_collision
)
2012 /* Rename 2->1 conflicts need the branch name appended */
2013 our_label_raw
= data
->opts
.our_label
? data
->opts
.our_label
: "ours";
2014 their_label_raw
= data
->opts
.their_label
? data
->opts
.their_label
: "theirs";
2015 suffix
= strcmp(result
->path
, conflict
->ours
->path
) == 0 ? our_label_raw
: their_label_raw
;
2017 if ((error
= checkout_path_suffixed(out
, suffix
)) < 0)
2023 static int checkout_write_merge(
2024 checkout_data
*data
,
2025 checkout_conflictdata
*conflict
)
2027 git_buf our_label
= GIT_BUF_INIT
, their_label
= GIT_BUF_INIT
,
2028 path_suffixed
= GIT_BUF_INIT
, path_workdir
= GIT_BUF_INIT
,
2029 in_data
= GIT_BUF_INIT
, out_data
= GIT_BUF_INIT
;
2030 git_merge_file_options opts
= GIT_MERGE_FILE_OPTIONS_INIT
;
2031 git_merge_file_result result
= {0};
2032 git_filebuf output
= GIT_FILEBUF_INIT
;
2033 git_filter_list
*fl
= NULL
;
2034 git_filter_options filter_opts
= GIT_FILTER_OPTIONS_INIT
;
2037 if (data
->opts
.checkout_strategy
& GIT_CHECKOUT_CONFLICT_STYLE_DIFF3
)
2038 opts
.flags
|= GIT_MERGE_FILE_STYLE_DIFF3
;
2040 opts
.ancestor_label
= data
->opts
.ancestor_label
?
2041 data
->opts
.ancestor_label
: "ancestor";
2042 opts
.our_label
= data
->opts
.our_label
?
2043 data
->opts
.our_label
: "ours";
2044 opts
.their_label
= data
->opts
.their_label
?
2045 data
->opts
.their_label
: "theirs";
2047 /* If all the paths are identical, decorate the diff3 file with the branch
2048 * names. Otherwise, append branch_name:path.
2050 if (conflict
->ours
&& conflict
->theirs
&&
2051 strcmp(conflict
->ours
->path
, conflict
->theirs
->path
) != 0) {
2053 if ((error
= conflict_entry_name(
2054 &our_label
, opts
.our_label
, conflict
->ours
->path
)) < 0 ||
2055 (error
= conflict_entry_name(
2056 &their_label
, opts
.their_label
, conflict
->theirs
->path
)) < 0)
2059 opts
.our_label
= git_buf_cstr(&our_label
);
2060 opts
.their_label
= git_buf_cstr(&their_label
);
2063 if ((error
= git_merge_file_from_index(&result
, data
->repo
,
2064 conflict
->ancestor
, conflict
->ours
, conflict
->theirs
, &opts
)) < 0)
2067 if (result
.path
== NULL
|| result
.mode
== 0) {
2068 giterr_set(GITERR_CHECKOUT
, "Could not merge contents of file");
2069 error
= GIT_ECONFLICT
;
2073 if ((error
= checkout_merge_path(&path_workdir
, data
, conflict
, &result
)) < 0)
2076 if ((data
->strategy
& GIT_CHECKOUT_UPDATE_ONLY
) != 0 &&
2077 (error
= checkout_safe_for_update_only(data
, git_buf_cstr(&path_workdir
), result
.mode
)) <= 0)
2080 if (!data
->opts
.disable_filters
) {
2081 in_data
.ptr
= (char *)result
.ptr
;
2082 in_data
.size
= result
.len
;
2084 filter_opts
.attr_session
= &data
->attr_session
;
2085 filter_opts
.temp_buf
= &data
->tmp
;
2087 if ((error
= git_filter_list__load_ext(
2088 &fl
, data
->repo
, NULL
, git_buf_cstr(&path_workdir
),
2089 GIT_FILTER_TO_WORKTREE
, &filter_opts
)) < 0 ||
2090 (error
= git_filter_list_apply_to_data(&out_data
, fl
, &in_data
)) < 0)
2093 out_data
.ptr
= (char *)result
.ptr
;
2094 out_data
.size
= result
.len
;
2097 if ((error
= mkpath2file(data
, path_workdir
.ptr
, data
->opts
.dir_mode
)) < 0 ||
2098 (error
= git_filebuf_open(&output
, git_buf_cstr(&path_workdir
), GIT_FILEBUF_DO_NOT_BUFFER
, result
.mode
)) < 0 ||
2099 (error
= git_filebuf_write(&output
, out_data
.ptr
, out_data
.size
)) < 0 ||
2100 (error
= git_filebuf_commit(&output
)) < 0)
2104 git_filter_list_free(fl
);
2106 git_buf_free(&out_data
);
2107 git_buf_free(&our_label
);
2108 git_buf_free(&their_label
);
2110 git_merge_file_result_free(&result
);
2111 git_buf_free(&path_workdir
);
2112 git_buf_free(&path_suffixed
);
2117 static int checkout_conflict_add(
2118 checkout_data
*data
,
2119 const git_index_entry
*conflict
)
2121 int error
= git_index_remove(data
->index
, conflict
->path
, 0);
2123 if (error
== GIT_ENOTFOUND
)
2128 return git_index_add(data
->index
, conflict
);
2131 static int checkout_conflict_update_index(
2132 checkout_data
*data
,
2133 checkout_conflictdata
*conflict
)
2137 if (conflict
->ancestor
)
2138 error
= checkout_conflict_add(data
, conflict
->ancestor
);
2140 if (!error
&& conflict
->ours
)
2141 error
= checkout_conflict_add(data
, conflict
->ours
);
2143 if (!error
&& conflict
->theirs
)
2144 error
= checkout_conflict_add(data
, conflict
->theirs
);
2149 static int checkout_create_conflicts(checkout_data
*data
)
2151 checkout_conflictdata
*conflict
;
2155 git_vector_foreach(&data
->update_conflicts
, i
, conflict
) {
2157 /* Both deleted: nothing to do */
2158 if (conflict
->ours
== NULL
&& conflict
->theirs
== NULL
)
2161 else if ((data
->strategy
& GIT_CHECKOUT_USE_OURS
) &&
2163 error
= checkout_write_entry(data
, conflict
, conflict
->ours
);
2164 else if ((data
->strategy
& GIT_CHECKOUT_USE_THEIRS
) &&
2166 error
= checkout_write_entry(data
, conflict
, conflict
->theirs
);
2168 /* Ignore the other side of name collisions. */
2169 else if ((data
->strategy
& GIT_CHECKOUT_USE_OURS
) &&
2170 !conflict
->ours
&& conflict
->name_collision
)
2172 else if ((data
->strategy
& GIT_CHECKOUT_USE_THEIRS
) &&
2173 !conflict
->theirs
&& conflict
->name_collision
)
2176 /* For modify/delete, name collisions and d/f conflicts, write
2177 * the file (potentially with the name mangled.
2179 else if (conflict
->ours
!= NULL
&& conflict
->theirs
== NULL
)
2180 error
= checkout_write_entry(data
, conflict
, conflict
->ours
);
2181 else if (conflict
->ours
== NULL
&& conflict
->theirs
!= NULL
)
2182 error
= checkout_write_entry(data
, conflict
, conflict
->theirs
);
2184 /* Add/add conflicts and rename 1->2 conflicts, write the
2185 * ours/theirs sides (potentially name mangled).
2187 else if (conflict
->one_to_two
)
2188 error
= checkout_write_entries(data
, conflict
);
2190 /* If all sides are links, write the ours side */
2191 else if (S_ISLNK(conflict
->ours
->mode
) &&
2192 S_ISLNK(conflict
->theirs
->mode
))
2193 error
= checkout_write_entry(data
, conflict
, conflict
->ours
);
2194 /* Link/file conflicts, write the file side */
2195 else if (S_ISLNK(conflict
->ours
->mode
))
2196 error
= checkout_write_entry(data
, conflict
, conflict
->theirs
);
2197 else if (S_ISLNK(conflict
->theirs
->mode
))
2198 error
= checkout_write_entry(data
, conflict
, conflict
->ours
);
2200 /* If any side is a gitlink, do nothing. */
2201 else if (conflict
->submodule
)
2204 /* If any side is binary, write the ours side */
2205 else if (conflict
->binary
)
2206 error
= checkout_write_entry(data
, conflict
, conflict
->ours
);
2209 error
= checkout_write_merge(data
, conflict
);
2211 /* Update the index extensions (REUC and NAME) if we're checking
2212 * out a different index. (Otherwise just leave them there.)
2214 if (!error
&& (data
->strategy
& GIT_CHECKOUT_DONT_UPDATE_INDEX
) == 0)
2215 error
= checkout_conflict_update_index(data
, conflict
);
2220 data
->completed_steps
++;
2221 report_progress(data
,
2222 conflict
->ours
? conflict
->ours
->path
:
2223 (conflict
->theirs
? conflict
->theirs
->path
: conflict
->ancestor
->path
));
2229 static int checkout_remove_conflicts(checkout_data
*data
)
2231 const char *conflict
;
2234 git_vector_foreach(&data
->remove_conflicts
, i
, conflict
) {
2235 if (git_index_conflict_remove(data
->index
, conflict
) < 0)
2238 data
->completed_steps
++;
2244 static int checkout_extensions_update_index(checkout_data
*data
)
2246 const git_index_reuc_entry
*reuc_entry
;
2247 const git_index_name_entry
*name_entry
;
2251 if ((data
->strategy
& GIT_CHECKOUT_UPDATE_ONLY
) != 0)
2254 if (data
->update_reuc
) {
2255 git_vector_foreach(data
->update_reuc
, i
, reuc_entry
) {
2256 if ((error
= git_index_reuc_add(data
->index
, reuc_entry
->path
,
2257 reuc_entry
->mode
[0], &reuc_entry
->oid
[0],
2258 reuc_entry
->mode
[1], &reuc_entry
->oid
[1],
2259 reuc_entry
->mode
[2], &reuc_entry
->oid
[2])) < 0)
2264 if (data
->update_names
) {
2265 git_vector_foreach(data
->update_names
, i
, name_entry
) {
2266 if ((error
= git_index_name_add(data
->index
, name_entry
->ancestor
,
2267 name_entry
->ours
, name_entry
->theirs
)) < 0)
2276 static void checkout_data_clear(checkout_data
*data
)
2278 if (data
->opts_free_baseline
) {
2279 git_tree_free(data
->opts
.baseline
);
2280 data
->opts
.baseline
= NULL
;
2283 git_vector_free(&data
->removes
);
2284 git_pool_clear(&data
->pool
);
2286 git_vector_free_deep(&data
->remove_conflicts
);
2287 git_vector_free_deep(&data
->update_conflicts
);
2289 git__free(data
->pfx
);
2292 git_strmap_free(data
->mkdir_map
);
2294 git_buf_free(&data
->path
);
2295 git_buf_free(&data
->tmp
);
2297 git_index_free(data
->index
);
2300 git_strmap_free(data
->mkdir_map
);
2302 git_attr_session__free(&data
->attr_session
);
2305 static int checkout_data_init(
2306 checkout_data
*data
,
2307 git_iterator
*target
,
2308 const git_checkout_options
*proposed
)
2311 git_repository
*repo
= git_iterator_owner(target
);
2313 memset(data
, 0, sizeof(*data
));
2316 giterr_set(GITERR_CHECKOUT
, "Cannot checkout nothing");
2320 if ((!proposed
|| !proposed
->target_directory
) &&
2321 (error
= git_repository__ensure_not_bare(repo
, "checkout")) < 0)
2325 data
->target
= target
;
2327 GITERR_CHECK_VERSION(
2328 proposed
, GIT_CHECKOUT_OPTIONS_VERSION
, "git_checkout_options");
2331 GIT_INIT_STRUCTURE(&data
->opts
, GIT_CHECKOUT_OPTIONS_VERSION
);
2333 memmove(&data
->opts
, proposed
, sizeof(git_checkout_options
));
2335 if (!data
->opts
.target_directory
)
2336 data
->opts
.target_directory
= git_repository_workdir(repo
);
2337 else if (!git_path_isdir(data
->opts
.target_directory
) &&
2338 (error
= checkout_mkdir(data
,
2339 data
->opts
.target_directory
, NULL
,
2340 GIT_DIR_MODE
, GIT_MKDIR_VERIFY_DIR
)) < 0)
2343 /* refresh config and index content unless NO_REFRESH is given */
2344 if ((data
->opts
.checkout_strategy
& GIT_CHECKOUT_NO_REFRESH
) == 0) {
2347 if ((error
= git_repository_config__weakptr(&cfg
, repo
)) < 0)
2350 /* Get the repository index and reload it (unless we're checking
2351 * out the index; then it has the changes we're trying to check
2352 * out and those should not be overwritten.)
2354 if ((error
= git_repository_index(&data
->index
, data
->repo
)) < 0)
2357 if (data
->index
!= git_iterator_get_index(target
)) {
2358 if ((error
= git_index_read(data
->index
, true)) < 0)
2361 /* cannot checkout if unresolved conflicts exist */
2362 if ((data
->opts
.checkout_strategy
& GIT_CHECKOUT_FORCE
) == 0 &&
2363 git_index_has_conflicts(data
->index
)) {
2364 error
= GIT_ECONFLICT
;
2365 giterr_set(GITERR_CHECKOUT
,
2366 "unresolved conflicts exist in the index");
2370 /* clean conflict data in the current index */
2371 git_index_name_clear(data
->index
);
2372 git_index_reuc_clear(data
->index
);
2376 /* if you are forcing, allow all safe updates, plus recreate missing */
2377 if ((data
->opts
.checkout_strategy
& GIT_CHECKOUT_FORCE
) != 0)
2378 data
->opts
.checkout_strategy
|= GIT_CHECKOUT_SAFE
|
2379 GIT_CHECKOUT_RECREATE_MISSING
;
2381 /* if the repository does not actually have an index file, then this
2382 * is an initial checkout (perhaps from clone), so we allow safe updates
2384 if (!data
->index
->on_disk
&&
2385 (data
->opts
.checkout_strategy
& GIT_CHECKOUT_SAFE
) != 0)
2386 data
->opts
.checkout_strategy
|= GIT_CHECKOUT_RECREATE_MISSING
;
2388 data
->strategy
= data
->opts
.checkout_strategy
;
2390 /* opts->disable_filters is false by default */
2392 if (!data
->opts
.dir_mode
)
2393 data
->opts
.dir_mode
= GIT_DIR_MODE
;
2395 if (!data
->opts
.file_open_flags
)
2396 data
->opts
.file_open_flags
= O_CREAT
| O_TRUNC
| O_WRONLY
;
2398 data
->pfx
= git_pathspec_prefix(&data
->opts
.paths
);
2400 if ((error
= git_repository__cvar(
2401 &data
->can_symlink
, repo
, GIT_CVAR_SYMLINKS
)) < 0)
2404 if (!data
->opts
.baseline
&& !data
->opts
.baseline_index
) {
2405 data
->opts_free_baseline
= true;
2407 error
= checkout_lookup_head_tree(&data
->opts
.baseline
, repo
);
2409 if (error
== GIT_EUNBORNBRANCH
) {
2418 if ((data
->opts
.checkout_strategy
&
2419 (GIT_CHECKOUT_CONFLICT_STYLE_MERGE
| GIT_CHECKOUT_CONFLICT_STYLE_DIFF3
)) == 0) {
2420 git_config_entry
*conflict_style
= NULL
;
2421 git_config
*cfg
= NULL
;
2423 if ((error
= git_repository_config__weakptr(&cfg
, repo
)) < 0 ||
2424 (error
= git_config_get_entry(&conflict_style
, cfg
, "merge.conflictstyle")) < 0 ||
2425 error
== GIT_ENOTFOUND
)
2429 else if (strcmp(conflict_style
->value
, "merge") == 0)
2430 data
->opts
.checkout_strategy
|= GIT_CHECKOUT_CONFLICT_STYLE_MERGE
;
2431 else if (strcmp(conflict_style
->value
, "diff3") == 0)
2432 data
->opts
.checkout_strategy
|= GIT_CHECKOUT_CONFLICT_STYLE_DIFF3
;
2434 giterr_set(GITERR_CHECKOUT
, "unknown style '%s' given for 'merge.conflictstyle'",
2437 git_config_entry_free(conflict_style
);
2440 git_config_entry_free(conflict_style
);
2443 git_pool_init(&data
->pool
, 1);
2445 if ((error
= git_vector_init(&data
->removes
, 0, git__strcmp_cb
)) < 0 ||
2446 (error
= git_vector_init(&data
->remove_conflicts
, 0, NULL
)) < 0 ||
2447 (error
= git_vector_init(&data
->update_conflicts
, 0, NULL
)) < 0 ||
2448 (error
= git_buf_puts(&data
->path
, data
->opts
.target_directory
)) < 0 ||
2449 (error
= git_path_to_dir(&data
->path
)) < 0 ||
2450 (error
= git_strmap_alloc(&data
->mkdir_map
)) < 0)
2453 data
->workdir_len
= git_buf_len(&data
->path
);
2455 git_attr_session__init(&data
->attr_session
, data
->repo
);
2459 checkout_data_clear(data
);
2464 #define CHECKOUT_INDEX_DONT_WRITE_MASK \
2465 (GIT_CHECKOUT_DONT_UPDATE_INDEX | GIT_CHECKOUT_DONT_WRITE_INDEX)
2467 int git_checkout_iterator(
2468 git_iterator
*target
,
2470 const git_checkout_options
*opts
)
2473 git_iterator
*baseline
= NULL
, *workdir
= NULL
;
2474 git_iterator_options baseline_opts
= GIT_ITERATOR_OPTIONS_INIT
,
2475 workdir_opts
= GIT_ITERATOR_OPTIONS_INIT
;
2476 checkout_data data
= {0};
2477 git_diff_options diff_opts
= GIT_DIFF_OPTIONS_INIT
;
2478 uint32_t *actions
= NULL
;
2479 size_t *counts
= NULL
;
2481 /* initialize structures and options */
2482 error
= checkout_data_init(&data
, target
, opts
);
2487 GIT_DIFF_INCLUDE_UNMODIFIED
|
2488 GIT_DIFF_INCLUDE_UNREADABLE
|
2489 GIT_DIFF_INCLUDE_UNTRACKED
|
2490 GIT_DIFF_RECURSE_UNTRACKED_DIRS
| /* needed to match baseline */
2491 GIT_DIFF_INCLUDE_IGNORED
|
2492 GIT_DIFF_INCLUDE_TYPECHANGE
|
2493 GIT_DIFF_INCLUDE_TYPECHANGE_TREES
|
2494 GIT_DIFF_SKIP_BINARY_CHECK
|
2495 GIT_DIFF_INCLUDE_CASECHANGE
;
2496 if (data
.opts
.checkout_strategy
& GIT_CHECKOUT_DISABLE_PATHSPEC_MATCH
)
2497 diff_opts
.flags
|= GIT_DIFF_DISABLE_PATHSPEC_MATCH
;
2498 if (data
.opts
.paths
.count
> 0)
2499 diff_opts
.pathspec
= data
.opts
.paths
;
2501 /* set up iterators */
2503 workdir_opts
.flags
= git_iterator_ignore_case(target
) ?
2504 GIT_ITERATOR_IGNORE_CASE
: GIT_ITERATOR_DONT_IGNORE_CASE
;
2505 workdir_opts
.flags
|= GIT_ITERATOR_DONT_AUTOEXPAND
;
2506 workdir_opts
.start
= data
.pfx
;
2507 workdir_opts
.end
= data
.pfx
;
2509 if ((error
= git_iterator_reset(target
, data
.pfx
, data
.pfx
)) < 0 ||
2510 (error
= git_iterator_for_workdir_ext(
2511 &workdir
, data
.repo
, data
.opts
.target_directory
, index
, NULL
,
2512 &workdir_opts
)) < 0)
2515 baseline_opts
.flags
= git_iterator_ignore_case(target
) ?
2516 GIT_ITERATOR_IGNORE_CASE
: GIT_ITERATOR_DONT_IGNORE_CASE
;
2517 baseline_opts
.start
= data
.pfx
;
2518 baseline_opts
.end
= data
.pfx
;
2520 if (data
.opts
.baseline_index
) {
2521 if ((error
= git_iterator_for_index(
2522 &baseline
, data
.opts
.baseline_index
, &baseline_opts
)) < 0)
2525 if ((error
= git_iterator_for_tree(
2526 &baseline
, data
.opts
.baseline
, &baseline_opts
)) < 0)
2530 /* Should not have case insensitivity mismatch */
2531 assert(git_iterator_ignore_case(workdir
) == git_iterator_ignore_case(baseline
));
2533 /* Generate baseline-to-target diff which will include an entry for
2534 * every possible update that might need to be made.
2536 if ((error
= git_diff__from_iterators(
2537 &data
.diff
, data
.repo
, baseline
, target
, &diff_opts
)) < 0)
2540 /* Loop through diff (and working directory iterator) building a list of
2541 * actions to be taken, plus look for conflicts and send notifications,
2542 * then loop through conflicts.
2544 if ((error
= checkout_get_actions(&actions
, &counts
, &data
, workdir
)) != 0)
2547 data
.total_steps
= counts
[CHECKOUT_ACTION__REMOVE
] +
2548 counts
[CHECKOUT_ACTION__REMOVE_CONFLICT
] +
2549 counts
[CHECKOUT_ACTION__UPDATE_BLOB
] +
2550 counts
[CHECKOUT_ACTION__UPDATE_SUBMODULE
] +
2551 counts
[CHECKOUT_ACTION__UPDATE_CONFLICT
];
2553 report_progress(&data
, NULL
); /* establish 0 baseline */
2555 /* To deal with some order dependencies, perform remaining checkout
2556 * in three passes: removes, then update blobs, then update submodules.
2558 if (counts
[CHECKOUT_ACTION__REMOVE
] > 0 &&
2559 (error
= checkout_remove_the_old(actions
, &data
)) < 0)
2562 if (counts
[CHECKOUT_ACTION__REMOVE_CONFLICT
] > 0 &&
2563 (error
= checkout_remove_conflicts(&data
)) < 0)
2566 if (counts
[CHECKOUT_ACTION__UPDATE_BLOB
] > 0 &&
2567 (error
= checkout_create_the_new(actions
, &data
)) < 0)
2570 if (counts
[CHECKOUT_ACTION__UPDATE_SUBMODULE
] > 0 &&
2571 (error
= checkout_create_submodules(actions
, &data
)) < 0)
2574 if (counts
[CHECKOUT_ACTION__UPDATE_CONFLICT
] > 0 &&
2575 (error
= checkout_create_conflicts(&data
)) < 0)
2578 if (data
.index
!= git_iterator_get_index(target
) &&
2579 (error
= checkout_extensions_update_index(&data
)) < 0)
2582 assert(data
.completed_steps
== data
.total_steps
);
2584 if (data
.opts
.perfdata_cb
)
2585 data
.opts
.perfdata_cb(&data
.perfdata
, data
.opts
.perfdata_payload
);
2588 if (!error
&& data
.index
!= NULL
&&
2589 (data
.strategy
& CHECKOUT_INDEX_DONT_WRITE_MASK
) == 0)
2590 error
= git_index_write(data
.index
);
2592 git_diff_free(data
.diff
);
2593 git_iterator_free(workdir
);
2594 git_iterator_free(baseline
);
2597 checkout_data_clear(&data
);
2602 int git_checkout_index(
2603 git_repository
*repo
,
2605 const git_checkout_options
*opts
)
2607 int error
, owned
= 0;
2608 git_iterator
*index_i
;
2610 if (!index
&& !repo
) {
2611 giterr_set(GITERR_CHECKOUT
,
2612 "Must provide either repository or index to checkout");
2616 if (index
&& repo
&&
2617 git_index_owner(index
) &&
2618 git_index_owner(index
) != repo
) {
2619 giterr_set(GITERR_CHECKOUT
,
2620 "Index to checkout does not match repository");
2622 } else if(index
&& repo
&& !git_index_owner(index
)) {
2623 GIT_REFCOUNT_OWN(index
, repo
);
2628 repo
= git_index_owner(index
);
2630 if (!index
&& (error
= git_repository_index__weakptr(&index
, repo
)) < 0)
2632 GIT_REFCOUNT_INC(index
);
2634 if (!(error
= git_iterator_for_index(&index_i
, index
, NULL
)))
2635 error
= git_checkout_iterator(index_i
, index
, opts
);
2638 GIT_REFCOUNT_OWN(index
, NULL
);
2640 git_iterator_free(index_i
);
2641 git_index_free(index
);
2646 int git_checkout_tree(
2647 git_repository
*repo
,
2648 const git_object
*treeish
,
2649 const git_checkout_options
*opts
)
2653 git_tree
*tree
= NULL
;
2654 git_iterator
*tree_i
= NULL
;
2655 git_iterator_options iter_opts
= GIT_ITERATOR_OPTIONS_INIT
;
2657 if (!treeish
&& !repo
) {
2658 giterr_set(GITERR_CHECKOUT
,
2659 "Must provide either repository or tree to checkout");
2662 if (treeish
&& repo
&& git_object_owner(treeish
) != repo
) {
2663 giterr_set(GITERR_CHECKOUT
,
2664 "Object to checkout does not match repository");
2669 repo
= git_object_owner(treeish
);
2672 if (git_object_peel((git_object
**)&tree
, treeish
, GIT_OBJ_TREE
) < 0) {
2674 GITERR_CHECKOUT
, "Provided object cannot be peeled to a tree");
2679 if ((error
= checkout_lookup_head_tree(&tree
, repo
)) < 0) {
2680 if (error
!= GIT_EUNBORNBRANCH
)
2683 "HEAD could not be peeled to a tree and no treeish given");
2688 if ((error
= git_repository_index(&index
, repo
)) < 0)
2691 if ((opts
->checkout_strategy
& GIT_CHECKOUT_DISABLE_PATHSPEC_MATCH
)) {
2692 iter_opts
.pathlist
.count
= opts
->paths
.count
;
2693 iter_opts
.pathlist
.strings
= opts
->paths
.strings
;
2696 if (!(error
= git_iterator_for_tree(&tree_i
, tree
, &iter_opts
)))
2697 error
= git_checkout_iterator(tree_i
, index
, opts
);
2699 git_iterator_free(tree_i
);
2700 git_index_free(index
);
2701 git_tree_free(tree
);
2706 int git_checkout_head(
2707 git_repository
*repo
,
2708 const git_checkout_options
*opts
)
2711 return git_checkout_tree(repo
, NULL
, opts
);
2714 int git_checkout_init_options(git_checkout_options
*opts
, unsigned int version
)
2716 GIT_INIT_STRUCTURE_FROM_TEMPLATE(
2717 opts
, version
, git_checkout_options
, GIT_CHECKOUT_OPTIONS_INIT
);