[PATCH] cleanup of in-code names
[git.git] / checkout-cache.c
1 /*
2  * Check-out files from the "current cache directory"
3  *
4  * Copyright (C) 2005 Linus Torvalds
5  *
6  * Careful: order of argument flags does matter. For example,
7  *
8  *      git-checkout-cache -a -f file.c
9  *
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).
14  *
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".
18  *
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
22  *
23  *      find . -name '*.h' -print0 | xargs -0 git-checkout-cache -f --
24  *
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
28  * was not the point.
29  *
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!).
34  */
35 #include <sys/types.h>
36 #include <dirent.h>
37 #include "cache.h"
38
39 static int force = 0, quiet = 0, not_new = 0, refresh_cache = 0;
40
41 static void create_directories(const char *path)
42 {
43         int len = strlen(path);
44         char *buf = xmalloc(len + 1);
45         const char *slash = path;
46
47         while ((slash = strchr(slash+1, '/')) != NULL) {
48                 len = slash - path;
49                 memcpy(buf, path, len);
50                 buf[len] = 0;
51                 if (mkdir(buf, 0755)) {
52                         if (errno == EEXIST) {
53                                 struct stat st;
54                                 if (!lstat(buf, &st) && S_ISDIR(st.st_mode))
55                                         continue; /* ok */
56                                 if (force && !unlink(buf) && !mkdir(buf, 0755))
57                                         continue;
58                         }
59                         die("cannot create directory at %s", buf);
60                 }
61         }
62         free(buf);
63 }
64
65 static void remove_subtree(const char *path)
66 {
67         DIR *dir = opendir(path);
68         struct dirent *de;
69         char pathbuf[PATH_MAX];
70         char *name;
71         
72         if (!dir)
73                 die("cannot opendir %s", path);
74         strcpy(pathbuf, path);
75         name = pathbuf + strlen(path);
76         *name++ = '/';
77         while ((de = readdir(dir)) != NULL) {
78                 struct stat st;
79                 if ((de->d_name[0] == '.') &&
80                     ((de->d_name[1] == 0) ||
81                      ((de->d_name[1] == '.') && de->d_name[2] == 0)))
82                         continue;
83                 strcpy(name, de->d_name);
84                 if (lstat(pathbuf, &st))
85                         die("cannot lstat %s", pathbuf);
86                 if (S_ISDIR(st.st_mode))
87                         remove_subtree(pathbuf);
88                 else if (unlink(pathbuf))
89                         die("cannot unlink %s", pathbuf);
90         }
91         closedir(dir);
92         if (rmdir(path))
93                 die("cannot rmdir %s", path);
94 }
95
96 static int create_file(const char *path, unsigned int mode)
97 {
98         int fd;
99
100         mode = (mode & 0100) ? 0777 : 0666;
101         create_directories(path);
102         fd = open(path, O_WRONLY | O_TRUNC | O_CREAT, mode);
103         if (fd < 0) {
104                 if (errno == EISDIR && force) {
105                         remove_subtree(path);
106                         fd = open(path, O_WRONLY | O_TRUNC | O_CREAT, mode);
107                 }
108         }
109         return fd;
110 }
111
112 static int write_entry(struct cache_entry *ce, const char *path)
113 {
114         int fd;
115         void *new;
116         unsigned long size;
117         long wrote;
118         char type[20];
119         char target[1024];
120
121         new = read_sha1_file(ce->sha1, type, &size);
122         if (!new || strcmp(type, "blob")) {
123                 if (new)
124                         free(new);
125                 return error("git-checkout-cache: unable to read sha1 file of %s (%s)",
126                         path, sha1_to_hex(ce->sha1));
127         }
128         switch (ntohl(ce->ce_mode) & S_IFMT) {
129         case S_IFREG:
130                 fd = create_file(path, ntohl(ce->ce_mode));
131                 if (fd < 0) {
132                         free(new);
133                         return error("git-checkout-cache: unable to create file %s (%s)",
134                                 path, strerror(errno));
135                 }
136                 wrote = write(fd, new, size);
137                 close(fd);
138                 free(new);
139                 if (wrote != size)
140                         return error("git-checkout-cache: unable to write file %s", path);
141                 break;
142         case S_IFLNK:
143                 memcpy(target, new, size);
144                 target[size] = '\0';
145                 create_directories(path);
146                 if (symlink(target, path)) {
147                         free(new);
148                         return error("git-checkout-cache: unable to create symlink %s (%s)",
149                                 path, strerror(errno));
150                 }
151                 free(new);
152                 break;
153         default:
154                 free(new);
155                 return error("git-checkout-cache: unknown file mode for %s", path);
156         }
157
158         if (refresh_cache) {
159                 struct stat st;
160                 lstat(ce->name, &st);
161                 fill_stat_cache_info(ce, &st);
162         }
163         return 0;
164 }
165
166 static int checkout_entry(struct cache_entry *ce, const char *base_dir)
167 {
168         struct stat st;
169         static char path[MAXPATHLEN+1];
170         int len = strlen(base_dir);
171
172         memcpy(path, base_dir, len);
173         strcpy(path + len, ce->name);
174
175         if (!lstat(path, &st)) {
176                 unsigned changed = ce_match_stat(ce, &st);
177                 if (!changed)
178                         return 0;
179                 if (!force) {
180                         if (!quiet)
181                                 fprintf(stderr, "git-checkout-cache: %s already exists\n", path);
182                         return 0;
183                 }
184
185                 /*
186                  * We unlink the old file, to get the new one with the
187                  * right permissions (including umask, which is nasty
188                  * to emulate by hand - much easier to let the system
189                  * just do the right thing)
190                  */
191                 unlink(path);
192         } else if (not_new) 
193                 return 0;
194         return write_entry(ce, path);
195 }
196
197 static int checkout_file(const char *name, const char *base_dir)
198 {
199         int pos = cache_name_pos(name, strlen(name));
200         if (pos < 0) {
201                 if (!quiet) {
202                         pos = -pos - 1;
203                         fprintf(stderr,
204                                 "git-checkout-cache: %s is %s.\n",
205                                 name,
206                                 (pos < active_nr &&
207                                  !strcmp(active_cache[pos]->name, name)) ?
208                                 "unmerged" : "not in the cache");
209                 }
210                 return -1;
211         }
212         return checkout_entry(active_cache[pos], base_dir);
213 }
214
215 static int checkout_all(const char *base_dir)
216 {
217         int i;
218
219         for (i = 0; i < active_nr ; i++) {
220                 struct cache_entry *ce = active_cache[i];
221                 if (ce_stage(ce))
222                         continue;
223                 if (checkout_entry(ce, base_dir) < 0)
224                         return -1;
225         }
226         return 0;
227 }
228
229 int main(int argc, char **argv)
230 {
231         int i, force_filename = 0;
232         const char *base_dir = "";
233         struct cache_file cache_file;
234         int newfd = -1;
235
236         if (read_cache() < 0) {
237                 die("invalid cache");
238         }
239
240         for (i = 1; i < argc; i++) {
241                 const char *arg = argv[i];
242                 if (!force_filename) {
243                         if (!strcmp(arg, "-a")) {
244                                 checkout_all(base_dir);
245                                 continue;
246                         }
247                         if (!strcmp(arg, "--")) {
248                                 force_filename = 1;
249                                 continue;
250                         }
251                         if (!strcmp(arg, "-f")) {
252                                 force = 1;
253                                 continue;
254                         }
255                         if (!strcmp(arg, "-q")) {
256                                 quiet = 1;
257                                 continue;
258                         }
259                         if (!strcmp(arg, "-n")) {
260                                 not_new = 1;
261                                 continue;
262                         }
263                         if (!strcmp(arg, "-u")) {
264                                 refresh_cache = 1;
265                                 if (newfd < 0)
266                                         newfd = hold_index_file_for_update
267                                                 (&cache_file,
268                                                  get_index_file());
269                                 if (newfd < 0)
270                                         die("cannot open index.lock file.");
271                                 continue;
272                         }
273                         if (!memcmp(arg, "--prefix=", 9)) {
274                                 base_dir = arg+9;
275                                 continue;
276                         }
277                 }
278                 if (base_dir[0]) {
279                         /* when --prefix is specified we do not
280                          * want to update cache.
281                          */
282                         if (refresh_cache) {
283                                 close(newfd); newfd = -1;
284                                 rollback_index_file(&cache_file);
285                         }
286                         refresh_cache = 0;
287                 }
288                 checkout_file(arg, base_dir);
289         }
290
291         if (0 <= newfd &&
292             (write_cache(newfd, active_cache, active_nr) ||
293              commit_index_file(&cache_file)))
294                 die("Unable to write new cachefile");
295         return 0;
296 }