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