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-cache -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-cache" does nothing. You probably
16 * meant "git-checkout-cache -a". And if you want to force it, you
17 * want "git-checkout-cache -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-cache -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 struct checkout state
= {
46 static int checkout_file(const char *name
)
48 int pos
= cache_name_pos(name
, strlen(name
));
53 "git-checkout-cache: %s is %s.\n",
56 !strcmp(active_cache
[pos
]->name
, name
)) ?
57 "unmerged" : "not in the cache");
61 return checkout_entry(active_cache
[pos
], &state
);
64 static int checkout_all(void)
68 for (i
= 0; i
< active_nr
; i
++) {
69 struct cache_entry
*ce
= active_cache
[i
];
72 if (checkout_entry(ce
, &state
) < 0)
78 static const char checkout_cache_usage
[] =
79 "git-checkout-cache [-u] [-q] [-a] [-f] [-n] [--prefix=<string>] [--] <file>...";
81 static struct cache_file cache_file
;
83 int main(int argc
, char **argv
)
85 int i
, force_filename
= 0;
88 if (read_cache() < 0) {
92 for (i
= 1; i
< argc
; i
++) {
93 const char *arg
= argv
[i
];
94 if (!force_filename
) {
95 if (!strcmp(arg
, "-a")) {
99 if (!strcmp(arg
, "--")) {
103 if (!strcmp(arg
, "-f")) {
107 if (!strcmp(arg
, "-q")) {
111 if (!strcmp(arg
, "-n")) {
115 if (!strcmp(arg
, "-u")) {
116 state
.refresh_cache
= 1;
118 newfd
= hold_index_file_for_update
122 die("cannot open index.lock file.");
125 if (!memcmp(arg
, "--prefix=", 9)) {
126 state
.base_dir
= arg
+9;
127 state
.base_dir_len
= strlen(state
.base_dir
);
131 usage(checkout_cache_usage
);
133 if (state
.base_dir_len
) {
134 /* when --prefix is specified we do not
135 * want to update cache.
137 if (state
.refresh_cache
) {
138 close(newfd
); newfd
= -1;
139 rollback_index_file(&cache_file
);
141 state
.refresh_cache
= 0;
147 (write_cache(newfd
, active_cache
, active_nr
) ||
148 commit_index_file(&cache_file
)))
149 die("Unable to write new cachefile");