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 --
25 * which will force all existing *.h files to be replaced with
26 * their cached copies. If an empty command line implied "all",
27 * then this would force-refresh everything in the cache, which
30 * Oh, and the "--" is just a good idea when you know the rest
31 * will be filenames. Just so that you wouldn't have a filename
32 * of "-a" causing problems (not possible in the above example,
33 * but get used to it in scripting!).
37 static const char *prefix
;
38 static int prefix_length
;
39 static int checkout_stage
; /* default to checkout stage0 */
41 static struct checkout state
= {
50 static int checkout_file(const char *name
)
52 int namelen
= strlen(name
);
53 int pos
= cache_name_pos(name
, namelen
);
54 int has_same_name
= 0;
59 while (pos
< active_nr
) {
60 struct cache_entry
*ce
= active_cache
[pos
];
61 if (ce_namelen(ce
) != namelen
||
62 memcmp(ce
->name
, name
, namelen
))
65 if (checkout_stage
== ce_stage(ce
))
66 return checkout_entry(ce
, &state
);
71 fprintf(stderr
, "git-checkout-index: %s ", name
);
73 fprintf(stderr
, "is not in the cache");
74 else if (checkout_stage
)
75 fprintf(stderr
, "does not exist at stage %d",
78 fprintf(stderr
, "is unmerged");
84 static int checkout_all(void)
88 for (i
= 0; i
< active_nr
; i
++) {
89 struct cache_entry
*ce
= active_cache
[i
];
90 if (ce_stage(ce
) != checkout_stage
)
92 if (prefix
&& *prefix
&&
93 (ce_namelen(ce
) <= prefix_length
||
94 memcmp(prefix
, ce
->name
, prefix_length
)))
96 if (checkout_entry(ce
, &state
) < 0)
100 /* we have already done our error reporting.
101 * exit with the same code as die().
107 static const char checkout_cache_usage
[] =
108 "git-checkout-index [-u] [-q] [-a] [-f] [-n] [--stage=[123]] [--prefix=<string>] [--] <file>...";
110 static struct cache_file cache_file
;
112 int main(int argc
, char **argv
)
118 prefix
= setup_git_directory();
119 prefix_length
= prefix
? strlen(prefix
) : 0;
121 if (read_cache() < 0) {
122 die("invalid cache");
125 for (i
= 1; i
< argc
; i
++) {
126 const char *arg
= argv
[i
];
128 if (!strcmp(arg
, "--")) {
132 if (!strcmp(arg
, "-a") || !strcmp(arg
, "--all")) {
136 if (!strcmp(arg
, "-f") || !strcmp(arg
, "--force")) {
140 if (!strcmp(arg
, "-q") || !strcmp(arg
, "--quiet")) {
144 if (!strcmp(arg
, "-n") || !strcmp(arg
, "--no-create")) {
148 if (!strcmp(arg
, "-u") || !strcmp(arg
, "--index")) {
149 state
.refresh_cache
= 1;
151 newfd
= hold_index_file_for_update
155 die("cannot open index.lock file.");
158 if (!strncmp(arg
, "--prefix=", 9)) {
159 state
.base_dir
= arg
+9;
160 state
.base_dir_len
= strlen(state
.base_dir
);
163 if (!strncmp(arg
, "--stage=", 8)) {
165 if ('1' <= ch
&& ch
<= '3')
166 checkout_stage
= arg
[8] - '0';
168 die("stage should be between 1 and 3");
172 usage(checkout_cache_usage
);
176 if (state
.base_dir_len
) {
177 /* when --prefix is specified we do not
178 * want to update cache.
180 if (state
.refresh_cache
) {
181 close(newfd
); newfd
= -1;
182 rollback_index_file(&cache_file
);
184 state
.refresh_cache
= 0;
187 /* Check out named files first */
188 for ( ; i
< argc
; i
++) {
189 const char *arg
= argv
[i
];
192 die("git-checkout-index: don't mix '--all' and explicit filenames");
193 checkout_file(prefix_path(prefix
, prefix_length
, arg
));
200 (write_cache(newfd
, active_cache
, active_nr
) ||
201 commit_index_file(&cache_file
)))
202 die("Unable to write new cachefile");