checkout-index: add --ignore-skip-worktree-bits option
[alt-git.git] / builtin / checkout-index.c
blob615a118e2f5e107789a87602dca4a20300f593ce
1 /*
2 * Check-out files from the "current cache directory"
4 * Copyright (C) 2005 Linus Torvalds
6 */
7 #define USE_THE_INDEX_COMPATIBILITY_MACROS
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 = cache_name_pos(name, namelen);
69 int has_same_name = 0;
70 int is_skipped = 1;
71 int did_checkout = 0;
72 int errs = 0;
74 if (pos < 0)
75 pos = -pos - 1;
77 while (pos < active_nr) {
78 struct cache_entry *ce = active_cache[pos];
79 if (ce_namelen(ce) != namelen ||
80 memcmp(ce->name, name, namelen))
81 break;
82 has_same_name = 1;
83 pos++;
84 if (!ignore_skip_worktree && ce_skip_worktree(ce))
85 break;
86 is_skipped = 0;
87 if (ce_stage(ce) != checkout_stage
88 && (CHECKOUT_ALL != checkout_stage || !ce_stage(ce)))
89 continue;
90 did_checkout = 1;
91 if (checkout_entry(ce, &state,
92 to_tempfile ? topath[ce_stage(ce)] : NULL,
93 NULL) < 0)
94 errs++;
97 if (did_checkout) {
98 if (to_tempfile)
99 write_tempfile_record(name, prefix);
100 return errs > 0 ? -1 : 0;
104 * At this point we know we didn't try to check anything out. If it was
105 * because we did find an entry but it was stage 0, that's not an
106 * error.
108 if (has_same_name && checkout_stage == CHECKOUT_ALL)
109 return 0;
111 if (!state.quiet) {
112 fprintf(stderr, "git checkout-index: %s ", name);
113 if (!has_same_name)
114 fprintf(stderr, "is not in the cache");
115 else if (is_skipped)
116 fprintf(stderr, "has skip-worktree enabled; "
117 "use '--ignore-skip-worktree-bits' to checkout");
118 else if (checkout_stage)
119 fprintf(stderr, "does not exist at stage %d",
120 checkout_stage);
121 else
122 fprintf(stderr, "is unmerged");
123 fputc('\n', stderr);
125 return -1;
128 static int checkout_all(const char *prefix, int prefix_length)
130 int i, errs = 0;
131 struct cache_entry *last_ce = NULL;
133 /* TODO: audit for interaction with sparse-index. */
134 ensure_full_index(&the_index);
135 for (i = 0; i < active_nr ; i++) {
136 struct cache_entry *ce = active_cache[i];
137 if (!ignore_skip_worktree && ce_skip_worktree(ce))
138 continue;
139 if (ce_stage(ce) != checkout_stage
140 && (CHECKOUT_ALL != checkout_stage || !ce_stage(ce)))
141 continue;
142 if (prefix && *prefix &&
143 (ce_namelen(ce) <= prefix_length ||
144 memcmp(prefix, ce->name, prefix_length)))
145 continue;
146 if (last_ce && to_tempfile) {
147 if (ce_namelen(last_ce) != ce_namelen(ce)
148 || memcmp(last_ce->name, ce->name, ce_namelen(ce)))
149 write_tempfile_record(last_ce->name, prefix);
151 if (checkout_entry(ce, &state,
152 to_tempfile ? topath[ce_stage(ce)] : NULL,
153 NULL) < 0)
154 errs++;
155 last_ce = ce;
157 if (last_ce && to_tempfile)
158 write_tempfile_record(last_ce->name, prefix);
159 return !!errs;
162 static const char * const builtin_checkout_index_usage[] = {
163 N_("git checkout-index [<options>] [--] [<file>...]"),
164 NULL
167 static int option_parse_stage(const struct option *opt,
168 const char *arg, int unset)
170 BUG_ON_OPT_NEG(unset);
172 if (!strcmp(arg, "all")) {
173 to_tempfile = 1;
174 checkout_stage = CHECKOUT_ALL;
175 } else {
176 int ch = arg[0];
177 if ('1' <= ch && ch <= '3')
178 checkout_stage = arg[0] - '0';
179 else
180 die(_("stage should be between 1 and 3 or all"));
182 return 0;
185 int cmd_checkout_index(int argc, const char **argv, const char *prefix)
187 int i;
188 struct lock_file lock_file = LOCK_INIT;
189 int all = 0;
190 int read_from_stdin = 0;
191 int prefix_length;
192 int force = 0, quiet = 0, not_new = 0;
193 int index_opt = 0;
194 int err = 0;
195 int pc_workers, pc_threshold;
196 struct option builtin_checkout_index_options[] = {
197 OPT_BOOL('a', "all", &all,
198 N_("check out all files in the index")),
199 OPT_BOOL(0, "ignore-skip-worktree-bits", &ignore_skip_worktree,
200 N_("do not skip files with skip-worktree set")),
201 OPT__FORCE(&force, N_("force overwrite of existing files"), 0),
202 OPT__QUIET(&quiet,
203 N_("no warning for existing files and files not in index")),
204 OPT_BOOL('n', "no-create", &not_new,
205 N_("don't checkout new files")),
206 OPT_BOOL('u', "index", &index_opt,
207 N_("update stat information in the index file")),
208 OPT_BOOL('z', NULL, &nul_term_line,
209 N_("paths are separated with NUL character")),
210 OPT_BOOL(0, "stdin", &read_from_stdin,
211 N_("read list of paths from the standard input")),
212 OPT_BOOL(0, "temp", &to_tempfile,
213 N_("write the content to temporary files")),
214 OPT_STRING(0, "prefix", &state.base_dir, N_("string"),
215 N_("when creating files, prepend <string>")),
216 OPT_CALLBACK_F(0, "stage", NULL, "(1|2|3|all)",
217 N_("copy out the files from named stage"),
218 PARSE_OPT_NONEG, option_parse_stage),
219 OPT_END()
222 if (argc == 2 && !strcmp(argv[1], "-h"))
223 usage_with_options(builtin_checkout_index_usage,
224 builtin_checkout_index_options);
225 git_config(git_default_config, NULL);
226 prefix_length = prefix ? strlen(prefix) : 0;
228 if (read_cache() < 0) {
229 die("invalid cache");
232 argc = parse_options(argc, argv, prefix, builtin_checkout_index_options,
233 builtin_checkout_index_usage, 0);
234 state.istate = &the_index;
235 state.force = force;
236 state.quiet = quiet;
237 state.not_new = not_new;
239 if (!state.base_dir)
240 state.base_dir = "";
241 state.base_dir_len = strlen(state.base_dir);
244 * when --prefix is specified we do not want to update cache.
246 if (index_opt && !state.base_dir_len && !to_tempfile) {
247 state.refresh_cache = 1;
248 state.istate = &the_index;
249 hold_locked_index(&lock_file, LOCK_DIE_ON_ERROR);
252 get_parallel_checkout_configs(&pc_workers, &pc_threshold);
253 if (pc_workers > 1)
254 init_parallel_checkout();
256 /* Check out named files first */
257 for (i = 0; i < argc; i++) {
258 const char *arg = argv[i];
259 char *p;
261 if (all)
262 die("git checkout-index: don't mix '--all' and explicit filenames");
263 if (read_from_stdin)
264 die("git checkout-index: don't mix '--stdin' and explicit filenames");
265 p = prefix_path(prefix, prefix_length, arg);
266 err |= checkout_file(p, prefix);
267 free(p);
270 if (read_from_stdin) {
271 struct strbuf buf = STRBUF_INIT;
272 struct strbuf unquoted = STRBUF_INIT;
273 strbuf_getline_fn getline_fn;
275 if (all)
276 die("git checkout-index: don't mix '--all' and '--stdin'");
278 getline_fn = nul_term_line ? strbuf_getline_nul : strbuf_getline_lf;
279 while (getline_fn(&buf, stdin) != EOF) {
280 char *p;
281 if (!nul_term_line && buf.buf[0] == '"') {
282 strbuf_reset(&unquoted);
283 if (unquote_c_style(&unquoted, buf.buf, NULL))
284 die("line is badly quoted");
285 strbuf_swap(&buf, &unquoted);
287 p = prefix_path(prefix, prefix_length, buf.buf);
288 err |= checkout_file(p, prefix);
289 free(p);
291 strbuf_release(&unquoted);
292 strbuf_release(&buf);
295 if (all)
296 err |= checkout_all(prefix, prefix_length);
298 if (pc_workers > 1)
299 err |= run_parallel_checkout(&state, pc_workers, pc_threshold,
300 NULL, NULL);
302 if (err)
303 return 1;
305 if (is_lock_file_locked(&lock_file) &&
306 write_locked_index(&the_index, &lock_file, COMMIT_LOCK))
307 die("Unable to write new index file");
308 return 0;