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