2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
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
)
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
);
50 ce
->ce_ctime
.nsec
= htonl(st
->st_ctim
.tv_nsec
);
51 ce
->ce_mtime
.nsec
= htonl(st
->st_mtim
.tv_nsec
);
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
)
63 struct cache_entry
*ce
;
69 if (lstat(path
, &st
) < 0) {
70 if (errno
== ENOENT
|| errno
== ENOTDIR
) {
72 return remove_file_from_cache(path
);
76 namelen
= strlen(path
);
77 size
= cache_entry_size(namelen
);
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
) {
86 fd
= open(path
, O_RDONLY
);
89 if (index_fd(ce
->sha1
, fd
, &st
) < 0)
93 len
= readlink(path
, target
, sizeof(target
));
94 if (len
== -1 || len
+1 > sizeof(target
))
96 if (write_sha1_file(target
, len
, "blob", ce
->sha1
))
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
);
110 !memcmp(active_cache
[pos
], ce
, sizeof(*ce
))) {
112 /* magic to tell add_file_to_cache that
113 * we have not updated anything.
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
);
131 static int match_data(int fd
, void *buffer
, unsigned long size
)
135 int ret
= read(fd
, compare
, sizeof(compare
));
137 if (ret
<= 0 || ret
> size
|| memcmp(buffer
, compare
, ret
))
145 static int compare_data(struct cache_entry
*ce
, unsigned long expected_size
)
148 int fd
= open(ce
->name
, O_RDONLY
);
155 buffer
= read_sha1_file(ce
->sha1
, type
, &size
);
157 if (size
== expected_size
&& !strcmp(type
, "blob"))
158 match
= match_data(fd
, buffer
, size
);
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
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
)
180 struct cache_entry
*updated
;
183 if (lstat(ce
->name
, &st
) < 0)
184 return ERR_PTR(-errno
);
186 changed
= cache_match_stat(ce
, &st
);
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
);
202 updated
= xmalloc(size
);
203 memcpy(updated
, ce
, size
);
204 fill_stat_cache_info(updated
, &st
);
208 static int refresh_cache(void)
213 for (i
= 0; i
< active_nr
; i
++) {
214 struct cache_entry
*ce
, *new;
215 ce
= active_cache
[i
];
217 printf("%s: needs merge\n", ce
->name
);
219 while ((i
< active_nr
) &&
220 ! strcmp(active_cache
[i
]->name
, ce
->name
))
226 new = refresh_entry(ce
);
228 if (!(not_new
&& PTR_ERR(new) == -ENOENT
)) {
229 printf("%s: needs update\n", ce
->name
);
234 active_cache
[i
] = new;
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
)
259 if (c
!= '/' && c
!= '.' && c
!= '\0')
267 static int add_cacheinfo(char *arg1
, char *arg2
, char *arg3
)
271 unsigned char sha1
[20];
272 struct cache_entry
*ce
;
274 if (sscanf(arg1
, "%o", &mode
) != 1)
276 if (get_sha1_hex(arg2
, sha1
))
278 if (!verify_path(arg3
))
283 size
= cache_entry_size(len
);
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)
299 unlink(lockfile_name
);
302 static void remove_lock_file_on_signal(int signo
)
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);
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();
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
, "--")) {
336 if (!strcmp(path
, "--add")) {
340 if (!strcmp(path
, "--remove")) {
344 if (!strcmp(path
, "--refresh")) {
345 has_errors
|= refresh_cache();
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>");
354 if (!strcmp(path
, "--force-remove")) {
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]);
363 if (!strcmp(path
, "--ignore-missing")) {
367 die("unknown option %s", path
);
369 if (!verify_path(path
)) {
370 fprintf(stderr
, "Ignoring path %s\n", argv
[i
]);
373 if (add_file_to_cache(path
))
374 die("Unable to add %s to database", path
);
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;