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