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