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