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 const char *prefix;
46 static int prefix_length;
47 static int line_termination = '\n';
48 static int checkout_stage; /* default to checkout stage0 */
49 static int to_tempfile;
50 static char topath[4][MAXPATHLEN+1];
52 static struct checkout state;
54 static void write_tempfile_record (const char *name)
58 if (CHECKOUT_ALL == checkout_stage) {
59 for (i = 1; i < 4; i++) {
63 fputs(topath[i], stdout);
68 fputs(topath[checkout_stage], stdout);
71 write_name_quoted("", 0, name + prefix_length,
72 line_termination, stdout);
73 putchar(line_termination);
75 for (i = 0; i < 4; i++) {
80 static int checkout_file(const char *name)
82 int namelen = strlen(name);
83 int pos = cache_name_pos(name, namelen);
84 int has_same_name = 0;
91 while (pos < active_nr) {
92 struct cache_entry *ce = active_cache[pos];
93 if (ce_namelen(ce) != namelen ||
94 memcmp(ce->name, name, namelen))
98 if (ce_stage(ce) != checkout_stage
99 && (CHECKOUT_ALL != checkout_stage || !ce_stage(ce)))
102 if (checkout_entry(ce, &state,
103 to_tempfile ? topath[ce_stage(ce)] : NULL) < 0)
109 write_tempfile_record(name);
110 return errs > 0 ? -1 : 0;
114 fprintf(stderr, "git-checkout-index: %s ", name);
116 fprintf(stderr, "is not in the cache");
117 else if (checkout_stage)
118 fprintf(stderr, "does not exist at stage %d",
121 fprintf(stderr, "is unmerged");
127 static int checkout_all(void)
130 struct cache_entry* last_ce = NULL;
132 for (i = 0; i < active_nr ; i++) {
133 struct cache_entry *ce = active_cache[i];
134 if (ce_stage(ce) != checkout_stage
135 && (CHECKOUT_ALL != checkout_stage || !ce_stage(ce)))
137 if (prefix && *prefix &&
138 (ce_namelen(ce) <= prefix_length ||
139 memcmp(prefix, ce->name, prefix_length)))
141 if (last_ce && to_tempfile) {
142 if (ce_namelen(last_ce) != ce_namelen(ce)
143 || memcmp(last_ce->name, ce->name, ce_namelen(ce)))
144 write_tempfile_record(last_ce->name);
146 if (checkout_entry(ce, &state,
147 to_tempfile ? topath[ce_stage(ce)] : NULL) < 0)
151 if (last_ce && to_tempfile)
152 write_tempfile_record(last_ce->name);
154 /* we have already done our error reporting.
155 * exit with the same code as die().
161 static const char checkout_cache_usage[] =
162 "git-checkout-index [-u] [-q] [-a] [-f] [-n] [--stage=[123]|all] [--prefix=<string>] [--temp] [--] <file>...";
164 static struct lock_file lock_file;
166 int main(int argc, char **argv)
171 int read_from_stdin = 0;
174 prefix = setup_git_directory();
175 git_config(git_default_config);
176 prefix_length = prefix ? strlen(prefix) : 0;
178 if (read_cache() < 0) {
179 die("invalid cache");
182 for (i = 1; i < argc; i++) {
183 const char *arg = argv[i];
185 if (!strcmp(arg, "--")) {
189 if (!strcmp(arg, "-a") || !strcmp(arg, "--all")) {
193 if (!strcmp(arg, "-f") || !strcmp(arg, "--force")) {
197 if (!strcmp(arg, "-q") || !strcmp(arg, "--quiet")) {
201 if (!strcmp(arg, "-n") || !strcmp(arg, "--no-create")) {
205 if (!strcmp(arg, "-u") || !strcmp(arg, "--index")) {
206 state.refresh_cache = 1;
208 newfd = hold_lock_file_for_update
209 (&lock_file, get_index_file());
211 die("cannot open index.lock file.");
214 if (!strcmp(arg, "-z")) {
215 line_termination = 0;
218 if (!strcmp(arg, "--stdin")) {
220 die("--stdin must be at the end");
222 i++; /* do not consider arg as a file name */
225 if (!strcmp(arg, "--temp")) {
229 if (!strncmp(arg, "--prefix=", 9)) {
230 state.base_dir = arg+9;
231 state.base_dir_len = strlen(state.base_dir);
234 if (!strncmp(arg, "--stage=", 8)) {
235 if (!strcmp(arg + 8, "all")) {
237 checkout_stage = CHECKOUT_ALL;
240 if ('1' <= ch && ch <= '3')
241 checkout_stage = arg[8] - '0';
243 die("stage should be between 1 and 3 or all");
248 usage(checkout_cache_usage);
252 if (state.base_dir_len || to_tempfile) {
253 /* when --prefix is specified we do not
254 * want to update cache.
256 if (state.refresh_cache) {
257 close(newfd); newfd = -1;
258 rollback_lock_file(&lock_file);
260 state.refresh_cache = 0;
263 /* Check out named files first */
264 for ( ; i < argc; i++) {
265 const char *arg = argv[i];
269 die("git-checkout-index: don't mix '--all' and explicit filenames");
271 die("git-checkout-index: don't mix '--stdin' and explicit filenames");
272 p = prefix_path(prefix, prefix_length, arg);
274 if (p < arg || p > arg + strlen(arg))
278 if (read_from_stdin) {
281 die("git-checkout-index: don't mix '--all' and '--stdin'");
287 read_line(&buf, stdin, line_termination);
290 if (line_termination && buf.buf[0] == '"')
291 path_name = unquote_c_style(buf.buf, NULL);
294 p = prefix_path(prefix, prefix_length, path_name);
296 if (p < path_name || p > path_name + strlen(path_name))
298 if (path_name != buf.buf)
307 (write_cache(newfd, active_cache, active_nr) ||
308 close(newfd) || commit_lock_file(&lock_file)))
309 die("Unable to write new index file");