Linus Torvalds | 8bc9a0c | 2005-04-07 15:16:10 -0700 | [diff] [blame] | 1 | /* |
| 2 | * GIT - The information manager from hell |
| 3 | * |
| 4 | * Copyright (C) Linus Torvalds, 2005 |
| 5 | */ |
Linus Torvalds | e83c516 | 2005-04-07 15:13:13 -0700 | [diff] [blame] | 6 | #include "cache.h" |
| 7 | |
Linus Torvalds | 121481a | 2005-04-10 11:32:54 -0700 | [diff] [blame] | 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 "update-cache *" and suddenly having all the object |
| 13 | * files be revision controlled. |
| 14 | */ |
Junio C Hamano | 192268c | 2005-05-07 21:55:21 -0700 | [diff] [blame] | 15 | static int allow_add = 0, allow_remove = 0, allow_replace = 0, not_new = 0; |
James Bottomley | c6e007b | 2005-04-24 15:14:16 -0700 | [diff] [blame] | 16 | |
| 17 | /* Three functions to allow overloaded pointer return; see linux/err.h */ |
| 18 | static inline void *ERR_PTR(long error) |
| 19 | { |
| 20 | return (void *) error; |
| 21 | } |
| 22 | |
| 23 | static inline long PTR_ERR(const void *ptr) |
| 24 | { |
| 25 | return (long) ptr; |
| 26 | } |
| 27 | |
| 28 | static inline long IS_ERR(const void *ptr) |
| 29 | { |
| 30 | return (unsigned long)ptr > (unsigned long)-1000L; |
| 31 | } |
Linus Torvalds | 121481a | 2005-04-10 11:32:54 -0700 | [diff] [blame] | 32 | |
Linus Torvalds | ee26752 | 2005-05-06 16:48:43 -0700 | [diff] [blame] | 33 | static int add_file_to_cache(char *path) |
Linus Torvalds | e83c516 | 2005-04-07 15:13:13 -0700 | [diff] [blame] | 34 | { |
Junio C Hamano | 4c5abf4 | 2005-05-08 00:05:18 -0700 | [diff] [blame] | 35 | int size, namelen, option, status; |
Linus Torvalds | e83c516 | 2005-04-07 15:13:13 -0700 | [diff] [blame] | 36 | struct cache_entry *ce; |
| 37 | struct stat st; |
| 38 | int fd; |
Kay Sievers | ffbe1ad | 2005-05-06 15:45:01 +0200 | [diff] [blame] | 39 | char *target; |
Linus Torvalds | e83c516 | 2005-04-07 15:13:13 -0700 | [diff] [blame] | 40 | |
Junio C Hamano | 4c5abf4 | 2005-05-08 00:05:18 -0700 | [diff] [blame] | 41 | status = lstat(path, &st); |
| 42 | if (status < 0 || S_ISDIR(st.st_mode)) { |
| 43 | /* When we used to have "path" and now we want to add |
| 44 | * "path/file", we need a way to remove "path" before |
| 45 | * being able to add "path/file". However, |
| 46 | * "git-update-cache --remove path" would not work. |
| 47 | * --force-remove can be used but this is more user |
| 48 | * friendly, especially since we can do the opposite |
| 49 | * case just fine without --force-remove. |
| 50 | */ |
| 51 | if (status == 0 || (errno == ENOENT || errno == ENOTDIR)) { |
Linus Torvalds | 121481a | 2005-04-10 11:32:54 -0700 | [diff] [blame] | 52 | if (allow_remove) |
| 53 | return remove_file_from_cache(path); |
| 54 | } |
Petr Baudis | 071c41a | 2005-04-26 21:04:55 +0200 | [diff] [blame] | 55 | return error("open(\"%s\"): %s", path, strerror(errno)); |
Linus Torvalds | e83c516 | 2005-04-07 15:13:13 -0700 | [diff] [blame] | 56 | } |
Linus Torvalds | e83c516 | 2005-04-07 15:13:13 -0700 | [diff] [blame] | 57 | namelen = strlen(path); |
| 58 | size = cache_entry_size(namelen); |
Christopher Li | 812666c | 2005-04-26 12:00:58 -0700 | [diff] [blame] | 59 | ce = xmalloc(size); |
Linus Torvalds | e83c516 | 2005-04-07 15:13:13 -0700 | [diff] [blame] | 60 | memset(ce, 0, size); |
| 61 | memcpy(ce->name, path, namelen); |
Linus Torvalds | 711cf3a | 2005-04-11 09:39:21 -0700 | [diff] [blame] | 62 | fill_stat_cache_info(ce, &st); |
Linus Torvalds | e447947 | 2005-04-16 22:26:31 -0700 | [diff] [blame] | 63 | ce->ce_mode = create_ce_mode(st.st_mode); |
Linus Torvalds | f5cabd1 | 2005-04-15 21:45:38 -0700 | [diff] [blame] | 64 | ce->ce_flags = htons(namelen); |
Kay Sievers | 8ae0a8c | 2005-05-05 14:38:25 +0200 | [diff] [blame] | 65 | switch (st.st_mode & S_IFMT) { |
| 66 | case S_IFREG: |
| 67 | fd = open(path, O_RDONLY); |
| 68 | if (fd < 0) |
| 69 | return -1; |
| 70 | if (index_fd(ce->sha1, fd, &st) < 0) |
| 71 | return -1; |
| 72 | break; |
| 73 | case S_IFLNK: |
Kay Sievers | ffbe1ad | 2005-05-06 15:45:01 +0200 | [diff] [blame] | 74 | target = xmalloc(st.st_size+1); |
| 75 | if (readlink(path, target, st.st_size+1) != st.st_size) { |
| 76 | free(target); |
Kay Sievers | 8ae0a8c | 2005-05-05 14:38:25 +0200 | [diff] [blame] | 77 | return -1; |
Kay Sievers | ffbe1ad | 2005-05-06 15:45:01 +0200 | [diff] [blame] | 78 | } |
| 79 | if (write_sha1_file(target, st.st_size, "blob", ce->sha1)) |
Kay Sievers | 8ae0a8c | 2005-05-05 14:38:25 +0200 | [diff] [blame] | 80 | return -1; |
Kay Sievers | ffbe1ad | 2005-05-06 15:45:01 +0200 | [diff] [blame] | 81 | free(target); |
Kay Sievers | 8ae0a8c | 2005-05-05 14:38:25 +0200 | [diff] [blame] | 82 | break; |
| 83 | default: |
Linus Torvalds | e83c516 | 2005-04-07 15:13:13 -0700 | [diff] [blame] | 84 | return -1; |
Kay Sievers | 8ae0a8c | 2005-05-05 14:38:25 +0200 | [diff] [blame] | 85 | } |
Junio C Hamano | 192268c | 2005-05-07 21:55:21 -0700 | [diff] [blame] | 86 | option = allow_add ? ADD_CACHE_OK_TO_ADD : 0; |
| 87 | option |= allow_replace ? ADD_CACHE_OK_TO_REPLACE : 0; |
| 88 | return add_cache_entry(ce, option); |
Linus Torvalds | 121481a | 2005-04-10 11:32:54 -0700 | [diff] [blame] | 89 | } |
| 90 | |
Linus Torvalds | 711cf3a | 2005-04-11 09:39:21 -0700 | [diff] [blame] | 91 | static int match_data(int fd, void *buffer, unsigned long size) |
Linus Torvalds | 121481a | 2005-04-10 11:32:54 -0700 | [diff] [blame] | 92 | { |
Linus Torvalds | 711cf3a | 2005-04-11 09:39:21 -0700 | [diff] [blame] | 93 | while (size) { |
| 94 | char compare[1024]; |
| 95 | int ret = read(fd, compare, sizeof(compare)); |
| 96 | |
| 97 | if (ret <= 0 || ret > size || memcmp(buffer, compare, ret)) |
| 98 | return -1; |
| 99 | size -= ret; |
| 100 | buffer += ret; |
| 101 | } |
| 102 | return 0; |
| 103 | } |
| 104 | |
Linus Torvalds | 32d197f | 2005-04-11 11:33:58 -0700 | [diff] [blame] | 105 | static int compare_data(struct cache_entry *ce, unsigned long expected_size) |
Linus Torvalds | 711cf3a | 2005-04-11 09:39:21 -0700 | [diff] [blame] | 106 | { |
| 107 | int match = -1; |
| 108 | int fd = open(ce->name, O_RDONLY); |
| 109 | |
| 110 | if (fd >= 0) { |
| 111 | void *buffer; |
| 112 | unsigned long size; |
Ingo Molnar | cb1da3a | 2005-04-14 13:18:19 +0200 | [diff] [blame] | 113 | char type[20]; |
Linus Torvalds | 711cf3a | 2005-04-11 09:39:21 -0700 | [diff] [blame] | 114 | |
| 115 | buffer = read_sha1_file(ce->sha1, type, &size); |
| 116 | if (buffer) { |
Linus Torvalds | 32d197f | 2005-04-11 11:33:58 -0700 | [diff] [blame] | 117 | if (size == expected_size && !strcmp(type, "blob")) |
Linus Torvalds | 711cf3a | 2005-04-11 09:39:21 -0700 | [diff] [blame] | 118 | match = match_data(fd, buffer, size); |
| 119 | free(buffer); |
| 120 | } |
| 121 | close(fd); |
| 122 | } |
| 123 | return match; |
| 124 | } |
| 125 | |
Kay Sievers | ffbe1ad | 2005-05-06 15:45:01 +0200 | [diff] [blame] | 126 | static int compare_link(struct cache_entry *ce, unsigned long expected_size) |
| 127 | { |
| 128 | int match = -1; |
| 129 | char *target; |
| 130 | void *buffer; |
| 131 | unsigned long size; |
| 132 | char type[10]; |
| 133 | int len; |
| 134 | |
| 135 | target = xmalloc(expected_size); |
| 136 | len = readlink(ce->name, target, expected_size); |
| 137 | if (len != expected_size) { |
| 138 | free(target); |
| 139 | return -1; |
| 140 | } |
| 141 | buffer = read_sha1_file(ce->sha1, type, &size); |
| 142 | if (!buffer) { |
| 143 | free(target); |
| 144 | return -1; |
| 145 | } |
| 146 | if (size == expected_size) |
| 147 | match = memcmp(buffer, target, size); |
| 148 | free(buffer); |
| 149 | free(target); |
| 150 | return match; |
| 151 | } |
| 152 | |
Linus Torvalds | 711cf3a | 2005-04-11 09:39:21 -0700 | [diff] [blame] | 153 | /* |
| 154 | * "refresh" does not calculate a new sha1 file or bring the |
| 155 | * cache up-to-date for mode/content changes. But what it |
| 156 | * _does_ do is to "re-match" the stat information of a file |
| 157 | * with the cache, so that you can refresh the cache for a |
| 158 | * file that hasn't been changed but where the stat entry is |
| 159 | * out of date. |
| 160 | * |
| 161 | * For example, you'd want to do this after doing a "read-tree", |
| 162 | * to link up the stat cache details with the proper files. |
| 163 | */ |
| 164 | static struct cache_entry *refresh_entry(struct cache_entry *ce) |
| 165 | { |
| 166 | struct stat st; |
| 167 | struct cache_entry *updated; |
| 168 | int changed, size; |
| 169 | |
Kay Sievers | 8ae0a8c | 2005-05-05 14:38:25 +0200 | [diff] [blame] | 170 | if (lstat(ce->name, &st) < 0) |
James Bottomley | c6e007b | 2005-04-24 15:14:16 -0700 | [diff] [blame] | 171 | return ERR_PTR(-errno); |
Linus Torvalds | 711cf3a | 2005-04-11 09:39:21 -0700 | [diff] [blame] | 172 | |
Brad Roberts | 5d728c8 | 2005-05-14 19:04:25 -0700 | [diff] [blame] | 173 | changed = ce_match_stat(ce, &st); |
Linus Torvalds | 711cf3a | 2005-04-11 09:39:21 -0700 | [diff] [blame] | 174 | if (!changed) |
| 175 | return ce; |
| 176 | |
Linus Torvalds | 121481a | 2005-04-10 11:32:54 -0700 | [diff] [blame] | 177 | /* |
Kay Sievers | 8ae0a8c | 2005-05-05 14:38:25 +0200 | [diff] [blame] | 178 | * If the mode or type has changed, there's no point in trying |
Linus Torvalds | 711cf3a | 2005-04-11 09:39:21 -0700 | [diff] [blame] | 179 | * to refresh the entry - it's not going to match |
Linus Torvalds | 121481a | 2005-04-10 11:32:54 -0700 | [diff] [blame] | 180 | */ |
Kay Sievers | 8ae0a8c | 2005-05-05 14:38:25 +0200 | [diff] [blame] | 181 | if (changed & (MODE_CHANGED | TYPE_CHANGED)) |
James Bottomley | c6e007b | 2005-04-24 15:14:16 -0700 | [diff] [blame] | 182 | return ERR_PTR(-EINVAL); |
Linus Torvalds | 711cf3a | 2005-04-11 09:39:21 -0700 | [diff] [blame] | 183 | |
Kay Sievers | ffbe1ad | 2005-05-06 15:45:01 +0200 | [diff] [blame] | 184 | switch (st.st_mode & S_IFMT) { |
| 185 | case S_IFREG: |
| 186 | if (compare_data(ce, st.st_size)) |
| 187 | return ERR_PTR(-EINVAL); |
| 188 | break; |
| 189 | case S_IFLNK: |
| 190 | if (compare_link(ce, st.st_size)) |
| 191 | return ERR_PTR(-EINVAL); |
| 192 | break; |
| 193 | default: |
James Bottomley | c6e007b | 2005-04-24 15:14:16 -0700 | [diff] [blame] | 194 | return ERR_PTR(-EINVAL); |
Kay Sievers | ffbe1ad | 2005-05-06 15:45:01 +0200 | [diff] [blame] | 195 | } |
Linus Torvalds | 711cf3a | 2005-04-11 09:39:21 -0700 | [diff] [blame] | 196 | |
| 197 | size = ce_size(ce); |
Christopher Li | 812666c | 2005-04-26 12:00:58 -0700 | [diff] [blame] | 198 | updated = xmalloc(size); |
Linus Torvalds | 711cf3a | 2005-04-11 09:39:21 -0700 | [diff] [blame] | 199 | memcpy(updated, ce, size); |
| 200 | fill_stat_cache_info(updated, &st); |
| 201 | return updated; |
Linus Torvalds | 121481a | 2005-04-10 11:32:54 -0700 | [diff] [blame] | 202 | } |
| 203 | |
Junio C Hamano | 9053521 | 2005-05-01 21:07:40 -0700 | [diff] [blame] | 204 | static int refresh_cache(void) |
Linus Torvalds | 121481a | 2005-04-10 11:32:54 -0700 | [diff] [blame] | 205 | { |
| 206 | int i; |
Junio C Hamano | 9053521 | 2005-05-01 21:07:40 -0700 | [diff] [blame] | 207 | int has_errors = 0; |
Linus Torvalds | 121481a | 2005-04-10 11:32:54 -0700 | [diff] [blame] | 208 | |
Linus Torvalds | 711cf3a | 2005-04-11 09:39:21 -0700 | [diff] [blame] | 209 | for (i = 0; i < active_nr; i++) { |
Junio C Hamano | 1bc992a | 2005-04-18 10:42:48 -0700 | [diff] [blame] | 210 | struct cache_entry *ce, *new; |
| 211 | ce = active_cache[i]; |
| 212 | if (ce_stage(ce)) { |
| 213 | printf("%s: needs merge\n", ce->name); |
Junio C Hamano | 9053521 | 2005-05-01 21:07:40 -0700 | [diff] [blame] | 214 | has_errors = 1; |
Junio C Hamano | 1bc992a | 2005-04-18 10:42:48 -0700 | [diff] [blame] | 215 | while ((i < active_nr) && |
| 216 | ! strcmp(active_cache[i]->name, ce->name)) |
| 217 | i++; |
| 218 | i--; |
| 219 | continue; |
| 220 | } |
Linus Torvalds | 711cf3a | 2005-04-11 09:39:21 -0700 | [diff] [blame] | 221 | |
Junio C Hamano | 1bc992a | 2005-04-18 10:42:48 -0700 | [diff] [blame] | 222 | new = refresh_entry(ce); |
James Bottomley | c6e007b | 2005-04-24 15:14:16 -0700 | [diff] [blame] | 223 | if (IS_ERR(new)) { |
Junio C Hamano | 9053521 | 2005-05-01 21:07:40 -0700 | [diff] [blame] | 224 | if (!(not_new && PTR_ERR(new) == -ENOENT)) { |
James Bottomley | c6e007b | 2005-04-24 15:14:16 -0700 | [diff] [blame] | 225 | printf("%s: needs update\n", ce->name); |
Junio C Hamano | 9053521 | 2005-05-01 21:07:40 -0700 | [diff] [blame] | 226 | has_errors = 1; |
| 227 | } |
Linus Torvalds | 711cf3a | 2005-04-11 09:39:21 -0700 | [diff] [blame] | 228 | continue; |
| 229 | } |
Linus Torvalds | ee26752 | 2005-05-06 16:48:43 -0700 | [diff] [blame] | 230 | active_cache_changed = 1; |
Petr Baudis | 62d046a | 2005-04-17 23:34:51 +0200 | [diff] [blame] | 231 | /* You can NOT just free active_cache[i] here, since it |
| 232 | * might not be necessarily malloc()ed but can also come |
| 233 | * from mmap(). */ |
Linus Torvalds | 711cf3a | 2005-04-11 09:39:21 -0700 | [diff] [blame] | 234 | active_cache[i] = new; |
| 235 | } |
Junio C Hamano | 9053521 | 2005-05-01 21:07:40 -0700 | [diff] [blame] | 236 | return has_errors; |
Linus Torvalds | e83c516 | 2005-04-07 15:13:13 -0700 | [diff] [blame] | 237 | } |
| 238 | |
Linus Torvalds | e83c516 | 2005-04-07 15:13:13 -0700 | [diff] [blame] | 239 | /* |
| 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 |
Linus Torvalds | 4bb04f2 | 2005-04-11 15:47:57 -0700 | [diff] [blame] | 242 | * any other dot-files (.git or anything else). They |
Linus Torvalds | e83c516 | 2005-04-07 15:13:13 -0700 | [diff] [blame] | 243 | * are hidden, for chist sake. |
| 244 | * |
| 245 | * Also, we don't want double slashes or slashes at the |
Ingo Molnar | aebb267 | 2005-04-12 11:36:26 -0700 | [diff] [blame] | 246 | * end that can make pathnames ambiguous. |
Linus Torvalds | e83c516 | 2005-04-07 15:13:13 -0700 | [diff] [blame] | 247 | */ |
| 248 | static int verify_path(char *path) |
| 249 | { |
| 250 | char c; |
| 251 | |
| 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; |
| 262 | } |
| 263 | c = *path++; |
| 264 | } |
| 265 | } |
| 266 | |
Linus Torvalds | 9945d98 | 2005-04-15 11:08:33 -0700 | [diff] [blame] | 267 | static int add_cacheinfo(char *arg1, char *arg2, char *arg3) |
| 268 | { |
Junio C Hamano | 192268c | 2005-05-07 21:55:21 -0700 | [diff] [blame] | 269 | int size, len, option; |
Linus Torvalds | 9945d98 | 2005-04-15 11:08:33 -0700 | [diff] [blame] | 270 | unsigned int mode; |
| 271 | unsigned char sha1[20]; |
| 272 | struct cache_entry *ce; |
| 273 | |
| 274 | if (sscanf(arg1, "%o", &mode) != 1) |
| 275 | return -1; |
Linus Torvalds | 9945d98 | 2005-04-15 11:08:33 -0700 | [diff] [blame] | 276 | if (get_sha1_hex(arg2, sha1)) |
| 277 | return -1; |
Linus Torvalds | 9945d98 | 2005-04-15 11:08:33 -0700 | [diff] [blame] | 278 | if (!verify_path(arg3)) |
| 279 | return -1; |
Linus Torvalds | 9945d98 | 2005-04-15 11:08:33 -0700 | [diff] [blame] | 280 | |
| 281 | len = strlen(arg3); |
| 282 | size = cache_entry_size(len); |
Christopher Li | 812666c | 2005-04-26 12:00:58 -0700 | [diff] [blame] | 283 | ce = xmalloc(size); |
Linus Torvalds | 9945d98 | 2005-04-15 11:08:33 -0700 | [diff] [blame] | 284 | memset(ce, 0, size); |
| 285 | |
| 286 | memcpy(ce->sha1, sha1, 20); |
| 287 | memcpy(ce->name, arg3, len); |
Linus Torvalds | f5cabd1 | 2005-04-15 21:45:38 -0700 | [diff] [blame] | 288 | ce->ce_flags = htons(len); |
Linus Torvalds | e447947 | 2005-04-16 22:26:31 -0700 | [diff] [blame] | 289 | ce->ce_mode = create_ce_mode(mode); |
Junio C Hamano | 192268c | 2005-05-07 21:55:21 -0700 | [diff] [blame] | 290 | option = allow_add ? ADD_CACHE_OK_TO_ADD : 0; |
| 291 | option |= allow_replace ? ADD_CACHE_OK_TO_REPLACE : 0; |
| 292 | return add_cache_entry(ce, option); |
Linus Torvalds | 9945d98 | 2005-04-15 11:08:33 -0700 | [diff] [blame] | 293 | } |
| 294 | |
Junio C Hamano | 415e96c | 2005-05-15 14:23:12 -0700 | [diff] [blame^] | 295 | struct cache_file cache_file; |
Linus Torvalds | f2a1934 | 2005-04-26 11:55:42 -0700 | [diff] [blame] | 296 | |
Linus Torvalds | e83c516 | 2005-04-07 15:13:13 -0700 | [diff] [blame] | 297 | int main(int argc, char **argv) |
| 298 | { |
Junio C Hamano | 9053521 | 2005-05-01 21:07:40 -0700 | [diff] [blame] | 299 | int i, newfd, entries, has_errors = 0; |
Linus Torvalds | 121481a | 2005-04-10 11:32:54 -0700 | [diff] [blame] | 300 | int allow_options = 1; |
Linus Torvalds | e83c516 | 2005-04-07 15:13:13 -0700 | [diff] [blame] | 301 | |
Junio C Hamano | 415e96c | 2005-05-15 14:23:12 -0700 | [diff] [blame^] | 302 | newfd = hold_index_file_for_update(&cache_file, get_index_file()); |
Linus Torvalds | 9614b8d | 2005-04-11 15:39:26 -0700 | [diff] [blame] | 303 | if (newfd < 0) |
Petr Baudis | 2de381f | 2005-04-13 02:28:48 -0700 | [diff] [blame] | 304 | die("unable to create new cachefile"); |
Linus Torvalds | 9614b8d | 2005-04-11 15:39:26 -0700 | [diff] [blame] | 305 | |
Linus Torvalds | 9614b8d | 2005-04-11 15:39:26 -0700 | [diff] [blame] | 306 | entries = read_cache(); |
| 307 | if (entries < 0) |
Petr Baudis | 2de381f | 2005-04-13 02:28:48 -0700 | [diff] [blame] | 308 | die("cache corrupted"); |
Linus Torvalds | 9614b8d | 2005-04-11 15:39:26 -0700 | [diff] [blame] | 309 | |
Linus Torvalds | e83c516 | 2005-04-07 15:13:13 -0700 | [diff] [blame] | 310 | for (i = 1 ; i < argc; i++) { |
| 311 | char *path = argv[i]; |
Linus Torvalds | 121481a | 2005-04-10 11:32:54 -0700 | [diff] [blame] | 312 | |
| 313 | if (allow_options && *path == '-') { |
| 314 | if (!strcmp(path, "--")) { |
| 315 | allow_options = 0; |
| 316 | continue; |
| 317 | } |
| 318 | if (!strcmp(path, "--add")) { |
| 319 | allow_add = 1; |
| 320 | continue; |
| 321 | } |
Junio C Hamano | 192268c | 2005-05-07 21:55:21 -0700 | [diff] [blame] | 322 | if (!strcmp(path, "--replace")) { |
| 323 | allow_replace = 1; |
| 324 | continue; |
| 325 | } |
Linus Torvalds | 121481a | 2005-04-10 11:32:54 -0700 | [diff] [blame] | 326 | if (!strcmp(path, "--remove")) { |
| 327 | allow_remove = 1; |
| 328 | continue; |
| 329 | } |
| 330 | if (!strcmp(path, "--refresh")) { |
Junio C Hamano | 9053521 | 2005-05-01 21:07:40 -0700 | [diff] [blame] | 331 | has_errors |= refresh_cache(); |
Linus Torvalds | 121481a | 2005-04-10 11:32:54 -0700 | [diff] [blame] | 332 | continue; |
| 333 | } |
Linus Torvalds | 9945d98 | 2005-04-15 11:08:33 -0700 | [diff] [blame] | 334 | if (!strcmp(path, "--cacheinfo")) { |
Junio C Hamano | b3f94c4 | 2005-05-08 15:31:33 -0700 | [diff] [blame] | 335 | if (i+3 >= argc) |
Linus Torvalds | 9945d98 | 2005-04-15 11:08:33 -0700 | [diff] [blame] | 336 | die("update-cache: --cacheinfo <mode> <sha1> <path>"); |
Junio C Hamano | b3f94c4 | 2005-05-08 15:31:33 -0700 | [diff] [blame] | 337 | if (add_cacheinfo(argv[i+1], argv[i+2], argv[i+3])) |
| 338 | die("update-cache: --cacheinfo cannot add %s", argv[i+3]); |
Linus Torvalds | 9945d98 | 2005-04-15 11:08:33 -0700 | [diff] [blame] | 339 | i += 3; |
| 340 | continue; |
| 341 | } |
Junio C Hamano | 0ff5bf7 | 2005-05-01 23:50:51 -0700 | [diff] [blame] | 342 | if (!strcmp(path, "--force-remove")) { |
| 343 | if (argc <= i + 1) |
| 344 | die("update-cache: --force-remove <path>"); |
| 345 | if (remove_file_from_cache(argv[i+1])) |
| 346 | die("update-cache: --force-remove cannot remove %s", argv[i+1]); |
| 347 | i++; |
| 348 | continue; |
| 349 | } |
| 350 | |
James Bottomley | c6e007b | 2005-04-24 15:14:16 -0700 | [diff] [blame] | 351 | if (!strcmp(path, "--ignore-missing")) { |
| 352 | not_new = 1; |
| 353 | continue; |
| 354 | } |
Petr Baudis | 2de381f | 2005-04-13 02:28:48 -0700 | [diff] [blame] | 355 | die("unknown option %s", path); |
Linus Torvalds | 121481a | 2005-04-10 11:32:54 -0700 | [diff] [blame] | 356 | } |
Linus Torvalds | e83c516 | 2005-04-07 15:13:13 -0700 | [diff] [blame] | 357 | if (!verify_path(path)) { |
| 358 | fprintf(stderr, "Ignoring path %s\n", argv[i]); |
| 359 | continue; |
| 360 | } |
Linus Torvalds | 9614b8d | 2005-04-11 15:39:26 -0700 | [diff] [blame] | 361 | if (add_file_to_cache(path)) |
Petr Baudis | 2de381f | 2005-04-13 02:28:48 -0700 | [diff] [blame] | 362 | die("Unable to add %s to database", path); |
Linus Torvalds | e83c516 | 2005-04-07 15:13:13 -0700 | [diff] [blame] | 363 | } |
Junio C Hamano | 415e96c | 2005-05-15 14:23:12 -0700 | [diff] [blame^] | 364 | if (write_cache(newfd, active_cache, active_nr) || |
| 365 | commit_index_file(&cache_file)) |
Petr Baudis | 2de381f | 2005-04-13 02:28:48 -0700 | [diff] [blame] | 366 | die("Unable to write new cachefile"); |
Linus Torvalds | 9614b8d | 2005-04-11 15:39:26 -0700 | [diff] [blame] | 367 | |
Junio C Hamano | c4b83e6 | 2005-05-05 15:29:06 -0700 | [diff] [blame] | 368 | return has_errors ? 1 : 0; |
Linus Torvalds | e83c516 | 2005-04-07 15:13:13 -0700 | [diff] [blame] | 369 | } |