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"
44 #define CHECKOUT_ALL 4
45 static int line_termination
= '\n';
46 static int checkout_stage
; /* default to checkout stage0 */
47 static int to_tempfile
;
48 static char topath
[4][PATH_MAX
+ 1];
50 static struct checkout state
;
52 static void write_tempfile_record(const char *name
, int prefix_length
)
56 if (CHECKOUT_ALL
== checkout_stage
) {
57 for (i
= 1; i
< 4; i
++) {
61 fputs(topath
[i
], stdout
);
66 fputs(topath
[checkout_stage
], stdout
);
69 write_name_quoted(name
+ prefix_length
, stdout
, line_termination
);
71 for (i
= 0; i
< 4; i
++) {
76 static int checkout_file(const char *name
, int prefix_length
)
78 int namelen
= strlen(name
);
79 int pos
= cache_name_pos(name
, namelen
);
80 int has_same_name
= 0;
87 while (pos
< active_nr
) {
88 struct cache_entry
*ce
= active_cache
[pos
];
89 if (ce_namelen(ce
) != namelen
||
90 memcmp(ce
->name
, name
, namelen
))
94 if (ce_stage(ce
) != checkout_stage
95 && (CHECKOUT_ALL
!= checkout_stage
|| !ce_stage(ce
)))
98 if (checkout_entry(ce
, &state
,
99 to_tempfile
? topath
[ce_stage(ce
)] : NULL
) < 0)
105 write_tempfile_record(name
, prefix_length
);
106 return errs
> 0 ? -1 : 0;
110 fprintf(stderr
, "git-checkout-index: %s ", name
);
112 fprintf(stderr
, "is not in the cache");
113 else if (checkout_stage
)
114 fprintf(stderr
, "does not exist at stage %d",
117 fprintf(stderr
, "is unmerged");
123 static void checkout_all(const char *prefix
, int prefix_length
)
126 struct cache_entry
* last_ce
= NULL
;
128 for (i
= 0; i
< active_nr
; i
++) {
129 struct cache_entry
*ce
= active_cache
[i
];
130 if (ce_stage(ce
) != checkout_stage
131 && (CHECKOUT_ALL
!= checkout_stage
|| !ce_stage(ce
)))
133 if (prefix
&& *prefix
&&
134 (ce_namelen(ce
) <= prefix_length
||
135 memcmp(prefix
, ce
->name
, prefix_length
)))
137 if (last_ce
&& to_tempfile
) {
138 if (ce_namelen(last_ce
) != ce_namelen(ce
)
139 || memcmp(last_ce
->name
, ce
->name
, ce_namelen(ce
)))
140 write_tempfile_record(last_ce
->name
, prefix_length
);
142 if (checkout_entry(ce
, &state
,
143 to_tempfile
? topath
[ce_stage(ce
)] : NULL
) < 0)
147 if (last_ce
&& to_tempfile
)
148 write_tempfile_record(last_ce
->name
, prefix_length
);
150 /* we have already done our error reporting.
151 * exit with the same code as die().
156 static const char checkout_cache_usage
[] =
157 "git-checkout-index [-u] [-q] [-a] [-f] [-n] [--stage=[123]|all] [--prefix=<string>] [--temp] [--] <file>...";
159 static struct lock_file lock_file
;
161 int cmd_checkout_index(int argc
, const char **argv
, const char *prefix
)
166 int read_from_stdin
= 0;
169 git_config(git_default_config
);
171 prefix_length
= prefix
? strlen(prefix
) : 0;
173 if (read_cache() < 0) {
174 die("invalid cache");
177 for (i
= 1; i
< argc
; i
++) {
178 const char *arg
= argv
[i
];
180 if (!strcmp(arg
, "--")) {
184 if (!strcmp(arg
, "-a") || !strcmp(arg
, "--all")) {
188 if (!strcmp(arg
, "-f") || !strcmp(arg
, "--force")) {
192 if (!strcmp(arg
, "-q") || !strcmp(arg
, "--quiet")) {
196 if (!strcmp(arg
, "-n") || !strcmp(arg
, "--no-create")) {
200 if (!strcmp(arg
, "-u") || !strcmp(arg
, "--index")) {
201 state
.refresh_cache
= 1;
203 newfd
= hold_locked_index(&lock_file
, 1);
206 if (!strcmp(arg
, "-z")) {
207 line_termination
= 0;
210 if (!strcmp(arg
, "--stdin")) {
212 die("--stdin must be at the end");
214 i
++; /* do not consider arg as a file name */
217 if (!strcmp(arg
, "--temp")) {
221 if (!prefixcmp(arg
, "--prefix=")) {
222 state
.base_dir
= arg
+9;
223 state
.base_dir_len
= strlen(state
.base_dir
);
226 if (!prefixcmp(arg
, "--stage=")) {
227 if (!strcmp(arg
+ 8, "all")) {
229 checkout_stage
= CHECKOUT_ALL
;
232 if ('1' <= ch
&& ch
<= '3')
233 checkout_stage
= arg
[8] - '0';
235 die("stage should be between 1 and 3 or all");
240 usage(checkout_cache_usage
);
244 if (state
.base_dir_len
|| to_tempfile
) {
245 /* when --prefix is specified we do not
246 * want to update cache.
248 if (state
.refresh_cache
) {
249 close(newfd
); newfd
= -1;
250 rollback_lock_file(&lock_file
);
252 state
.refresh_cache
= 0;
255 /* Check out named files first */
256 for ( ; i
< argc
; i
++) {
257 const char *arg
= argv
[i
];
261 die("git-checkout-index: don't mix '--all' and explicit filenames");
263 die("git-checkout-index: don't mix '--stdin' and explicit filenames");
264 p
= prefix_path(prefix
, prefix_length
, arg
);
265 checkout_file(p
, prefix_length
);
266 if (p
< arg
|| p
> arg
+ strlen(arg
))
270 if (read_from_stdin
) {
271 struct strbuf buf
, nbuf
;
274 die("git-checkout-index: don't mix '--all' and '--stdin'");
276 strbuf_init(&buf
, 0);
277 strbuf_init(&nbuf
, 0);
278 while (strbuf_getline(&buf
, stdin
, line_termination
) != EOF
) {
280 if (line_termination
&& buf
.buf
[0] == '"') {
282 if (unquote_c_style(&nbuf
, buf
.buf
, NULL
))
283 die("line is badly quoted");
284 strbuf_swap(&buf
, &nbuf
);
286 p
= prefix_path(prefix
, prefix_length
, buf
.buf
);
287 checkout_file(p
, prefix_length
);
288 if (p
< buf
.buf
|| p
> buf
.buf
+ buf
.len
)
291 strbuf_release(&nbuf
);
292 strbuf_release(&buf
);
296 checkout_all(prefix
, prefix_length
);
299 (write_cache(newfd
, active_cache
, active_nr
) ||
300 close(newfd
) || commit_locked_index(&lock_file
)))
301 die("Unable to write new index file");