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