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