Fast-path 'update-index --refresh' a bit.
[git.git] / update-index.c
1 /*
2  * GIT - The information manager from hell
3  *
4  * Copyright (C) Linus Torvalds, 2005
5  */
6 #include "cache.h"
7
8 /*
9  * Default to not allowing changes to the list of files. The
10  * tool doesn't actually care, but this makes it harder to add
11  * files to the revision control by mistake by doing something
12  * like "git-update-index *" and suddenly having all the object
13  * files be revision controlled.
14  */
15 static int allow_add = 0, allow_remove = 0, allow_replace = 0, not_new = 0, quiet = 0, info_only = 0;
16 static int force_remove;
17
18 /* Three functions to allow overloaded pointer return; see linux/err.h */
19 static inline void *ERR_PTR(long error)
20 {
21         return (void *) error;
22 }
23
24 static inline long PTR_ERR(const void *ptr)
25 {
26         return (long) ptr;
27 }
28
29 static inline long IS_ERR(const void *ptr)
30 {
31         return (unsigned long)ptr > (unsigned long)-1000L;
32 }
33
34 static int add_file_to_cache(char *path)
35 {
36         int size, namelen, option, status;
37         struct cache_entry *ce;
38         struct stat st;
39         int fd;
40         char *target;
41
42         status = lstat(path, &st);
43         if (status < 0 || S_ISDIR(st.st_mode)) {
44                 /* When we used to have "path" and now we want to add
45                  * "path/file", we need a way to remove "path" before
46                  * being able to add "path/file".  However,
47                  * "git-update-index --remove path" would not work.
48                  * --force-remove can be used but this is more user
49                  * friendly, especially since we can do the opposite
50                  * case just fine without --force-remove.
51                  */
52                 if (status == 0 || (errno == ENOENT || errno == ENOTDIR)) {
53                         if (allow_remove) {
54                                 if (remove_file_from_cache(path))
55                                         return error("%s: cannot remove from the index",
56                                                      path);
57                                 else
58                                         return 0;
59                         } else if (status < 0) {
60                                 return error("%s: does not exist and --remove not passed",
61                                              path);
62                         }
63                 }
64                 if (0 == status)
65                         return error("%s: is a directory - add files inside instead",
66                                      path);
67                 else
68                         return error("lstat(\"%s\"): %s", path,
69                                      strerror(errno));
70         }
71         namelen = strlen(path);
72         size = cache_entry_size(namelen);
73         ce = xmalloc(size);
74         memset(ce, 0, size);
75         memcpy(ce->name, path, namelen);
76         fill_stat_cache_info(ce, &st);
77         ce->ce_mode = create_ce_mode(st.st_mode);
78         ce->ce_flags = htons(namelen);
79         switch (st.st_mode & S_IFMT) {
80         case S_IFREG:
81                 fd = open(path, O_RDONLY);
82                 if (fd < 0)
83                         return error("open(\"%s\"): %s", path, strerror(errno));
84                 if (index_fd(ce->sha1, fd, &st, !info_only, NULL) < 0)
85                         return error("%s: failed to insert into database", path);
86                 break;
87         case S_IFLNK:
88                 target = xmalloc(st.st_size+1);
89                 if (readlink(path, target, st.st_size+1) != st.st_size) {
90                         char *errstr = strerror(errno);
91                         free(target);
92                         return error("readlink(\"%s\"): %s", path,
93                                      errstr);
94                 }
95                 if (info_only) {
96                         unsigned char hdr[50];
97                         int hdrlen;
98                         write_sha1_file_prepare(target, st.st_size, "blob",
99                                                 ce->sha1, hdr, &hdrlen);
100                 } else if (write_sha1_file(target, st.st_size, "blob", ce->sha1))
101                         return error("%s: failed to insert into database", path);
102                 free(target);
103                 break;
104         default:
105                 return error("%s: unsupported file type", path);
106         }
107         option = allow_add ? ADD_CACHE_OK_TO_ADD : 0;
108         option |= allow_replace ? ADD_CACHE_OK_TO_REPLACE : 0;
109         if (add_cache_entry(ce, option))
110                 return error("%s: cannot add to the index - missing --add option?",
111                              path);
112         return 0;
113 }
114
115 static int compare_data(struct cache_entry *ce, struct stat *st)
116 {
117         int match = -1;
118         int fd = open(ce->name, O_RDONLY);
119
120         if (fd >= 0) {
121                 unsigned char sha1[20];
122                 if (!index_fd(sha1, fd, st, 0, NULL))
123                         match = memcmp(sha1, ce->sha1, 20);
124                 close(fd);
125         }
126         return match;
127 }
128
129 static int compare_link(struct cache_entry *ce, unsigned long expected_size)
130 {
131         int match = -1;
132         char *target;
133         void *buffer;
134         unsigned long size;
135         char type[10];
136         int len;
137
138         target = xmalloc(expected_size);
139         len = readlink(ce->name, target, expected_size);
140         if (len != expected_size) {
141                 free(target);
142                 return -1;
143         }
144         buffer = read_sha1_file(ce->sha1, type, &size);
145         if (!buffer) {
146                 free(target);
147                 return -1;
148         }
149         if (size == expected_size)
150                 match = memcmp(buffer, target, size);
151         free(buffer);
152         free(target);
153         return match;
154 }
155
156 /*
157  * "refresh" does not calculate a new sha1 file or bring the
158  * cache up-to-date for mode/content changes. But what it
159  * _does_ do is to "re-match" the stat information of a file
160  * with the cache, so that you can refresh the cache for a
161  * file that hasn't been changed but where the stat entry is
162  * out of date.
163  *
164  * For example, you'd want to do this after doing a "git-read-tree",
165  * to link up the stat cache details with the proper files.
166  */
167 static struct cache_entry *refresh_entry(struct cache_entry *ce)
168 {
169         struct stat st;
170         struct cache_entry *updated;
171         int changed, size;
172
173         if (lstat(ce->name, &st) < 0)
174                 return ERR_PTR(-errno);
175
176         changed = ce_match_stat(ce, &st);
177         if (!changed)
178                 return ce;
179
180         /*
181          * If the mode or type has changed, there's no point in trying
182          * to refresh the entry - it's not going to match
183          */
184         if (changed & (MODE_CHANGED | TYPE_CHANGED))
185                 return ERR_PTR(-EINVAL);
186
187         /* Immediately after read-tree or update-index --cacheinfo,
188          * the length field is zero.  For other cases the ce_size
189          * should match the SHA1 recorded in the index entry.
190          */
191         if ((changed & DATA_CHANGED) && ce->ce_size != htonl(0))
192                 return ERR_PTR(-EINVAL);
193
194         switch (st.st_mode & S_IFMT) {
195         case S_IFREG:
196                 if (compare_data(ce, &st))
197                         return ERR_PTR(-EINVAL);
198                 break;
199         case S_IFLNK:
200                 if (compare_link(ce, st.st_size))
201                         return ERR_PTR(-EINVAL);
202                 break;
203         default:
204                 return ERR_PTR(-EINVAL);
205         }
206
207         size = ce_size(ce);
208         updated = xmalloc(size);
209         memcpy(updated, ce, size);
210         fill_stat_cache_info(updated, &st);
211         return updated;
212 }
213
214 static int refresh_cache(void)
215 {
216         int i;
217         int has_errors = 0;
218
219         for (i = 0; i < active_nr; i++) {
220                 struct cache_entry *ce, *new;
221                 ce = active_cache[i];
222                 if (ce_stage(ce)) {
223                         printf("%s: needs merge\n", ce->name);
224                         has_errors = 1;
225                         while ((i < active_nr) &&
226                                ! strcmp(active_cache[i]->name, ce->name))
227                                 i++;
228                         i--;
229                         continue;
230                 }
231
232                 new = refresh_entry(ce);
233                 if (IS_ERR(new)) {
234                         if (not_new && PTR_ERR(new) == -ENOENT)
235                                 continue;
236                         if (quiet)
237                                 continue;
238                         printf("%s: needs update\n", ce->name);
239                         has_errors = 1;
240                         continue;
241                 }
242                 active_cache_changed = 1;
243                 /* You can NOT just free active_cache[i] here, since it
244                  * might not be necessarily malloc()ed but can also come
245                  * from mmap(). */
246                 active_cache[i] = new;
247         }
248         return has_errors;
249 }
250
251 /*
252  * We fundamentally don't like some paths: we don't want
253  * dot or dot-dot anywhere, and for obvious reasons don't
254  * want to recurse into ".git" either.
255  *
256  * Also, we don't want double slashes or slashes at the
257  * end that can make pathnames ambiguous.
258  */
259 static int verify_dotfile(const char *rest)
260 {
261         /*
262          * The first character was '.', but that
263          * has already been discarded, we now test
264          * the rest.
265          */
266         switch (*rest) {
267         /* "." is not allowed */
268         case '\0': case '/':
269                 return 0;
270
271         /*
272          * ".git" followed by  NUL or slash is bad. This
273          * shares the path end test with the ".." case.
274          */
275         case 'g':
276                 if (rest[1] != 'i')
277                         break;
278                 if (rest[2] != 't')
279                         break;
280                 rest += 2;
281         /* fallthrough */
282         case '.':
283                 if (rest[1] == '\0' || rest[1] == '/')
284                         return 0;
285         }
286         return 1;
287 }
288
289 static int verify_path(char *path)
290 {
291         char c;
292
293         goto inside;
294         for (;;) {
295                 if (!c)
296                         return 1;
297                 if (c == '/') {
298 inside:
299                         c = *path++;
300                         switch (c) {
301                         default:
302                                 continue;
303                         case '/': case '\0':
304                                 break;
305                         case '.':
306                                 if (verify_dotfile(path))
307                                         continue;
308                         }
309                         return 0;
310                 }
311                 c = *path++;
312         }
313 }
314
315 static int add_cacheinfo(char *arg1, char *arg2, char *arg3)
316 {
317         int size, len, option;
318         unsigned int mode;
319         unsigned char sha1[20];
320         struct cache_entry *ce;
321
322         if (sscanf(arg1, "%o", &mode) != 1)
323                 return -1;
324         if (get_sha1_hex(arg2, sha1))
325                 return -1;
326         if (!verify_path(arg3))
327                 return -1;
328
329         len = strlen(arg3);
330         size = cache_entry_size(len);
331         ce = xmalloc(size);
332         memset(ce, 0, size);
333
334         memcpy(ce->sha1, sha1, 20);
335         memcpy(ce->name, arg3, len);
336         ce->ce_flags = htons(len);
337         ce->ce_mode = create_ce_mode(mode);
338         option = allow_add ? ADD_CACHE_OK_TO_ADD : 0;
339         option |= allow_replace ? ADD_CACHE_OK_TO_REPLACE : 0;
340         return add_cache_entry(ce, option);
341 }
342
343 static struct cache_file cache_file;
344
345 int main(int argc, char **argv)
346 {
347         int i, newfd, entries, has_errors = 0;
348         int allow_options = 1;
349         const char *prefix = setup_git_directory();
350
351         newfd = hold_index_file_for_update(&cache_file, get_index_file());
352         if (newfd < 0)
353                 die("unable to create new cachefile");
354
355         entries = read_cache();
356         if (entries < 0)
357                 die("cache corrupted");
358
359         for (i = 1 ; i < argc; i++) {
360                 char *path = argv[i];
361
362                 if (allow_options && *path == '-') {
363                         if (!strcmp(path, "--")) {
364                                 allow_options = 0;
365                                 continue;
366                         }
367                         if (!strcmp(path, "-q")) {
368                                 quiet = 1;
369                                 continue;
370                         }
371                         if (!strcmp(path, "--add")) {
372                                 allow_add = 1;
373                                 continue;
374                         }
375                         if (!strcmp(path, "--replace")) {
376                                 allow_replace = 1;
377                                 continue;
378                         }
379                         if (!strcmp(path, "--remove")) {
380                                 allow_remove = 1;
381                                 continue;
382                         }
383                         if (!strcmp(path, "--refresh")) {
384                                 has_errors |= refresh_cache();
385                                 continue;
386                         }
387                         if (!strcmp(path, "--cacheinfo")) {
388                                 if (i+3 >= argc)
389                                         die("git-update-index: --cacheinfo <mode> <sha1> <path>");
390                                 if (add_cacheinfo(argv[i+1], argv[i+2], argv[i+3]))
391                                         die("git-update-index: --cacheinfo cannot add %s", argv[i+3]);
392                                 i += 3;
393                                 continue;
394                         }
395                         if (!strcmp(path, "--info-only")) {
396                                 info_only = 1;
397                                 continue;
398                         }
399                         if (!strcmp(path, "--force-remove")) {
400                                 force_remove = 1;
401                                 continue;
402                         }
403
404                         if (!strcmp(path, "--ignore-missing")) {
405                                 not_new = 1;
406                                 continue;
407                         }
408                         die("unknown option %s", path);
409                 }
410                 path = prefix_path(prefix, prefix ? strlen(prefix) : 0, path);
411                 if (!verify_path(path)) {
412                         fprintf(stderr, "Ignoring path %s\n", argv[i]);
413                         continue;
414                 }
415                 if (force_remove) {
416                         if (remove_file_from_cache(path))
417                                 die("git-update-index: unable to remove %s", path);
418                         continue;
419                 }
420                 if (add_file_to_cache(path))
421                         die("Unable to process file %s", path);
422         }
423         if (write_cache(newfd, active_cache, active_nr) ||
424             commit_index_file(&cache_file))
425                 die("Unable to write new cachefile");
426
427         return has_errors ? 1 : 0;
428 }