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!).
35 #include <sys/types.h>
39 static int force = 0, quiet = 0, not_new = 0, refresh_cache = 0;
40 static const char *base_dir = "";
41 static int base_dir_len = 0;
43 static void create_directories(const char *path)
45 int len = strlen(path);
46 char *buf = xmalloc(len + 1);
47 const char *slash = path;
49 while ((slash = strchr(slash+1, '/')) != NULL) {
51 memcpy(buf, path, len);
53 if (mkdir(buf, 0755)) {
54 if (errno == EEXIST) {
56 if (len > base_dir_len && force && !unlink(buf) && !mkdir(buf, 0755))
58 if (!stat(buf, &st) && S_ISDIR(st.st_mode))
61 die("cannot create directory at %s", buf);
67 static void remove_subtree(const char *path)
69 DIR *dir = opendir(path);
71 char pathbuf[PATH_MAX];
75 die("cannot opendir %s", path);
76 strcpy(pathbuf, path);
77 name = pathbuf + strlen(path);
79 while ((de = readdir(dir)) != NULL) {
81 if ((de->d_name[0] == '.') &&
82 ((de->d_name[1] == 0) ||
83 ((de->d_name[1] == '.') && de->d_name[2] == 0)))
85 strcpy(name, de->d_name);
86 if (lstat(pathbuf, &st))
87 die("cannot lstat %s", pathbuf);
88 if (S_ISDIR(st.st_mode))
89 remove_subtree(pathbuf);
90 else if (unlink(pathbuf))
91 die("cannot unlink %s", pathbuf);
95 die("cannot rmdir %s", path);
98 static int create_file(const char *path, unsigned int mode)
102 mode = (mode & 0100) ? 0777 : 0666;
103 create_directories(path);
104 fd = open(path, O_WRONLY | O_TRUNC | O_CREAT, mode);
106 if (errno == EISDIR && force) {
107 remove_subtree(path);
108 fd = open(path, O_WRONLY | O_TRUNC | O_CREAT, mode);
114 static int write_entry(struct cache_entry *ce, const char *path)
123 new = read_sha1_file(ce->sha1, type, &size);
124 if (!new || strcmp(type, "blob")) {
127 return error("git-checkout-cache: unable to read sha1 file of %s (%s)",
128 path, sha1_to_hex(ce->sha1));
130 switch (ntohl(ce->ce_mode) & S_IFMT) {
132 fd = create_file(path, ntohl(ce->ce_mode));
135 return error("git-checkout-cache: unable to create file %s (%s)",
136 path, strerror(errno));
138 wrote = write(fd, new, size);
142 return error("git-checkout-cache: unable to write file %s", path);
145 memcpy(target, new, size);
147 create_directories(path);
148 if (symlink(target, path)) {
150 return error("git-checkout-cache: unable to create symlink %s (%s)",
151 path, strerror(errno));
157 return error("git-checkout-cache: unknown file mode for %s", path);
162 lstat(ce->name, &st);
163 fill_stat_cache_info(ce, &st);
168 static int checkout_entry(struct cache_entry *ce)
171 static char path[MAXPATHLEN+1];
172 int len = base_dir_len;
174 memcpy(path, base_dir, len);
175 strcpy(path + len, ce->name);
177 if (!lstat(path, &st)) {
178 unsigned changed = ce_match_stat(ce, &st);
183 fprintf(stderr, "git-checkout-cache: %s already exists\n", path);
188 * We unlink the old file, to get the new one with the
189 * right permissions (including umask, which is nasty
190 * to emulate by hand - much easier to let the system
191 * just do the right thing)
196 return write_entry(ce, path);
199 static int checkout_file(const char *name)
201 int pos = cache_name_pos(name, strlen(name));
206 "git-checkout-cache: %s is %s.\n",
209 !strcmp(active_cache[pos]->name, name)) ?
210 "unmerged" : "not in the cache");
214 return checkout_entry(active_cache[pos]);
217 static int checkout_all(void)
221 for (i = 0; i < active_nr ; i++) {
222 struct cache_entry *ce = active_cache[i];
225 if (checkout_entry(ce) < 0)
231 int main(int argc, char **argv)
233 int i, force_filename = 0;
234 struct cache_file cache_file;
237 if (read_cache() < 0) {
238 die("invalid cache");
241 for (i = 1; i < argc; i++) {
242 const char *arg = argv[i];
243 if (!force_filename) {
244 if (!strcmp(arg, "-a")) {
248 if (!strcmp(arg, "--")) {
252 if (!strcmp(arg, "-f")) {
256 if (!strcmp(arg, "-q")) {
260 if (!strcmp(arg, "-n")) {
264 if (!strcmp(arg, "-u")) {
267 newfd = hold_index_file_for_update
271 die("cannot open index.lock file.");
274 if (!memcmp(arg, "--prefix=", 9)) {
276 base_dir_len = strlen(base_dir);
281 /* when --prefix is specified we do not
282 * want to update cache.
285 close(newfd); newfd = -1;
286 rollback_index_file(&cache_file);
294 (write_cache(newfd, active_cache, active_nr) ||
295 commit_index_file(&cache_file)))
296 die("Unable to write new cachefile");