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