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