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