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