]> git.proxmox.com Git - libgit2.git/blob - src/checkout.c
Merge pull request #3892 from mitesch/shared_buffer
[libgit2.git] / src / checkout.c
1 /*
2 * Copyright (C) the libgit2 contributors. All rights reserved.
3 *
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.
6 */
7
8 #include <assert.h>
9
10 #include "checkout.h"
11
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"
22
23 #include "refs.h"
24 #include "repository.h"
25 #include "index.h"
26 #include "filter.h"
27 #include "blob.h"
28 #include "diff.h"
29 #include "diff_generate.h"
30 #include "pathspec.h"
31 #include "buf_text.h"
32 #include "diff_xdiff.h"
33 #include "path.h"
34 #include "attr.h"
35 #include "pool.h"
36 #include "strmap.h"
37
38 GIT__USE_STRMAP
39
40 /* See docs/checkout-internals.md for more information */
41
42 enum {
43 CHECKOUT_ACTION__NONE = 0,
44 CHECKOUT_ACTION__REMOVE = 1,
45 CHECKOUT_ACTION__UPDATE_BLOB = 2,
46 CHECKOUT_ACTION__UPDATE_SUBMODULE = 4,
47 CHECKOUT_ACTION__CONFLICT = 8,
48 CHECKOUT_ACTION__REMOVE_CONFLICT = 16,
49 CHECKOUT_ACTION__UPDATE_CONFLICT = 32,
50 CHECKOUT_ACTION__MAX = 32,
51 CHECKOUT_ACTION__DEFER_REMOVE = 64,
52 CHECKOUT_ACTION__REMOVE_AND_UPDATE =
53 (CHECKOUT_ACTION__UPDATE_BLOB | CHECKOUT_ACTION__REMOVE),
54 };
55
56 typedef struct {
57 git_repository *repo;
58 git_iterator *target;
59 git_diff *diff;
60 git_checkout_options opts;
61 bool opts_free_baseline;
62 char *pfx;
63 git_index *index;
64 git_pool pool;
65 git_vector removes;
66 git_vector remove_conflicts;
67 git_vector update_conflicts;
68 git_vector *update_reuc;
69 git_vector *update_names;
70 git_buf target_path;
71 size_t target_len;
72 git_buf tmp;
73 unsigned int strategy;
74 int can_symlink;
75 bool reload_submodules;
76 size_t total_steps;
77 size_t completed_steps;
78 git_checkout_perfdata perfdata;
79 git_strmap *mkdir_map;
80 git_attr_session attr_session;
81 } checkout_data;
82
83 typedef struct {
84 const git_index_entry *ancestor;
85 const git_index_entry *ours;
86 const git_index_entry *theirs;
87
88 int name_collision:1,
89 directoryfile:1,
90 one_to_two:1,
91 binary:1,
92 submodule:1;
93 } checkout_conflictdata;
94
95 static int checkout_notify(
96 checkout_data *data,
97 git_checkout_notify_t why,
98 const git_diff_delta *delta,
99 const git_index_entry *wditem)
100 {
101 git_diff_file wdfile;
102 const git_diff_file *baseline = NULL, *target = NULL, *workdir = NULL;
103 const char *path = NULL;
104
105 if (!data->opts.notify_cb ||
106 (why & data->opts.notify_flags) == 0)
107 return 0;
108
109 if (wditem) {
110 memset(&wdfile, 0, sizeof(wdfile));
111
112 git_oid_cpy(&wdfile.id, &wditem->id);
113 wdfile.path = wditem->path;
114 wdfile.size = wditem->file_size;
115 wdfile.flags = GIT_DIFF_FLAG_VALID_ID;
116 wdfile.mode = wditem->mode;
117
118 workdir = &wdfile;
119
120 path = wditem->path;
121 }
122
123 if (delta) {
124 switch (delta->status) {
125 case GIT_DELTA_UNMODIFIED:
126 case GIT_DELTA_MODIFIED:
127 case GIT_DELTA_TYPECHANGE:
128 default:
129 baseline = &delta->old_file;
130 target = &delta->new_file;
131 break;
132 case GIT_DELTA_ADDED:
133 case GIT_DELTA_IGNORED:
134 case GIT_DELTA_UNTRACKED:
135 case GIT_DELTA_UNREADABLE:
136 target = &delta->new_file;
137 break;
138 case GIT_DELTA_DELETED:
139 baseline = &delta->old_file;
140 break;
141 }
142
143 path = delta->old_file.path;
144 }
145
146 {
147 int error = data->opts.notify_cb(
148 why, path, baseline, target, workdir, data->opts.notify_payload);
149
150 return giterr_set_after_callback_function(
151 error, "git_checkout notification");
152 }
153 }
154
155 GIT_INLINE(bool) is_workdir_base_or_new(
156 const git_oid *workdir_id,
157 const git_diff_file *baseitem,
158 const git_diff_file *newitem)
159 {
160 return (git_oid__cmp(&baseitem->id, workdir_id) == 0 ||
161 git_oid__cmp(&newitem->id, workdir_id) == 0);
162 }
163
164 static bool checkout_is_workdir_modified(
165 checkout_data *data,
166 const git_diff_file *baseitem,
167 const git_diff_file *newitem,
168 const git_index_entry *wditem)
169 {
170 git_oid oid;
171 const git_index_entry *ie;
172
173 /* handle "modified" submodule */
174 if (wditem->mode == GIT_FILEMODE_COMMIT) {
175 git_submodule *sm;
176 unsigned int sm_status = 0;
177 const git_oid *sm_oid = NULL;
178 bool rval = false;
179
180 if (git_submodule_lookup(&sm, data->repo, wditem->path) < 0) {
181 giterr_clear();
182 return true;
183 }
184
185 if (git_submodule_status(&sm_status, data->repo, wditem->path, GIT_SUBMODULE_IGNORE_UNSPECIFIED) < 0 ||
186 GIT_SUBMODULE_STATUS_IS_WD_DIRTY(sm_status))
187 rval = true;
188 else if ((sm_oid = git_submodule_wd_id(sm)) == NULL)
189 rval = false;
190 else
191 rval = (git_oid__cmp(&baseitem->id, sm_oid) != 0);
192
193 git_submodule_free(sm);
194 return rval;
195 }
196
197 /* Look at the cache to decide if the workdir is modified. If not,
198 * we can simply compare the oid in the cache to the baseitem instead
199 * of hashing the file. If so, we allow the checkout to proceed if the
200 * oid is identical (ie, the staged item is what we're trying to check
201 * out.)
202 */
203 if ((ie = git_index_get_bypath(data->index, wditem->path, 0)) != NULL) {
204 if (git_index_time_eq(&wditem->mtime, &ie->mtime) &&
205 wditem->file_size == ie->file_size)
206 return !is_workdir_base_or_new(&ie->id, baseitem, newitem);
207 }
208
209 /* depending on where base is coming from, we may or may not know
210 * the actual size of the data, so we can't rely on this shortcut.
211 */
212 if (baseitem->size && wditem->file_size != baseitem->size)
213 return true;
214
215 /* if the workdir item is a directory, it cannot be a modified file */
216 if (S_ISDIR(wditem->mode))
217 return false;
218
219 if (git_diff__oid_for_entry(&oid, data->diff, wditem, wditem->mode, NULL) < 0)
220 return false;
221
222 /* Allow the checkout if the workdir is not modified *or* if the checkout
223 * target's contents are already in the working directory.
224 */
225 return !is_workdir_base_or_new(&oid, baseitem, newitem);
226 }
227
228 #define CHECKOUT_ACTION_IF(FLAG,YES,NO) \
229 ((data->strategy & GIT_CHECKOUT_##FLAG) ? CHECKOUT_ACTION__##YES : CHECKOUT_ACTION__##NO)
230
231 static int checkout_action_common(
232 int *action,
233 checkout_data *data,
234 const git_diff_delta *delta,
235 const git_index_entry *wd)
236 {
237 git_checkout_notify_t notify = GIT_CHECKOUT_NOTIFY_NONE;
238
239 if ((data->strategy & GIT_CHECKOUT_UPDATE_ONLY) != 0)
240 *action = (*action & ~CHECKOUT_ACTION__REMOVE);
241
242 if ((*action & CHECKOUT_ACTION__UPDATE_BLOB) != 0) {
243 if (S_ISGITLINK(delta->new_file.mode))
244 *action = (*action & ~CHECKOUT_ACTION__UPDATE_BLOB) |
245 CHECKOUT_ACTION__UPDATE_SUBMODULE;
246
247 /* to "update" a symlink, we must remove the old one first */
248 if (delta->new_file.mode == GIT_FILEMODE_LINK && wd != NULL)
249 *action |= CHECKOUT_ACTION__REMOVE;
250
251 /* if the file is on disk and doesn't match our mode, force update */
252 if (wd &&
253 GIT_PERMS_IS_EXEC(wd->mode) !=
254 GIT_PERMS_IS_EXEC(delta->new_file.mode))
255 *action |= CHECKOUT_ACTION__REMOVE;
256
257 notify = GIT_CHECKOUT_NOTIFY_UPDATED;
258 }
259
260 if ((*action & CHECKOUT_ACTION__CONFLICT) != 0)
261 notify = GIT_CHECKOUT_NOTIFY_CONFLICT;
262
263 return checkout_notify(data, notify, delta, wd);
264 }
265
266 static int checkout_action_no_wd(
267 int *action,
268 checkout_data *data,
269 const git_diff_delta *delta)
270 {
271 int error = 0;
272
273 *action = CHECKOUT_ACTION__NONE;
274
275 switch (delta->status) {
276 case GIT_DELTA_UNMODIFIED: /* case 12 */
277 error = checkout_notify(data, GIT_CHECKOUT_NOTIFY_DIRTY, delta, NULL);
278 if (error)
279 return error;
280 *action = CHECKOUT_ACTION_IF(RECREATE_MISSING, UPDATE_BLOB, NONE);
281 break;
282 case GIT_DELTA_ADDED: /* case 2 or 28 (and 5 but not really) */
283 *action = CHECKOUT_ACTION_IF(SAFE, UPDATE_BLOB, NONE);
284 break;
285 case GIT_DELTA_MODIFIED: /* case 13 (and 35 but not really) */
286 *action = CHECKOUT_ACTION_IF(RECREATE_MISSING, UPDATE_BLOB, CONFLICT);
287 break;
288 case GIT_DELTA_TYPECHANGE: /* case 21 (B->T) and 28 (T->B)*/
289 if (delta->new_file.mode == GIT_FILEMODE_TREE)
290 *action = CHECKOUT_ACTION_IF(SAFE, UPDATE_BLOB, NONE);
291 break;
292 case GIT_DELTA_DELETED: /* case 8 or 25 */
293 *action = CHECKOUT_ACTION_IF(SAFE, REMOVE, NONE);
294 break;
295 default: /* impossible */
296 break;
297 }
298
299 return checkout_action_common(action, data, delta, NULL);
300 }
301
302 static int checkout_target_fullpath(
303 git_buf **out, checkout_data *data, const char *path)
304 {
305 git_buf_truncate(&data->target_path, data->target_len);
306
307 if (path && git_buf_puts(&data->target_path, path) < 0)
308 return -1;
309
310 *out = &data->target_path;
311
312 return 0;
313 }
314
315 static bool wd_item_is_removable(
316 checkout_data *data, const git_index_entry *wd)
317 {
318 git_buf *full;
319
320 if (wd->mode != GIT_FILEMODE_TREE)
321 return true;
322
323 if (checkout_target_fullpath(&full, data, wd->path) < 0)
324 return false;
325
326 return !full || !git_path_contains(full, DOT_GIT);
327 }
328
329 static int checkout_queue_remove(checkout_data *data, const char *path)
330 {
331 char *copy = git_pool_strdup(&data->pool, path);
332 GITERR_CHECK_ALLOC(copy);
333 return git_vector_insert(&data->removes, copy);
334 }
335
336 /* note that this advances the iterator over the wd item */
337 static int checkout_action_wd_only(
338 checkout_data *data,
339 git_iterator *workdir,
340 const git_index_entry **wditem,
341 git_vector *pathspec)
342 {
343 int error = 0;
344 bool remove = false;
345 git_checkout_notify_t notify = GIT_CHECKOUT_NOTIFY_NONE;
346 const git_index_entry *wd = *wditem;
347
348 if (!git_pathspec__match(
349 pathspec, wd->path,
350 (data->strategy & GIT_CHECKOUT_DISABLE_PATHSPEC_MATCH) != 0,
351 git_iterator_ignore_case(workdir), NULL, NULL))
352 return git_iterator_advance(wditem, workdir);
353
354 /* check if item is tracked in the index but not in the checkout diff */
355 if (data->index != NULL) {
356 size_t pos;
357
358 error = git_index__find_pos(
359 &pos, data->index, wd->path, 0, GIT_INDEX_STAGE_ANY);
360
361 if (wd->mode != GIT_FILEMODE_TREE) {
362 if (!error) { /* found by git_index__find_pos call */
363 notify = GIT_CHECKOUT_NOTIFY_DIRTY;
364 remove = ((data->strategy & GIT_CHECKOUT_FORCE) != 0);
365 } else if (error != GIT_ENOTFOUND)
366 return error;
367 else
368 error = 0; /* git_index__find_pos does not set error msg */
369 } else {
370 /* for tree entries, we have to see if there are any index
371 * entries that are contained inside that tree
372 */
373 const git_index_entry *e = git_index_get_byindex(data->index, pos);
374
375 if (e != NULL && data->diff->pfxcomp(e->path, wd->path) == 0) {
376 notify = GIT_CHECKOUT_NOTIFY_DIRTY;
377 remove = ((data->strategy & GIT_CHECKOUT_FORCE) != 0);
378 }
379 }
380 }
381
382 if (notify != GIT_CHECKOUT_NOTIFY_NONE) {
383 /* if we found something in the index, notify and advance */
384 if ((error = checkout_notify(data, notify, NULL, wd)) != 0)
385 return error;
386
387 if (remove && wd_item_is_removable(data, wd))
388 error = checkout_queue_remove(data, wd->path);
389
390 if (!error)
391 error = git_iterator_advance(wditem, workdir);
392 } else {
393 /* untracked or ignored - can't know which until we advance through */
394 bool over = false, removable = wd_item_is_removable(data, wd);
395 git_iterator_status_t untracked_state;
396
397 /* copy the entry for issuing notification callback later */
398 git_index_entry saved_wd = *wd;
399 git_buf_sets(&data->tmp, wd->path);
400 saved_wd.path = data->tmp.ptr;
401
402 error = git_iterator_advance_over(
403 wditem, &untracked_state, workdir);
404 if (error == GIT_ITEROVER)
405 over = true;
406 else if (error < 0)
407 return error;
408
409 if (untracked_state == GIT_ITERATOR_STATUS_IGNORED) {
410 notify = GIT_CHECKOUT_NOTIFY_IGNORED;
411 remove = ((data->strategy & GIT_CHECKOUT_REMOVE_IGNORED) != 0);
412 } else {
413 notify = GIT_CHECKOUT_NOTIFY_UNTRACKED;
414 remove = ((data->strategy & GIT_CHECKOUT_REMOVE_UNTRACKED) != 0);
415 }
416
417 if ((error = checkout_notify(data, notify, NULL, &saved_wd)) != 0)
418 return error;
419
420 if (remove && removable)
421 error = checkout_queue_remove(data, saved_wd.path);
422
423 if (!error && over) /* restore ITEROVER if needed */
424 error = GIT_ITEROVER;
425 }
426
427 return error;
428 }
429
430 static bool submodule_is_config_only(
431 checkout_data *data,
432 const char *path)
433 {
434 git_submodule *sm = NULL;
435 unsigned int sm_loc = 0;
436 bool rval = false;
437
438 if (git_submodule_lookup(&sm, data->repo, path) < 0)
439 return true;
440
441 if (git_submodule_location(&sm_loc, sm) < 0 ||
442 sm_loc == GIT_SUBMODULE_STATUS_IN_CONFIG)
443 rval = true;
444
445 git_submodule_free(sm);
446
447 return rval;
448 }
449
450 static bool checkout_is_empty_dir(checkout_data *data, const char *path)
451 {
452 git_buf *fullpath;
453
454 if (checkout_target_fullpath(&fullpath, data, path) < 0)
455 return false;
456
457 return git_path_is_empty_dir(fullpath->ptr);
458 }
459
460 static int checkout_action_with_wd(
461 int *action,
462 checkout_data *data,
463 const git_diff_delta *delta,
464 git_iterator *workdir,
465 const git_index_entry *wd)
466 {
467 *action = CHECKOUT_ACTION__NONE;
468
469 switch (delta->status) {
470 case GIT_DELTA_UNMODIFIED: /* case 14/15 or 33 */
471 if (checkout_is_workdir_modified(data, &delta->old_file, &delta->new_file, wd)) {
472 GITERR_CHECK_ERROR(
473 checkout_notify(data, GIT_CHECKOUT_NOTIFY_DIRTY, delta, wd) );
474 *action = CHECKOUT_ACTION_IF(FORCE, UPDATE_BLOB, NONE);
475 }
476 break;
477 case GIT_DELTA_ADDED: /* case 3, 4 or 6 */
478 if (git_iterator_current_is_ignored(workdir))
479 *action = CHECKOUT_ACTION_IF(DONT_OVERWRITE_IGNORED, CONFLICT, UPDATE_BLOB);
480 else
481 *action = CHECKOUT_ACTION_IF(FORCE, UPDATE_BLOB, CONFLICT);
482 break;
483 case GIT_DELTA_DELETED: /* case 9 or 10 (or 26 but not really) */
484 if (checkout_is_workdir_modified(data, &delta->old_file, &delta->new_file, wd))
485 *action = CHECKOUT_ACTION_IF(FORCE, REMOVE, CONFLICT);
486 else
487 *action = CHECKOUT_ACTION_IF(SAFE, REMOVE, NONE);
488 break;
489 case GIT_DELTA_MODIFIED: /* case 16, 17, 18 (or 36 but not really) */
490 if (wd->mode != GIT_FILEMODE_COMMIT &&
491 checkout_is_workdir_modified(data, &delta->old_file, &delta->new_file, wd))
492 *action = CHECKOUT_ACTION_IF(FORCE, UPDATE_BLOB, CONFLICT);
493 else
494 *action = CHECKOUT_ACTION_IF(SAFE, UPDATE_BLOB, NONE);
495 break;
496 case GIT_DELTA_TYPECHANGE: /* case 22, 23, 29, 30 */
497 if (delta->old_file.mode == GIT_FILEMODE_TREE) {
498 if (wd->mode == GIT_FILEMODE_TREE)
499 /* either deleting items in old tree will delete the wd dir,
500 * or we'll get a conflict when we attempt blob update...
501 */
502 *action = CHECKOUT_ACTION_IF(SAFE, UPDATE_BLOB, NONE);
503 else if (wd->mode == GIT_FILEMODE_COMMIT) {
504 /* workdir is possibly a "phantom" submodule - treat as a
505 * tree if the only submodule info came from the config
506 */
507 if (submodule_is_config_only(data, wd->path))
508 *action = CHECKOUT_ACTION_IF(SAFE, UPDATE_BLOB, NONE);
509 else
510 *action = CHECKOUT_ACTION_IF(FORCE, REMOVE_AND_UPDATE, CONFLICT);
511 } else
512 *action = CHECKOUT_ACTION_IF(FORCE, REMOVE, CONFLICT);
513 }
514 else if (checkout_is_workdir_modified(data, &delta->old_file, &delta->new_file, wd))
515 *action = CHECKOUT_ACTION_IF(FORCE, REMOVE_AND_UPDATE, CONFLICT);
516 else
517 *action = CHECKOUT_ACTION_IF(SAFE, REMOVE_AND_UPDATE, NONE);
518
519 /* don't update if the typechange is to a tree */
520 if (delta->new_file.mode == GIT_FILEMODE_TREE)
521 *action = (*action & ~CHECKOUT_ACTION__UPDATE_BLOB);
522 break;
523 default: /* impossible */
524 break;
525 }
526
527 return checkout_action_common(action, data, delta, wd);
528 }
529
530 static int checkout_action_with_wd_blocker(
531 int *action,
532 checkout_data *data,
533 const git_diff_delta *delta,
534 const git_index_entry *wd)
535 {
536 *action = CHECKOUT_ACTION__NONE;
537
538 switch (delta->status) {
539 case GIT_DELTA_UNMODIFIED:
540 /* should show delta as dirty / deleted */
541 GITERR_CHECK_ERROR(
542 checkout_notify(data, GIT_CHECKOUT_NOTIFY_DIRTY, delta, wd) );
543 *action = CHECKOUT_ACTION_IF(FORCE, REMOVE_AND_UPDATE, NONE);
544 break;
545 case GIT_DELTA_ADDED:
546 case GIT_DELTA_MODIFIED:
547 *action = CHECKOUT_ACTION_IF(FORCE, REMOVE_AND_UPDATE, CONFLICT);
548 break;
549 case GIT_DELTA_DELETED:
550 *action = CHECKOUT_ACTION_IF(FORCE, REMOVE, CONFLICT);
551 break;
552 case GIT_DELTA_TYPECHANGE:
553 /* not 100% certain about this... */
554 *action = CHECKOUT_ACTION_IF(FORCE, REMOVE_AND_UPDATE, CONFLICT);
555 break;
556 default: /* impossible */
557 break;
558 }
559
560 return checkout_action_common(action, data, delta, wd);
561 }
562
563 static int checkout_action_with_wd_dir(
564 int *action,
565 checkout_data *data,
566 const git_diff_delta *delta,
567 git_iterator *workdir,
568 const git_index_entry *wd)
569 {
570 *action = CHECKOUT_ACTION__NONE;
571
572 switch (delta->status) {
573 case GIT_DELTA_UNMODIFIED: /* case 19 or 24 (or 34 but not really) */
574 GITERR_CHECK_ERROR(
575 checkout_notify(data, GIT_CHECKOUT_NOTIFY_DIRTY, delta, NULL));
576 GITERR_CHECK_ERROR(
577 checkout_notify(data, GIT_CHECKOUT_NOTIFY_UNTRACKED, NULL, wd));
578 *action = CHECKOUT_ACTION_IF(FORCE, REMOVE_AND_UPDATE, NONE);
579 break;
580 case GIT_DELTA_ADDED:/* case 4 (and 7 for dir) */
581 case GIT_DELTA_MODIFIED: /* case 20 (or 37 but not really) */
582 if (delta->old_file.mode == GIT_FILEMODE_COMMIT)
583 /* expected submodule (and maybe found one) */;
584 else if (delta->new_file.mode != GIT_FILEMODE_TREE)
585 *action = git_iterator_current_is_ignored(workdir) ?
586 CHECKOUT_ACTION_IF(DONT_OVERWRITE_IGNORED, CONFLICT, REMOVE_AND_UPDATE) :
587 CHECKOUT_ACTION_IF(FORCE, REMOVE_AND_UPDATE, CONFLICT);
588 break;
589 case GIT_DELTA_DELETED: /* case 11 (and 27 for dir) */
590 if (delta->old_file.mode != GIT_FILEMODE_TREE)
591 GITERR_CHECK_ERROR(
592 checkout_notify(data, GIT_CHECKOUT_NOTIFY_UNTRACKED, NULL, wd));
593 break;
594 case GIT_DELTA_TYPECHANGE: /* case 24 or 31 */
595 if (delta->old_file.mode == GIT_FILEMODE_TREE) {
596 /* For typechange from dir, remove dir and add blob, but it is
597 * not safe to remove dir if it contains modified files.
598 * However, safely removing child files will remove the parent
599 * directory if is it left empty, so we can defer removing the
600 * dir and it will succeed if no children are left.
601 */
602 *action = CHECKOUT_ACTION_IF(SAFE, UPDATE_BLOB, NONE);
603 }
604 else if (delta->new_file.mode != GIT_FILEMODE_TREE)
605 /* For typechange to dir, dir is already created so no action */
606 *action = CHECKOUT_ACTION_IF(FORCE, REMOVE_AND_UPDATE, CONFLICT);
607 break;
608 default: /* impossible */
609 break;
610 }
611
612 return checkout_action_common(action, data, delta, wd);
613 }
614
615 static int checkout_action_with_wd_dir_empty(
616 int *action,
617 checkout_data *data,
618 const git_diff_delta *delta)
619 {
620 int error = checkout_action_no_wd(action, data, delta);
621
622 /* We can always safely remove an empty directory. */
623 if (error == 0 && *action != CHECKOUT_ACTION__NONE)
624 *action |= CHECKOUT_ACTION__REMOVE;
625
626 return error;
627 }
628
629 static int checkout_action(
630 int *action,
631 checkout_data *data,
632 git_diff_delta *delta,
633 git_iterator *workdir,
634 const git_index_entry **wditem,
635 git_vector *pathspec)
636 {
637 int cmp = -1, error;
638 int (*strcomp)(const char *, const char *) = data->diff->strcomp;
639 int (*pfxcomp)(const char *str, const char *pfx) = data->diff->pfxcomp;
640 int (*advance)(const git_index_entry **, git_iterator *) = NULL;
641
642 /* move workdir iterator to follow along with deltas */
643
644 while (1) {
645 const git_index_entry *wd = *wditem;
646
647 if (!wd)
648 return checkout_action_no_wd(action, data, delta);
649
650 cmp = strcomp(wd->path, delta->old_file.path);
651
652 /* 1. wd before delta ("a/a" before "a/b")
653 * 2. wd prefixes delta & should expand ("a/" before "a/b")
654 * 3. wd prefixes delta & cannot expand ("a/b" before "a/b/c")
655 * 4. wd equals delta ("a/b" and "a/b")
656 * 5. wd after delta & delta prefixes wd ("a/b/c" after "a/b/" or "a/b")
657 * 6. wd after delta ("a/c" after "a/b")
658 */
659
660 if (cmp < 0) {
661 cmp = pfxcomp(delta->old_file.path, wd->path);
662
663 if (cmp == 0) {
664 if (wd->mode == GIT_FILEMODE_TREE) {
665 /* case 2 - entry prefixed by workdir tree */
666 error = git_iterator_advance_into(wditem, workdir);
667 if (error < 0 && error != GIT_ITEROVER)
668 goto done;
669 continue;
670 }
671
672 /* case 3 maybe - wd contains non-dir where dir expected */
673 if (delta->old_file.path[strlen(wd->path)] == '/') {
674 error = checkout_action_with_wd_blocker(
675 action, data, delta, wd);
676 advance = git_iterator_advance;
677 goto done;
678 }
679 }
680
681 /* case 1 - handle wd item (if it matches pathspec) */
682 error = checkout_action_wd_only(data, workdir, wditem, pathspec);
683 if (error && error != GIT_ITEROVER)
684 goto done;
685 continue;
686 }
687
688 if (cmp == 0) {
689 /* case 4 */
690 error = checkout_action_with_wd(action, data, delta, workdir, wd);
691 advance = git_iterator_advance;
692 goto done;
693 }
694
695 cmp = pfxcomp(wd->path, delta->old_file.path);
696
697 if (cmp == 0) { /* case 5 */
698 if (wd->path[strlen(delta->old_file.path)] != '/')
699 return checkout_action_no_wd(action, data, delta);
700
701 if (delta->status == GIT_DELTA_TYPECHANGE) {
702 if (delta->old_file.mode == GIT_FILEMODE_TREE) {
703 error = checkout_action_with_wd(action, data, delta, workdir, wd);
704 advance = git_iterator_advance_into;
705 goto done;
706 }
707
708 if (delta->new_file.mode == GIT_FILEMODE_TREE ||
709 delta->new_file.mode == GIT_FILEMODE_COMMIT ||
710 delta->old_file.mode == GIT_FILEMODE_COMMIT)
711 {
712 error = checkout_action_with_wd(action, data, delta, workdir, wd);
713 advance = git_iterator_advance;
714 goto done;
715 }
716 }
717
718 return checkout_is_empty_dir(data, wd->path) ?
719 checkout_action_with_wd_dir_empty(action, data, delta) :
720 checkout_action_with_wd_dir(action, data, delta, workdir, wd);
721 }
722
723 /* case 6 - wd is after delta */
724 return checkout_action_no_wd(action, data, delta);
725 }
726
727 done:
728 if (!error && advance != NULL &&
729 (error = advance(wditem, workdir)) < 0) {
730 *wditem = NULL;
731 if (error == GIT_ITEROVER)
732 error = 0;
733 }
734
735 return error;
736 }
737
738 static int checkout_remaining_wd_items(
739 checkout_data *data,
740 git_iterator *workdir,
741 const git_index_entry *wd,
742 git_vector *spec)
743 {
744 int error = 0;
745
746 while (wd && !error)
747 error = checkout_action_wd_only(data, workdir, &wd, spec);
748
749 if (error == GIT_ITEROVER)
750 error = 0;
751
752 return error;
753 }
754
755 GIT_INLINE(int) checkout_idxentry_cmp(
756 const git_index_entry *a,
757 const git_index_entry *b)
758 {
759 if (!a && !b)
760 return 0;
761 else if (!a && b)
762 return -1;
763 else if(a && !b)
764 return 1;
765 else
766 return strcmp(a->path, b->path);
767 }
768
769 static int checkout_conflictdata_cmp(const void *a, const void *b)
770 {
771 const checkout_conflictdata *ca = a;
772 const checkout_conflictdata *cb = b;
773 int diff;
774
775 if ((diff = checkout_idxentry_cmp(ca->ancestor, cb->ancestor)) == 0 &&
776 (diff = checkout_idxentry_cmp(ca->ours, cb->theirs)) == 0)
777 diff = checkout_idxentry_cmp(ca->theirs, cb->theirs);
778
779 return diff;
780 }
781
782 int checkout_conflictdata_empty(
783 const git_vector *conflicts, size_t idx, void *payload)
784 {
785 checkout_conflictdata *conflict;
786
787 GIT_UNUSED(payload);
788
789 if ((conflict = git_vector_get(conflicts, idx)) == NULL)
790 return -1;
791
792 if (conflict->ancestor || conflict->ours || conflict->theirs)
793 return 0;
794
795 git__free(conflict);
796 return 1;
797 }
798
799 GIT_INLINE(bool) conflict_pathspec_match(
800 checkout_data *data,
801 git_iterator *workdir,
802 git_vector *pathspec,
803 const git_index_entry *ancestor,
804 const git_index_entry *ours,
805 const git_index_entry *theirs)
806 {
807 /* if the pathspec matches ours *or* theirs, proceed */
808 if (ours && git_pathspec__match(pathspec, ours->path,
809 (data->strategy & GIT_CHECKOUT_DISABLE_PATHSPEC_MATCH) != 0,
810 git_iterator_ignore_case(workdir), NULL, NULL))
811 return true;
812
813 if (theirs && git_pathspec__match(pathspec, theirs->path,
814 (data->strategy & GIT_CHECKOUT_DISABLE_PATHSPEC_MATCH) != 0,
815 git_iterator_ignore_case(workdir), NULL, NULL))
816 return true;
817
818 if (ancestor && git_pathspec__match(pathspec, ancestor->path,
819 (data->strategy & GIT_CHECKOUT_DISABLE_PATHSPEC_MATCH) != 0,
820 git_iterator_ignore_case(workdir), NULL, NULL))
821 return true;
822
823 return false;
824 }
825
826 GIT_INLINE(int) checkout_conflict_detect_submodule(checkout_conflictdata *conflict)
827 {
828 conflict->submodule = ((conflict->ancestor && S_ISGITLINK(conflict->ancestor->mode)) ||
829 (conflict->ours && S_ISGITLINK(conflict->ours->mode)) ||
830 (conflict->theirs && S_ISGITLINK(conflict->theirs->mode)));
831 return 0;
832 }
833
834 GIT_INLINE(int) checkout_conflict_detect_binary(git_repository *repo, checkout_conflictdata *conflict)
835 {
836 git_blob *ancestor_blob = NULL, *our_blob = NULL, *their_blob = NULL;
837 int error = 0;
838
839 if (conflict->submodule)
840 return 0;
841
842 if (conflict->ancestor) {
843 if ((error = git_blob_lookup(&ancestor_blob, repo, &conflict->ancestor->id)) < 0)
844 goto done;
845
846 conflict->binary = git_blob_is_binary(ancestor_blob);
847 }
848
849 if (!conflict->binary && conflict->ours) {
850 if ((error = git_blob_lookup(&our_blob, repo, &conflict->ours->id)) < 0)
851 goto done;
852
853 conflict->binary = git_blob_is_binary(our_blob);
854 }
855
856 if (!conflict->binary && conflict->theirs) {
857 if ((error = git_blob_lookup(&their_blob, repo, &conflict->theirs->id)) < 0)
858 goto done;
859
860 conflict->binary = git_blob_is_binary(their_blob);
861 }
862
863 done:
864 git_blob_free(ancestor_blob);
865 git_blob_free(our_blob);
866 git_blob_free(their_blob);
867
868 return error;
869 }
870
871 static int checkout_conflict_append_update(
872 const git_index_entry *ancestor,
873 const git_index_entry *ours,
874 const git_index_entry *theirs,
875 void *payload)
876 {
877 checkout_data *data = payload;
878 checkout_conflictdata *conflict;
879 int error;
880
881 conflict = git__calloc(1, sizeof(checkout_conflictdata));
882 GITERR_CHECK_ALLOC(conflict);
883
884 conflict->ancestor = ancestor;
885 conflict->ours = ours;
886 conflict->theirs = theirs;
887
888 if ((error = checkout_conflict_detect_submodule(conflict)) < 0 ||
889 (error = checkout_conflict_detect_binary(data->repo, conflict)) < 0)
890 {
891 git__free(conflict);
892 return error;
893 }
894
895 if (git_vector_insert(&data->update_conflicts, conflict))
896 return -1;
897
898 return 0;
899 }
900
901 static int checkout_conflicts_foreach(
902 checkout_data *data,
903 git_index *index,
904 git_iterator *workdir,
905 git_vector *pathspec,
906 int (*cb)(const git_index_entry *, const git_index_entry *, const git_index_entry *, void *),
907 void *payload)
908 {
909 git_index_conflict_iterator *iterator = NULL;
910 const git_index_entry *ancestor, *ours, *theirs;
911 int error = 0;
912
913 if ((error = git_index_conflict_iterator_new(&iterator, index)) < 0)
914 goto done;
915
916 /* Collect the conflicts */
917 while ((error = git_index_conflict_next(&ancestor, &ours, &theirs, iterator)) == 0) {
918 if (!conflict_pathspec_match(data, workdir, pathspec, ancestor, ours, theirs))
919 continue;
920
921 if ((error = cb(ancestor, ours, theirs, payload)) < 0)
922 goto done;
923 }
924
925 if (error == GIT_ITEROVER)
926 error = 0;
927
928 done:
929 git_index_conflict_iterator_free(iterator);
930
931 return error;
932 }
933
934 static int checkout_conflicts_load(checkout_data *data, git_iterator *workdir, git_vector *pathspec)
935 {
936 git_index *index;
937
938 /* Only write conficts from sources that have them: indexes. */
939 if ((index = git_iterator_index(data->target)) == NULL)
940 return 0;
941
942 data->update_conflicts._cmp = checkout_conflictdata_cmp;
943
944 if (checkout_conflicts_foreach(data, index, workdir, pathspec, checkout_conflict_append_update, data) < 0)
945 return -1;
946
947 /* Collect the REUC and NAME entries */
948 data->update_reuc = &index->reuc;
949 data->update_names = &index->names;
950
951 return 0;
952 }
953
954 GIT_INLINE(int) checkout_conflicts_cmp_entry(
955 const char *path,
956 const git_index_entry *entry)
957 {
958 return strcmp((const char *)path, entry->path);
959 }
960
961 static int checkout_conflicts_cmp_ancestor(const void *p, const void *c)
962 {
963 const char *path = p;
964 const checkout_conflictdata *conflict = c;
965
966 if (!conflict->ancestor)
967 return 1;
968
969 return checkout_conflicts_cmp_entry(path, conflict->ancestor);
970 }
971
972 static checkout_conflictdata *checkout_conflicts_search_ancestor(
973 checkout_data *data,
974 const char *path)
975 {
976 size_t pos;
977
978 if (git_vector_bsearch2(&pos, &data->update_conflicts, checkout_conflicts_cmp_ancestor, path) < 0)
979 return NULL;
980
981 return git_vector_get(&data->update_conflicts, pos);
982 }
983
984 static checkout_conflictdata *checkout_conflicts_search_branch(
985 checkout_data *data,
986 const char *path)
987 {
988 checkout_conflictdata *conflict;
989 size_t i;
990
991 git_vector_foreach(&data->update_conflicts, i, conflict) {
992 int cmp = -1;
993
994 if (conflict->ancestor)
995 break;
996
997 if (conflict->ours)
998 cmp = checkout_conflicts_cmp_entry(path, conflict->ours);
999 else if (conflict->theirs)
1000 cmp = checkout_conflicts_cmp_entry(path, conflict->theirs);
1001
1002 if (cmp == 0)
1003 return conflict;
1004 }
1005
1006 return NULL;
1007 }
1008
1009 static int checkout_conflicts_load_byname_entry(
1010 checkout_conflictdata **ancestor_out,
1011 checkout_conflictdata **ours_out,
1012 checkout_conflictdata **theirs_out,
1013 checkout_data *data,
1014 const git_index_name_entry *name_entry)
1015 {
1016 checkout_conflictdata *ancestor, *ours = NULL, *theirs = NULL;
1017 int error = 0;
1018
1019 *ancestor_out = NULL;
1020 *ours_out = NULL;
1021 *theirs_out = NULL;
1022
1023 if (!name_entry->ancestor) {
1024 giterr_set(GITERR_INDEX, "a NAME entry exists without an ancestor");
1025 error = -1;
1026 goto done;
1027 }
1028
1029 if (!name_entry->ours && !name_entry->theirs) {
1030 giterr_set(GITERR_INDEX, "a NAME entry exists without an ours or theirs");
1031 error = -1;
1032 goto done;
1033 }
1034
1035 if ((ancestor = checkout_conflicts_search_ancestor(data,
1036 name_entry->ancestor)) == NULL) {
1037 giterr_set(GITERR_INDEX,
1038 "a NAME entry referenced ancestor entry '%s' which does not exist in the main index",
1039 name_entry->ancestor);
1040 error = -1;
1041 goto done;
1042 }
1043
1044 if (name_entry->ours) {
1045 if (strcmp(name_entry->ancestor, name_entry->ours) == 0)
1046 ours = ancestor;
1047 else if ((ours = checkout_conflicts_search_branch(data, name_entry->ours)) == NULL ||
1048 ours->ours == NULL) {
1049 giterr_set(GITERR_INDEX,
1050 "a NAME entry referenced our entry '%s' which does not exist in the main index",
1051 name_entry->ours);
1052 error = -1;
1053 goto done;
1054 }
1055 }
1056
1057 if (name_entry->theirs) {
1058 if (strcmp(name_entry->ancestor, name_entry->theirs) == 0)
1059 theirs = ancestor;
1060 else if (name_entry->ours && strcmp(name_entry->ours, name_entry->theirs) == 0)
1061 theirs = ours;
1062 else if ((theirs = checkout_conflicts_search_branch(data, name_entry->theirs)) == NULL ||
1063 theirs->theirs == NULL) {
1064 giterr_set(GITERR_INDEX,
1065 "a NAME entry referenced their entry '%s' which does not exist in the main index",
1066 name_entry->theirs);
1067 error = -1;
1068 goto done;
1069 }
1070 }
1071
1072 *ancestor_out = ancestor;
1073 *ours_out = ours;
1074 *theirs_out = theirs;
1075
1076 done:
1077 return error;
1078 }
1079
1080 static int checkout_conflicts_coalesce_renames(
1081 checkout_data *data)
1082 {
1083 git_index *index;
1084 const git_index_name_entry *name_entry;
1085 checkout_conflictdata *ancestor_conflict, *our_conflict, *their_conflict;
1086 size_t i, names;
1087 int error = 0;
1088
1089 if ((index = git_iterator_index(data->target)) == NULL)
1090 return 0;
1091
1092 /* Juggle entries based on renames */
1093 names = git_index_name_entrycount(index);
1094
1095 for (i = 0; i < names; i++) {
1096 name_entry = git_index_name_get_byindex(index, i);
1097
1098 if ((error = checkout_conflicts_load_byname_entry(
1099 &ancestor_conflict, &our_conflict, &their_conflict,
1100 data, name_entry)) < 0)
1101 goto done;
1102
1103 if (our_conflict && our_conflict != ancestor_conflict) {
1104 ancestor_conflict->ours = our_conflict->ours;
1105 our_conflict->ours = NULL;
1106
1107 if (our_conflict->theirs)
1108 our_conflict->name_collision = 1;
1109
1110 if (our_conflict->name_collision)
1111 ancestor_conflict->name_collision = 1;
1112 }
1113
1114 if (their_conflict && their_conflict != ancestor_conflict) {
1115 ancestor_conflict->theirs = their_conflict->theirs;
1116 their_conflict->theirs = NULL;
1117
1118 if (their_conflict->ours)
1119 their_conflict->name_collision = 1;
1120
1121 if (their_conflict->name_collision)
1122 ancestor_conflict->name_collision = 1;
1123 }
1124
1125 if (our_conflict && our_conflict != ancestor_conflict &&
1126 their_conflict && their_conflict != ancestor_conflict)
1127 ancestor_conflict->one_to_two = 1;
1128 }
1129
1130 git_vector_remove_matching(
1131 &data->update_conflicts, checkout_conflictdata_empty, NULL);
1132
1133 done:
1134 return error;
1135 }
1136
1137 static int checkout_conflicts_mark_directoryfile(
1138 checkout_data *data)
1139 {
1140 git_index *index;
1141 checkout_conflictdata *conflict;
1142 const git_index_entry *entry;
1143 size_t i, j, len;
1144 const char *path;
1145 int prefixed, error = 0;
1146
1147 if ((index = git_iterator_index(data->target)) == NULL)
1148 return 0;
1149
1150 len = git_index_entrycount(index);
1151
1152 /* Find d/f conflicts */
1153 git_vector_foreach(&data->update_conflicts, i, conflict) {
1154 if ((conflict->ours && conflict->theirs) ||
1155 (!conflict->ours && !conflict->theirs))
1156 continue;
1157
1158 path = conflict->ours ?
1159 conflict->ours->path : conflict->theirs->path;
1160
1161 if ((error = git_index_find(&j, index, path)) < 0) {
1162 if (error == GIT_ENOTFOUND)
1163 giterr_set(GITERR_INDEX,
1164 "index inconsistency, could not find entry for expected conflict '%s'", path);
1165
1166 goto done;
1167 }
1168
1169 for (; j < len; j++) {
1170 if ((entry = git_index_get_byindex(index, j)) == NULL) {
1171 giterr_set(GITERR_INDEX,
1172 "index inconsistency, truncated index while loading expected conflict '%s'", path);
1173 error = -1;
1174 goto done;
1175 }
1176
1177 prefixed = git_path_equal_or_prefixed(path, entry->path, NULL);
1178
1179 if (prefixed == GIT_PATH_EQUAL)
1180 continue;
1181
1182 if (prefixed == GIT_PATH_PREFIX)
1183 conflict->directoryfile = 1;
1184
1185 break;
1186 }
1187 }
1188
1189 done:
1190 return error;
1191 }
1192
1193 static int checkout_get_update_conflicts(
1194 checkout_data *data,
1195 git_iterator *workdir,
1196 git_vector *pathspec)
1197 {
1198 int error = 0;
1199
1200 if (data->strategy & GIT_CHECKOUT_SKIP_UNMERGED)
1201 return 0;
1202
1203 if ((error = checkout_conflicts_load(data, workdir, pathspec)) < 0 ||
1204 (error = checkout_conflicts_coalesce_renames(data)) < 0 ||
1205 (error = checkout_conflicts_mark_directoryfile(data)) < 0)
1206 goto done;
1207
1208 done:
1209 return error;
1210 }
1211
1212 static int checkout_conflict_append_remove(
1213 const git_index_entry *ancestor,
1214 const git_index_entry *ours,
1215 const git_index_entry *theirs,
1216 void *payload)
1217 {
1218 checkout_data *data = payload;
1219 const char *name;
1220
1221 assert(ancestor || ours || theirs);
1222
1223 if (ancestor)
1224 name = git__strdup(ancestor->path);
1225 else if (ours)
1226 name = git__strdup(ours->path);
1227 else if (theirs)
1228 name = git__strdup(theirs->path);
1229 else
1230 abort();
1231
1232 GITERR_CHECK_ALLOC(name);
1233
1234 return git_vector_insert(&data->remove_conflicts, (char *)name);
1235 }
1236
1237 static int checkout_get_remove_conflicts(
1238 checkout_data *data,
1239 git_iterator *workdir,
1240 git_vector *pathspec)
1241 {
1242 if ((data->strategy & GIT_CHECKOUT_DONT_UPDATE_INDEX) != 0)
1243 return 0;
1244
1245 return checkout_conflicts_foreach(data, data->index, workdir, pathspec, checkout_conflict_append_remove, data);
1246 }
1247
1248 static int checkout_verify_paths(
1249 git_repository *repo,
1250 int action,
1251 git_diff_delta *delta)
1252 {
1253 unsigned int flags = GIT_PATH_REJECT_WORKDIR_DEFAULTS;
1254
1255 if (action & CHECKOUT_ACTION__REMOVE) {
1256 if (!git_path_isvalid(repo, delta->old_file.path, flags)) {
1257 giterr_set(GITERR_CHECKOUT, "cannot remove invalid path '%s'", delta->old_file.path);
1258 return -1;
1259 }
1260 }
1261
1262 if (action & ~CHECKOUT_ACTION__REMOVE) {
1263 if (!git_path_isvalid(repo, delta->new_file.path, flags)) {
1264 giterr_set(GITERR_CHECKOUT, "cannot checkout to invalid path '%s'", delta->new_file.path);
1265 return -1;
1266 }
1267 }
1268
1269 return 0;
1270 }
1271
1272 static int checkout_get_actions(
1273 uint32_t **actions_ptr,
1274 size_t **counts_ptr,
1275 checkout_data *data,
1276 git_iterator *workdir)
1277 {
1278 int error = 0, act;
1279 const git_index_entry *wditem;
1280 git_vector pathspec = GIT_VECTOR_INIT, *deltas;
1281 git_pool pathpool;
1282 git_diff_delta *delta;
1283 size_t i, *counts = NULL;
1284 uint32_t *actions = NULL;
1285
1286 git_pool_init(&pathpool, 1);
1287
1288 if (data->opts.paths.count > 0 &&
1289 git_pathspec__vinit(&pathspec, &data->opts.paths, &pathpool) < 0)
1290 return -1;
1291
1292 if ((error = git_iterator_current(&wditem, workdir)) < 0 &&
1293 error != GIT_ITEROVER)
1294 goto fail;
1295
1296 deltas = &data->diff->deltas;
1297
1298 *counts_ptr = counts = git__calloc(CHECKOUT_ACTION__MAX+1, sizeof(size_t));
1299 *actions_ptr = actions = git__calloc(
1300 deltas->length ? deltas->length : 1, sizeof(uint32_t));
1301 if (!counts || !actions) {
1302 error = -1;
1303 goto fail;
1304 }
1305
1306 git_vector_foreach(deltas, i, delta) {
1307 if ((error = checkout_action(&act, data, delta, workdir, &wditem, &pathspec)) == 0)
1308 error = checkout_verify_paths(data->repo, act, delta);
1309
1310 if (error != 0)
1311 goto fail;
1312
1313 actions[i] = act;
1314
1315 if (act & CHECKOUT_ACTION__REMOVE)
1316 counts[CHECKOUT_ACTION__REMOVE]++;
1317 if (act & CHECKOUT_ACTION__UPDATE_BLOB)
1318 counts[CHECKOUT_ACTION__UPDATE_BLOB]++;
1319 if (act & CHECKOUT_ACTION__UPDATE_SUBMODULE)
1320 counts[CHECKOUT_ACTION__UPDATE_SUBMODULE]++;
1321 if (act & CHECKOUT_ACTION__CONFLICT)
1322 counts[CHECKOUT_ACTION__CONFLICT]++;
1323 }
1324
1325 error = checkout_remaining_wd_items(data, workdir, wditem, &pathspec);
1326 if (error)
1327 goto fail;
1328
1329 counts[CHECKOUT_ACTION__REMOVE] += data->removes.length;
1330
1331 if (counts[CHECKOUT_ACTION__CONFLICT] > 0 &&
1332 (data->strategy & GIT_CHECKOUT_ALLOW_CONFLICTS) == 0)
1333 {
1334 giterr_set(GITERR_CHECKOUT, "%"PRIuZ" %s checkout",
1335 counts[CHECKOUT_ACTION__CONFLICT],
1336 counts[CHECKOUT_ACTION__CONFLICT] == 1 ?
1337 "conflict prevents" : "conflicts prevent");
1338 error = GIT_ECONFLICT;
1339 goto fail;
1340 }
1341
1342
1343 if ((error = checkout_get_remove_conflicts(data, workdir, &pathspec)) < 0 ||
1344 (error = checkout_get_update_conflicts(data, workdir, &pathspec)) < 0)
1345 goto fail;
1346
1347 counts[CHECKOUT_ACTION__REMOVE_CONFLICT] = git_vector_length(&data->remove_conflicts);
1348 counts[CHECKOUT_ACTION__UPDATE_CONFLICT] = git_vector_length(&data->update_conflicts);
1349
1350 git_pathspec__vfree(&pathspec);
1351 git_pool_clear(&pathpool);
1352
1353 return 0;
1354
1355 fail:
1356 *counts_ptr = NULL;
1357 git__free(counts);
1358 *actions_ptr = NULL;
1359 git__free(actions);
1360
1361 git_pathspec__vfree(&pathspec);
1362 git_pool_clear(&pathpool);
1363
1364 return error;
1365 }
1366
1367 static bool should_remove_existing(checkout_data *data)
1368 {
1369 int ignorecase;
1370
1371 if (git_repository__cvar(&ignorecase, data->repo, GIT_CVAR_IGNORECASE) < 0) {
1372 ignorecase = 0;
1373 }
1374
1375 return (ignorecase &&
1376 (data->strategy & GIT_CHECKOUT_DONT_REMOVE_EXISTING) == 0);
1377 }
1378
1379 #define MKDIR_NORMAL \
1380 GIT_MKDIR_PATH | GIT_MKDIR_VERIFY_DIR
1381 #define MKDIR_REMOVE_EXISTING \
1382 MKDIR_NORMAL | GIT_MKDIR_REMOVE_FILES | GIT_MKDIR_REMOVE_SYMLINKS
1383
1384 static int checkout_mkdir(
1385 checkout_data *data,
1386 const char *path,
1387 const char *base,
1388 mode_t mode,
1389 unsigned int flags)
1390 {
1391 struct git_futils_mkdir_options mkdir_opts = {0};
1392 int error;
1393
1394 mkdir_opts.dir_map = data->mkdir_map;
1395 mkdir_opts.pool = &data->pool;
1396
1397 error = git_futils_mkdir_relative(
1398 path, base, mode, flags, &mkdir_opts);
1399
1400 data->perfdata.mkdir_calls += mkdir_opts.perfdata.mkdir_calls;
1401 data->perfdata.stat_calls += mkdir_opts.perfdata.stat_calls;
1402 data->perfdata.chmod_calls += mkdir_opts.perfdata.chmod_calls;
1403
1404 return error;
1405 }
1406
1407 static int mkpath2file(
1408 checkout_data *data, const char *path, unsigned int mode)
1409 {
1410 struct stat st;
1411 bool remove_existing = should_remove_existing(data);
1412 unsigned int flags =
1413 (remove_existing ? MKDIR_REMOVE_EXISTING : MKDIR_NORMAL) |
1414 GIT_MKDIR_SKIP_LAST;
1415 int error;
1416
1417 if ((error = checkout_mkdir(
1418 data, path, data->opts.target_directory, mode, flags)) < 0)
1419 return error;
1420
1421 if (remove_existing) {
1422 data->perfdata.stat_calls++;
1423
1424 if (p_lstat(path, &st) == 0) {
1425
1426 /* Some file, symlink or folder already exists at this name.
1427 * We would have removed it in remove_the_old unless we're on
1428 * a case inensitive filesystem (or the user has asked us not
1429 * to). Remove the similarly named file to write the new.
1430 */
1431 error = git_futils_rmdir_r(path, NULL, GIT_RMDIR_REMOVE_FILES);
1432 } else if (errno != ENOENT) {
1433 giterr_set(GITERR_OS, "failed to stat '%s'", path);
1434 return GIT_EEXISTS;
1435 } else {
1436 giterr_clear();
1437 }
1438 }
1439
1440 return error;
1441 }
1442
1443 struct checkout_stream {
1444 git_writestream base;
1445 const char *path;
1446 int fd;
1447 int open;
1448 };
1449
1450 static int checkout_stream_write(
1451 git_writestream *s, const char *buffer, size_t len)
1452 {
1453 struct checkout_stream *stream = (struct checkout_stream *)s;
1454 int ret;
1455
1456 if ((ret = p_write(stream->fd, buffer, len)) < 0)
1457 giterr_set(GITERR_OS, "could not write to '%s'", stream->path);
1458
1459 return ret;
1460 }
1461
1462 static int checkout_stream_close(git_writestream *s)
1463 {
1464 struct checkout_stream *stream = (struct checkout_stream *)s;
1465 assert(stream && stream->open);
1466
1467 stream->open = 0;
1468 return p_close(stream->fd);
1469 }
1470
1471 static void checkout_stream_free(git_writestream *s)
1472 {
1473 GIT_UNUSED(s);
1474 }
1475
1476 static int blob_content_to_file(
1477 checkout_data *data,
1478 struct stat *st,
1479 git_blob *blob,
1480 const char *path,
1481 const char *hint_path,
1482 mode_t entry_filemode)
1483 {
1484 int flags = data->opts.file_open_flags;
1485 mode_t file_mode = data->opts.file_mode ?
1486 data->opts.file_mode : entry_filemode;
1487 git_filter_options filter_opts = GIT_FILTER_OPTIONS_INIT;
1488 struct checkout_stream writer;
1489 mode_t mode;
1490 git_filter_list *fl = NULL;
1491 int fd;
1492 int error = 0;
1493
1494 if (hint_path == NULL)
1495 hint_path = path;
1496
1497 if ((error = mkpath2file(data, path, data->opts.dir_mode)) < 0)
1498 return error;
1499
1500 if (flags <= 0)
1501 flags = O_CREAT | O_TRUNC | O_WRONLY;
1502 if (!(mode = file_mode))
1503 mode = GIT_FILEMODE_BLOB;
1504
1505 if ((fd = p_open(path, flags, mode)) < 0) {
1506 giterr_set(GITERR_OS, "could not open '%s' for writing", path);
1507 return fd;
1508 }
1509
1510 filter_opts.attr_session = &data->attr_session;
1511 filter_opts.temp_buf = &data->tmp;
1512
1513 if (!data->opts.disable_filters &&
1514 (error = git_filter_list__load_ext(
1515 &fl, data->repo, blob, hint_path,
1516 GIT_FILTER_TO_WORKTREE, &filter_opts))) {
1517 p_close(fd);
1518 return error;
1519 }
1520
1521 /* setup the writer */
1522 memset(&writer, 0, sizeof(struct checkout_stream));
1523 writer.base.write = checkout_stream_write;
1524 writer.base.close = checkout_stream_close;
1525 writer.base.free = checkout_stream_free;
1526 writer.path = path;
1527 writer.fd = fd;
1528 writer.open = 1;
1529
1530 error = git_filter_list_stream_blob(fl, blob, &writer.base);
1531
1532 assert(writer.open == 0);
1533
1534 git_filter_list_free(fl);
1535
1536 if (error < 0)
1537 return error;
1538
1539 if (st) {
1540 data->perfdata.stat_calls++;
1541
1542 if ((error = p_stat(path, st)) < 0) {
1543 giterr_set(GITERR_OS, "failed to stat '%s'", path);
1544 return error;
1545 }
1546
1547 st->st_mode = entry_filemode;
1548 }
1549
1550 return 0;
1551 }
1552
1553 static int blob_content_to_link(
1554 checkout_data *data,
1555 struct stat *st,
1556 git_blob *blob,
1557 const char *path)
1558 {
1559 git_buf linktarget = GIT_BUF_INIT;
1560 int error;
1561
1562 if ((error = mkpath2file(data, path, data->opts.dir_mode)) < 0)
1563 return error;
1564
1565 if ((error = git_blob__getbuf(&linktarget, blob)) < 0)
1566 return error;
1567
1568 if (data->can_symlink) {
1569 if ((error = p_symlink(git_buf_cstr(&linktarget), path)) < 0)
1570 giterr_set(GITERR_OS, "could not create symlink %s", path);
1571 } else {
1572 error = git_futils_fake_symlink(git_buf_cstr(&linktarget), path);
1573 }
1574
1575 if (!error) {
1576 data->perfdata.stat_calls++;
1577
1578 if ((error = p_lstat(path, st)) < 0)
1579 giterr_set(GITERR_CHECKOUT, "could not stat symlink %s", path);
1580
1581 st->st_mode = GIT_FILEMODE_LINK;
1582 }
1583
1584 git_buf_free(&linktarget);
1585
1586 return error;
1587 }
1588
1589 static int checkout_update_index(
1590 checkout_data *data,
1591 const git_diff_file *file,
1592 struct stat *st)
1593 {
1594 git_index_entry entry;
1595
1596 if (!data->index)
1597 return 0;
1598
1599 memset(&entry, 0, sizeof(entry));
1600 entry.path = (char *)file->path; /* cast to prevent warning */
1601 git_index_entry__init_from_stat(&entry, st, true);
1602 git_oid_cpy(&entry.id, &file->id);
1603
1604 return git_index_add(data->index, &entry);
1605 }
1606
1607 static int checkout_submodule_update_index(
1608 checkout_data *data,
1609 const git_diff_file *file)
1610 {
1611 git_buf *fullpath;
1612 struct stat st;
1613
1614 /* update the index unless prevented */
1615 if ((data->strategy & GIT_CHECKOUT_DONT_UPDATE_INDEX) != 0)
1616 return 0;
1617
1618 if (checkout_target_fullpath(&fullpath, data, file->path) < 0)
1619 return -1;
1620
1621 data->perfdata.stat_calls++;
1622 if (p_stat(fullpath->ptr, &st) < 0) {
1623 giterr_set(
1624 GITERR_CHECKOUT, "could not stat submodule %s\n", file->path);
1625 return GIT_ENOTFOUND;
1626 }
1627
1628 st.st_mode = GIT_FILEMODE_COMMIT;
1629
1630 return checkout_update_index(data, file, &st);
1631 }
1632
1633 static int checkout_submodule(
1634 checkout_data *data,
1635 const git_diff_file *file)
1636 {
1637 bool remove_existing = should_remove_existing(data);
1638 int error = 0;
1639
1640 /* Until submodules are supported, UPDATE_ONLY means do nothing here */
1641 if ((data->strategy & GIT_CHECKOUT_UPDATE_ONLY) != 0)
1642 return 0;
1643
1644 if ((error = checkout_mkdir(
1645 data,
1646 file->path, data->opts.target_directory, data->opts.dir_mode,
1647 remove_existing ? MKDIR_REMOVE_EXISTING : MKDIR_NORMAL)) < 0)
1648 return error;
1649
1650 if ((error = git_submodule_lookup(NULL, data->repo, file->path)) < 0) {
1651 /* I've observed repos with submodules in the tree that do not
1652 * have a .gitmodules - core Git just makes an empty directory
1653 */
1654 if (error == GIT_ENOTFOUND) {
1655 giterr_clear();
1656 return checkout_submodule_update_index(data, file);
1657 }
1658
1659 return error;
1660 }
1661
1662 /* TODO: Support checkout_strategy options. Two circumstances:
1663 * 1 - submodule already checked out, but we need to move the HEAD
1664 * to the new OID, or
1665 * 2 - submodule not checked out and we should recursively check it out
1666 *
1667 * Checkout will not execute a pull on the submodule, but a clone
1668 * command should probably be able to. Do we need a submodule callback?
1669 */
1670
1671 return checkout_submodule_update_index(data, file);
1672 }
1673
1674 static void report_progress(
1675 checkout_data *data,
1676 const char *path)
1677 {
1678 if (data->opts.progress_cb)
1679 data->opts.progress_cb(
1680 path, data->completed_steps, data->total_steps,
1681 data->opts.progress_payload);
1682 }
1683
1684 static int checkout_safe_for_update_only(
1685 checkout_data *data, const char *path, mode_t expected_mode)
1686 {
1687 struct stat st;
1688
1689 data->perfdata.stat_calls++;
1690
1691 if (p_lstat(path, &st) < 0) {
1692 /* if doesn't exist, then no error and no update */
1693 if (errno == ENOENT || errno == ENOTDIR)
1694 return 0;
1695
1696 /* otherwise, stat error and no update */
1697 giterr_set(GITERR_OS, "failed to stat '%s'", path);
1698 return -1;
1699 }
1700
1701 /* only safe for update if this is the same type of file */
1702 if ((st.st_mode & ~0777) == (expected_mode & ~0777))
1703 return 1;
1704
1705 return 0;
1706 }
1707
1708 static int checkout_write_content(
1709 checkout_data *data,
1710 const git_oid *oid,
1711 const char *full_path,
1712 const char *hint_path,
1713 unsigned int mode,
1714 struct stat *st)
1715 {
1716 int error = 0;
1717 git_blob *blob;
1718
1719 if ((error = git_blob_lookup(&blob, data->repo, oid)) < 0)
1720 return error;
1721
1722 if (S_ISLNK(mode))
1723 error = blob_content_to_link(data, st, blob, full_path);
1724 else
1725 error = blob_content_to_file(data, st, blob, full_path, hint_path, mode);
1726
1727 git_blob_free(blob);
1728
1729 /* if we try to create the blob and an existing directory blocks it from
1730 * being written, then there must have been a typechange conflict in a
1731 * parent directory - suppress the error and try to continue.
1732 */
1733 if ((data->strategy & GIT_CHECKOUT_ALLOW_CONFLICTS) != 0 &&
1734 (error == GIT_ENOTFOUND || error == GIT_EEXISTS))
1735 {
1736 giterr_clear();
1737 error = 0;
1738 }
1739
1740 return error;
1741 }
1742
1743 static int checkout_blob(
1744 checkout_data *data,
1745 const git_diff_file *file)
1746 {
1747 git_buf *fullpath;
1748 struct stat st;
1749 int error = 0;
1750
1751 if (checkout_target_fullpath(&fullpath, data, file->path) < 0)
1752 return -1;
1753
1754 if ((data->strategy & GIT_CHECKOUT_UPDATE_ONLY) != 0) {
1755 int rval = checkout_safe_for_update_only(
1756 data, fullpath->ptr, file->mode);
1757
1758 if (rval <= 0)
1759 return rval;
1760 }
1761
1762 error = checkout_write_content(
1763 data, &file->id, fullpath->ptr, NULL, file->mode, &st);
1764
1765 /* update the index unless prevented */
1766 if (!error && (data->strategy & GIT_CHECKOUT_DONT_UPDATE_INDEX) == 0)
1767 error = checkout_update_index(data, file, &st);
1768
1769 /* update the submodule data if this was a new .gitmodules file */
1770 if (!error && strcmp(file->path, ".gitmodules") == 0)
1771 data->reload_submodules = true;
1772
1773 return error;
1774 }
1775
1776 static int checkout_remove_the_old(
1777 unsigned int *actions,
1778 checkout_data *data)
1779 {
1780 int error = 0;
1781 git_diff_delta *delta;
1782 const char *str;
1783 size_t i;
1784 git_buf *fullpath;
1785 uint32_t flg = GIT_RMDIR_EMPTY_PARENTS |
1786 GIT_RMDIR_REMOVE_FILES | GIT_RMDIR_REMOVE_BLOCKERS;
1787
1788 if (data->opts.checkout_strategy & GIT_CHECKOUT_SKIP_LOCKED_DIRECTORIES)
1789 flg |= GIT_RMDIR_SKIP_NONEMPTY;
1790
1791 if (checkout_target_fullpath(&fullpath, data, NULL) < 0)
1792 return -1;
1793
1794 git_vector_foreach(&data->diff->deltas, i, delta) {
1795 if (actions[i] & CHECKOUT_ACTION__REMOVE) {
1796 error = git_futils_rmdir_r(
1797 delta->old_file.path, fullpath->ptr, flg);
1798
1799 if (error < 0)
1800 return error;
1801
1802 data->completed_steps++;
1803 report_progress(data, delta->old_file.path);
1804
1805 if ((actions[i] & CHECKOUT_ACTION__UPDATE_BLOB) == 0 &&
1806 (data->strategy & GIT_CHECKOUT_DONT_UPDATE_INDEX) == 0 &&
1807 data->index != NULL)
1808 {
1809 (void)git_index_remove(data->index, delta->old_file.path, 0);
1810 }
1811 }
1812 }
1813
1814 git_vector_foreach(&data->removes, i, str) {
1815 error = git_futils_rmdir_r(str, fullpath->ptr, flg);
1816 if (error < 0)
1817 return error;
1818
1819 data->completed_steps++;
1820 report_progress(data, str);
1821
1822 if ((data->strategy & GIT_CHECKOUT_DONT_UPDATE_INDEX) == 0 &&
1823 data->index != NULL)
1824 {
1825 if (str[strlen(str) - 1] == '/')
1826 (void)git_index_remove_directory(data->index, str, 0);
1827 else
1828 (void)git_index_remove(data->index, str, 0);
1829 }
1830 }
1831
1832 return 0;
1833 }
1834
1835 static int checkout_deferred_remove(git_repository *repo, const char *path)
1836 {
1837 #if 0
1838 int error = git_futils_rmdir_r(
1839 path, data->opts.target_directory, GIT_RMDIR_EMPTY_PARENTS);
1840
1841 if (error == GIT_ENOTFOUND) {
1842 error = 0;
1843 giterr_clear();
1844 }
1845
1846 return error;
1847 #else
1848 GIT_UNUSED(repo);
1849 GIT_UNUSED(path);
1850 assert(false);
1851 return 0;
1852 #endif
1853 }
1854
1855 static int checkout_create_the_new(
1856 unsigned int *actions,
1857 checkout_data *data)
1858 {
1859 int error = 0;
1860 git_diff_delta *delta;
1861 size_t i;
1862
1863 git_vector_foreach(&data->diff->deltas, i, delta) {
1864 if (actions[i] & CHECKOUT_ACTION__DEFER_REMOVE) {
1865 /* this had a blocker directory that should only be removed iff
1866 * all of the contents of the directory were safely removed
1867 */
1868 if ((error = checkout_deferred_remove(
1869 data->repo, delta->old_file.path)) < 0)
1870 return error;
1871 }
1872
1873 if (actions[i] & CHECKOUT_ACTION__UPDATE_BLOB) {
1874 error = checkout_blob(data, &delta->new_file);
1875 if (error < 0)
1876 return error;
1877
1878 data->completed_steps++;
1879 report_progress(data, delta->new_file.path);
1880 }
1881 }
1882
1883 return 0;
1884 }
1885
1886 static int checkout_create_submodules(
1887 unsigned int *actions,
1888 checkout_data *data)
1889 {
1890 int error = 0;
1891 git_diff_delta *delta;
1892 size_t i;
1893
1894 git_vector_foreach(&data->diff->deltas, i, delta) {
1895 if (actions[i] & CHECKOUT_ACTION__DEFER_REMOVE) {
1896 /* this has a blocker directory that should only be removed iff
1897 * all of the contents of the directory were safely removed
1898 */
1899 if ((error = checkout_deferred_remove(
1900 data->repo, delta->old_file.path)) < 0)
1901 return error;
1902 }
1903
1904 if (actions[i] & CHECKOUT_ACTION__UPDATE_SUBMODULE) {
1905 int error = checkout_submodule(data, &delta->new_file);
1906 if (error < 0)
1907 return error;
1908
1909 data->completed_steps++;
1910 report_progress(data, delta->new_file.path);
1911 }
1912 }
1913
1914 return 0;
1915 }
1916
1917 static int checkout_lookup_head_tree(git_tree **out, git_repository *repo)
1918 {
1919 int error = 0;
1920 git_reference *ref = NULL;
1921 git_object *head;
1922
1923 if (!(error = git_repository_head(&ref, repo)) &&
1924 !(error = git_reference_peel(&head, ref, GIT_OBJ_TREE)))
1925 *out = (git_tree *)head;
1926
1927 git_reference_free(ref);
1928
1929 return error;
1930 }
1931
1932
1933 static int conflict_entry_name(
1934 git_buf *out,
1935 const char *side_name,
1936 const char *filename)
1937 {
1938 if (git_buf_puts(out, side_name) < 0 ||
1939 git_buf_putc(out, ':') < 0 ||
1940 git_buf_puts(out, filename) < 0)
1941 return -1;
1942
1943 return 0;
1944 }
1945
1946 static int checkout_path_suffixed(git_buf *path, const char *suffix)
1947 {
1948 size_t path_len;
1949 int i = 0, error = 0;
1950
1951 if ((error = git_buf_putc(path, '~')) < 0 || (error = git_buf_puts(path, suffix)) < 0)
1952 return -1;
1953
1954 path_len = git_buf_len(path);
1955
1956 while (git_path_exists(git_buf_cstr(path)) && i < INT_MAX) {
1957 git_buf_truncate(path, path_len);
1958
1959 if ((error = git_buf_putc(path, '_')) < 0 ||
1960 (error = git_buf_printf(path, "%d", i)) < 0)
1961 return error;
1962
1963 i++;
1964 }
1965
1966 if (i == INT_MAX) {
1967 git_buf_truncate(path, path_len);
1968
1969 giterr_set(GITERR_CHECKOUT, "could not write '%s': working directory file exists", path->ptr);
1970 return GIT_EEXISTS;
1971 }
1972
1973 return 0;
1974 }
1975
1976 static int checkout_write_entry(
1977 checkout_data *data,
1978 checkout_conflictdata *conflict,
1979 const git_index_entry *side)
1980 {
1981 const char *hint_path = NULL, *suffix;
1982 git_buf *fullpath;
1983 struct stat st;
1984 int error;
1985
1986 assert (side == conflict->ours || side == conflict->theirs);
1987
1988 if (checkout_target_fullpath(&fullpath, data, side->path) < 0)
1989 return -1;
1990
1991 if ((conflict->name_collision || conflict->directoryfile) &&
1992 (data->strategy & GIT_CHECKOUT_USE_OURS) == 0 &&
1993 (data->strategy & GIT_CHECKOUT_USE_THEIRS) == 0) {
1994
1995 if (side == conflict->ours)
1996 suffix = data->opts.our_label ? data->opts.our_label :
1997 "ours";
1998 else
1999 suffix = data->opts.their_label ? data->opts.their_label :
2000 "theirs";
2001
2002 if (checkout_path_suffixed(fullpath, suffix) < 0)
2003 return -1;
2004
2005 hint_path = side->path;
2006 }
2007
2008 if ((data->strategy & GIT_CHECKOUT_UPDATE_ONLY) != 0 &&
2009 (error = checkout_safe_for_update_only(data, fullpath->ptr, side->mode)) <= 0)
2010 return error;
2011
2012 return checkout_write_content(data,
2013 &side->id, fullpath->ptr, hint_path, side->mode, &st);
2014 }
2015
2016 static int checkout_write_entries(
2017 checkout_data *data,
2018 checkout_conflictdata *conflict)
2019 {
2020 int error = 0;
2021
2022 if ((error = checkout_write_entry(data, conflict, conflict->ours)) >= 0)
2023 error = checkout_write_entry(data, conflict, conflict->theirs);
2024
2025 return error;
2026 }
2027
2028 static int checkout_merge_path(
2029 git_buf *out,
2030 checkout_data *data,
2031 checkout_conflictdata *conflict,
2032 git_merge_file_result *result)
2033 {
2034 const char *our_label_raw, *their_label_raw, *suffix;
2035 int error = 0;
2036
2037 if ((error = git_buf_joinpath(out, git_repository_workdir(data->repo), result->path)) < 0)
2038 return error;
2039
2040 /* Most conflicts simply use the filename in the index */
2041 if (!conflict->name_collision)
2042 return 0;
2043
2044 /* Rename 2->1 conflicts need the branch name appended */
2045 our_label_raw = data->opts.our_label ? data->opts.our_label : "ours";
2046 their_label_raw = data->opts.their_label ? data->opts.their_label : "theirs";
2047 suffix = strcmp(result->path, conflict->ours->path) == 0 ? our_label_raw : their_label_raw;
2048
2049 if ((error = checkout_path_suffixed(out, suffix)) < 0)
2050 return error;
2051
2052 return 0;
2053 }
2054
2055 static int checkout_write_merge(
2056 checkout_data *data,
2057 checkout_conflictdata *conflict)
2058 {
2059 git_buf our_label = GIT_BUF_INIT, their_label = GIT_BUF_INIT,
2060 path_suffixed = GIT_BUF_INIT, path_workdir = GIT_BUF_INIT,
2061 in_data = GIT_BUF_INIT, out_data = GIT_BUF_INIT;
2062 git_merge_file_options opts = GIT_MERGE_FILE_OPTIONS_INIT;
2063 git_merge_file_result result = {0};
2064 git_filebuf output = GIT_FILEBUF_INIT;
2065 git_filter_list *fl = NULL;
2066 git_filter_options filter_opts = GIT_FILTER_OPTIONS_INIT;
2067 int error = 0;
2068
2069 if (data->opts.checkout_strategy & GIT_CHECKOUT_CONFLICT_STYLE_DIFF3)
2070 opts.flags |= GIT_MERGE_FILE_STYLE_DIFF3;
2071
2072 opts.ancestor_label = data->opts.ancestor_label ?
2073 data->opts.ancestor_label : "ancestor";
2074 opts.our_label = data->opts.our_label ?
2075 data->opts.our_label : "ours";
2076 opts.their_label = data->opts.their_label ?
2077 data->opts.their_label : "theirs";
2078
2079 /* If all the paths are identical, decorate the diff3 file with the branch
2080 * names. Otherwise, append branch_name:path.
2081 */
2082 if (conflict->ours && conflict->theirs &&
2083 strcmp(conflict->ours->path, conflict->theirs->path) != 0) {
2084
2085 if ((error = conflict_entry_name(
2086 &our_label, opts.our_label, conflict->ours->path)) < 0 ||
2087 (error = conflict_entry_name(
2088 &their_label, opts.their_label, conflict->theirs->path)) < 0)
2089 goto done;
2090
2091 opts.our_label = git_buf_cstr(&our_label);
2092 opts.their_label = git_buf_cstr(&their_label);
2093 }
2094
2095 if ((error = git_merge_file_from_index(&result, data->repo,
2096 conflict->ancestor, conflict->ours, conflict->theirs, &opts)) < 0)
2097 goto done;
2098
2099 if (result.path == NULL || result.mode == 0) {
2100 giterr_set(GITERR_CHECKOUT, "could not merge contents of file");
2101 error = GIT_ECONFLICT;
2102 goto done;
2103 }
2104
2105 if ((error = checkout_merge_path(&path_workdir, data, conflict, &result)) < 0)
2106 goto done;
2107
2108 if ((data->strategy & GIT_CHECKOUT_UPDATE_ONLY) != 0 &&
2109 (error = checkout_safe_for_update_only(data, git_buf_cstr(&path_workdir), result.mode)) <= 0)
2110 goto done;
2111
2112 if (!data->opts.disable_filters) {
2113 in_data.ptr = (char *)result.ptr;
2114 in_data.size = result.len;
2115
2116 filter_opts.attr_session = &data->attr_session;
2117 filter_opts.temp_buf = &data->tmp;
2118
2119 if ((error = git_filter_list__load_ext(
2120 &fl, data->repo, NULL, git_buf_cstr(&path_workdir),
2121 GIT_FILTER_TO_WORKTREE, &filter_opts)) < 0 ||
2122 (error = git_filter_list_apply_to_data(&out_data, fl, &in_data)) < 0)
2123 goto done;
2124 } else {
2125 out_data.ptr = (char *)result.ptr;
2126 out_data.size = result.len;
2127 }
2128
2129 if ((error = mkpath2file(data, path_workdir.ptr, data->opts.dir_mode)) < 0 ||
2130 (error = git_filebuf_open(&output, git_buf_cstr(&path_workdir), GIT_FILEBUF_DO_NOT_BUFFER, result.mode)) < 0 ||
2131 (error = git_filebuf_write(&output, out_data.ptr, out_data.size)) < 0 ||
2132 (error = git_filebuf_commit(&output)) < 0)
2133 goto done;
2134
2135 done:
2136 git_filter_list_free(fl);
2137
2138 git_buf_free(&out_data);
2139 git_buf_free(&our_label);
2140 git_buf_free(&their_label);
2141
2142 git_merge_file_result_free(&result);
2143 git_buf_free(&path_workdir);
2144 git_buf_free(&path_suffixed);
2145
2146 return error;
2147 }
2148
2149 static int checkout_conflict_add(
2150 checkout_data *data,
2151 const git_index_entry *conflict)
2152 {
2153 int error = git_index_remove(data->index, conflict->path, 0);
2154
2155 if (error == GIT_ENOTFOUND)
2156 giterr_clear();
2157 else if (error < 0)
2158 return error;
2159
2160 return git_index_add(data->index, conflict);
2161 }
2162
2163 static int checkout_conflict_update_index(
2164 checkout_data *data,
2165 checkout_conflictdata *conflict)
2166 {
2167 int error = 0;
2168
2169 if (conflict->ancestor)
2170 error = checkout_conflict_add(data, conflict->ancestor);
2171
2172 if (!error && conflict->ours)
2173 error = checkout_conflict_add(data, conflict->ours);
2174
2175 if (!error && conflict->theirs)
2176 error = checkout_conflict_add(data, conflict->theirs);
2177
2178 return error;
2179 }
2180
2181 static int checkout_create_conflicts(checkout_data *data)
2182 {
2183 checkout_conflictdata *conflict;
2184 size_t i;
2185 int error = 0;
2186
2187 git_vector_foreach(&data->update_conflicts, i, conflict) {
2188
2189 /* Both deleted: nothing to do */
2190 if (conflict->ours == NULL && conflict->theirs == NULL)
2191 error = 0;
2192
2193 else if ((data->strategy & GIT_CHECKOUT_USE_OURS) &&
2194 conflict->ours)
2195 error = checkout_write_entry(data, conflict, conflict->ours);
2196 else if ((data->strategy & GIT_CHECKOUT_USE_THEIRS) &&
2197 conflict->theirs)
2198 error = checkout_write_entry(data, conflict, conflict->theirs);
2199
2200 /* Ignore the other side of name collisions. */
2201 else if ((data->strategy & GIT_CHECKOUT_USE_OURS) &&
2202 !conflict->ours && conflict->name_collision)
2203 error = 0;
2204 else if ((data->strategy & GIT_CHECKOUT_USE_THEIRS) &&
2205 !conflict->theirs && conflict->name_collision)
2206 error = 0;
2207
2208 /* For modify/delete, name collisions and d/f conflicts, write
2209 * the file (potentially with the name mangled.
2210 */
2211 else if (conflict->ours != NULL && conflict->theirs == NULL)
2212 error = checkout_write_entry(data, conflict, conflict->ours);
2213 else if (conflict->ours == NULL && conflict->theirs != NULL)
2214 error = checkout_write_entry(data, conflict, conflict->theirs);
2215
2216 /* Add/add conflicts and rename 1->2 conflicts, write the
2217 * ours/theirs sides (potentially name mangled).
2218 */
2219 else if (conflict->one_to_two)
2220 error = checkout_write_entries(data, conflict);
2221
2222 /* If all sides are links, write the ours side */
2223 else if (S_ISLNK(conflict->ours->mode) &&
2224 S_ISLNK(conflict->theirs->mode))
2225 error = checkout_write_entry(data, conflict, conflict->ours);
2226 /* Link/file conflicts, write the file side */
2227 else if (S_ISLNK(conflict->ours->mode))
2228 error = checkout_write_entry(data, conflict, conflict->theirs);
2229 else if (S_ISLNK(conflict->theirs->mode))
2230 error = checkout_write_entry(data, conflict, conflict->ours);
2231
2232 /* If any side is a gitlink, do nothing. */
2233 else if (conflict->submodule)
2234 error = 0;
2235
2236 /* If any side is binary, write the ours side */
2237 else if (conflict->binary)
2238 error = checkout_write_entry(data, conflict, conflict->ours);
2239
2240 else if (!error)
2241 error = checkout_write_merge(data, conflict);
2242
2243 /* Update the index extensions (REUC and NAME) if we're checking
2244 * out a different index. (Otherwise just leave them there.)
2245 */
2246 if (!error && (data->strategy & GIT_CHECKOUT_DONT_UPDATE_INDEX) == 0)
2247 error = checkout_conflict_update_index(data, conflict);
2248
2249 if (error)
2250 break;
2251
2252 data->completed_steps++;
2253 report_progress(data,
2254 conflict->ours ? conflict->ours->path :
2255 (conflict->theirs ? conflict->theirs->path : conflict->ancestor->path));
2256 }
2257
2258 return error;
2259 }
2260
2261 static int checkout_remove_conflicts(checkout_data *data)
2262 {
2263 const char *conflict;
2264 size_t i;
2265
2266 git_vector_foreach(&data->remove_conflicts, i, conflict) {
2267 if (git_index_conflict_remove(data->index, conflict) < 0)
2268 return -1;
2269
2270 data->completed_steps++;
2271 }
2272
2273 return 0;
2274 }
2275
2276 static int checkout_extensions_update_index(checkout_data *data)
2277 {
2278 const git_index_reuc_entry *reuc_entry;
2279 const git_index_name_entry *name_entry;
2280 size_t i;
2281 int error = 0;
2282
2283 if ((data->strategy & GIT_CHECKOUT_UPDATE_ONLY) != 0)
2284 return 0;
2285
2286 if (data->update_reuc) {
2287 git_vector_foreach(data->update_reuc, i, reuc_entry) {
2288 if ((error = git_index_reuc_add(data->index, reuc_entry->path,
2289 reuc_entry->mode[0], &reuc_entry->oid[0],
2290 reuc_entry->mode[1], &reuc_entry->oid[1],
2291 reuc_entry->mode[2], &reuc_entry->oid[2])) < 0)
2292 goto done;
2293 }
2294 }
2295
2296 if (data->update_names) {
2297 git_vector_foreach(data->update_names, i, name_entry) {
2298 if ((error = git_index_name_add(data->index, name_entry->ancestor,
2299 name_entry->ours, name_entry->theirs)) < 0)
2300 goto done;
2301 }
2302 }
2303
2304 done:
2305 return error;
2306 }
2307
2308 static void checkout_data_clear(checkout_data *data)
2309 {
2310 if (data->opts_free_baseline) {
2311 git_tree_free(data->opts.baseline);
2312 data->opts.baseline = NULL;
2313 }
2314
2315 git_vector_free(&data->removes);
2316 git_pool_clear(&data->pool);
2317
2318 git_vector_free_deep(&data->remove_conflicts);
2319 git_vector_free_deep(&data->update_conflicts);
2320
2321 git__free(data->pfx);
2322 data->pfx = NULL;
2323
2324 git_strmap_free(data->mkdir_map);
2325
2326 git_buf_free(&data->target_path);
2327 git_buf_free(&data->tmp);
2328
2329 git_index_free(data->index);
2330 data->index = NULL;
2331
2332 git_strmap_free(data->mkdir_map);
2333
2334 git_attr_session__free(&data->attr_session);
2335 }
2336
2337 static int checkout_data_init(
2338 checkout_data *data,
2339 git_iterator *target,
2340 const git_checkout_options *proposed)
2341 {
2342 int error = 0;
2343 git_repository *repo = git_iterator_owner(target);
2344
2345 memset(data, 0, sizeof(*data));
2346
2347 if (!repo) {
2348 giterr_set(GITERR_CHECKOUT, "cannot checkout nothing");
2349 return -1;
2350 }
2351
2352 if ((!proposed || !proposed->target_directory) &&
2353 (error = git_repository__ensure_not_bare(repo, "checkout")) < 0)
2354 return error;
2355
2356 data->repo = repo;
2357 data->target = target;
2358
2359 GITERR_CHECK_VERSION(
2360 proposed, GIT_CHECKOUT_OPTIONS_VERSION, "git_checkout_options");
2361
2362 if (!proposed)
2363 GIT_INIT_STRUCTURE(&data->opts, GIT_CHECKOUT_OPTIONS_VERSION);
2364 else
2365 memmove(&data->opts, proposed, sizeof(git_checkout_options));
2366
2367 if (!data->opts.target_directory)
2368 data->opts.target_directory = git_repository_workdir(repo);
2369 else if (!git_path_isdir(data->opts.target_directory) &&
2370 (error = checkout_mkdir(data,
2371 data->opts.target_directory, NULL,
2372 GIT_DIR_MODE, GIT_MKDIR_VERIFY_DIR)) < 0)
2373 goto cleanup;
2374
2375 /* refresh config and index content unless NO_REFRESH is given */
2376 if ((data->opts.checkout_strategy & GIT_CHECKOUT_NO_REFRESH) == 0) {
2377 git_config *cfg;
2378
2379 if ((error = git_repository_config__weakptr(&cfg, repo)) < 0)
2380 goto cleanup;
2381
2382 /* Get the repository index and reload it (unless we're checking
2383 * out the index; then it has the changes we're trying to check
2384 * out and those should not be overwritten.)
2385 */
2386 if ((error = git_repository_index(&data->index, data->repo)) < 0)
2387 goto cleanup;
2388
2389 if (data->index != git_iterator_index(target)) {
2390 if ((error = git_index_read(data->index, true)) < 0)
2391 goto cleanup;
2392
2393 /* cannot checkout if unresolved conflicts exist */
2394 if ((data->opts.checkout_strategy & GIT_CHECKOUT_FORCE) == 0 &&
2395 git_index_has_conflicts(data->index)) {
2396 error = GIT_ECONFLICT;
2397 giterr_set(GITERR_CHECKOUT,
2398 "unresolved conflicts exist in the index");
2399 goto cleanup;
2400 }
2401
2402 /* clean conflict data in the current index */
2403 git_index_name_clear(data->index);
2404 git_index_reuc_clear(data->index);
2405 }
2406 }
2407
2408 /* if you are forcing, allow all safe updates, plus recreate missing */
2409 if ((data->opts.checkout_strategy & GIT_CHECKOUT_FORCE) != 0)
2410 data->opts.checkout_strategy |= GIT_CHECKOUT_SAFE |
2411 GIT_CHECKOUT_RECREATE_MISSING;
2412
2413 /* if the repository does not actually have an index file, then this
2414 * is an initial checkout (perhaps from clone), so we allow safe updates
2415 */
2416 if (!data->index->on_disk &&
2417 (data->opts.checkout_strategy & GIT_CHECKOUT_SAFE) != 0)
2418 data->opts.checkout_strategy |= GIT_CHECKOUT_RECREATE_MISSING;
2419
2420 data->strategy = data->opts.checkout_strategy;
2421
2422 /* opts->disable_filters is false by default */
2423
2424 if (!data->opts.dir_mode)
2425 data->opts.dir_mode = GIT_DIR_MODE;
2426
2427 if (!data->opts.file_open_flags)
2428 data->opts.file_open_flags = O_CREAT | O_TRUNC | O_WRONLY;
2429
2430 data->pfx = git_pathspec_prefix(&data->opts.paths);
2431
2432 if ((error = git_repository__cvar(
2433 &data->can_symlink, repo, GIT_CVAR_SYMLINKS)) < 0)
2434 goto cleanup;
2435
2436 if (!data->opts.baseline && !data->opts.baseline_index) {
2437 data->opts_free_baseline = true;
2438 error = 0;
2439
2440 /* if we don't have an index, this is an initial checkout and
2441 * should be against an empty baseline
2442 */
2443 if (data->index->on_disk)
2444 error = checkout_lookup_head_tree(&data->opts.baseline, repo);
2445
2446 if (error == GIT_EUNBORNBRANCH) {
2447 error = 0;
2448 giterr_clear();
2449 }
2450
2451 if (error < 0)
2452 goto cleanup;
2453 }
2454
2455 if ((data->opts.checkout_strategy &
2456 (GIT_CHECKOUT_CONFLICT_STYLE_MERGE | GIT_CHECKOUT_CONFLICT_STYLE_DIFF3)) == 0) {
2457 git_config_entry *conflict_style = NULL;
2458 git_config *cfg = NULL;
2459
2460 if ((error = git_repository_config__weakptr(&cfg, repo)) < 0 ||
2461 (error = git_config_get_entry(&conflict_style, cfg, "merge.conflictstyle")) < 0 ||
2462 error == GIT_ENOTFOUND)
2463 ;
2464 else if (error)
2465 goto cleanup;
2466 else if (strcmp(conflict_style->value, "merge") == 0)
2467 data->opts.checkout_strategy |= GIT_CHECKOUT_CONFLICT_STYLE_MERGE;
2468 else if (strcmp(conflict_style->value, "diff3") == 0)
2469 data->opts.checkout_strategy |= GIT_CHECKOUT_CONFLICT_STYLE_DIFF3;
2470 else {
2471 giterr_set(GITERR_CHECKOUT, "unknown style '%s' given for 'merge.conflictstyle'",
2472 conflict_style->value);
2473 error = -1;
2474 git_config_entry_free(conflict_style);
2475 goto cleanup;
2476 }
2477 git_config_entry_free(conflict_style);
2478 }
2479
2480 git_pool_init(&data->pool, 1);
2481
2482 if ((error = git_vector_init(&data->removes, 0, git__strcmp_cb)) < 0 ||
2483 (error = git_vector_init(&data->remove_conflicts, 0, NULL)) < 0 ||
2484 (error = git_vector_init(&data->update_conflicts, 0, NULL)) < 0 ||
2485 (error = git_buf_puts(&data->target_path, data->opts.target_directory)) < 0 ||
2486 (error = git_path_to_dir(&data->target_path)) < 0 ||
2487 (error = git_strmap_alloc(&data->mkdir_map)) < 0)
2488 goto cleanup;
2489
2490 data->target_len = git_buf_len(&data->target_path);
2491
2492 git_attr_session__init(&data->attr_session, data->repo);
2493
2494 cleanup:
2495 if (error < 0)
2496 checkout_data_clear(data);
2497
2498 return error;
2499 }
2500
2501 #define CHECKOUT_INDEX_DONT_WRITE_MASK \
2502 (GIT_CHECKOUT_DONT_UPDATE_INDEX | GIT_CHECKOUT_DONT_WRITE_INDEX)
2503
2504 int git_checkout_iterator(
2505 git_iterator *target,
2506 git_index *index,
2507 const git_checkout_options *opts)
2508 {
2509 int error = 0;
2510 git_iterator *baseline = NULL, *workdir = NULL;
2511 git_iterator_options baseline_opts = GIT_ITERATOR_OPTIONS_INIT,
2512 workdir_opts = GIT_ITERATOR_OPTIONS_INIT;
2513 checkout_data data = {0};
2514 git_diff_options diff_opts = GIT_DIFF_OPTIONS_INIT;
2515 uint32_t *actions = NULL;
2516 size_t *counts = NULL;
2517
2518 /* initialize structures and options */
2519 error = checkout_data_init(&data, target, opts);
2520 if (error < 0)
2521 return error;
2522
2523 diff_opts.flags =
2524 GIT_DIFF_INCLUDE_UNMODIFIED |
2525 GIT_DIFF_INCLUDE_UNREADABLE |
2526 GIT_DIFF_INCLUDE_UNTRACKED |
2527 GIT_DIFF_RECURSE_UNTRACKED_DIRS | /* needed to match baseline */
2528 GIT_DIFF_INCLUDE_IGNORED |
2529 GIT_DIFF_INCLUDE_TYPECHANGE |
2530 GIT_DIFF_INCLUDE_TYPECHANGE_TREES |
2531 GIT_DIFF_SKIP_BINARY_CHECK |
2532 GIT_DIFF_INCLUDE_CASECHANGE;
2533 if (data.opts.checkout_strategy & GIT_CHECKOUT_DISABLE_PATHSPEC_MATCH)
2534 diff_opts.flags |= GIT_DIFF_DISABLE_PATHSPEC_MATCH;
2535 if (data.opts.paths.count > 0)
2536 diff_opts.pathspec = data.opts.paths;
2537
2538 /* set up iterators */
2539
2540 workdir_opts.flags = git_iterator_ignore_case(target) ?
2541 GIT_ITERATOR_IGNORE_CASE : GIT_ITERATOR_DONT_IGNORE_CASE;
2542 workdir_opts.flags |= GIT_ITERATOR_DONT_AUTOEXPAND;
2543 workdir_opts.start = data.pfx;
2544 workdir_opts.end = data.pfx;
2545
2546 if ((error = git_iterator_reset_range(target, data.pfx, data.pfx)) < 0 ||
2547 (error = git_iterator_for_workdir_ext(
2548 &workdir, data.repo, data.opts.target_directory, index, NULL,
2549 &workdir_opts)) < 0)
2550 goto cleanup;
2551
2552 baseline_opts.flags = git_iterator_ignore_case(target) ?
2553 GIT_ITERATOR_IGNORE_CASE : GIT_ITERATOR_DONT_IGNORE_CASE;
2554 baseline_opts.start = data.pfx;
2555 baseline_opts.end = data.pfx;
2556 if (opts && (opts->checkout_strategy & GIT_CHECKOUT_DISABLE_PATHSPEC_MATCH)) {
2557 baseline_opts.pathlist.count = opts->paths.count;
2558 baseline_opts.pathlist.strings = opts->paths.strings;
2559 }
2560
2561 if (data.opts.baseline_index) {
2562 if ((error = git_iterator_for_index(
2563 &baseline, git_index_owner(data.opts.baseline_index),
2564 data.opts.baseline_index, &baseline_opts)) < 0)
2565 goto cleanup;
2566 } else {
2567 if ((error = git_iterator_for_tree(
2568 &baseline, data.opts.baseline, &baseline_opts)) < 0)
2569 goto cleanup;
2570 }
2571
2572 /* Should not have case insensitivity mismatch */
2573 assert(git_iterator_ignore_case(workdir) == git_iterator_ignore_case(baseline));
2574
2575 /* Generate baseline-to-target diff which will include an entry for
2576 * every possible update that might need to be made.
2577 */
2578 if ((error = git_diff__from_iterators(
2579 &data.diff, data.repo, baseline, target, &diff_opts)) < 0)
2580 goto cleanup;
2581
2582 /* Loop through diff (and working directory iterator) building a list of
2583 * actions to be taken, plus look for conflicts and send notifications,
2584 * then loop through conflicts.
2585 */
2586 if ((error = checkout_get_actions(&actions, &counts, &data, workdir)) != 0)
2587 goto cleanup;
2588
2589 data.total_steps = counts[CHECKOUT_ACTION__REMOVE] +
2590 counts[CHECKOUT_ACTION__REMOVE_CONFLICT] +
2591 counts[CHECKOUT_ACTION__UPDATE_BLOB] +
2592 counts[CHECKOUT_ACTION__UPDATE_SUBMODULE] +
2593 counts[CHECKOUT_ACTION__UPDATE_CONFLICT];
2594
2595 report_progress(&data, NULL); /* establish 0 baseline */
2596
2597 /* To deal with some order dependencies, perform remaining checkout
2598 * in three passes: removes, then update blobs, then update submodules.
2599 */
2600 if (counts[CHECKOUT_ACTION__REMOVE] > 0 &&
2601 (error = checkout_remove_the_old(actions, &data)) < 0)
2602 goto cleanup;
2603
2604 if (counts[CHECKOUT_ACTION__REMOVE_CONFLICT] > 0 &&
2605 (error = checkout_remove_conflicts(&data)) < 0)
2606 goto cleanup;
2607
2608 if (counts[CHECKOUT_ACTION__UPDATE_BLOB] > 0 &&
2609 (error = checkout_create_the_new(actions, &data)) < 0)
2610 goto cleanup;
2611
2612 if (counts[CHECKOUT_ACTION__UPDATE_SUBMODULE] > 0 &&
2613 (error = checkout_create_submodules(actions, &data)) < 0)
2614 goto cleanup;
2615
2616 if (counts[CHECKOUT_ACTION__UPDATE_CONFLICT] > 0 &&
2617 (error = checkout_create_conflicts(&data)) < 0)
2618 goto cleanup;
2619
2620 if (data.index != git_iterator_index(target) &&
2621 (error = checkout_extensions_update_index(&data)) < 0)
2622 goto cleanup;
2623
2624 assert(data.completed_steps == data.total_steps);
2625
2626 if (data.opts.perfdata_cb)
2627 data.opts.perfdata_cb(&data.perfdata, data.opts.perfdata_payload);
2628
2629 cleanup:
2630 if (!error && data.index != NULL &&
2631 (data.strategy & CHECKOUT_INDEX_DONT_WRITE_MASK) == 0)
2632 error = git_index_write(data.index);
2633
2634 git_diff_free(data.diff);
2635 git_iterator_free(workdir);
2636 git_iterator_free(baseline);
2637 git__free(actions);
2638 git__free(counts);
2639 checkout_data_clear(&data);
2640
2641 return error;
2642 }
2643
2644 int git_checkout_index(
2645 git_repository *repo,
2646 git_index *index,
2647 const git_checkout_options *opts)
2648 {
2649 int error, owned = 0;
2650 git_iterator *index_i;
2651
2652 if (!index && !repo) {
2653 giterr_set(GITERR_CHECKOUT,
2654 "must provide either repository or index to checkout");
2655 return -1;
2656 }
2657
2658 if (index && repo &&
2659 git_index_owner(index) &&
2660 git_index_owner(index) != repo) {
2661 giterr_set(GITERR_CHECKOUT,
2662 "index to checkout does not match repository");
2663 return -1;
2664 } else if(index && repo && !git_index_owner(index)) {
2665 GIT_REFCOUNT_OWN(index, repo);
2666 owned = 1;
2667 }
2668
2669 if (!repo)
2670 repo = git_index_owner(index);
2671
2672 if (!index && (error = git_repository_index__weakptr(&index, repo)) < 0)
2673 return error;
2674 GIT_REFCOUNT_INC(index);
2675
2676 if (!(error = git_iterator_for_index(&index_i, repo, index, NULL)))
2677 error = git_checkout_iterator(index_i, index, opts);
2678
2679 if (owned)
2680 GIT_REFCOUNT_OWN(index, NULL);
2681
2682 git_iterator_free(index_i);
2683 git_index_free(index);
2684
2685 return error;
2686 }
2687
2688 int git_checkout_tree(
2689 git_repository *repo,
2690 const git_object *treeish,
2691 const git_checkout_options *opts)
2692 {
2693 int error;
2694 git_index *index;
2695 git_tree *tree = NULL;
2696 git_iterator *tree_i = NULL;
2697 git_iterator_options iter_opts = GIT_ITERATOR_OPTIONS_INIT;
2698
2699 if (!treeish && !repo) {
2700 giterr_set(GITERR_CHECKOUT,
2701 "must provide either repository or tree to checkout");
2702 return -1;
2703 }
2704 if (treeish && repo && git_object_owner(treeish) != repo) {
2705 giterr_set(GITERR_CHECKOUT,
2706 "object to checkout does not match repository");
2707 return -1;
2708 }
2709
2710 if (!repo)
2711 repo = git_object_owner(treeish);
2712
2713 if (treeish) {
2714 if (git_object_peel((git_object **)&tree, treeish, GIT_OBJ_TREE) < 0) {
2715 giterr_set(
2716 GITERR_CHECKOUT, "provided object cannot be peeled to a tree");
2717 return -1;
2718 }
2719 }
2720 else {
2721 if ((error = checkout_lookup_head_tree(&tree, repo)) < 0) {
2722 if (error != GIT_EUNBORNBRANCH)
2723 giterr_set(
2724 GITERR_CHECKOUT,
2725 "HEAD could not be peeled to a tree and no treeish given");
2726 return error;
2727 }
2728 }
2729
2730 if ((error = git_repository_index(&index, repo)) < 0)
2731 return error;
2732
2733 if (opts && (opts->checkout_strategy & GIT_CHECKOUT_DISABLE_PATHSPEC_MATCH)) {
2734 iter_opts.pathlist.count = opts->paths.count;
2735 iter_opts.pathlist.strings = opts->paths.strings;
2736 }
2737
2738 if (!(error = git_iterator_for_tree(&tree_i, tree, &iter_opts)))
2739 error = git_checkout_iterator(tree_i, index, opts);
2740
2741 git_iterator_free(tree_i);
2742 git_index_free(index);
2743 git_tree_free(tree);
2744
2745 return error;
2746 }
2747
2748 int git_checkout_head(
2749 git_repository *repo,
2750 const git_checkout_options *opts)
2751 {
2752 assert(repo);
2753 return git_checkout_tree(repo, NULL, opts);
2754 }
2755
2756 int git_checkout_init_options(git_checkout_options *opts, unsigned int version)
2757 {
2758 GIT_INIT_STRUCTURE_FROM_TEMPLATE(
2759 opts, version, git_checkout_options, GIT_CHECKOUT_OPTIONS_INIT);
2760 return 0;
2761 }