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