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