2 * Check-out files from the "current cache directory"
4 * Copyright (C) 2005 Linus Torvalds
6 * Careful: order of argument flags does matter. For example,
8 * git checkout-index -a -f file.c
10 * Will first check out all files listed in the cache (but not
11 * overwrite any old ones), and then force-checkout "file.c" a
12 * second time (ie that one _will_ overwrite any old contents
13 * with the same filename).
15 * Also, just doing "git checkout-index" does nothing. You probably
16 * meant "git checkout-index -a". And if you want to force it, you
17 * want "git checkout-index -f -a".
19 * Intuitiveness is not the goal here. Repeatability is. The
20 * reason for the "no arguments means no work" thing is that
21 * from scripts you are supposed to be able to do things like
23 * find . -name '*.h' -print0 | xargs -0 git checkout-index -f --
27 * find . -name '*.h' -print0 | git checkout-index -f -z --stdin
29 * which will force all existing *.h files to be replaced with
30 * their cached copies. If an empty command line implied "all",
31 * then this would force-refresh everything in the cache, which
34 * Oh, and the "--" is just a good idea when you know the rest
35 * will be filenames. Just so that you wouldn't have a filename
36 * of "-a" causing problems (not possible in the above example,
37 * but get used to it in scripting!).
42 #include "cache-tree.h"
43 #include "parse-options.h"
45 #define CHECKOUT_ALL 4
46 static int line_termination
= '\n';
47 static int checkout_stage
; /* default to checkout stage0 */
48 static int to_tempfile
;
49 static char topath
[4][PATH_MAX
+ 1];
51 static struct checkout state
;
53 static void write_tempfile_record(const char *name
, int prefix_length
)
57 if (CHECKOUT_ALL
== checkout_stage
) {
58 for (i
= 1; i
< 4; i
++) {
62 fputs(topath
[i
], stdout
);
67 fputs(topath
[checkout_stage
], stdout
);
70 write_name_quoted(name
+ prefix_length
, stdout
, line_termination
);
72 for (i
= 0; i
< 4; i
++) {
77 static int checkout_file(const char *name
, int prefix_length
)
79 int namelen
= strlen(name
);
80 int pos
= cache_name_pos(name
, namelen
);
81 int has_same_name
= 0;
88 while (pos
< active_nr
) {
89 struct cache_entry
*ce
= active_cache
[pos
];
90 if (ce_namelen(ce
) != namelen
||
91 memcmp(ce
->name
, name
, namelen
))
95 if (ce_stage(ce
) != checkout_stage
96 && (CHECKOUT_ALL
!= checkout_stage
|| !ce_stage(ce
)))
99 if (checkout_entry(ce
, &state
,
100 to_tempfile
? topath
[ce_stage(ce
)] : NULL
) < 0)
106 write_tempfile_record(name
, prefix_length
);
107 return errs
> 0 ? -1 : 0;
111 fprintf(stderr
, "git checkout-index: %s ", name
);
113 fprintf(stderr
, "is not in the cache");
114 else if (checkout_stage
)
115 fprintf(stderr
, "does not exist at stage %d",
118 fprintf(stderr
, "is unmerged");
124 static void checkout_all(const char *prefix
, int prefix_length
)
127 struct cache_entry
*last_ce
= NULL
;
129 for (i
= 0; i
< active_nr
; i
++) {
130 struct cache_entry
*ce
= active_cache
[i
];
131 if (ce_stage(ce
) != checkout_stage
132 && (CHECKOUT_ALL
!= checkout_stage
|| !ce_stage(ce
)))
134 if (prefix
&& *prefix
&&
135 (ce_namelen(ce
) <= prefix_length
||
136 memcmp(prefix
, ce
->name
, prefix_length
)))
138 if (last_ce
&& to_tempfile
) {
139 if (ce_namelen(last_ce
) != ce_namelen(ce
)
140 || memcmp(last_ce
->name
, ce
->name
, ce_namelen(ce
)))
141 write_tempfile_record(last_ce
->name
, prefix_length
);
143 if (checkout_entry(ce
, &state
,
144 to_tempfile
? topath
[ce_stage(ce
)] : NULL
) < 0)
148 if (last_ce
&& to_tempfile
)
149 write_tempfile_record(last_ce
->name
, prefix_length
);
151 /* we have already done our error reporting.
152 * exit with the same code as die().
157 static const char * const builtin_checkout_index_usage
[] = {
158 "git checkout-index [options] [--] [<file>...]",
162 static struct lock_file lock_file
;
164 static int option_parse_u(const struct option
*opt
,
165 const char *arg
, int unset
)
167 int *newfd
= opt
->value
;
169 state
.refresh_cache
= 1;
171 *newfd
= hold_locked_index(&lock_file
, 1);
175 static int option_parse_z(const struct option
*opt
,
176 const char *arg
, int unset
)
179 line_termination
= '\n';
181 line_termination
= 0;
185 static int option_parse_prefix(const struct option
*opt
,
186 const char *arg
, int unset
)
188 state
.base_dir
= arg
;
189 state
.base_dir_len
= strlen(arg
);
193 static int option_parse_stage(const struct option
*opt
,
194 const char *arg
, int unset
)
196 if (!strcmp(arg
, "all")) {
198 checkout_stage
= CHECKOUT_ALL
;
201 if ('1' <= ch
&& ch
<= '3')
202 checkout_stage
= arg
[0] - '0';
204 die("stage should be between 1 and 3 or all");
209 int cmd_checkout_index(int argc
, const char **argv
, const char *prefix
)
214 int read_from_stdin
= 0;
216 int force
= 0, quiet
= 0, not_new
= 0;
217 struct option builtin_checkout_index_options
[] = {
218 OPT_BOOLEAN('a', "all", &all
,
219 "checks out all files in the index"),
220 OPT__FORCE(&force
, "forces overwrite of existing files"),
222 "no warning for existing files and files not in index"),
223 OPT_BOOLEAN('n', "no-create", ¬_new
,
224 "don't checkout new files"),
225 { OPTION_CALLBACK
, 'u', "index", &newfd
, NULL
,
226 "update stat information in the index file",
227 PARSE_OPT_NOARG
, option_parse_u
},
228 { OPTION_CALLBACK
, 'z', NULL
, NULL
, NULL
,
229 "paths are separated with NUL character",
230 PARSE_OPT_NOARG
, option_parse_z
},
231 OPT_BOOLEAN(0, "stdin", &read_from_stdin
,
232 "read list of paths from the standard input"),
233 OPT_BOOLEAN(0, "temp", &to_tempfile
,
234 "write the content to temporary files"),
235 OPT_CALLBACK(0, "prefix", NULL
, "string",
236 "when creating files, prepend <string>",
237 option_parse_prefix
),
238 OPT_CALLBACK(0, "stage", NULL
, NULL
,
239 "copy out the files from named stage",
244 git_config(git_default_config
, NULL
);
246 prefix_length
= prefix
? strlen(prefix
) : 0;
248 if (read_cache() < 0) {
249 die("invalid cache");
252 argc
= parse_options(argc
, argv
, prefix
, builtin_checkout_index_options
,
253 builtin_checkout_index_usage
, 0);
256 state
.not_new
= not_new
;
258 if (state
.base_dir_len
|| to_tempfile
) {
259 /* when --prefix is specified we do not
260 * want to update cache.
262 if (state
.refresh_cache
) {
263 rollback_lock_file(&lock_file
);
266 state
.refresh_cache
= 0;
269 /* Check out named files first */
270 for (i
= 0; i
< argc
; i
++) {
271 const char *arg
= argv
[i
];
275 die("git checkout-index: don't mix '--all' and explicit filenames");
277 die("git checkout-index: don't mix '--stdin' and explicit filenames");
278 p
= prefix_path(prefix
, prefix_length
, arg
);
279 checkout_file(p
, prefix_length
);
280 if (p
< arg
|| p
> arg
+ strlen(arg
))
284 if (read_from_stdin
) {
285 struct strbuf buf
= STRBUF_INIT
, nbuf
= STRBUF_INIT
;
288 die("git checkout-index: don't mix '--all' and '--stdin'");
290 while (strbuf_getline(&buf
, stdin
, line_termination
) != EOF
) {
292 if (line_termination
&& buf
.buf
[0] == '"') {
294 if (unquote_c_style(&nbuf
, buf
.buf
, NULL
))
295 die("line is badly quoted");
296 strbuf_swap(&buf
, &nbuf
);
298 p
= prefix_path(prefix
, prefix_length
, buf
.buf
);
299 checkout_file(p
, prefix_length
);
300 if (p
< buf
.buf
|| p
> buf
.buf
+ buf
.len
)
303 strbuf_release(&nbuf
);
304 strbuf_release(&buf
);
308 checkout_all(prefix
, prefix_length
);
311 (write_cache(newfd
, active_cache
, active_nr
) ||
312 commit_locked_index(&lock_file
)))
313 die("Unable to write new index file");