Start the 2.48 cycle
[git/gitster.git] / builtin / checkout-index.c
blob6dd38eb05d49d6de451a063756b950122c626d21
1 /*
2 * Check-out files from the "current cache directory"
4 * Copyright (C) 2005 Linus Torvalds
6 */
7 #define USE_THE_REPOSITORY_VARIABLE
8 #include "builtin.h"
9 #include "config.h"
10 #include "gettext.h"
11 #include "lockfile.h"
12 #include "quote.h"
13 #include "cache-tree.h"
14 #include "parse-options.h"
15 #include "entry.h"
16 #include "parallel-checkout.h"
17 #include "read-cache-ll.h"
18 #include "setup.h"
19 #include "sparse-index.h"
21 #define CHECKOUT_ALL 4
22 static int nul_term_line;
23 static int checkout_stage; /* default to checkout stage0 */
24 static int ignore_skip_worktree; /* default to 0 */
25 static int to_tempfile = -1;
26 static char topath[4][TEMPORARY_FILENAME_LENGTH + 1];
28 static struct checkout state = CHECKOUT_INIT;
30 static void write_tempfile_record(const char *name, const char *prefix)
32 int i;
33 int have_tempname = 0;
35 if (CHECKOUT_ALL == checkout_stage) {
36 for (i = 1; i < 4; i++)
37 if (topath[i][0]) {
38 have_tempname = 1;
39 break;
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('.');
52 } else if (topath[checkout_stage][0]) {
53 have_tempname = 1;
54 fputs(topath[checkout_stage], stdout);
57 if (have_tempname) {
58 putchar('\t');
59 write_name_quoted_relative(name, prefix, stdout,
60 nul_term_line ? '\0' : '\n');
63 for (i = 0; i < 4; i++) {
64 topath[i][0] = 0;
68 static int checkout_file(const char *name, const char *prefix)
70 int namelen = strlen(name);
71 int pos = index_name_pos(the_repository->index, name, namelen);
72 int has_same_name = 0;
73 int is_file = 0;
74 int is_skipped = 1;
75 int did_checkout = 0;
76 int errs = 0;
78 if (pos < 0)
79 pos = -pos - 1;
81 while (pos <the_repository->index->cache_nr) {
82 struct cache_entry *ce =the_repository->index->cache[pos];
83 if (ce_namelen(ce) != namelen ||
84 memcmp(ce->name, name, namelen))
85 break;
86 has_same_name = 1;
87 pos++;
88 if (S_ISSPARSEDIR(ce->ce_mode))
89 break;
90 is_file = 1;
91 if (!ignore_skip_worktree && ce_skip_worktree(ce))
92 break;
93 is_skipped = 0;
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,
99 to_tempfile ? topath[ce_stage(ce)] : NULL,
100 NULL) < 0)
101 errs++;
104 if (did_checkout) {
105 if (to_tempfile)
106 write_tempfile_record(name, prefix);
107 return errs > 0 ? -1 : 0;
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.
115 if (has_same_name && checkout_stage == CHECKOUT_ALL)
116 return 0;
118 if (!state.quiet) {
119 fprintf(stderr, "git checkout-index: %s ", name);
120 if (!has_same_name)
121 fprintf(stderr, "is not in the cache");
122 else if (!is_file)
123 fprintf(stderr, "is a sparse directory");
124 else if (is_skipped)
125 fprintf(stderr, "has skip-worktree enabled; "
126 "use '--ignore-skip-worktree-bits' to checkout");
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);
134 return -1;
137 static int checkout_all(const char *prefix, int prefix_length)
139 int i, errs = 0;
140 struct cache_entry *last_ce = NULL;
142 for (i = 0; i < the_repository->index->cache_nr ; i++) {
143 struct cache_entry *ce = the_repository->index->cache[i];
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);
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).
155 if (ignore_skip_worktree) {
156 ensure_full_index(the_repository->index);
157 ce = the_repository->index->cache[i];
161 if (!ignore_skip_worktree && ce_skip_worktree(ce))
162 continue;
163 if (ce_stage(ce) != checkout_stage
164 && (CHECKOUT_ALL != checkout_stage || !ce_stage(ce)))
165 continue;
166 if (prefix && *prefix &&
167 (ce_namelen(ce) <= prefix_length ||
168 memcmp(prefix, ce->name, prefix_length)))
169 continue;
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)))
173 write_tempfile_record(last_ce->name, prefix);
175 if (checkout_entry(ce, &state,
176 to_tempfile ? topath[ce_stage(ce)] : NULL,
177 NULL) < 0)
178 errs++;
179 last_ce = ce;
181 if (last_ce && to_tempfile)
182 write_tempfile_record(last_ce->name, prefix);
183 return !!errs;
186 static const char * const builtin_checkout_index_usage[] = {
187 N_("git checkout-index [<options>] [--] [<file>...]"),
188 NULL
191 static int option_parse_stage(const struct option *opt,
192 const char *arg, int unset)
194 int *stage = opt->value;
196 BUG_ON_OPT_NEG(unset);
198 if (!strcmp(arg, "all")) {
199 *stage = CHECKOUT_ALL;
200 } else {
201 int ch = arg[0];
202 if ('1' <= ch && ch <= '3')
203 *stage = arg[0] - '0';
204 else
205 die(_("stage should be between 1 and 3 or all"));
207 return 0;
210 int cmd_checkout_index(int argc,
211 const char **argv,
212 const char *prefix,
213 struct repository *repo UNUSED)
215 int i;
216 struct lock_file lock_file = LOCK_INIT;
217 int all = 0;
218 int read_from_stdin = 0;
219 int prefix_length;
220 int force = 0, quiet = 0, not_new = 0;
221 int index_opt = 0;
222 int err = 0;
223 int pc_workers, pc_threshold;
224 struct option builtin_checkout_index_options[] = {
225 OPT_BOOL('a', "all", &all,
226 N_("check out all files in the index")),
227 OPT_BOOL(0, "ignore-skip-worktree-bits", &ignore_skip_worktree,
228 N_("do not skip files with skip-worktree set")),
229 OPT__FORCE(&force, N_("force overwrite of existing files"), 0),
230 OPT__QUIET(&quiet,
231 N_("no warning for existing files and files not in index")),
232 OPT_BOOL('n', "no-create", &not_new,
233 N_("don't checkout new files")),
234 OPT_BOOL('u', "index", &index_opt,
235 N_("update stat information in the index file")),
236 OPT_BOOL('z', NULL, &nul_term_line,
237 N_("paths are separated with NUL character")),
238 OPT_BOOL(0, "stdin", &read_from_stdin,
239 N_("read list of paths from the standard input")),
240 OPT_BOOL(0, "temp", &to_tempfile,
241 N_("write the content to temporary files")),
242 OPT_STRING(0, "prefix", &state.base_dir, N_("string"),
243 N_("when creating files, prepend <string>")),
244 OPT_CALLBACK_F(0, "stage", &checkout_stage, "(1|2|3|all)",
245 N_("copy out the files from named stage"),
246 PARSE_OPT_NONEG, option_parse_stage),
247 OPT_END()
250 if (argc == 2 && !strcmp(argv[1], "-h"))
251 usage_with_options(builtin_checkout_index_usage,
252 builtin_checkout_index_options);
253 git_config(git_default_config, NULL);
254 prefix_length = prefix ? strlen(prefix) : 0;
256 prepare_repo_settings(the_repository);
257 the_repository->settings.command_requires_full_index = 0;
259 if (repo_read_index(the_repository) < 0) {
260 die("invalid cache");
263 argc = parse_options(argc, argv, prefix, builtin_checkout_index_options,
264 builtin_checkout_index_usage, 0);
265 state.istate = the_repository->index;
266 state.force = force;
267 state.quiet = quiet;
268 state.not_new = not_new;
270 if (!state.base_dir)
271 state.base_dir = "";
272 state.base_dir_len = strlen(state.base_dir);
274 if (to_tempfile < 0)
275 to_tempfile = (checkout_stage == CHECKOUT_ALL);
276 if (!to_tempfile && checkout_stage == CHECKOUT_ALL)
277 die(_("options '%s' and '%s' cannot be used together"),
278 "--stage=all", "--no-temp");
281 * when --prefix is specified we do not want to update cache.
283 if (index_opt && !state.base_dir_len && !to_tempfile) {
284 state.refresh_cache = 1;
285 state.istate = the_repository->index;
286 repo_hold_locked_index(the_repository, &lock_file,
287 LOCK_DIE_ON_ERROR);
290 get_parallel_checkout_configs(&pc_workers, &pc_threshold);
291 if (pc_workers > 1)
292 init_parallel_checkout();
294 /* Check out named files first */
295 for (i = 0; i < argc; i++) {
296 const char *arg = argv[i];
297 char *p;
299 if (all)
300 die("git checkout-index: don't mix '--all' and explicit filenames");
301 if (read_from_stdin)
302 die("git checkout-index: don't mix '--stdin' and explicit filenames");
303 p = prefix_path(prefix, prefix_length, arg);
304 err |= checkout_file(p, prefix);
305 free(p);
308 if (read_from_stdin) {
309 struct strbuf buf = STRBUF_INIT;
310 struct strbuf unquoted = STRBUF_INIT;
311 strbuf_getline_fn getline_fn;
313 if (all)
314 die("git checkout-index: don't mix '--all' and '--stdin'");
316 getline_fn = nul_term_line ? strbuf_getline_nul : strbuf_getline_lf;
317 while (getline_fn(&buf, stdin) != EOF) {
318 char *p;
319 if (!nul_term_line && buf.buf[0] == '"') {
320 strbuf_reset(&unquoted);
321 if (unquote_c_style(&unquoted, buf.buf, NULL))
322 die("line is badly quoted");
323 strbuf_swap(&buf, &unquoted);
325 p = prefix_path(prefix, prefix_length, buf.buf);
326 err |= checkout_file(p, prefix);
327 free(p);
329 strbuf_release(&unquoted);
330 strbuf_release(&buf);
333 if (all)
334 err |= checkout_all(prefix, prefix_length);
336 if (pc_workers > 1)
337 err |= run_parallel_checkout(&state, pc_workers, pc_threshold,
338 NULL, NULL);
340 if (err)
341 return 1;
343 if (is_lock_file_locked(&lock_file) &&
344 write_locked_index(the_repository->index, &lock_file, COMMIT_LOCK))
345 die("Unable to write new index file");
346 return 0;