object: add object_array initializer helper function
[git/gitster.git] / builtin / checkout-index.c
blob7df673e3e70c26dd10407060623e1276f343f432
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 "cache-tree.h"
15 #include "parse-options.h"
16 #include "entry.h"
17 #include "parallel-checkout.h"
18 #include "setup.h"
20 #define CHECKOUT_ALL 4
21 static int nul_term_line;
22 static int checkout_stage; /* default to checkout stage0 */
23 static int ignore_skip_worktree; /* default to 0 */
24 static int to_tempfile;
25 static char topath[4][TEMPORARY_FILENAME_LENGTH + 1];
27 static struct checkout state = CHECKOUT_INIT;
29 static void write_tempfile_record(const char *name, const char *prefix)
31 int i;
32 int have_tempname = 0;
34 if (CHECKOUT_ALL == checkout_stage) {
35 for (i = 1; i < 4; i++)
36 if (topath[i][0]) {
37 have_tempname = 1;
38 break;
41 if (have_tempname) {
42 for (i = 1; i < 4; i++) {
43 if (i > 1)
44 putchar(' ');
45 if (topath[i][0])
46 fputs(topath[i], stdout);
47 else
48 putchar('.');
51 } else if (topath[checkout_stage][0]) {
52 have_tempname = 1;
53 fputs(topath[checkout_stage], stdout);
56 if (have_tempname) {
57 putchar('\t');
58 write_name_quoted_relative(name, prefix, stdout,
59 nul_term_line ? '\0' : '\n');
62 for (i = 0; i < 4; i++) {
63 topath[i][0] = 0;
67 static int checkout_file(const char *name, const char *prefix)
69 int namelen = strlen(name);
70 int pos = index_name_pos(&the_index, name, namelen);
71 int has_same_name = 0;
72 int is_file = 0;
73 int is_skipped = 1;
74 int did_checkout = 0;
75 int errs = 0;
77 if (pos < 0)
78 pos = -pos - 1;
80 while (pos < the_index.cache_nr) {
81 struct cache_entry *ce = the_index.cache[pos];
82 if (ce_namelen(ce) != namelen ||
83 memcmp(ce->name, name, namelen))
84 break;
85 has_same_name = 1;
86 pos++;
87 if (S_ISSPARSEDIR(ce->ce_mode))
88 break;
89 is_file = 1;
90 if (!ignore_skip_worktree && ce_skip_worktree(ce))
91 break;
92 is_skipped = 0;
93 if (ce_stage(ce) != checkout_stage
94 && (CHECKOUT_ALL != checkout_stage || !ce_stage(ce)))
95 continue;
96 did_checkout = 1;
97 if (checkout_entry(ce, &state,
98 to_tempfile ? topath[ce_stage(ce)] : NULL,
99 NULL) < 0)
100 errs++;
103 if (did_checkout) {
104 if (to_tempfile)
105 write_tempfile_record(name, prefix);
106 return errs > 0 ? -1 : 0;
110 * At this point we know we didn't try to check anything out. If it was
111 * because we did find an entry but it was stage 0, that's not an
112 * error.
114 if (has_same_name && checkout_stage == CHECKOUT_ALL)
115 return 0;
117 if (!state.quiet) {
118 fprintf(stderr, "git checkout-index: %s ", name);
119 if (!has_same_name)
120 fprintf(stderr, "is not in the cache");
121 else if (!is_file)
122 fprintf(stderr, "is a sparse directory");
123 else if (is_skipped)
124 fprintf(stderr, "has skip-worktree enabled; "
125 "use '--ignore-skip-worktree-bits' to checkout");
126 else if (checkout_stage)
127 fprintf(stderr, "does not exist at stage %d",
128 checkout_stage);
129 else
130 fprintf(stderr, "is unmerged");
131 fputc('\n', stderr);
133 return -1;
136 static int checkout_all(const char *prefix, int prefix_length)
138 int i, errs = 0;
139 struct cache_entry *last_ce = NULL;
141 for (i = 0; i < the_index.cache_nr ; i++) {
142 struct cache_entry *ce = the_index.cache[i];
144 if (S_ISSPARSEDIR(ce->ce_mode)) {
145 if (!ce_skip_worktree(ce))
146 BUG("sparse directory '%s' does not have skip-worktree set", ce->name);
149 * If the current entry is a sparse directory and skip-worktree
150 * entries are being checked out, expand the index and continue
151 * the loop on the current index position (now pointing to the
152 * first entry inside the expanded sparse directory).
154 if (ignore_skip_worktree) {
155 ensure_full_index(&the_index);
156 ce = the_index.cache[i];
160 if (!ignore_skip_worktree && ce_skip_worktree(ce))
161 continue;
162 if (ce_stage(ce) != checkout_stage
163 && (CHECKOUT_ALL != checkout_stage || !ce_stage(ce)))
164 continue;
165 if (prefix && *prefix &&
166 (ce_namelen(ce) <= prefix_length ||
167 memcmp(prefix, ce->name, prefix_length)))
168 continue;
169 if (last_ce && to_tempfile) {
170 if (ce_namelen(last_ce) != ce_namelen(ce)
171 || memcmp(last_ce->name, ce->name, ce_namelen(ce)))
172 write_tempfile_record(last_ce->name, prefix);
174 if (checkout_entry(ce, &state,
175 to_tempfile ? topath[ce_stage(ce)] : NULL,
176 NULL) < 0)
177 errs++;
178 last_ce = ce;
180 if (last_ce && to_tempfile)
181 write_tempfile_record(last_ce->name, prefix);
182 return !!errs;
185 static const char * const builtin_checkout_index_usage[] = {
186 N_("git checkout-index [<options>] [--] [<file>...]"),
187 NULL
190 static int option_parse_stage(const struct option *opt,
191 const char *arg, int unset)
193 BUG_ON_OPT_NEG(unset);
195 if (!strcmp(arg, "all")) {
196 to_tempfile = 1;
197 checkout_stage = CHECKOUT_ALL;
198 } else {
199 int ch = arg[0];
200 if ('1' <= ch && ch <= '3')
201 checkout_stage = arg[0] - '0';
202 else
203 die(_("stage should be between 1 and 3 or all"));
205 return 0;
208 int cmd_checkout_index(int argc, const char **argv, const char *prefix)
210 int i;
211 struct lock_file lock_file = LOCK_INIT;
212 int all = 0;
213 int read_from_stdin = 0;
214 int prefix_length;
215 int force = 0, quiet = 0, not_new = 0;
216 int index_opt = 0;
217 int err = 0;
218 int pc_workers, pc_threshold;
219 struct option builtin_checkout_index_options[] = {
220 OPT_BOOL('a', "all", &all,
221 N_("check out all files in the index")),
222 OPT_BOOL(0, "ignore-skip-worktree-bits", &ignore_skip_worktree,
223 N_("do not skip files with skip-worktree set")),
224 OPT__FORCE(&force, N_("force overwrite of existing files"), 0),
225 OPT__QUIET(&quiet,
226 N_("no warning for existing files and files not in index")),
227 OPT_BOOL('n', "no-create", &not_new,
228 N_("don't checkout new files")),
229 OPT_BOOL('u', "index", &index_opt,
230 N_("update stat information in the index file")),
231 OPT_BOOL('z', NULL, &nul_term_line,
232 N_("paths are separated with NUL character")),
233 OPT_BOOL(0, "stdin", &read_from_stdin,
234 N_("read list of paths from the standard input")),
235 OPT_BOOL(0, "temp", &to_tempfile,
236 N_("write the content to temporary files")),
237 OPT_STRING(0, "prefix", &state.base_dir, N_("string"),
238 N_("when creating files, prepend <string>")),
239 OPT_CALLBACK_F(0, "stage", NULL, "(1|2|3|all)",
240 N_("copy out the files from named stage"),
241 PARSE_OPT_NONEG, option_parse_stage),
242 OPT_END()
245 if (argc == 2 && !strcmp(argv[1], "-h"))
246 usage_with_options(builtin_checkout_index_usage,
247 builtin_checkout_index_options);
248 git_config(git_default_config, NULL);
249 prefix_length = prefix ? strlen(prefix) : 0;
251 prepare_repo_settings(the_repository);
252 the_repository->settings.command_requires_full_index = 0;
254 if (repo_read_index(the_repository) < 0) {
255 die("invalid cache");
258 argc = parse_options(argc, argv, prefix, builtin_checkout_index_options,
259 builtin_checkout_index_usage, 0);
260 state.istate = &the_index;
261 state.force = force;
262 state.quiet = quiet;
263 state.not_new = not_new;
265 if (!state.base_dir)
266 state.base_dir = "";
267 state.base_dir_len = strlen(state.base_dir);
270 * when --prefix is specified we do not want to update cache.
272 if (index_opt && !state.base_dir_len && !to_tempfile) {
273 state.refresh_cache = 1;
274 state.istate = &the_index;
275 repo_hold_locked_index(the_repository, &lock_file,
276 LOCK_DIE_ON_ERROR);
279 get_parallel_checkout_configs(&pc_workers, &pc_threshold);
280 if (pc_workers > 1)
281 init_parallel_checkout();
283 /* Check out named files first */
284 for (i = 0; i < argc; i++) {
285 const char *arg = argv[i];
286 char *p;
288 if (all)
289 die("git checkout-index: don't mix '--all' and explicit filenames");
290 if (read_from_stdin)
291 die("git checkout-index: don't mix '--stdin' and explicit filenames");
292 p = prefix_path(prefix, prefix_length, arg);
293 err |= checkout_file(p, prefix);
294 free(p);
297 if (read_from_stdin) {
298 struct strbuf buf = STRBUF_INIT;
299 struct strbuf unquoted = STRBUF_INIT;
300 strbuf_getline_fn getline_fn;
302 if (all)
303 die("git checkout-index: don't mix '--all' and '--stdin'");
305 getline_fn = nul_term_line ? strbuf_getline_nul : strbuf_getline_lf;
306 while (getline_fn(&buf, stdin) != EOF) {
307 char *p;
308 if (!nul_term_line && buf.buf[0] == '"') {
309 strbuf_reset(&unquoted);
310 if (unquote_c_style(&unquoted, buf.buf, NULL))
311 die("line is badly quoted");
312 strbuf_swap(&buf, &unquoted);
314 p = prefix_path(prefix, prefix_length, buf.buf);
315 err |= checkout_file(p, prefix);
316 free(p);
318 strbuf_release(&unquoted);
319 strbuf_release(&buf);
322 if (all)
323 err |= checkout_all(prefix, prefix_length);
325 if (pc_workers > 1)
326 err |= run_parallel_checkout(&state, pc_workers, pc_threshold,
327 NULL, NULL);
329 if (err)
330 return 1;
332 if (is_lock_file_locked(&lock_file) &&
333 write_locked_index(&the_index, &lock_file, COMMIT_LOCK))
334 die("Unable to write new index file");
335 return 0;