Diff clean-up.
[git/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(const 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 /*
116 * "refresh" does not calculate a new sha1 file or bring the
117 * cache up-to-date for mode/content changes. But what it
118 * _does_ do is to "re-match" the stat information of a file
119 * with the cache, so that you can refresh the cache for a
120 * file that hasn't been changed but where the stat entry is
121 * out of date.
122 *
123 * For example, you'd want to do this after doing a "git-read-tree",
124 * to link up the stat cache details with the proper files.
125 */
126 static struct cache_entry *refresh_entry(struct cache_entry *ce)
127 {
128 struct stat st;
129 struct cache_entry *updated;
130 int changed, size;
131
132 if (lstat(ce->name, &st) < 0)
133 return ERR_PTR(-errno);
134
135 changed = ce_match_stat(ce, &st);
136 if (!changed)
137 return ce;
138
139 if (ce_modified(ce, &st))
140 return ERR_PTR(-EINVAL);
141
142 size = ce_size(ce);
143 updated = xmalloc(size);
144 memcpy(updated, ce, size);
145 fill_stat_cache_info(updated, &st);
146 return updated;
147 }
148
149 static int refresh_cache(void)
150 {
151 int i;
152 int has_errors = 0;
153
154 for (i = 0; i < active_nr; i++) {
155 struct cache_entry *ce, *new;
156 ce = active_cache[i];
157 if (ce_stage(ce)) {
158 printf("%s: needs merge\n", ce->name);
159 has_errors = 1;
160 while ((i < active_nr) &&
161 ! strcmp(active_cache[i]->name, ce->name))
162 i++;
163 i--;
164 continue;
165 }
166
167 new = refresh_entry(ce);
168 if (IS_ERR(new)) {
169 if (not_new && PTR_ERR(new) == -ENOENT)
170 continue;
171 if (quiet)
172 continue;
173 printf("%s: needs update\n", ce->name);
174 has_errors = 1;
175 continue;
176 }
177 active_cache_changed = 1;
178 /* You can NOT just free active_cache[i] here, since it
179 * might not be necessarily malloc()ed but can also come
180 * from mmap(). */
181 active_cache[i] = new;
182 }
183 return has_errors;
184 }
185
186 /*
187 * We fundamentally don't like some paths: we don't want
188 * dot or dot-dot anywhere, and for obvious reasons don't
189 * want to recurse into ".git" either.
190 *
191 * Also, we don't want double slashes or slashes at the
192 * end that can make pathnames ambiguous.
193 */
194 static int verify_dotfile(const char *rest)
195 {
196 /*
197 * The first character was '.', but that
198 * has already been discarded, we now test
199 * the rest.
200 */
201 switch (*rest) {
202 /* "." is not allowed */
203 case '\0': case '/':
204 return 0;
205
206 /*
207 * ".git" followed by NUL or slash is bad. This
208 * shares the path end test with the ".." case.
209 */
210 case 'g':
211 if (rest[1] != 'i')
212 break;
213 if (rest[2] != 't')
214 break;
215 rest += 2;
216 /* fallthrough */
217 case '.':
218 if (rest[1] == '\0' || rest[1] == '/')
219 return 0;
220 }
221 return 1;
222 }
223
224 static int verify_path(const char *path)
225 {
226 char c;
227
228 goto inside;
229 for (;;) {
230 if (!c)
231 return 1;
232 if (c == '/') {
233 inside:
234 c = *path++;
235 switch (c) {
236 default:
237 continue;
238 case '/': case '\0':
239 break;
240 case '.':
241 if (verify_dotfile(path))
242 continue;
243 }
244 return 0;
245 }
246 c = *path++;
247 }
248 }
249
250 static int add_cacheinfo(const char *arg1, const char *arg2, const char *arg3)
251 {
252 int size, len, option;
253 unsigned int mode;
254 unsigned char sha1[20];
255 struct cache_entry *ce;
256
257 if (sscanf(arg1, "%o", &mode) != 1)
258 return -1;
259 if (get_sha1_hex(arg2, sha1))
260 return -1;
261 if (!verify_path(arg3))
262 return -1;
263
264 len = strlen(arg3);
265 size = cache_entry_size(len);
266 ce = xmalloc(size);
267 memset(ce, 0, size);
268
269 memcpy(ce->sha1, sha1, 20);
270 memcpy(ce->name, arg3, len);
271 ce->ce_flags = htons(len);
272 ce->ce_mode = create_ce_mode(mode);
273 option = allow_add ? ADD_CACHE_OK_TO_ADD : 0;
274 option |= allow_replace ? ADD_CACHE_OK_TO_REPLACE : 0;
275 return add_cache_entry(ce, option);
276 }
277
278 static struct cache_file cache_file;
279
280 int main(int argc, const char **argv)
281 {
282 int i, newfd, entries, has_errors = 0;
283 int allow_options = 1;
284 const char *prefix = setup_git_directory();
285
286 newfd = hold_index_file_for_update(&cache_file, get_index_file());
287 if (newfd < 0)
288 die("unable to create new cachefile");
289
290 entries = read_cache();
291 if (entries < 0)
292 die("cache corrupted");
293
294 for (i = 1 ; i < argc; i++) {
295 const char *path = argv[i];
296
297 if (allow_options && *path == '-') {
298 if (!strcmp(path, "--")) {
299 allow_options = 0;
300 continue;
301 }
302 if (!strcmp(path, "-q")) {
303 quiet = 1;
304 continue;
305 }
306 if (!strcmp(path, "--add")) {
307 allow_add = 1;
308 continue;
309 }
310 if (!strcmp(path, "--replace")) {
311 allow_replace = 1;
312 continue;
313 }
314 if (!strcmp(path, "--remove")) {
315 allow_remove = 1;
316 continue;
317 }
318 if (!strcmp(path, "--refresh")) {
319 has_errors |= refresh_cache();
320 continue;
321 }
322 if (!strcmp(path, "--cacheinfo")) {
323 if (i+3 >= argc)
324 die("git-update-index: --cacheinfo <mode> <sha1> <path>");
325 if (add_cacheinfo(argv[i+1], argv[i+2], argv[i+3]))
326 die("git-update-index: --cacheinfo cannot add %s", argv[i+3]);
327 i += 3;
328 continue;
329 }
330 if (!strcmp(path, "--info-only")) {
331 info_only = 1;
332 continue;
333 }
334 if (!strcmp(path, "--force-remove")) {
335 force_remove = 1;
336 continue;
337 }
338
339 if (!strcmp(path, "--ignore-missing")) {
340 not_new = 1;
341 continue;
342 }
343 die("unknown option %s", path);
344 }
345 path = prefix_path(prefix, prefix ? strlen(prefix) : 0, path);
346 if (!verify_path(path)) {
347 fprintf(stderr, "Ignoring path %s\n", argv[i]);
348 continue;
349 }
350 if (force_remove) {
351 if (remove_file_from_cache(path))
352 die("git-update-index: unable to remove %s", path);
353 continue;
354 }
355 if (add_file_to_cache(path))
356 die("Unable to process file %s", path);
357 }
358 if (write_cache(newfd, active_cache, active_nr) ||
359 commit_index_file(&cache_file))
360 die("Unable to write new cachefile");
361
362 return has_errors ? 1 : 0;
363 }