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