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!).
43 static const char *prefix
;
44 static int prefix_length
;
45 static int checkout_stage
; /* default to checkout stage0 */
47 static struct checkout state
= {
56 static int checkout_file(const char *name
)
58 int namelen
= strlen(name
);
59 int pos
= cache_name_pos(name
, namelen
);
60 int has_same_name
= 0;
65 while (pos
< active_nr
) {
66 struct cache_entry
*ce
= active_cache
[pos
];
67 if (ce_namelen(ce
) != namelen
||
68 memcmp(ce
->name
, name
, namelen
))
71 if (checkout_stage
== ce_stage(ce
))
72 return checkout_entry(ce
, &state
);
77 fprintf(stderr
, "git-checkout-index: %s ", name
);
79 fprintf(stderr
, "is not in the cache");
80 else if (checkout_stage
)
81 fprintf(stderr
, "does not exist at stage %d",
84 fprintf(stderr
, "is unmerged");
90 static int checkout_all(void)
94 for (i
= 0; i
< active_nr
; i
++) {
95 struct cache_entry
*ce
= active_cache
[i
];
96 if (ce_stage(ce
) != checkout_stage
)
98 if (prefix
&& *prefix
&&
99 (ce_namelen(ce
) <= prefix_length
||
100 memcmp(prefix
, ce
->name
, prefix_length
)))
102 if (checkout_entry(ce
, &state
) < 0)
106 /* we have already done our error reporting.
107 * exit with the same code as die().
113 static const char checkout_cache_usage
[] =
114 "git-checkout-index [-u] [-q] [-a] [-f] [-n] [--stage=[123]] [--prefix=<string>] [--] <file>...";
116 static struct cache_file cache_file
;
118 int main(int argc
, char **argv
)
123 int read_from_stdin
= 0;
124 int line_termination
= '\n';
126 prefix
= setup_git_directory();
127 git_config(git_default_config
);
128 prefix_length
= prefix
? strlen(prefix
) : 0;
130 if (read_cache() < 0) {
131 die("invalid cache");
134 for (i
= 1; i
< argc
; i
++) {
135 const char *arg
= argv
[i
];
137 if (!strcmp(arg
, "--")) {
141 if (!strcmp(arg
, "-a") || !strcmp(arg
, "--all")) {
145 if (!strcmp(arg
, "-f") || !strcmp(arg
, "--force")) {
149 if (!strcmp(arg
, "-q") || !strcmp(arg
, "--quiet")) {
153 if (!strcmp(arg
, "-n") || !strcmp(arg
, "--no-create")) {
157 if (!strcmp(arg
, "-u") || !strcmp(arg
, "--index")) {
158 state
.refresh_cache
= 1;
160 newfd
= hold_index_file_for_update
164 die("cannot open index.lock file.");
167 if (!strcmp(arg
, "-z")) {
168 line_termination
= 0;
171 if (!strcmp(arg
, "--stdin")) {
173 die("--stdin must be at the end");
175 i
++; /* do not consider arg as a file name */
178 if (!strncmp(arg
, "--prefix=", 9)) {
179 state
.base_dir
= arg
+9;
180 state
.base_dir_len
= strlen(state
.base_dir
);
183 if (!strncmp(arg
, "--stage=", 8)) {
185 if ('1' <= ch
&& ch
<= '3')
186 checkout_stage
= arg
[8] - '0';
188 die("stage should be between 1 and 3");
192 usage(checkout_cache_usage
);
196 if (state
.base_dir_len
) {
197 /* when --prefix is specified we do not
198 * want to update cache.
200 if (state
.refresh_cache
) {
201 close(newfd
); newfd
= -1;
202 rollback_index_file(&cache_file
);
204 state
.refresh_cache
= 0;
207 /* Check out named files first */
208 for ( ; i
< argc
; i
++) {
209 const char *arg
= argv
[i
];
212 die("git-checkout-index: don't mix '--all' and explicit filenames");
214 die("git-checkout-index: don't mix '--stdin' and explicit filenames");
215 checkout_file(prefix_path(prefix
, prefix_length
, arg
));
218 if (read_from_stdin
) {
221 die("git-checkout-index: don't mix '--all' and '--stdin'");
225 read_line(&buf
, stdin
, line_termination
);
228 if (line_termination
&& buf
.buf
[0] == '"')
229 path_name
= unquote_c_style(buf
.buf
, NULL
);
232 checkout_file(prefix_path(prefix
, prefix_length
, path_name
));
233 if (path_name
!= buf
.buf
)
242 (write_cache(newfd
, active_cache
, active_nr
) ||
243 commit_index_file(&cache_file
)))
244 die("Unable to write new cachefile");