t0301: fixes for windows compatibility
[git/debian.git] / builtin / checkout-index.c
blobe21620d964e5e89f7254dc4d99597d220fb3d650
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 "lockfile.h"
11 #include "quote.h"
12 #include "cache-tree.h"
13 #include "parse-options.h"
14 #include "entry.h"
15 #include "parallel-checkout.h"
17 #define CHECKOUT_ALL 4
18 static int nul_term_line;
19 static int checkout_stage; /* default to checkout stage0 */
20 static int to_tempfile;
21 static char topath[4][TEMPORARY_FILENAME_LENGTH + 1];
23 static struct checkout state = CHECKOUT_INIT;
25 static void write_tempfile_record(const char *name, const char *prefix)
27 int i;
28 int have_tempname = 0;
30 if (CHECKOUT_ALL == checkout_stage) {
31 for (i = 1; i < 4; i++)
32 if (topath[i][0]) {
33 have_tempname = 1;
34 break;
37 if (have_tempname) {
38 for (i = 1; i < 4; i++) {
39 if (i > 1)
40 putchar(' ');
41 if (topath[i][0])
42 fputs(topath[i], stdout);
43 else
44 putchar('.');
47 } else if (topath[checkout_stage][0]) {
48 have_tempname = 1;
49 fputs(topath[checkout_stage], stdout);
52 if (have_tempname) {
53 putchar('\t');
54 write_name_quoted_relative(name, prefix, stdout,
55 nul_term_line ? '\0' : '\n');
58 for (i = 0; i < 4; i++) {
59 topath[i][0] = 0;
63 static int checkout_file(const char *name, const char *prefix)
65 int namelen = strlen(name);
66 int pos = cache_name_pos(name, namelen);
67 int has_same_name = 0;
68 int did_checkout = 0;
69 int errs = 0;
71 if (pos < 0)
72 pos = -pos - 1;
74 while (pos < active_nr) {
75 struct cache_entry *ce = active_cache[pos];
76 if (ce_namelen(ce) != namelen ||
77 memcmp(ce->name, name, namelen))
78 break;
79 has_same_name = 1;
80 pos++;
81 if (ce_stage(ce) != checkout_stage
82 && (CHECKOUT_ALL != checkout_stage || !ce_stage(ce)))
83 continue;
84 did_checkout = 1;
85 if (checkout_entry(ce, &state,
86 to_tempfile ? topath[ce_stage(ce)] : NULL,
87 NULL) < 0)
88 errs++;
91 if (did_checkout) {
92 if (to_tempfile)
93 write_tempfile_record(name, prefix);
94 return errs > 0 ? -1 : 0;
98 * At this point we know we didn't try to check anything out. If it was
99 * because we did find an entry but it was stage 0, that's not an
100 * error.
102 if (has_same_name && checkout_stage == CHECKOUT_ALL)
103 return 0;
105 if (!state.quiet) {
106 fprintf(stderr, "git checkout-index: %s ", name);
107 if (!has_same_name)
108 fprintf(stderr, "is not in the cache");
109 else if (checkout_stage)
110 fprintf(stderr, "does not exist at stage %d",
111 checkout_stage);
112 else
113 fprintf(stderr, "is unmerged");
114 fputc('\n', stderr);
116 return -1;
119 static int checkout_all(const char *prefix, int prefix_length)
121 int i, errs = 0;
122 struct cache_entry *last_ce = NULL;
124 /* TODO: audit for interaction with sparse-index. */
125 ensure_full_index(&the_index);
126 for (i = 0; i < active_nr ; i++) {
127 struct cache_entry *ce = active_cache[i];
128 if (ce_stage(ce) != checkout_stage
129 && (CHECKOUT_ALL != checkout_stage || !ce_stage(ce)))
130 continue;
131 if (prefix && *prefix &&
132 (ce_namelen(ce) <= prefix_length ||
133 memcmp(prefix, ce->name, prefix_length)))
134 continue;
135 if (last_ce && to_tempfile) {
136 if (ce_namelen(last_ce) != ce_namelen(ce)
137 || memcmp(last_ce->name, ce->name, ce_namelen(ce)))
138 write_tempfile_record(last_ce->name, prefix);
140 if (checkout_entry(ce, &state,
141 to_tempfile ? topath[ce_stage(ce)] : NULL,
142 NULL) < 0)
143 errs++;
144 last_ce = ce;
146 if (last_ce && to_tempfile)
147 write_tempfile_record(last_ce->name, prefix);
148 return !!errs;
151 static const char * const builtin_checkout_index_usage[] = {
152 N_("git checkout-index [<options>] [--] [<file>...]"),
153 NULL
156 static int option_parse_stage(const struct option *opt,
157 const char *arg, int unset)
159 BUG_ON_OPT_NEG(unset);
161 if (!strcmp(arg, "all")) {
162 to_tempfile = 1;
163 checkout_stage = CHECKOUT_ALL;
164 } else {
165 int ch = arg[0];
166 if ('1' <= ch && ch <= '3')
167 checkout_stage = arg[0] - '0';
168 else
169 die(_("stage should be between 1 and 3 or all"));
171 return 0;
174 int cmd_checkout_index(int argc, const char **argv, const char *prefix)
176 int i;
177 struct lock_file lock_file = LOCK_INIT;
178 int all = 0;
179 int read_from_stdin = 0;
180 int prefix_length;
181 int force = 0, quiet = 0, not_new = 0;
182 int index_opt = 0;
183 int err = 0;
184 int pc_workers, pc_threshold;
185 struct option builtin_checkout_index_options[] = {
186 OPT_BOOL('a', "all", &all,
187 N_("check out all files in the index")),
188 OPT__FORCE(&force, N_("force overwrite of existing files"), 0),
189 OPT__QUIET(&quiet,
190 N_("no warning for existing files and files not in index")),
191 OPT_BOOL('n', "no-create", &not_new,
192 N_("don't checkout new files")),
193 OPT_BOOL('u', "index", &index_opt,
194 N_("update stat information in the index file")),
195 OPT_BOOL('z', NULL, &nul_term_line,
196 N_("paths are separated with NUL character")),
197 OPT_BOOL(0, "stdin", &read_from_stdin,
198 N_("read list of paths from the standard input")),
199 OPT_BOOL(0, "temp", &to_tempfile,
200 N_("write the content to temporary files")),
201 OPT_STRING(0, "prefix", &state.base_dir, N_("string"),
202 N_("when creating files, prepend <string>")),
203 OPT_CALLBACK_F(0, "stage", NULL, "(1|2|3|all)",
204 N_("copy out the files from named stage"),
205 PARSE_OPT_NONEG, option_parse_stage),
206 OPT_END()
209 if (argc == 2 && !strcmp(argv[1], "-h"))
210 usage_with_options(builtin_checkout_index_usage,
211 builtin_checkout_index_options);
212 git_config(git_default_config, NULL);
213 prefix_length = prefix ? strlen(prefix) : 0;
215 if (read_cache() < 0) {
216 die("invalid cache");
219 argc = parse_options(argc, argv, prefix, builtin_checkout_index_options,
220 builtin_checkout_index_usage, 0);
221 state.istate = &the_index;
222 state.force = force;
223 state.quiet = quiet;
224 state.not_new = not_new;
226 if (!state.base_dir)
227 state.base_dir = "";
228 state.base_dir_len = strlen(state.base_dir);
231 * when --prefix is specified we do not want to update cache.
233 if (index_opt && !state.base_dir_len && !to_tempfile) {
234 state.refresh_cache = 1;
235 state.istate = &the_index;
236 hold_locked_index(&lock_file, LOCK_DIE_ON_ERROR);
239 get_parallel_checkout_configs(&pc_workers, &pc_threshold);
240 if (pc_workers > 1)
241 init_parallel_checkout();
243 /* Check out named files first */
244 for (i = 0; i < argc; i++) {
245 const char *arg = argv[i];
246 char *p;
248 if (all)
249 die("git checkout-index: don't mix '--all' and explicit filenames");
250 if (read_from_stdin)
251 die("git checkout-index: don't mix '--stdin' and explicit filenames");
252 p = prefix_path(prefix, prefix_length, arg);
253 err |= checkout_file(p, prefix);
254 free(p);
257 if (read_from_stdin) {
258 struct strbuf buf = STRBUF_INIT;
259 struct strbuf unquoted = STRBUF_INIT;
260 strbuf_getline_fn getline_fn;
262 if (all)
263 die("git checkout-index: don't mix '--all' and '--stdin'");
265 getline_fn = nul_term_line ? strbuf_getline_nul : strbuf_getline_lf;
266 while (getline_fn(&buf, stdin) != EOF) {
267 char *p;
268 if (!nul_term_line && buf.buf[0] == '"') {
269 strbuf_reset(&unquoted);
270 if (unquote_c_style(&unquoted, buf.buf, NULL))
271 die("line is badly quoted");
272 strbuf_swap(&buf, &unquoted);
274 p = prefix_path(prefix, prefix_length, buf.buf);
275 err |= checkout_file(p, prefix);
276 free(p);
278 strbuf_release(&unquoted);
279 strbuf_release(&buf);
282 if (all)
283 err |= checkout_all(prefix, prefix_length);
285 if (pc_workers > 1)
286 err |= run_parallel_checkout(&state, pc_workers, pc_threshold,
287 NULL, NULL);
289 if (err)
290 return 1;
292 if (is_lock_file_locked(&lock_file) &&
293 write_locked_index(&the_index, &lock_file, COMMIT_LOCK))
294 die("Unable to write new index file");
295 return 0;