Do not write out new index if nothing has changed.
[git.git] / update-cache.c
blob893ba8679d5764ba84627558d567268ff2500558
1 /*
2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
5 */
6 #include <signal.h>
7 #include "cache.h"
9 /*
10 * Default to not allowing changes to the list of files. The
11 * tool doesn't actually care, but this makes it harder to add
12 * files to the revision control by mistake by doing something
13 * like "update-cache *" and suddenly having all the object
14 * files be revision controlled.
16 static int allow_add = 0, allow_remove = 0, not_new = 0;
19 * update-cache --refresh may not touch anything at all, in which case
20 * writing 1.6MB of the same thing is a waste.
22 static int cache_changed = 0;
24 /* Three functions to allow overloaded pointer return; see linux/err.h */
25 static inline void *ERR_PTR(long error)
27 return (void *) error;
30 static inline long PTR_ERR(const void *ptr)
32 return (long) ptr;
35 static inline long IS_ERR(const void *ptr)
37 return (unsigned long)ptr > (unsigned long)-1000L;
41 * This only updates the "non-critical" parts of the directory
42 * cache, ie the parts that aren't tracked by GIT, and only used
43 * to validate the cache.
45 static void fill_stat_cache_info(struct cache_entry *ce, struct stat *st)
47 ce->ce_ctime.sec = htonl(st->st_ctime);
48 ce->ce_mtime.sec = htonl(st->st_mtime);
49 #ifdef NSEC
50 ce->ce_ctime.nsec = htonl(st->st_ctim.tv_nsec);
51 ce->ce_mtime.nsec = htonl(st->st_mtim.tv_nsec);
52 #endif
53 ce->ce_dev = htonl(st->st_dev);
54 ce->ce_ino = htonl(st->st_ino);
55 ce->ce_uid = htonl(st->st_uid);
56 ce->ce_gid = htonl(st->st_gid);
57 ce->ce_size = htonl(st->st_size);
60 static int add_file_to_cache_1(char *path)
62 int size, namelen;
63 struct cache_entry *ce;
64 struct stat st;
65 int fd;
66 unsigned int len;
67 char target[1024];
69 if (lstat(path, &st) < 0) {
70 if (errno == ENOENT || errno == ENOTDIR) {
71 if (allow_remove)
72 return remove_file_from_cache(path);
74 return -1;
76 namelen = strlen(path);
77 size = cache_entry_size(namelen);
78 ce = xmalloc(size);
79 memset(ce, 0, size);
80 memcpy(ce->name, path, namelen);
81 fill_stat_cache_info(ce, &st);
82 ce->ce_mode = create_ce_mode(st.st_mode);
83 ce->ce_flags = htons(namelen);
84 switch (st.st_mode & S_IFMT) {
85 case S_IFREG:
86 fd = open(path, O_RDONLY);
87 if (fd < 0)
88 return -1;
89 if (index_fd(ce->sha1, fd, &st) < 0)
90 return -1;
91 break;
92 case S_IFLNK:
93 len = readlink(path, target, sizeof(target));
94 if (len == -1 || len+1 > sizeof(target))
95 return -1;
96 if (write_sha1_file(target, len, "blob", ce->sha1))
97 return -1;
98 break;
99 default:
100 return -1;
102 if (!cache_changed) {
103 /* If we have not smudged the cache, be careful
104 * to keep it clean. Find out if we have a matching
105 * cache entry that add_cache_entry would replace with,
106 * and if it matches then do not bother calling it.
108 int pos = cache_name_pos(ce->name, namelen);
109 if ((0 <= pos) &&
110 !memcmp(active_cache[pos], ce, sizeof(*ce))) {
111 free(ce);
112 /* magic to tell add_file_to_cache that
113 * we have not updated anything.
115 return 999;
118 return add_cache_entry(ce, allow_add);
121 static int add_file_to_cache(char *path)
123 int ret = add_file_to_cache_1(path);
124 if (ret == 0)
125 cache_changed = 1;
126 else if (ret == 999)
127 ret = 0;
128 return ret;
131 static int match_data(int fd, void *buffer, unsigned long size)
133 while (size) {
134 char compare[1024];
135 int ret = read(fd, compare, sizeof(compare));
137 if (ret <= 0 || ret > size || memcmp(buffer, compare, ret))
138 return -1;
139 size -= ret;
140 buffer += ret;
142 return 0;
145 static int compare_data(struct cache_entry *ce, unsigned long expected_size)
147 int match = -1;
148 int fd = open(ce->name, O_RDONLY);
150 if (fd >= 0) {
151 void *buffer;
152 unsigned long size;
153 char type[10];
155 buffer = read_sha1_file(ce->sha1, type, &size);
156 if (buffer) {
157 if (size == expected_size && !strcmp(type, "blob"))
158 match = match_data(fd, buffer, size);
159 free(buffer);
161 close(fd);
163 return match;
167 * "refresh" does not calculate a new sha1 file or bring the
168 * cache up-to-date for mode/content changes. But what it
169 * _does_ do is to "re-match" the stat information of a file
170 * with the cache, so that you can refresh the cache for a
171 * file that hasn't been changed but where the stat entry is
172 * out of date.
174 * For example, you'd want to do this after doing a "read-tree",
175 * to link up the stat cache details with the proper files.
177 static struct cache_entry *refresh_entry(struct cache_entry *ce)
179 struct stat st;
180 struct cache_entry *updated;
181 int changed, size;
183 if (lstat(ce->name, &st) < 0)
184 return ERR_PTR(-errno);
186 changed = cache_match_stat(ce, &st);
187 if (!changed)
188 return ce;
191 * If the mode or type has changed, there's no point in trying
192 * to refresh the entry - it's not going to match
194 if (changed & (MODE_CHANGED | TYPE_CHANGED))
195 return ERR_PTR(-EINVAL);
197 if (compare_data(ce, st.st_size))
198 return ERR_PTR(-EINVAL);
200 cache_changed = 1;
201 size = ce_size(ce);
202 updated = xmalloc(size);
203 memcpy(updated, ce, size);
204 fill_stat_cache_info(updated, &st);
205 return updated;
208 static int refresh_cache(void)
210 int i;
211 int has_errors = 0;
213 for (i = 0; i < active_nr; i++) {
214 struct cache_entry *ce, *new;
215 ce = active_cache[i];
216 if (ce_stage(ce)) {
217 printf("%s: needs merge\n", ce->name);
218 has_errors = 1;
219 while ((i < active_nr) &&
220 ! strcmp(active_cache[i]->name, ce->name))
221 i++;
222 i--;
223 continue;
226 new = refresh_entry(ce);
227 if (IS_ERR(new)) {
228 if (!(not_new && PTR_ERR(new) == -ENOENT)) {
229 printf("%s: needs update\n", ce->name);
230 has_errors = 1;
232 continue;
234 active_cache[i] = new;
236 return has_errors;
240 * We fundamentally don't like some paths: we don't want
241 * dot or dot-dot anywhere, and in fact, we don't even want
242 * any other dot-files (.git or anything else). They
243 * are hidden, for chist sake.
245 * Also, we don't want double slashes or slashes at the
246 * end that can make pathnames ambiguous.
248 static int verify_path(char *path)
250 char c;
252 goto inside;
253 for (;;) {
254 if (!c)
255 return 1;
256 if (c == '/') {
257 inside:
258 c = *path++;
259 if (c != '/' && c != '.' && c != '\0')
260 continue;
261 return 0;
263 c = *path++;
267 static int add_cacheinfo(char *arg1, char *arg2, char *arg3)
269 int size, len;
270 unsigned int mode;
271 unsigned char sha1[20];
272 struct cache_entry *ce;
274 if (sscanf(arg1, "%o", &mode) != 1)
275 return -1;
276 if (get_sha1_hex(arg2, sha1))
277 return -1;
278 if (!verify_path(arg3))
279 return -1;
281 cache_changed = 1;
282 len = strlen(arg3);
283 size = cache_entry_size(len);
284 ce = xmalloc(size);
285 memset(ce, 0, size);
287 memcpy(ce->sha1, sha1, 20);
288 memcpy(ce->name, arg3, len);
289 ce->ce_flags = htons(len);
290 ce->ce_mode = create_ce_mode(mode);
291 return add_cache_entry(ce, allow_add);
294 static const char *lockfile_name = NULL;
296 static void remove_lock_file(void)
298 if (lockfile_name)
299 unlink(lockfile_name);
302 static void remove_lock_file_on_signal(int signo)
304 remove_lock_file();
307 int main(int argc, char **argv)
309 int i, newfd, entries, has_errors = 0;
310 int allow_options = 1;
311 static char lockfile[MAXPATHLEN+1];
312 const char *indexfile = get_index_file();
314 snprintf(lockfile, sizeof(lockfile), "%s.lock", indexfile);
316 newfd = open(lockfile, O_RDWR | O_CREAT | O_EXCL, 0600);
317 if (newfd < 0)
318 die("unable to create new cachefile");
320 signal(SIGINT, remove_lock_file_on_signal);
321 atexit(remove_lock_file);
322 lockfile_name = lockfile;
324 entries = read_cache();
325 if (entries < 0)
326 die("cache corrupted");
328 for (i = 1 ; i < argc; i++) {
329 char *path = argv[i];
331 if (allow_options && *path == '-') {
332 if (!strcmp(path, "--")) {
333 allow_options = 0;
334 continue;
336 if (!strcmp(path, "--add")) {
337 allow_add = 1;
338 continue;
340 if (!strcmp(path, "--remove")) {
341 allow_remove = 1;
342 continue;
344 if (!strcmp(path, "--refresh")) {
345 has_errors |= refresh_cache();
346 continue;
348 if (!strcmp(path, "--cacheinfo")) {
349 if (i+3 >= argc || add_cacheinfo(argv[i+1], argv[i+2], argv[i+3]))
350 die("update-cache: --cacheinfo <mode> <sha1> <path>");
351 i += 3;
352 continue;
354 if (!strcmp(path, "--force-remove")) {
355 if (argc <= i + 1)
356 die("update-cache: --force-remove <path>");
357 if (remove_file_from_cache(argv[i+1]))
358 die("update-cache: --force-remove cannot remove %s", argv[i+1]);
359 i++;
360 continue;
363 if (!strcmp(path, "--ignore-missing")) {
364 not_new = 1;
365 continue;
367 die("unknown option %s", path);
369 if (!verify_path(path)) {
370 fprintf(stderr, "Ignoring path %s\n", argv[i]);
371 continue;
373 if (add_file_to_cache(path))
374 die("Unable to add %s to database", path);
377 if (!cache_changed)
378 unlink(lockfile);
379 else if (write_cache(newfd, active_cache, active_nr) ||
380 rename(lockfile, indexfile))
381 die("Unable to write new cachefile");
383 lockfile_name = NULL;
384 return has_errors ? 1 : 0;