2 * Check-out files from the "current cache directory"
4 * Copyright (C) 2005 Linus Torvalds
7 #define USE_THE_INDEX_COMPATIBILITY_MACROS
12 #include "cache-tree.h"
13 #include "parse-options.h"
16 #define CHECKOUT_ALL 4
17 static int nul_term_line;
18 static int checkout_stage; /* default to checkout stage0 */
19 static int to_tempfile;
20 static char topath[4][TEMPORARY_FILENAME_LENGTH + 1];
22 static struct checkout state = CHECKOUT_INIT;
24 static void write_tempfile_record(const char *name, const char *prefix)
27 int have_tempname = 0;
29 if (CHECKOUT_ALL == checkout_stage) {
30 for (i = 1; i < 4; i++)
37 for (i = 1; i < 4; i++) {
41 fputs(topath[i], stdout);
46 } else if (topath[checkout_stage][0]) {
48 fputs(topath[checkout_stage], stdout);
53 write_name_quoted_relative(name, prefix, stdout,
54 nul_term_line ? '\0' : '\n');
57 for (i = 0; i < 4; i++) {
62 static int checkout_file(const char *name, const char *prefix)
64 int namelen = strlen(name);
65 int pos = cache_name_pos(name, namelen);
66 int has_same_name = 0;
73 while (pos < active_nr) {
74 struct cache_entry *ce = active_cache[pos];
75 if (ce_namelen(ce) != namelen ||
76 memcmp(ce->name, name, namelen))
80 if (ce_stage(ce) != checkout_stage
81 && (CHECKOUT_ALL != checkout_stage || !ce_stage(ce)))
84 if (checkout_entry(ce, &state,
85 to_tempfile ? topath[ce_stage(ce)] : NULL,
92 write_tempfile_record(name, prefix);
93 return errs > 0 ? -1 : 0;
97 * At this point we know we didn't try to check anything out. If it was
98 * because we did find an entry but it was stage 0, that's not an
101 if (has_same_name && checkout_stage == CHECKOUT_ALL)
105 fprintf(stderr, "git checkout-index: %s ", name);
107 fprintf(stderr, "is not in the cache");
108 else if (checkout_stage)
109 fprintf(stderr, "does not exist at stage %d",
112 fprintf(stderr, "is unmerged");
118 static void checkout_all(const char *prefix, int prefix_length)
121 struct cache_entry *last_ce = NULL;
123 for (i = 0; i < active_nr ; i++) {
124 struct cache_entry *ce = active_cache[i];
125 if (ce_stage(ce) != checkout_stage
126 && (CHECKOUT_ALL != checkout_stage || !ce_stage(ce)))
128 if (prefix && *prefix &&
129 (ce_namelen(ce) <= prefix_length ||
130 memcmp(prefix, ce->name, prefix_length)))
132 if (last_ce && to_tempfile) {
133 if (ce_namelen(last_ce) != ce_namelen(ce)
134 || memcmp(last_ce->name, ce->name, ce_namelen(ce)))
135 write_tempfile_record(last_ce->name, prefix);
137 if (checkout_entry(ce, &state,
138 to_tempfile ? topath[ce_stage(ce)] : NULL,
143 if (last_ce && to_tempfile)
144 write_tempfile_record(last_ce->name, prefix);
146 /* we have already done our error reporting.
147 * exit with the same code as die().
152 static const char * const builtin_checkout_index_usage[] = {
153 N_("git checkout-index [<options>] [--] [<file>...]"),
157 static int option_parse_stage(const struct option *opt,
158 const char *arg, int unset)
160 BUG_ON_OPT_NEG(unset);
162 if (!strcmp(arg, "all")) {
164 checkout_stage = CHECKOUT_ALL;
167 if ('1' <= ch && ch <= '3')
168 checkout_stage = arg[0] - '0';
170 die(_("stage should be between 1 and 3 or all"));
175 int cmd_checkout_index(int argc, const char **argv, const char *prefix)
178 struct lock_file lock_file = LOCK_INIT;
180 int read_from_stdin = 0;
182 int force = 0, quiet = 0, not_new = 0;
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),
190 N_("no warning for existing files and files not in index")),
191 OPT_BOOL('n', "no-create", ¬_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),
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;
224 state.not_new = not_new;
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 /* Check out named files first */
240 for (i = 0; i < argc; i++) {
241 const char *arg = argv[i];
245 die("git checkout-index: don't mix '--all' and explicit filenames");
247 die("git checkout-index: don't mix '--stdin' and explicit filenames");
248 p = prefix_path(prefix, prefix_length, arg);
249 err |= checkout_file(p, prefix);
253 if (read_from_stdin) {
254 struct strbuf buf = STRBUF_INIT;
255 struct strbuf unquoted = STRBUF_INIT;
256 strbuf_getline_fn getline_fn;
259 die("git checkout-index: don't mix '--all' and '--stdin'");
261 getline_fn = nul_term_line ? strbuf_getline_nul : strbuf_getline_lf;
262 while (getline_fn(&buf, stdin) != EOF) {
264 if (!nul_term_line && buf.buf[0] == '"') {
265 strbuf_reset(&unquoted);
266 if (unquote_c_style(&unquoted, buf.buf, NULL))
267 die("line is badly quoted");
268 strbuf_swap(&buf, &unquoted);
270 p = prefix_path(prefix, prefix_length, buf.buf);
271 err |= checkout_file(p, prefix);
274 strbuf_release(&unquoted);
275 strbuf_release(&buf);
282 checkout_all(prefix, prefix_length);
284 if (is_lock_file_locked(&lock_file) &&
285 write_locked_index(&the_index, &lock_file, COMMIT_LOCK))
286 die("Unable to write new index file");