cocci: avoid "should ... be a metavariable" warnings
[git/debian.git] / builtin / checkout-index.c
blobcf6fba97ba784a201b8becff88cc82eac93cf7f7
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 "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"
18 #define CHECKOUT_ALL 4
19 static int nul_term_line;
20 static int checkout_stage; /* default to checkout stage0 */
21 static int ignore_skip_worktree; /* default to 0 */
22 static int to_tempfile;
23 static char topath[4][TEMPORARY_FILENAME_LENGTH + 1];
25 static struct checkout state = CHECKOUT_INIT;
27 static void write_tempfile_record(const char *name, const char *prefix)
29 int i;
30 int have_tempname = 0;
32 if (CHECKOUT_ALL == checkout_stage) {
33 for (i = 1; i < 4; i++)
34 if (topath[i][0]) {
35 have_tempname = 1;
36 break;
39 if (have_tempname) {
40 for (i = 1; i < 4; i++) {
41 if (i > 1)
42 putchar(' ');
43 if (topath[i][0])
44 fputs(topath[i], stdout);
45 else
46 putchar('.');
49 } else if (topath[checkout_stage][0]) {
50 have_tempname = 1;
51 fputs(topath[checkout_stage], stdout);
54 if (have_tempname) {
55 putchar('\t');
56 write_name_quoted_relative(name, prefix, stdout,
57 nul_term_line ? '\0' : '\n');
60 for (i = 0; i < 4; i++) {
61 topath[i][0] = 0;
65 static int checkout_file(const char *name, const char *prefix)
67 int namelen = strlen(name);
68 int pos = index_name_pos(&the_index, name, namelen);
69 int has_same_name = 0;
70 int is_file = 0;
71 int is_skipped = 1;
72 int did_checkout = 0;
73 int errs = 0;
75 if (pos < 0)
76 pos = -pos - 1;
78 while (pos < the_index.cache_nr) {
79 struct cache_entry *ce = the_index.cache[pos];
80 if (ce_namelen(ce) != namelen ||
81 memcmp(ce->name, name, namelen))
82 break;
83 has_same_name = 1;
84 pos++;
85 if (S_ISSPARSEDIR(ce->ce_mode))
86 break;
87 is_file = 1;
88 if (!ignore_skip_worktree && ce_skip_worktree(ce))
89 break;
90 is_skipped = 0;
91 if (ce_stage(ce) != checkout_stage
92 && (CHECKOUT_ALL != checkout_stage || !ce_stage(ce)))
93 continue;
94 did_checkout = 1;
95 if (checkout_entry(ce, &state,
96 to_tempfile ? topath[ce_stage(ce)] : NULL,
97 NULL) < 0)
98 errs++;
101 if (did_checkout) {
102 if (to_tempfile)
103 write_tempfile_record(name, prefix);
104 return errs > 0 ? -1 : 0;
108 * At this point we know we didn't try to check anything out. If it was
109 * because we did find an entry but it was stage 0, that's not an
110 * error.
112 if (has_same_name && checkout_stage == CHECKOUT_ALL)
113 return 0;
115 if (!state.quiet) {
116 fprintf(stderr, "git checkout-index: %s ", name);
117 if (!has_same_name)
118 fprintf(stderr, "is not in the cache");
119 else if (!is_file)
120 fprintf(stderr, "is a sparse directory");
121 else if (is_skipped)
122 fprintf(stderr, "has skip-worktree enabled; "
123 "use '--ignore-skip-worktree-bits' to checkout");
124 else if (checkout_stage)
125 fprintf(stderr, "does not exist at stage %d",
126 checkout_stage);
127 else
128 fprintf(stderr, "is unmerged");
129 fputc('\n', stderr);
131 return -1;
134 static int checkout_all(const char *prefix, int prefix_length)
136 int i, errs = 0;
137 struct cache_entry *last_ce = NULL;
139 for (i = 0; i < the_index.cache_nr ; i++) {
140 struct cache_entry *ce = the_index.cache[i];
142 if (S_ISSPARSEDIR(ce->ce_mode)) {
143 if (!ce_skip_worktree(ce))
144 BUG("sparse directory '%s' does not have skip-worktree set", ce->name);
147 * If the current entry is a sparse directory and skip-worktree
148 * entries are being checked out, expand the index and continue
149 * the loop on the current index position (now pointing to the
150 * first entry inside the expanded sparse directory).
152 if (ignore_skip_worktree) {
153 ensure_full_index(&the_index);
154 ce = the_index.cache[i];
158 if (!ignore_skip_worktree && ce_skip_worktree(ce))
159 continue;
160 if (ce_stage(ce) != checkout_stage
161 && (CHECKOUT_ALL != checkout_stage || !ce_stage(ce)))
162 continue;
163 if (prefix && *prefix &&
164 (ce_namelen(ce) <= prefix_length ||
165 memcmp(prefix, ce->name, prefix_length)))
166 continue;
167 if (last_ce && to_tempfile) {
168 if (ce_namelen(last_ce) != ce_namelen(ce)
169 || memcmp(last_ce->name, ce->name, ce_namelen(ce)))
170 write_tempfile_record(last_ce->name, prefix);
172 if (checkout_entry(ce, &state,
173 to_tempfile ? topath[ce_stage(ce)] : NULL,
174 NULL) < 0)
175 errs++;
176 last_ce = ce;
178 if (last_ce && to_tempfile)
179 write_tempfile_record(last_ce->name, prefix);
180 return !!errs;
183 static const char * const builtin_checkout_index_usage[] = {
184 N_("git checkout-index [<options>] [--] [<file>...]"),
185 NULL
188 static int option_parse_stage(const struct option *opt,
189 const char *arg, int unset)
191 BUG_ON_OPT_NEG(unset);
193 if (!strcmp(arg, "all")) {
194 to_tempfile = 1;
195 checkout_stage = CHECKOUT_ALL;
196 } else {
197 int ch = arg[0];
198 if ('1' <= ch && ch <= '3')
199 checkout_stage = arg[0] - '0';
200 else
201 die(_("stage should be between 1 and 3 or all"));
203 return 0;
206 int cmd_checkout_index(int argc, const char **argv, const char *prefix)
208 int i;
209 struct lock_file lock_file = LOCK_INIT;
210 int all = 0;
211 int read_from_stdin = 0;
212 int prefix_length;
213 int force = 0, quiet = 0, not_new = 0;
214 int index_opt = 0;
215 int err = 0;
216 int pc_workers, pc_threshold;
217 struct option builtin_checkout_index_options[] = {
218 OPT_BOOL('a', "all", &all,
219 N_("check out all files in the index")),
220 OPT_BOOL(0, "ignore-skip-worktree-bits", &ignore_skip_worktree,
221 N_("do not skip files with skip-worktree set")),
222 OPT__FORCE(&force, N_("force overwrite of existing files"), 0),
223 OPT__QUIET(&quiet,
224 N_("no warning for existing files and files not in index")),
225 OPT_BOOL('n', "no-create", &not_new,
226 N_("don't checkout new files")),
227 OPT_BOOL('u', "index", &index_opt,
228 N_("update stat information in the index file")),
229 OPT_BOOL('z', NULL, &nul_term_line,
230 N_("paths are separated with NUL character")),
231 OPT_BOOL(0, "stdin", &read_from_stdin,
232 N_("read list of paths from the standard input")),
233 OPT_BOOL(0, "temp", &to_tempfile,
234 N_("write the content to temporary files")),
235 OPT_STRING(0, "prefix", &state.base_dir, N_("string"),
236 N_("when creating files, prepend <string>")),
237 OPT_CALLBACK_F(0, "stage", NULL, "(1|2|3|all)",
238 N_("copy out the files from named stage"),
239 PARSE_OPT_NONEG, option_parse_stage),
240 OPT_END()
243 if (argc == 2 && !strcmp(argv[1], "-h"))
244 usage_with_options(builtin_checkout_index_usage,
245 builtin_checkout_index_options);
246 git_config(git_default_config, NULL);
247 prefix_length = prefix ? strlen(prefix) : 0;
249 prepare_repo_settings(the_repository);
250 the_repository->settings.command_requires_full_index = 0;
252 if (repo_read_index(the_repository) < 0) {
253 die("invalid cache");
256 argc = parse_options(argc, argv, prefix, builtin_checkout_index_options,
257 builtin_checkout_index_usage, 0);
258 state.istate = &the_index;
259 state.force = force;
260 state.quiet = quiet;
261 state.not_new = not_new;
263 if (!state.base_dir)
264 state.base_dir = "";
265 state.base_dir_len = strlen(state.base_dir);
268 * when --prefix is specified we do not want to update cache.
270 if (index_opt && !state.base_dir_len && !to_tempfile) {
271 state.refresh_cache = 1;
272 state.istate = &the_index;
273 repo_hold_locked_index(the_repository, &lock_file,
274 LOCK_DIE_ON_ERROR);
277 get_parallel_checkout_configs(&pc_workers, &pc_threshold);
278 if (pc_workers > 1)
279 init_parallel_checkout();
281 /* Check out named files first */
282 for (i = 0; i < argc; i++) {
283 const char *arg = argv[i];
284 char *p;
286 if (all)
287 die("git checkout-index: don't mix '--all' and explicit filenames");
288 if (read_from_stdin)
289 die("git checkout-index: don't mix '--stdin' and explicit filenames");
290 p = prefix_path(prefix, prefix_length, arg);
291 err |= checkout_file(p, prefix);
292 free(p);
295 if (read_from_stdin) {
296 struct strbuf buf = STRBUF_INIT;
297 struct strbuf unquoted = STRBUF_INIT;
298 strbuf_getline_fn getline_fn;
300 if (all)
301 die("git checkout-index: don't mix '--all' and '--stdin'");
303 getline_fn = nul_term_line ? strbuf_getline_nul : strbuf_getline_lf;
304 while (getline_fn(&buf, stdin) != EOF) {
305 char *p;
306 if (!nul_term_line && buf.buf[0] == '"') {
307 strbuf_reset(&unquoted);
308 if (unquote_c_style(&unquoted, buf.buf, NULL))
309 die("line is badly quoted");
310 strbuf_swap(&buf, &unquoted);
312 p = prefix_path(prefix, prefix_length, buf.buf);
313 err |= checkout_file(p, prefix);
314 free(p);
316 strbuf_release(&unquoted);
317 strbuf_release(&buf);
320 if (all)
321 err |= checkout_all(prefix, prefix_length);
323 if (pc_workers > 1)
324 err |= run_parallel_checkout(&state, pc_workers, pc_threshold,
325 NULL, NULL);
327 if (err)
328 return 1;
330 if (is_lock_file_locked(&lock_file) &&
331 write_locked_index(&the_index, &lock_file, COMMIT_LOCK))
332 die("Unable to write new index file");
333 return 0;