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