]>
Commit | Line | Data |
---|---|---|
33db5f4d LT |
1 | /* |
2 | * Check-out files from the "current cache directory" | |
3 | * | |
4 | * Copyright (C) 2005 Linus Torvalds | |
5 | * | |
33db5f4d | 6 | */ |
07047d68 | 7 | #define USE_THE_INDEX_VARIABLE |
baffc0e7 | 8 | #include "builtin.h" |
b2141fc1 | 9 | #include "config.h" |
88078f54 | 10 | #include "dir.h" |
f394e093 | 11 | #include "gettext.h" |
697cc8ef | 12 | #include "lockfile.h" |
9debe63d | 13 | #include "quote.h" |
bad68ec9 | 14 | #include "cache-tree.h" |
a6c7db1b | 15 | #include "parse-options.h" |
d052cc03 | 16 | #include "entry.h" |
70b052b2 | 17 | #include "parallel-checkout.h" |
33db5f4d | 18 | |
de84f99c | 19 | #define CHECKOUT_ALL 4 |
a392f57d | 20 | static int nul_term_line; |
3bd348ae | 21 | static int checkout_stage; /* default to checkout stage0 */ |
88078f54 | 22 | static int ignore_skip_worktree; /* default to 0 */ |
de84f99c | 23 | static int to_tempfile; |
af2a651d | 24 | static char topath[4][TEMPORARY_FILENAME_LENGTH + 1]; |
c3e9a653 | 25 | |
68e3d629 | 26 | static struct checkout state = CHECKOUT_INIT; |
33db5f4d | 27 | |
74c4de58 | 28 | static void write_tempfile_record(const char *name, const char *prefix) |
de84f99c SP |
29 | { |
30 | int i; | |
3f7ba603 | 31 | int have_tempname = 0; |
de84f99c SP |
32 | |
33 | if (CHECKOUT_ALL == checkout_stage) { | |
3f7ba603 MT |
34 | for (i = 1; i < 4; i++) |
35 | if (topath[i][0]) { | |
36 | have_tempname = 1; | |
37 | break; | |
38 | } | |
39 | ||
40 | if (have_tempname) { | |
41 | for (i = 1; i < 4; i++) { | |
42 | if (i > 1) | |
43 | putchar(' '); | |
44 | if (topath[i][0]) | |
45 | fputs(topath[i], stdout); | |
46 | else | |
47 | putchar('.'); | |
48 | } | |
de84f99c | 49 | } |
3f7ba603 MT |
50 | } else if (topath[checkout_stage][0]) { |
51 | have_tempname = 1; | |
de84f99c | 52 | fputs(topath[checkout_stage], stdout); |
3f7ba603 | 53 | } |
de84f99c | 54 | |
3f7ba603 MT |
55 | if (have_tempname) { |
56 | putchar('\t'); | |
57 | write_name_quoted_relative(name, prefix, stdout, | |
58 | nul_term_line ? '\0' : '\n'); | |
59 | } | |
de84f99c SP |
60 | |
61 | for (i = 0; i < 4; i++) { | |
62 | topath[i][0] = 0; | |
63 | } | |
64 | } | |
65 | ||
74c4de58 | 66 | static int checkout_file(const char *name, const char *prefix) |
33db5f4d | 67 | { |
3bd348ae | 68 | int namelen = strlen(name); |
07047d68 | 69 | int pos = index_name_pos(&the_index, name, namelen); |
3bd348ae | 70 | int has_same_name = 0; |
35682ada | 71 | int is_file = 0; |
88078f54 | 72 | int is_skipped = 1; |
de84f99c SP |
73 | int did_checkout = 0; |
74 | int errs = 0; | |
3bd348ae JH |
75 | |
76 | if (pos < 0) | |
77 | pos = -pos - 1; | |
78 | ||
dc594180 ÆAB |
79 | while (pos < the_index.cache_nr) { |
80 | struct cache_entry *ce = the_index.cache[pos]; | |
f4f9adae | 81 | if (ce_namelen(ce) != namelen || |
3bd348ae JH |
82 | memcmp(ce->name, name, namelen)) |
83 | break; | |
84 | has_same_name = 1; | |
3bd348ae | 85 | pos++; |
35682ada VD |
86 | if (S_ISSPARSEDIR(ce->ce_mode)) |
87 | break; | |
88 | is_file = 1; | |
88078f54 VD |
89 | if (!ignore_skip_worktree && ce_skip_worktree(ce)) |
90 | break; | |
91 | is_skipped = 0; | |
de84f99c SP |
92 | if (ce_stage(ce) != checkout_stage |
93 | && (CHECKOUT_ALL != checkout_stage || !ce_stage(ce))) | |
94 | continue; | |
95 | did_checkout = 1; | |
96 | if (checkout_entry(ce, &state, | |
0f086e6d NTND |
97 | to_tempfile ? topath[ce_stage(ce)] : NULL, |
98 | NULL) < 0) | |
de84f99c SP |
99 | errs++; |
100 | } | |
101 | ||
102 | if (did_checkout) { | |
103 | if (to_tempfile) | |
74c4de58 | 104 | write_tempfile_record(name, prefix); |
de84f99c | 105 | return errs > 0 ? -1 : 0; |
33db5f4d | 106 | } |
3bd348ae | 107 | |
0b809c82 JK |
108 | /* |
109 | * At this point we know we didn't try to check anything out. If it was | |
110 | * because we did find an entry but it was stage 0, that's not an | |
111 | * error. | |
112 | */ | |
113 | if (has_same_name && checkout_stage == CHECKOUT_ALL) | |
114 | return 0; | |
115 | ||
3bd348ae | 116 | if (!state.quiet) { |
05207a28 | 117 | fprintf(stderr, "git checkout-index: %s ", name); |
3bd348ae JH |
118 | if (!has_same_name) |
119 | fprintf(stderr, "is not in the cache"); | |
35682ada VD |
120 | else if (!is_file) |
121 | fprintf(stderr, "is a sparse directory"); | |
88078f54 VD |
122 | else if (is_skipped) |
123 | fprintf(stderr, "has skip-worktree enabled; " | |
124 | "use '--ignore-skip-worktree-bits' to checkout"); | |
3bd348ae JH |
125 | else if (checkout_stage) |
126 | fprintf(stderr, "does not exist at stage %d", | |
127 | checkout_stage); | |
128 | else | |
129 | fprintf(stderr, "is unmerged"); | |
130 | fputc('\n', stderr); | |
131 | } | |
132 | return -1; | |
33db5f4d LT |
133 | } |
134 | ||
70b052b2 | 135 | static int checkout_all(const char *prefix, int prefix_length) |
33db5f4d | 136 | { |
4b12dae6 | 137 | int i, errs = 0; |
4b25d091 | 138 | struct cache_entry *last_ce = NULL; |
33db5f4d | 139 | |
dc594180 ÆAB |
140 | for (i = 0; i < the_index.cache_nr ; i++) { |
141 | struct cache_entry *ce = the_index.cache[i]; | |
35682ada VD |
142 | |
143 | if (S_ISSPARSEDIR(ce->ce_mode)) { | |
144 | if (!ce_skip_worktree(ce)) | |
145 | BUG("sparse directory '%s' does not have skip-worktree set", ce->name); | |
146 | ||
147 | /* | |
148 | * If the current entry is a sparse directory and skip-worktree | |
149 | * entries are being checked out, expand the index and continue | |
150 | * the loop on the current index position (now pointing to the | |
151 | * first entry inside the expanded sparse directory). | |
152 | */ | |
153 | if (ignore_skip_worktree) { | |
154 | ensure_full_index(&the_index); | |
dc594180 | 155 | ce = the_index.cache[i]; |
35682ada VD |
156 | } |
157 | } | |
158 | ||
88078f54 VD |
159 | if (!ignore_skip_worktree && ce_skip_worktree(ce)) |
160 | continue; | |
de84f99c SP |
161 | if (ce_stage(ce) != checkout_stage |
162 | && (CHECKOUT_ALL != checkout_stage || !ce_stage(ce))) | |
d9f98eeb | 163 | continue; |
c3e9a653 | 164 | if (prefix && *prefix && |
3bd348ae JH |
165 | (ce_namelen(ce) <= prefix_length || |
166 | memcmp(prefix, ce->name, prefix_length))) | |
c3e9a653 | 167 | continue; |
de84f99c SP |
168 | if (last_ce && to_tempfile) { |
169 | if (ce_namelen(last_ce) != ce_namelen(ce) | |
170 | || memcmp(last_ce->name, ce->name, ce_namelen(ce))) | |
74c4de58 | 171 | write_tempfile_record(last_ce->name, prefix); |
de84f99c SP |
172 | } |
173 | if (checkout_entry(ce, &state, | |
0f086e6d NTND |
174 | to_tempfile ? topath[ce_stage(ce)] : NULL, |
175 | NULL) < 0) | |
4b12dae6 | 176 | errs++; |
de84f99c | 177 | last_ce = ce; |
33db5f4d | 178 | } |
de84f99c | 179 | if (last_ce && to_tempfile) |
74c4de58 | 180 | write_tempfile_record(last_ce->name, prefix); |
70b052b2 | 181 | return !!errs; |
33db5f4d LT |
182 | } |
183 | ||
a6c7db1b | 184 | static const char * const builtin_checkout_index_usage[] = { |
9c9b4f2f | 185 | N_("git checkout-index [<options>] [--] [<file>...]"), |
a6c7db1b MV |
186 | NULL |
187 | }; | |
d46ad9c9 | 188 | |
a6c7db1b MV |
189 | static int option_parse_stage(const struct option *opt, |
190 | const char *arg, int unset) | |
191 | { | |
517fe807 JK |
192 | BUG_ON_OPT_NEG(unset); |
193 | ||
a6c7db1b MV |
194 | if (!strcmp(arg, "all")) { |
195 | to_tempfile = 1; | |
196 | checkout_stage = CHECKOUT_ALL; | |
197 | } else { | |
198 | int ch = arg[0]; | |
199 | if ('1' <= ch && ch <= '3') | |
200 | checkout_stage = arg[0] - '0'; | |
201 | else | |
22396175 | 202 | die(_("stage should be between 1 and 3 or all")); |
a6c7db1b MV |
203 | } |
204 | return 0; | |
205 | } | |
206 | ||
e414156a | 207 | int cmd_checkout_index(int argc, const char **argv, const char *prefix) |
33db5f4d | 208 | { |
a65a686f | 209 | int i; |
02ae242f | 210 | struct lock_file lock_file = LOCK_INIT; |
a65a686f | 211 | int all = 0; |
9debe63d | 212 | int read_from_stdin = 0; |
e414156a | 213 | int prefix_length; |
a6c7db1b | 214 | int force = 0, quiet = 0, not_new = 0; |
6a6df8aa | 215 | int index_opt = 0; |
7e410615 | 216 | int err = 0; |
70b052b2 | 217 | int pc_workers, pc_threshold; |
a6c7db1b | 218 | struct option builtin_checkout_index_options[] = { |
d5d09d47 | 219 | OPT_BOOL('a', "all", &all, |
f63cf8c9 | 220 | N_("check out all files in the index")), |
88078f54 VD |
221 | OPT_BOOL(0, "ignore-skip-worktree-bits", &ignore_skip_worktree, |
222 | N_("do not skip files with skip-worktree set")), | |
1224781d | 223 | OPT__FORCE(&force, N_("force overwrite of existing files"), 0), |
8c839683 | 224 | OPT__QUIET(&quiet, |
0ed21718 | 225 | N_("no warning for existing files and files not in index")), |
5d4d1440 | 226 | OPT_BOOL('n', "no-create", ¬_new, |
0ed21718 | 227 | N_("don't checkout new files")), |
6a6df8aa JK |
228 | OPT_BOOL('u', "index", &index_opt, |
229 | N_("update stat information in the index file")), | |
9096ee16 JK |
230 | OPT_BOOL('z', NULL, &nul_term_line, |
231 | N_("paths are separated with NUL character")), | |
d5d09d47 | 232 | OPT_BOOL(0, "stdin", &read_from_stdin, |
0ed21718 | 233 | N_("read list of paths from the standard input")), |
d5d09d47 | 234 | OPT_BOOL(0, "temp", &to_tempfile, |
0ed21718 | 235 | N_("write the content to temporary files")), |
5ed5f671 JK |
236 | OPT_STRING(0, "prefix", &state.base_dir, N_("string"), |
237 | N_("when creating files, prepend <string>")), | |
203c8533 | 238 | OPT_CALLBACK_F(0, "stage", NULL, "(1|2|3|all)", |
0ed21718 | 239 | N_("copy out the files from named stage"), |
203c8533 | 240 | PARSE_OPT_NONEG, option_parse_stage), |
a6c7db1b MV |
241 | OPT_END() |
242 | }; | |
33db5f4d | 243 | |
cf9d52e4 NTND |
244 | if (argc == 2 && !strcmp(argv[1], "-h")) |
245 | usage_with_options(builtin_checkout_index_usage, | |
246 | builtin_checkout_index_options); | |
ef90d6d4 | 247 | git_config(git_default_config, NULL); |
c3e9a653 JH |
248 | prefix_length = prefix ? strlen(prefix) : 0; |
249 | ||
35682ada VD |
250 | prepare_repo_settings(the_repository); |
251 | the_repository->settings.command_requires_full_index = 0; | |
252 | ||
07047d68 | 253 | if (repo_read_index(the_repository) < 0) { |
2de381f9 | 254 | die("invalid cache"); |
33db5f4d LT |
255 | } |
256 | ||
37782920 | 257 | argc = parse_options(argc, argv, prefix, builtin_checkout_index_options, |
a6c7db1b | 258 | builtin_checkout_index_usage, 0); |
74cfc0ee | 259 | state.istate = &the_index; |
a6c7db1b MV |
260 | state.force = force; |
261 | state.quiet = quiet; | |
262 | state.not_new = not_new; | |
a65a686f | 263 | |
5ed5f671 JK |
264 | if (!state.base_dir) |
265 | state.base_dir = ""; | |
266 | state.base_dir_len = strlen(state.base_dir); | |
267 | ||
6a6df8aa JK |
268 | /* |
269 | * when --prefix is specified we do not want to update cache. | |
270 | */ | |
271 | if (index_opt && !state.base_dir_len && !to_tempfile) { | |
272 | state.refresh_cache = 1; | |
273 | state.istate = &the_index; | |
07047d68 ÆAB |
274 | repo_hold_locked_index(the_repository, &lock_file, |
275 | LOCK_DIE_ON_ERROR); | |
a65a686f LT |
276 | } |
277 | ||
70b052b2 MT |
278 | get_parallel_checkout_configs(&pc_workers, &pc_threshold); |
279 | if (pc_workers > 1) | |
280 | init_parallel_checkout(); | |
281 | ||
a65a686f | 282 | /* Check out named files first */ |
a6c7db1b | 283 | for (i = 0; i < argc; i++) { |
a65a686f | 284 | const char *arg = argv[i]; |
d7a643b7 | 285 | char *p; |
a65a686f LT |
286 | |
287 | if (all) | |
7e44c935 | 288 | die("git checkout-index: don't mix '--all' and explicit filenames"); |
9debe63d | 289 | if (read_from_stdin) |
7e44c935 | 290 | die("git checkout-index: don't mix '--stdin' and explicit filenames"); |
dc46da22 | 291 | p = prefix_path(prefix, prefix_length, arg); |
7e410615 | 292 | err |= checkout_file(p, prefix); |
d7a643b7 | 293 | free(p); |
33db5f4d | 294 | } |
415e96c8 | 295 | |
9debe63d | 296 | if (read_from_stdin) { |
0d4cc1b4 JK |
297 | struct strbuf buf = STRBUF_INIT; |
298 | struct strbuf unquoted = STRBUF_INIT; | |
a392f57d | 299 | strbuf_getline_fn getline_fn; |
7fb1011e | 300 | |
9debe63d | 301 | if (all) |
7e44c935 | 302 | die("git checkout-index: don't mix '--all' and '--stdin'"); |
7fb1011e | 303 | |
a392f57d JH |
304 | getline_fn = nul_term_line ? strbuf_getline_nul : strbuf_getline_lf; |
305 | while (getline_fn(&buf, stdin) != EOF) { | |
d7a643b7 | 306 | char *p; |
a392f57d | 307 | if (!nul_term_line && buf.buf[0] == '"') { |
0d4cc1b4 JK |
308 | strbuf_reset(&unquoted); |
309 | if (unquote_c_style(&unquoted, buf.buf, NULL)) | |
7fb1011e | 310 | die("line is badly quoted"); |
0d4cc1b4 | 311 | strbuf_swap(&buf, &unquoted); |
7fb1011e PH |
312 | } |
313 | p = prefix_path(prefix, prefix_length, buf.buf); | |
7e410615 | 314 | err |= checkout_file(p, prefix); |
d7a643b7 | 315 | free(p); |
9debe63d | 316 | } |
0d4cc1b4 | 317 | strbuf_release(&unquoted); |
e6c019d0 | 318 | strbuf_release(&buf); |
9debe63d SP |
319 | } |
320 | ||
70b052b2 MT |
321 | if (all) |
322 | err |= checkout_all(prefix, prefix_length); | |
323 | ||
324 | if (pc_workers > 1) | |
325 | err |= run_parallel_checkout(&state, pc_workers, pc_threshold, | |
326 | NULL, NULL); | |
327 | ||
7e410615 JK |
328 | if (err) |
329 | return 1; | |
330 | ||
02ae242f | 331 | if (is_lock_file_locked(&lock_file) && |
03b86647 | 332 | write_locked_index(&the_index, &lock_file, COMMIT_LOCK)) |
021b6e45 | 333 | die("Unable to write new index file"); |
33db5f4d LT |
334 | return 0; |
335 | } |