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