merge: simplify parsing of "-n" option
[git.git] / builtin / checkout-index.c
blobf62f13f2b5324e15f982071f73b24281d2d9637a
1 /*
2 * Check-out files from the "current cache directory"
4 * Copyright (C) 2005 Linus Torvalds
6 */
7 #define USE_THE_INDEX_VARIABLE
8 #include "builtin.h"
9 #include "config.h"
10 #include "dir.h"
11 #include "gettext.h"
12 #include "lockfile.h"
13 #include "quote.h"
14 #include "repository.h"
15 #include "cache-tree.h"
16 #include "parse-options.h"
17 #include "entry.h"
18 #include "parallel-checkout.h"
19 #include "read-cache-ll.h"
20 #include "setup.h"
21 #include "sparse-index.h"
23 #define CHECKOUT_ALL 4
24 static int nul_term_line;
25 static int checkout_stage; /* default to checkout stage0 */
26 static int ignore_skip_worktree; /* default to 0 */
27 static int to_tempfile;
28 static char topath[4][TEMPORARY_FILENAME_LENGTH + 1];
30 static struct checkout state = CHECKOUT_INIT;
32 static void write_tempfile_record(const char *name, const char *prefix)
34 int i;
35 int have_tempname = 0;
37 if (CHECKOUT_ALL == checkout_stage) {
38 for (i = 1; i < 4; i++)
39 if (topath[i][0]) {
40 have_tempname = 1;
41 break;
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('.');
54 } else if (topath[checkout_stage][0]) {
55 have_tempname = 1;
56 fputs(topath[checkout_stage], stdout);
59 if (have_tempname) {
60 putchar('\t');
61 write_name_quoted_relative(name, prefix, stdout,
62 nul_term_line ? '\0' : '\n');
65 for (i = 0; i < 4; i++) {
66 topath[i][0] = 0;
70 static int checkout_file(const char *name, const char *prefix)
72 int namelen = strlen(name);
73 int pos = index_name_pos(&the_index, name, namelen);
74 int has_same_name = 0;
75 int is_file = 0;
76 int is_skipped = 1;
77 int did_checkout = 0;
78 int errs = 0;
80 if (pos < 0)
81 pos = -pos - 1;
83 while (pos < the_index.cache_nr) {
84 struct cache_entry *ce = the_index.cache[pos];
85 if (ce_namelen(ce) != namelen ||
86 memcmp(ce->name, name, namelen))
87 break;
88 has_same_name = 1;
89 pos++;
90 if (S_ISSPARSEDIR(ce->ce_mode))
91 break;
92 is_file = 1;
93 if (!ignore_skip_worktree && ce_skip_worktree(ce))
94 break;
95 is_skipped = 0;
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,
101 to_tempfile ? topath[ce_stage(ce)] : NULL,
102 NULL) < 0)
103 errs++;
106 if (did_checkout) {
107 if (to_tempfile)
108 write_tempfile_record(name, prefix);
109 return errs > 0 ? -1 : 0;
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.
117 if (has_same_name && checkout_stage == CHECKOUT_ALL)
118 return 0;
120 if (!state.quiet) {
121 fprintf(stderr, "git checkout-index: %s ", name);
122 if (!has_same_name)
123 fprintf(stderr, "is not in the cache");
124 else if (!is_file)
125 fprintf(stderr, "is a sparse directory");
126 else if (is_skipped)
127 fprintf(stderr, "has skip-worktree enabled; "
128 "use '--ignore-skip-worktree-bits' to checkout");
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);
136 return -1;
139 static int checkout_all(const char *prefix, int prefix_length)
141 int i, errs = 0;
142 struct cache_entry *last_ce = NULL;
144 for (i = 0; i < the_index.cache_nr ; i++) {
145 struct cache_entry *ce = the_index.cache[i];
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);
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).
157 if (ignore_skip_worktree) {
158 ensure_full_index(&the_index);
159 ce = the_index.cache[i];
163 if (!ignore_skip_worktree && ce_skip_worktree(ce))
164 continue;
165 if (ce_stage(ce) != checkout_stage
166 && (CHECKOUT_ALL != checkout_stage || !ce_stage(ce)))
167 continue;
168 if (prefix && *prefix &&
169 (ce_namelen(ce) <= prefix_length ||
170 memcmp(prefix, ce->name, prefix_length)))
171 continue;
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)))
175 write_tempfile_record(last_ce->name, prefix);
177 if (checkout_entry(ce, &state,
178 to_tempfile ? topath[ce_stage(ce)] : NULL,
179 NULL) < 0)
180 errs++;
181 last_ce = ce;
183 if (last_ce && to_tempfile)
184 write_tempfile_record(last_ce->name, prefix);
185 return !!errs;
188 static const char * const builtin_checkout_index_usage[] = {
189 N_("git checkout-index [<options>] [--] [<file>...]"),
190 NULL
193 static int option_parse_stage(const struct option *opt,
194 const char *arg, int unset)
196 BUG_ON_OPT_NEG(unset);
198 if (!strcmp(arg, "all")) {
199 to_tempfile = 1;
200 checkout_stage = CHECKOUT_ALL;
201 } else {
202 int ch = arg[0];
203 if ('1' <= ch && ch <= '3')
204 checkout_stage = arg[0] - '0';
205 else
206 die(_("stage should be between 1 and 3 or all"));
208 return 0;
211 int cmd_checkout_index(int argc, const char **argv, const char *prefix)
213 int i;
214 struct lock_file lock_file = LOCK_INIT;
215 int all = 0;
216 int read_from_stdin = 0;
217 int prefix_length;
218 int force = 0, quiet = 0, not_new = 0;
219 int index_opt = 0;
220 int err = 0;
221 int pc_workers, pc_threshold;
222 struct option builtin_checkout_index_options[] = {
223 OPT_BOOL('a', "all", &all,
224 N_("check out all files in the index")),
225 OPT_BOOL(0, "ignore-skip-worktree-bits", &ignore_skip_worktree,
226 N_("do not skip files with skip-worktree set")),
227 OPT__FORCE(&force, N_("force overwrite of existing files"), 0),
228 OPT__QUIET(&quiet,
229 N_("no warning for existing files and files not in index")),
230 OPT_BOOL('n', "no-create", &not_new,
231 N_("don't checkout new files")),
232 OPT_BOOL('u', "index", &index_opt,
233 N_("update stat information in the index file")),
234 OPT_BOOL('z', NULL, &nul_term_line,
235 N_("paths are separated with NUL character")),
236 OPT_BOOL(0, "stdin", &read_from_stdin,
237 N_("read list of paths from the standard input")),
238 OPT_BOOL(0, "temp", &to_tempfile,
239 N_("write the content to temporary files")),
240 OPT_STRING(0, "prefix", &state.base_dir, N_("string"),
241 N_("when creating files, prepend <string>")),
242 OPT_CALLBACK_F(0, "stage", NULL, "(1|2|3|all)",
243 N_("copy out the files from named stage"),
244 PARSE_OPT_NONEG, option_parse_stage),
245 OPT_END()
248 if (argc == 2 && !strcmp(argv[1], "-h"))
249 usage_with_options(builtin_checkout_index_usage,
250 builtin_checkout_index_options);
251 git_config(git_default_config, NULL);
252 prefix_length = prefix ? strlen(prefix) : 0;
254 prepare_repo_settings(the_repository);
255 the_repository->settings.command_requires_full_index = 0;
257 if (repo_read_index(the_repository) < 0) {
258 die("invalid cache");
261 argc = parse_options(argc, argv, prefix, builtin_checkout_index_options,
262 builtin_checkout_index_usage, 0);
263 state.istate = &the_index;
264 state.force = force;
265 state.quiet = quiet;
266 state.not_new = not_new;
268 if (!state.base_dir)
269 state.base_dir = "";
270 state.base_dir_len = strlen(state.base_dir);
273 * when --prefix is specified we do not want to update cache.
275 if (index_opt && !state.base_dir_len && !to_tempfile) {
276 state.refresh_cache = 1;
277 state.istate = &the_index;
278 repo_hold_locked_index(the_repository, &lock_file,
279 LOCK_DIE_ON_ERROR);
282 get_parallel_checkout_configs(&pc_workers, &pc_threshold);
283 if (pc_workers > 1)
284 init_parallel_checkout();
286 /* Check out named files first */
287 for (i = 0; i < argc; i++) {
288 const char *arg = argv[i];
289 char *p;
291 if (all)
292 die("git checkout-index: don't mix '--all' and explicit filenames");
293 if (read_from_stdin)
294 die("git checkout-index: don't mix '--stdin' and explicit filenames");
295 p = prefix_path(prefix, prefix_length, arg);
296 err |= checkout_file(p, prefix);
297 free(p);
300 if (read_from_stdin) {
301 struct strbuf buf = STRBUF_INIT;
302 struct strbuf unquoted = STRBUF_INIT;
303 strbuf_getline_fn getline_fn;
305 if (all)
306 die("git checkout-index: don't mix '--all' and '--stdin'");
308 getline_fn = nul_term_line ? strbuf_getline_nul : strbuf_getline_lf;
309 while (getline_fn(&buf, stdin) != EOF) {
310 char *p;
311 if (!nul_term_line && buf.buf[0] == '"') {
312 strbuf_reset(&unquoted);
313 if (unquote_c_style(&unquoted, buf.buf, NULL))
314 die("line is badly quoted");
315 strbuf_swap(&buf, &unquoted);
317 p = prefix_path(prefix, prefix_length, buf.buf);
318 err |= checkout_file(p, prefix);
319 free(p);
321 strbuf_release(&unquoted);
322 strbuf_release(&buf);
325 if (all)
326 err |= checkout_all(prefix, prefix_length);
328 if (pc_workers > 1)
329 err |= run_parallel_checkout(&state, pc_workers, pc_threshold,
330 NULL, NULL);
332 if (err)
333 return 1;
335 if (is_lock_file_locked(&lock_file) &&
336 write_locked_index(&the_index, &lock_file, COMMIT_LOCK))
337 die("Unable to write new index file");
338 return 0;