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