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