]>
Commit | Line | Data |
---|---|---|
12dccc16 | 1 | #include "cache.h" |
8e440259 | 2 | #include "blob.h" |
cbd53a21 | 3 | #include "object-store.h" |
8ca12c0d | 4 | #include "dir.h" |
32a8f510 | 5 | #include "environment.h" |
f394e093 | 6 | #include "gettext.h" |
41771fa4 | 7 | #include "hex.h" |
dd8e9121 | 8 | #include "streaming.h" |
6d14eac3 | 9 | #include "submodule.h" |
cb2a5135 | 10 | #include "symlinks.h" |
52f1d62e | 11 | #include "progress.h" |
883e248b | 12 | #include "fsmonitor.h" |
d052cc03 | 13 | #include "entry.h" |
04155bda | 14 | #include "parallel-checkout.h" |
d5ebb50d | 15 | #include "wrapper.h" |
12dccc16 | 16 | |
81a9aa60 KB |
17 | static void create_directories(const char *path, int path_len, |
18 | const struct checkout *state) | |
12dccc16 | 19 | { |
3733e694 | 20 | char *buf = xmallocz(path_len); |
81a9aa60 KB |
21 | int len = 0; |
22 | ||
23 | while (len < path_len) { | |
24 | do { | |
25 | buf[len] = path[len]; | |
26 | len++; | |
27 | } while (len < path_len && path[len] != '/'); | |
28 | if (len >= path_len) | |
29 | break; | |
12dccc16 | 30 | buf[len] = 0; |
fa2e71c9 | 31 | |
bad4a54f KB |
32 | /* |
33 | * For 'checkout-index --prefix=<dir>', <dir> is | |
34 | * allowed to be a symlink to an existing directory, | |
35 | * and we set 'state->base_dir_len' below, such that | |
36 | * we test the path components of the prefix with the | |
37 | * stat() function instead of the lstat() function. | |
38 | */ | |
57199892 | 39 | if (has_dirs_only_path(buf, len, state->base_dir_len)) |
fa2e71c9 JH |
40 | continue; /* ok, it is already a directory. */ |
41 | ||
42 | /* | |
bad4a54f KB |
43 | * If this mkdir() would fail, it could be that there |
44 | * is already a symlink or something else exists | |
45 | * there, therefore we then try to unlink it and try | |
46 | * one more time to create the directory. | |
fa2e71c9 | 47 | */ |
f312de01 | 48 | if (mkdir(buf, 0777)) { |
fa2e71c9 | 49 | if (errno == EEXIST && state->force && |
691f1a28 | 50 | !unlink_or_warn(buf) && !mkdir(buf, 0777)) |
fa2e71c9 | 51 | continue; |
0721c314 | 52 | die_errno("cannot create directory at '%s'", buf); |
12dccc16 LT |
53 | } |
54 | } | |
55 | free(buf); | |
56 | } | |
57 | ||
2f29e0c6 | 58 | static void remove_subtree(struct strbuf *path) |
12dccc16 | 59 | { |
2f29e0c6 | 60 | DIR *dir = opendir(path->buf); |
12dccc16 | 61 | struct dirent *de; |
2f29e0c6 | 62 | int origlen = path->len; |
a6080a0a | 63 | |
12dccc16 | 64 | if (!dir) |
2f29e0c6 | 65 | die_errno("cannot opendir '%s'", path->buf); |
b548f0f1 | 66 | while ((de = readdir_skip_dot_and_dotdot(dir)) != NULL) { |
12dccc16 | 67 | struct stat st; |
2f29e0c6 | 68 | |
2f29e0c6 MH |
69 | strbuf_addch(path, '/'); |
70 | strbuf_addstr(path, de->d_name); | |
71 | if (lstat(path->buf, &st)) | |
72 | die_errno("cannot lstat '%s'", path->buf); | |
12dccc16 | 73 | if (S_ISDIR(st.st_mode)) |
2f29e0c6 MH |
74 | remove_subtree(path); |
75 | else if (unlink(path->buf)) | |
76 | die_errno("cannot unlink '%s'", path->buf); | |
77 | strbuf_setlen(path, origlen); | |
12dccc16 LT |
78 | } |
79 | closedir(dir); | |
2f29e0c6 MH |
80 | if (rmdir(path->buf)) |
81 | die_errno("cannot rmdir '%s'", path->buf); | |
12dccc16 LT |
82 | } |
83 | ||
d48a72f3 | 84 | static int create_file(const char *path, unsigned int mode) |
12dccc16 | 85 | { |
12dccc16 | 86 | mode = (mode & 0100) ? 0777 : 0666; |
781411ed | 87 | return open(path, O_WRONLY | O_CREAT | O_EXCL, mode); |
12dccc16 LT |
88 | } |
89 | ||
e9aa762c | 90 | void *read_blob_entry(const struct cache_entry *ce, size_t *size) |
f0807e62 LT |
91 | { |
92 | enum object_type type; | |
e9aa762c | 93 | unsigned long ul; |
bc726bd0 ÆAB |
94 | void *blob_data = repo_read_object_file(the_repository, &ce->oid, |
95 | &type, &ul); | |
f0807e62 | 96 | |
e9aa762c | 97 | *size = ul; |
d8f71807 | 98 | if (blob_data) { |
f0807e62 | 99 | if (type == OBJ_BLOB) |
d8f71807 BW |
100 | return blob_data; |
101 | free(blob_data); | |
f0807e62 LT |
102 | } |
103 | return NULL; | |
104 | } | |
105 | ||
9c5e6c80 | 106 | static int open_output_fd(char *path, const struct cache_entry *ce, int to_tempfile) |
fd5db55d JH |
107 | { |
108 | int symlink = (ce->ce_mode & S_IFMT) != S_IFREG; | |
109 | if (to_tempfile) { | |
330c8e26 JK |
110 | xsnprintf(path, TEMPORARY_FILENAME_LENGTH, "%s", |
111 | symlink ? ".merge_link_XXXXXX" : ".merge_file_XXXXXX"); | |
fd5db55d JH |
112 | return mkstemp(path); |
113 | } else { | |
114 | return create_file(path, !symlink ? ce->ce_mode : 0666); | |
115 | } | |
116 | } | |
117 | ||
49cfd903 | 118 | int fstat_checkout_output(int fd, const struct checkout *state, struct stat *st) |
fd5db55d JH |
119 | { |
120 | /* use fstat() only when path == ce->name */ | |
121 | if (fstat_is_reliable() && | |
122 | state->refresh_cache && !state->base_dir_len) { | |
35e6e212 | 123 | return !fstat(fd, st); |
fd5db55d JH |
124 | } |
125 | return 0; | |
126 | } | |
127 | ||
9c5e6c80 | 128 | static int streaming_write_entry(const struct cache_entry *ce, char *path, |
b6691092 | 129 | struct stream_filter *filter, |
dd8e9121 JH |
130 | const struct checkout *state, int to_tempfile, |
131 | int *fstat_done, struct stat *statbuf) | |
132 | { | |
d9c31e14 | 133 | int result = 0; |
47a02ff2 | 134 | int fd; |
dd8e9121 JH |
135 | |
136 | fd = open_output_fd(path, ce, to_tempfile); | |
d9c31e14 JK |
137 | if (fd < 0) |
138 | return -1; | |
139 | ||
7eda0e4f | 140 | result |= stream_blob_to_fd(fd, &ce->oid, filter, 1); |
49cfd903 | 141 | *fstat_done = fstat_checkout_output(fd, state, statbuf); |
d9c31e14 JK |
142 | result |= close(fd); |
143 | ||
144 | if (result) | |
dd8e9121 JH |
145 | unlink(path); |
146 | return result; | |
147 | } | |
148 | ||
2841e8f8 LS |
149 | void enable_delayed_checkout(struct checkout *state) |
150 | { | |
151 | if (!state->delayed_checkout) { | |
152 | state->delayed_checkout = xmalloc(sizeof(*state->delayed_checkout)); | |
153 | state->delayed_checkout->state = CE_CAN_DELAY; | |
bc40dfb1 ÆAB |
154 | string_list_init_nodup(&state->delayed_checkout->filters); |
155 | string_list_init_nodup(&state->delayed_checkout->paths); | |
2841e8f8 LS |
156 | } |
157 | } | |
158 | ||
159 | static int remove_available_paths(struct string_list_item *item, void *cb_data) | |
160 | { | |
161 | struct string_list *available_paths = cb_data; | |
162 | struct string_list_item *available; | |
163 | ||
164 | available = string_list_lookup(available_paths, item->string); | |
165 | if (available) | |
611c7785 | 166 | available->util = item->util; |
2841e8f8 LS |
167 | return !available; |
168 | } | |
169 | ||
611c7785 | 170 | int finish_delayed_checkout(struct checkout *state, int show_progress) |
2841e8f8 LS |
171 | { |
172 | int errs = 0; | |
bf6d819b | 173 | unsigned processed_paths = 0; |
52f1d62e | 174 | off_t filtered_bytes = 0; |
2841e8f8 | 175 | struct string_list_item *filter, *path; |
bf6d819b | 176 | struct progress *progress = NULL; |
2841e8f8 LS |
177 | struct delayed_checkout *dco = state->delayed_checkout; |
178 | ||
179 | if (!state->delayed_checkout) | |
180 | return errs; | |
181 | ||
182 | dco->state = CE_RETRY; | |
bf6d819b SG |
183 | if (show_progress) |
184 | progress = start_delayed_progress(_("Filtering content"), dco->paths.nr); | |
2841e8f8 LS |
185 | while (dco->filters.nr > 0) { |
186 | for_each_string_list_item(filter, &dco->filters) { | |
187 | struct string_list available_paths = STRING_LIST_INIT_NODUP; | |
188 | ||
189 | if (!async_query_available_blobs(filter->string, &available_paths)) { | |
190 | /* Filter reported an error */ | |
191 | errs = 1; | |
192 | filter->string = ""; | |
193 | continue; | |
194 | } | |
195 | if (available_paths.nr <= 0) { | |
196 | /* | |
197 | * Filter responded with no entries. That means | |
198 | * the filter is done and we can remove the | |
199 | * filter from the list (see | |
200 | * "string_list_remove_empty_items" call below). | |
201 | */ | |
202 | filter->string = ""; | |
203 | continue; | |
204 | } | |
205 | ||
206 | /* | |
207 | * In dco->paths we store a list of all delayed paths. | |
208 | * The filter just send us a list of available paths. | |
209 | * Remove them from the list. | |
210 | */ | |
211 | filter_string_list(&dco->paths, 0, | |
212 | &remove_available_paths, &available_paths); | |
213 | ||
214 | for_each_string_list_item(path, &available_paths) { | |
215 | struct cache_entry* ce; | |
216 | ||
217 | if (!path->util) { | |
218 | error("external filter '%s' signaled that '%s' " | |
219 | "is now available although it has not been " | |
220 | "delayed earlier", | |
221 | filter->string, path->string); | |
222 | errs |= 1; | |
223 | ||
224 | /* | |
225 | * Do not ask the filter for available blobs, | |
226 | * again, as the filter is likely buggy. | |
227 | */ | |
228 | filter->string = ""; | |
229 | continue; | |
230 | } | |
231 | ce = index_file_exists(state->istate, path->string, | |
232 | strlen(path->string), 0); | |
52f1d62e | 233 | if (ce) { |
bf6d819b | 234 | display_progress(progress, ++processed_paths); |
611c7785 | 235 | errs |= checkout_entry(ce, state, NULL, path->util); |
52f1d62e LS |
236 | filtered_bytes += ce->ce_stat_data.sd_size; |
237 | display_throughput(progress, filtered_bytes); | |
238 | } else | |
239 | errs = 1; | |
2841e8f8 LS |
240 | } |
241 | } | |
242 | string_list_remove_empty_items(&dco->filters, 0); | |
243 | } | |
52f1d62e | 244 | stop_progress(&progress); |
2841e8f8 LS |
245 | string_list_clear(&dco->filters, 0); |
246 | ||
247 | /* At this point we should not have any delayed paths anymore. */ | |
248 | errs |= dco->paths.nr; | |
249 | for_each_string_list_item(path, &dco->paths) { | |
250 | error("'%s' was not filtered properly", path->string); | |
251 | } | |
252 | string_list_clear(&dco->paths, 0); | |
253 | ||
254 | free(dco); | |
255 | state->delayed_checkout = NULL; | |
256 | ||
257 | return errs; | |
258 | } | |
259 | ||
584a0d13 MT |
260 | void update_ce_after_write(const struct checkout *state, struct cache_entry *ce, |
261 | struct stat *st) | |
262 | { | |
263 | if (state->refresh_cache) { | |
264 | assert(state->istate); | |
265 | fill_stat_cache_info(state->istate, ce, st); | |
266 | ce->ce_flags |= CE_UPDATE_IN_BASE; | |
267 | mark_fsmonitor_invalid(state->istate, ce); | |
268 | state->istate->cache_changed |= CE_ENTRY_CHANGED; | |
269 | } | |
270 | } | |
271 | ||
30419e7e MT |
272 | /* Note: ca is used (and required) iff the entry refers to a regular file. */ |
273 | static int write_entry(struct cache_entry *ce, char *path, struct conv_attrs *ca, | |
611c7785 MT |
274 | const struct checkout *state, int to_tempfile, |
275 | int *nr_checkouts) | |
12dccc16 | 276 | { |
4857c761 | 277 | unsigned int ce_mode_s_ifmt = ce->ce_mode & S_IFMT; |
c602d3a9 | 278 | struct delayed_checkout *dco = state->delayed_checkout; |
e4c72923 | 279 | int fd, ret, fstat_done = 0; |
d8f71807 | 280 | char *new_blob; |
4857c761 | 281 | struct strbuf buf = STRBUF_INIT; |
e9aa762c | 282 | size_t size; |
564bde9a JK |
283 | ssize_t wrote; |
284 | size_t newsize = 0; | |
e4c72923 | 285 | struct stat st; |
6d14eac3 | 286 | const struct submodule *sub; |
c397aac0 | 287 | struct checkout_metadata meta; |
611c7785 | 288 | static int scratch_nr_checkouts; |
c397aac0 | 289 | |
290 | clone_checkout_metadata(&meta, &state->meta, &ce->oid); | |
4857c761 | 291 | |
b6691092 | 292 | if (ce_mode_s_ifmt == S_IFREG) { |
30419e7e | 293 | struct stream_filter *filter = get_stream_filter_ca(ca, &ce->oid); |
b6691092 JH |
294 | if (filter && |
295 | !streaming_write_entry(ce, path, filter, | |
296 | state, to_tempfile, | |
297 | &fstat_done, &st)) | |
298 | goto finish; | |
299 | } | |
dd8e9121 | 300 | |
4857c761 | 301 | switch (ce_mode_s_ifmt) { |
4857c761 | 302 | case S_IFLNK: |
d8f71807 BW |
303 | new_blob = read_blob_entry(ce, &size); |
304 | if (!new_blob) | |
d43e9073 | 305 | return error("unable to read sha1 file of %s (%s)", |
9334ea8e | 306 | ce->name, oid_to_hex(&ce->oid)); |
1a9d7e9b | 307 | |
7cbbf9d6 JK |
308 | /* |
309 | * We can't make a real symlink; write out a regular file entry | |
310 | * with the symlink destination as its contents. | |
311 | */ | |
312 | if (!has_symlinks || to_tempfile) | |
313 | goto write_file_entry; | |
314 | ||
d8f71807 BW |
315 | ret = symlink(new_blob, path); |
316 | free(new_blob); | |
7cbbf9d6 JK |
317 | if (ret) |
318 | return error_errno("unable to create symlink %s", path); | |
319 | break; | |
320 | ||
321 | case S_IFREG: | |
c602d3a9 JK |
322 | /* |
323 | * We do not send the blob in case of a retry, so do not | |
324 | * bother reading it at all. | |
325 | */ | |
7cbbf9d6 | 326 | if (dco && dco->state == CE_RETRY) { |
d8f71807 | 327 | new_blob = NULL; |
c602d3a9 JK |
328 | size = 0; |
329 | } else { | |
d8f71807 BW |
330 | new_blob = read_blob_entry(ce, &size); |
331 | if (!new_blob) | |
c602d3a9 | 332 | return error("unable to read sha1 file of %s (%s)", |
9334ea8e | 333 | ce->name, oid_to_hex(&ce->oid)); |
4857c761 KB |
334 | } |
335 | ||
1a9d7e9b JH |
336 | /* |
337 | * Convert from git internal format to working tree format | |
338 | */ | |
7cbbf9d6 | 339 | if (dco && dco->state != CE_NO_DELAY) { |
30419e7e MT |
340 | ret = async_convert_to_working_tree_ca(ca, ce->name, |
341 | new_blob, size, | |
342 | &buf, &meta, dco); | |
611c7785 MT |
343 | if (ret) { |
344 | struct string_list_item *item = | |
345 | string_list_lookup(&dco->paths, ce->name); | |
346 | if (item) { | |
347 | item->util = nr_checkouts ? nr_checkouts | |
348 | : &scratch_nr_checkouts; | |
349 | free(new_blob); | |
350 | goto delayed; | |
351 | } | |
2841e8f8 | 352 | } |
30419e7e MT |
353 | } else { |
354 | ret = convert_to_working_tree_ca(ca, ce->name, new_blob, | |
355 | size, &buf, &meta); | |
356 | } | |
7cbbf9d6 JK |
357 | |
358 | if (ret) { | |
d8f71807 BW |
359 | free(new_blob); |
360 | new_blob = strbuf_detach(&buf, &newsize); | |
7cbbf9d6 | 361 | size = newsize; |
1a9d7e9b | 362 | } |
7cbbf9d6 JK |
363 | /* |
364 | * No "else" here as errors from convert are OK at this | |
365 | * point. If the error would have been fatal (e.g. | |
366 | * filter is required), then we would have died already. | |
367 | */ | |
1a9d7e9b | 368 | |
7cbbf9d6 | 369 | write_file_entry: |
fd5db55d | 370 | fd = open_output_fd(path, ce, to_tempfile); |
12dccc16 | 371 | if (fd < 0) { |
d8f71807 | 372 | free(new_blob); |
e1ebb3c2 | 373 | return error_errno("unable to create file %s", path); |
12dccc16 | 374 | } |
6c510bee | 375 | |
d8f71807 | 376 | wrote = write_in_full(fd, new_blob, size); |
fd5db55d | 377 | if (!to_tempfile) |
49cfd903 | 378 | fstat_done = fstat_checkout_output(fd, state, &st); |
12dccc16 | 379 | close(fd); |
d8f71807 | 380 | free(new_blob); |
564bde9a | 381 | if (wrote < 0) |
d43e9073 | 382 | return error("unable to write file %s", path); |
12dccc16 | 383 | break; |
7cbbf9d6 | 384 | |
302b9282 | 385 | case S_IFGITLINK: |
f0807e62 | 386 | if (to_tempfile) |
9334ea8e | 387 | return error("cannot create temporary submodule %s", ce->name); |
f0807e62 | 388 | if (mkdir(path, 0777) < 0) |
42063f95 | 389 | return error("cannot create submodule directory %s", path); |
6d14eac3 SB |
390 | sub = submodule_from_ce(ce); |
391 | if (sub) | |
4002ec3d | 392 | return submodule_move_head(ce->name, state->super_prefix, |
cd279e2e SB |
393 | NULL, oid_to_hex(&ce->oid), |
394 | state->force ? SUBMODULE_MOVE_HEAD_FORCE : 0); | |
f0807e62 | 395 | break; |
7cbbf9d6 | 396 | |
12dccc16 | 397 | default: |
9334ea8e | 398 | return error("unknown file mode for %s in index", ce->name); |
12dccc16 LT |
399 | } |
400 | ||
dd8e9121 | 401 | finish: |
6ee67f26 | 402 | if (state->refresh_cache) { |
584a0d13 MT |
403 | if (!fstat_done && lstat(ce->name, &st) < 0) |
404 | return error_errno("unable to stat just-written file %s", | |
405 | ce->name); | |
406 | update_ce_after_write(state, ce , &st); | |
12dccc16 | 407 | } |
611c7785 MT |
408 | if (nr_checkouts) |
409 | (*nr_checkouts)++; | |
03b95333 | 410 | delayed: |
12dccc16 LT |
411 | return 0; |
412 | } | |
413 | ||
b6986d8a LT |
414 | /* |
415 | * This is like 'lstat()', except it refuses to follow symlinks | |
da02ca50 | 416 | * in the path, after skipping "skiplen". |
b6986d8a | 417 | */ |
61b97df7 | 418 | static int check_path(const char *path, int len, struct stat *st, int skiplen) |
b6986d8a | 419 | { |
da02ca50 JH |
420 | const char *slash = path + len; |
421 | ||
422 | while (path < slash && *slash != '/') | |
423 | slash--; | |
424 | if (!has_dirs_only_path(path, slash - path, skiplen)) { | |
b6986d8a LT |
425 | errno = ENOENT; |
426 | return -1; | |
427 | } | |
428 | return lstat(path, st); | |
429 | } | |
430 | ||
b878579a NTND |
431 | static void mark_colliding_entries(const struct checkout *state, |
432 | struct cache_entry *ce, struct stat *st) | |
433 | { | |
434 | int i, trust_ino = check_stat; | |
435 | ||
e66ceca9 | 436 | #if defined(GIT_WINDOWS_NATIVE) || defined(__CYGWIN__) |
b878579a NTND |
437 | trust_ino = 0; |
438 | #endif | |
439 | ||
440 | ce->ce_flags |= CE_MATCHED; | |
441 | ||
3450a304 DS |
442 | /* TODO: audit for interaction with sparse-index. */ |
443 | ensure_full_index(state->istate); | |
b878579a NTND |
444 | for (i = 0; i < state->istate->cache_nr; i++) { |
445 | struct cache_entry *dup = state->istate->cache[i]; | |
446 | ||
04155bda MT |
447 | if (dup == ce) { |
448 | /* | |
449 | * Parallel checkout doesn't create the files in index | |
450 | * order. So the other side of the collision may appear | |
451 | * after the given cache_entry in the array. | |
452 | */ | |
453 | if (parallel_checkout_status() == PC_RUNNING) | |
454 | continue; | |
455 | else | |
456 | break; | |
457 | } | |
b878579a NTND |
458 | |
459 | if (dup->ce_flags & (CE_MATCHED | CE_VALID | CE_SKIP_WORKTREE)) | |
460 | continue; | |
461 | ||
e66ceca9 | 462 | if ((trust_ino && !match_stat_data(&dup->ce_stat_data, st)) || |
b878579a NTND |
463 | (!trust_ino && !fspathcmp(ce->name, dup->name))) { |
464 | dup->ce_flags |= CE_MATCHED; | |
465 | break; | |
466 | } | |
467 | } | |
468 | } | |
469 | ||
ae22751f MT |
470 | int checkout_entry_ca(struct cache_entry *ce, struct conv_attrs *ca, |
471 | const struct checkout *state, char *topath, | |
472 | int *nr_checkouts) | |
12dccc16 | 473 | { |
f63272a3 | 474 | static struct strbuf path = STRBUF_INIT; |
de84f99c | 475 | struct stat st; |
ae22751f | 476 | struct conv_attrs ca_buf; |
12dccc16 | 477 | |
536ec183 TG |
478 | if (ce->ce_flags & CE_WT_REMOVE) { |
479 | if (topath) | |
480 | /* | |
481 | * No content and thus no path to create, so we have | |
482 | * no pathname to return. | |
483 | */ | |
484 | BUG("Can't remove entry to a path"); | |
4002ec3d | 485 | unlink_entry(ce, state->super_prefix); |
536ec183 TG |
486 | return 0; |
487 | } | |
488 | ||
30419e7e | 489 | if (topath) { |
ae22751f | 490 | if (S_ISREG(ce->ce_mode) && !ca) { |
30419e7e MT |
491 | convert_attrs(state->istate, &ca_buf, ce->name); |
492 | ca = &ca_buf; | |
493 | } | |
611c7785 | 494 | return write_entry(ce, topath, ca, state, 1, nr_checkouts); |
30419e7e | 495 | } |
de84f99c | 496 | |
f63272a3 MH |
497 | strbuf_reset(&path); |
498 | strbuf_add(&path, state->base_dir, state->base_dir_len); | |
499 | strbuf_add(&path, ce->name, ce_namelen(ce)); | |
12dccc16 | 500 | |
f63272a3 | 501 | if (!check_path(path.buf, path.len, &st, state->base_dir_len)) { |
6d14eac3 | 502 | const struct submodule *sub; |
74cfc0ee NTND |
503 | unsigned changed = ie_match_stat(state->istate, ce, &st, |
504 | CE_MATCH_IGNORE_VALID | CE_MATCH_IGNORE_SKIP_WORKTREE); | |
6d14eac3 SB |
505 | /* |
506 | * Needs to be checked before !changed returns early, | |
507 | * as the possibly empty directory was not changed | |
508 | */ | |
509 | sub = submodule_from_ce(ce); | |
510 | if (sub) { | |
511 | int err; | |
512 | if (!is_submodule_populated_gently(ce->name, &err)) { | |
513 | struct stat sb; | |
514 | if (lstat(ce->name, &sb)) | |
515 | die(_("could not stat file '%s'"), ce->name); | |
516 | if (!(st.st_mode & S_IFDIR)) | |
517 | unlink_or_warn(ce->name); | |
518 | ||
4002ec3d | 519 | return submodule_move_head(ce->name, state->super_prefix, |
cd279e2e | 520 | NULL, oid_to_hex(&ce->oid), 0); |
6d14eac3 | 521 | } else |
4002ec3d | 522 | return submodule_move_head(ce->name, state->super_prefix, |
6d14eac3 | 523 | "HEAD", oid_to_hex(&ce->oid), |
cd279e2e | 524 | state->force ? SUBMODULE_MOVE_HEAD_FORCE : 0); |
6d14eac3 SB |
525 | } |
526 | ||
12dccc16 LT |
527 | if (!changed) |
528 | return 0; | |
529 | if (!state->force) { | |
530 | if (!state->quiet) | |
f63272a3 MH |
531 | fprintf(stderr, |
532 | "%s already exists, no checkout\n", | |
533 | path.buf); | |
4b12dae6 | 534 | return -1; |
12dccc16 LT |
535 | } |
536 | ||
b878579a NTND |
537 | if (state->clone) |
538 | mark_colliding_entries(state, ce, &st); | |
539 | ||
12dccc16 LT |
540 | /* |
541 | * We unlink the old file, to get the new one with the | |
542 | * right permissions (including umask, which is nasty | |
543 | * to emulate by hand - much easier to let the system | |
544 | * just do the right thing) | |
545 | */ | |
d48a72f3 | 546 | if (S_ISDIR(st.st_mode)) { |
f0807e62 | 547 | /* If it is a gitlink, leave it alone! */ |
7a51ed66 | 548 | if (S_ISGITLINK(ce->ce_mode)) |
f0807e62 | 549 | return 0; |
2f29e0c6 | 550 | remove_subtree(&path); |
f63272a3 | 551 | } else if (unlink(path.buf)) |
e1ebb3c2 | 552 | return error_errno("unable to unlink old '%s'", path.buf); |
de84f99c | 553 | } else if (state->not_new) |
12dccc16 | 554 | return 0; |
f63272a3 MH |
555 | |
556 | create_directories(path.buf, path.len, state); | |
30419e7e | 557 | |
ae22751f | 558 | if (S_ISREG(ce->ce_mode) && !ca) { |
30419e7e MT |
559 | convert_attrs(state->istate, &ca_buf, ce->name); |
560 | ca = &ca_buf; | |
561 | } | |
562 | ||
611c7785 | 563 | if (!enqueue_checkout(ce, ca, nr_checkouts)) |
04155bda MT |
564 | return 0; |
565 | ||
611c7785 | 566 | return write_entry(ce, path.buf, ca, state, 0, nr_checkouts); |
12dccc16 | 567 | } |
b702dd12 | 568 | |
4002ec3d | 569 | void unlink_entry(const struct cache_entry *ce, const char *super_prefix) |
b702dd12 TG |
570 | { |
571 | const struct submodule *sub = submodule_from_ce(ce); | |
572 | if (sub) { | |
573 | /* state.force is set at the caller. */ | |
4002ec3d | 574 | submodule_move_head(ce->name, super_prefix, "HEAD", NULL, |
b702dd12 TG |
575 | SUBMODULE_MOVE_HEAD_FORCE); |
576 | } | |
fab78a0c | 577 | if (check_leading_path(ce->name, ce_namelen(ce), 1) >= 0) |
b702dd12 TG |
578 | return; |
579 | if (remove_or_warn(ce->ce_mode, ce->name)) | |
580 | return; | |
581 | schedule_dir_for_removal(ce->name, ce_namelen(ce)); | |
582 | } |