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