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 #define CHECKOUT_ALL 4
44 static const char *prefix
;
45 static int prefix_length
;
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][MAXPATHLEN
+1];
51 static struct checkout state
= {
60 static void write_tempfile_record (const char *name
)
64 if (CHECKOUT_ALL
== checkout_stage
) {
65 for (i
= 1; i
< 4; i
++) {
69 fputs(topath
[i
], stdout
);
74 fputs(topath
[checkout_stage
], stdout
);
77 write_name_quoted("", 0, name
+ prefix_length
,
78 line_termination
, stdout
);
79 putchar(line_termination
);
81 for (i
= 0; i
< 4; i
++) {
86 static int checkout_file(const char *name
)
88 int namelen
= strlen(name
);
89 int pos
= cache_name_pos(name
, namelen
);
90 int has_same_name
= 0;
97 while (pos
< active_nr
) {
98 struct cache_entry
*ce
= active_cache
[pos
];
99 if (ce_namelen(ce
) != namelen
||
100 memcmp(ce
->name
, name
, namelen
))
104 if (ce_stage(ce
) != checkout_stage
105 && (CHECKOUT_ALL
!= checkout_stage
|| !ce_stage(ce
)))
108 if (checkout_entry(ce
, &state
,
109 to_tempfile
? topath
[ce_stage(ce
)] : NULL
) < 0)
115 write_tempfile_record(name
);
116 return errs
> 0 ? -1 : 0;
120 fprintf(stderr
, "git-checkout-index: %s ", name
);
122 fprintf(stderr
, "is not in the cache");
123 else if (checkout_stage
)
124 fprintf(stderr
, "does not exist at stage %d",
127 fprintf(stderr
, "is unmerged");
133 static int checkout_all(void)
136 struct cache_entry
* last_ce
= NULL
;
138 for (i
= 0; i
< active_nr
; i
++) {
139 struct cache_entry
*ce
= active_cache
[i
];
140 if (ce_stage(ce
) != checkout_stage
141 && (CHECKOUT_ALL
!= checkout_stage
|| !ce_stage(ce
)))
143 if (prefix
&& *prefix
&&
144 (ce_namelen(ce
) <= prefix_length
||
145 memcmp(prefix
, ce
->name
, prefix_length
)))
147 if (last_ce
&& to_tempfile
) {
148 if (ce_namelen(last_ce
) != ce_namelen(ce
)
149 || memcmp(last_ce
->name
, ce
->name
, ce_namelen(ce
)))
150 write_tempfile_record(last_ce
->name
);
152 if (checkout_entry(ce
, &state
,
153 to_tempfile
? topath
[ce_stage(ce
)] : NULL
) < 0)
157 if (last_ce
&& to_tempfile
)
158 write_tempfile_record(last_ce
->name
);
160 /* we have already done our error reporting.
161 * exit with the same code as die().
167 static const char checkout_cache_usage
[] =
168 "git-checkout-index [-u] [-q] [-a] [-f] [-n] [--stage=[123]|all] [--prefix=<string>] [--temp] [--] <file>...";
170 static struct cache_file cache_file
;
172 int main(int argc
, char **argv
)
177 int read_from_stdin
= 0;
179 prefix
= setup_git_directory();
180 git_config(git_default_config
);
181 prefix_length
= prefix
? strlen(prefix
) : 0;
183 if (read_cache() < 0) {
184 die("invalid cache");
187 for (i
= 1; i
< argc
; i
++) {
188 const char *arg
= argv
[i
];
190 if (!strcmp(arg
, "--")) {
194 if (!strcmp(arg
, "-a") || !strcmp(arg
, "--all")) {
198 if (!strcmp(arg
, "-f") || !strcmp(arg
, "--force")) {
202 if (!strcmp(arg
, "-q") || !strcmp(arg
, "--quiet")) {
206 if (!strcmp(arg
, "-n") || !strcmp(arg
, "--no-create")) {
210 if (!strcmp(arg
, "-u") || !strcmp(arg
, "--index")) {
211 state
.refresh_cache
= 1;
213 newfd
= hold_index_file_for_update
217 die("cannot open index.lock file.");
220 if (!strcmp(arg
, "-z")) {
221 line_termination
= 0;
224 if (!strcmp(arg
, "--stdin")) {
226 die("--stdin must be at the end");
228 i
++; /* do not consider arg as a file name */
231 if (!strcmp(arg
, "--temp")) {
235 if (!strncmp(arg
, "--prefix=", 9)) {
236 state
.base_dir
= arg
+9;
237 state
.base_dir_len
= strlen(state
.base_dir
);
240 if (!strncmp(arg
, "--stage=", 8)) {
241 if (!strcmp(arg
+ 8, "all")) {
243 checkout_stage
= CHECKOUT_ALL
;
246 if ('1' <= ch
&& ch
<= '3')
247 checkout_stage
= arg
[8] - '0';
249 die("stage should be between 1 and 3 or all");
254 usage(checkout_cache_usage
);
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 close(newfd
); newfd
= -1;
264 rollback_index_file(&cache_file
);
266 state
.refresh_cache
= 0;
269 /* Check out named files first */
270 for ( ; 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
);
280 if (p
< arg
|| p
> arg
+ strlen(arg
))
284 if (read_from_stdin
) {
287 die("git-checkout-index: don't mix '--all' and '--stdin'");
293 read_line(&buf
, stdin
, line_termination
);
296 if (line_termination
&& buf
.buf
[0] == '"')
297 path_name
= unquote_c_style(buf
.buf
, NULL
);
300 p
= prefix_path(prefix
, prefix_length
, path_name
);
302 if (p
< path_name
|| p
> path_name
+ strlen(path_name
))
304 if (path_name
!= buf
.buf
)
313 (write_cache(newfd
, active_cache
, active_nr
) ||
314 commit_index_file(&cache_file
)))
315 die("Unable to write new cachefile");