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