sparse-index.h: move declarations for sparse-index.c from cache.h
[git.git] / builtin / checkout-index.c
blobb1cd8bef2647997bd8c2456e40f72e2087673f4c
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 "setup.h"
20 #include "sparse-index.h"
22 #define CHECKOUT_ALL 4
23 static int nul_term_line;
24 static int checkout_stage; /* default to checkout stage0 */
25 static int ignore_skip_worktree; /* default to 0 */
26 static int to_tempfile;
27 static char topath[4][TEMPORARY_FILENAME_LENGTH + 1];
29 static struct checkout state = CHECKOUT_INIT;
31 static void write_tempfile_record(const char *name, const char *prefix)
33 int i;
34 int have_tempname = 0;
36 if (CHECKOUT_ALL == checkout_stage) {
37 for (i = 1; i < 4; i++)
38 if (topath[i][0]) {
39 have_tempname = 1;
40 break;
43 if (have_tempname) {
44 for (i = 1; i < 4; i++) {
45 if (i > 1)
46 putchar(' ');
47 if (topath[i][0])
48 fputs(topath[i], stdout);
49 else
50 putchar('.');
53 } else if (topath[checkout_stage][0]) {
54 have_tempname = 1;
55 fputs(topath[checkout_stage], stdout);
58 if (have_tempname) {
59 putchar('\t');
60 write_name_quoted_relative(name, prefix, stdout,
61 nul_term_line ? '\0' : '\n');
64 for (i = 0; i < 4; i++) {
65 topath[i][0] = 0;
69 static int checkout_file(const char *name, const char *prefix)
71 int namelen = strlen(name);
72 int pos = index_name_pos(&the_index, name, namelen);
73 int has_same_name = 0;
74 int is_file = 0;
75 int is_skipped = 1;
76 int did_checkout = 0;
77 int errs = 0;
79 if (pos < 0)
80 pos = -pos - 1;
82 while (pos < the_index.cache_nr) {
83 struct cache_entry *ce = the_index.cache[pos];
84 if (ce_namelen(ce) != namelen ||
85 memcmp(ce->name, name, namelen))
86 break;
87 has_same_name = 1;
88 pos++;
89 if (S_ISSPARSEDIR(ce->ce_mode))
90 break;
91 is_file = 1;
92 if (!ignore_skip_worktree && ce_skip_worktree(ce))
93 break;
94 is_skipped = 0;
95 if (ce_stage(ce) != checkout_stage
96 && (CHECKOUT_ALL != checkout_stage || !ce_stage(ce)))
97 continue;
98 did_checkout = 1;
99 if (checkout_entry(ce, &state,
100 to_tempfile ? topath[ce_stage(ce)] : NULL,
101 NULL) < 0)
102 errs++;
105 if (did_checkout) {
106 if (to_tempfile)
107 write_tempfile_record(name, prefix);
108 return errs > 0 ? -1 : 0;
112 * At this point we know we didn't try to check anything out. If it was
113 * because we did find an entry but it was stage 0, that's not an
114 * error.
116 if (has_same_name && checkout_stage == CHECKOUT_ALL)
117 return 0;
119 if (!state.quiet) {
120 fprintf(stderr, "git checkout-index: %s ", name);
121 if (!has_same_name)
122 fprintf(stderr, "is not in the cache");
123 else if (!is_file)
124 fprintf(stderr, "is a sparse directory");
125 else if (is_skipped)
126 fprintf(stderr, "has skip-worktree enabled; "
127 "use '--ignore-skip-worktree-bits' to checkout");
128 else if (checkout_stage)
129 fprintf(stderr, "does not exist at stage %d",
130 checkout_stage);
131 else
132 fprintf(stderr, "is unmerged");
133 fputc('\n', stderr);
135 return -1;
138 static int checkout_all(const char *prefix, int prefix_length)
140 int i, errs = 0;
141 struct cache_entry *last_ce = NULL;
143 for (i = 0; i < the_index.cache_nr ; i++) {
144 struct cache_entry *ce = the_index.cache[i];
146 if (S_ISSPARSEDIR(ce->ce_mode)) {
147 if (!ce_skip_worktree(ce))
148 BUG("sparse directory '%s' does not have skip-worktree set", ce->name);
151 * If the current entry is a sparse directory and skip-worktree
152 * entries are being checked out, expand the index and continue
153 * the loop on the current index position (now pointing to the
154 * first entry inside the expanded sparse directory).
156 if (ignore_skip_worktree) {
157 ensure_full_index(&the_index);
158 ce = the_index.cache[i];
162 if (!ignore_skip_worktree && ce_skip_worktree(ce))
163 continue;
164 if (ce_stage(ce) != checkout_stage
165 && (CHECKOUT_ALL != checkout_stage || !ce_stage(ce)))
166 continue;
167 if (prefix && *prefix &&
168 (ce_namelen(ce) <= prefix_length ||
169 memcmp(prefix, ce->name, prefix_length)))
170 continue;
171 if (last_ce && to_tempfile) {
172 if (ce_namelen(last_ce) != ce_namelen(ce)
173 || memcmp(last_ce->name, ce->name, ce_namelen(ce)))
174 write_tempfile_record(last_ce->name, prefix);
176 if (checkout_entry(ce, &state,
177 to_tempfile ? topath[ce_stage(ce)] : NULL,
178 NULL) < 0)
179 errs++;
180 last_ce = ce;
182 if (last_ce && to_tempfile)
183 write_tempfile_record(last_ce->name, prefix);
184 return !!errs;
187 static const char * const builtin_checkout_index_usage[] = {
188 N_("git checkout-index [<options>] [--] [<file>...]"),
189 NULL
192 static int option_parse_stage(const struct option *opt,
193 const char *arg, int unset)
195 BUG_ON_OPT_NEG(unset);
197 if (!strcmp(arg, "all")) {
198 to_tempfile = 1;
199 checkout_stage = CHECKOUT_ALL;
200 } else {
201 int ch = arg[0];
202 if ('1' <= ch && ch <= '3')
203 checkout_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, const char **argv, const char *prefix)
212 int i;
213 struct lock_file lock_file = LOCK_INIT;
214 int all = 0;
215 int read_from_stdin = 0;
216 int prefix_length;
217 int force = 0, quiet = 0, not_new = 0;
218 int index_opt = 0;
219 int err = 0;
220 int pc_workers, pc_threshold;
221 struct option builtin_checkout_index_options[] = {
222 OPT_BOOL('a', "all", &all,
223 N_("check out all files in the index")),
224 OPT_BOOL(0, "ignore-skip-worktree-bits", &ignore_skip_worktree,
225 N_("do not skip files with skip-worktree set")),
226 OPT__FORCE(&force, N_("force overwrite of existing files"), 0),
227 OPT__QUIET(&quiet,
228 N_("no warning for existing files and files not in index")),
229 OPT_BOOL('n', "no-create", &not_new,
230 N_("don't checkout new files")),
231 OPT_BOOL('u', "index", &index_opt,
232 N_("update stat information in the index file")),
233 OPT_BOOL('z', NULL, &nul_term_line,
234 N_("paths are separated with NUL character")),
235 OPT_BOOL(0, "stdin", &read_from_stdin,
236 N_("read list of paths from the standard input")),
237 OPT_BOOL(0, "temp", &to_tempfile,
238 N_("write the content to temporary files")),
239 OPT_STRING(0, "prefix", &state.base_dir, N_("string"),
240 N_("when creating files, prepend <string>")),
241 OPT_CALLBACK_F(0, "stage", NULL, "(1|2|3|all)",
242 N_("copy out the files from named stage"),
243 PARSE_OPT_NONEG, option_parse_stage),
244 OPT_END()
247 if (argc == 2 && !strcmp(argv[1], "-h"))
248 usage_with_options(builtin_checkout_index_usage,
249 builtin_checkout_index_options);
250 git_config(git_default_config, NULL);
251 prefix_length = prefix ? strlen(prefix) : 0;
253 prepare_repo_settings(the_repository);
254 the_repository->settings.command_requires_full_index = 0;
256 if (repo_read_index(the_repository) < 0) {
257 die("invalid cache");
260 argc = parse_options(argc, argv, prefix, builtin_checkout_index_options,
261 builtin_checkout_index_usage, 0);
262 state.istate = &the_index;
263 state.force = force;
264 state.quiet = quiet;
265 state.not_new = not_new;
267 if (!state.base_dir)
268 state.base_dir = "";
269 state.base_dir_len = strlen(state.base_dir);
272 * when --prefix is specified we do not want to update cache.
274 if (index_opt && !state.base_dir_len && !to_tempfile) {
275 state.refresh_cache = 1;
276 state.istate = &the_index;
277 repo_hold_locked_index(the_repository, &lock_file,
278 LOCK_DIE_ON_ERROR);
281 get_parallel_checkout_configs(&pc_workers, &pc_threshold);
282 if (pc_workers > 1)
283 init_parallel_checkout();
285 /* Check out named files first */
286 for (i = 0; i < argc; i++) {
287 const char *arg = argv[i];
288 char *p;
290 if (all)
291 die("git checkout-index: don't mix '--all' and explicit filenames");
292 if (read_from_stdin)
293 die("git checkout-index: don't mix '--stdin' and explicit filenames");
294 p = prefix_path(prefix, prefix_length, arg);
295 err |= checkout_file(p, prefix);
296 free(p);
299 if (read_from_stdin) {
300 struct strbuf buf = STRBUF_INIT;
301 struct strbuf unquoted = STRBUF_INIT;
302 strbuf_getline_fn getline_fn;
304 if (all)
305 die("git checkout-index: don't mix '--all' and '--stdin'");
307 getline_fn = nul_term_line ? strbuf_getline_nul : strbuf_getline_lf;
308 while (getline_fn(&buf, stdin) != EOF) {
309 char *p;
310 if (!nul_term_line && buf.buf[0] == '"') {
311 strbuf_reset(&unquoted);
312 if (unquote_c_style(&unquoted, buf.buf, NULL))
313 die("line is badly quoted");
314 strbuf_swap(&buf, &unquoted);
316 p = prefix_path(prefix, prefix_length, buf.buf);
317 err |= checkout_file(p, prefix);
318 free(p);
320 strbuf_release(&unquoted);
321 strbuf_release(&buf);
324 if (all)
325 err |= checkout_all(prefix, prefix_length);
327 if (pc_workers > 1)
328 err |= run_parallel_checkout(&state, pc_workers, pc_threshold,
329 NULL, NULL);
331 if (err)
332 return 1;
334 if (is_lock_file_locked(&lock_file) &&
335 write_locked_index(&the_index, &lock_file, COMMIT_LOCK))
336 die("Unable to write new index file");
337 return 0;