Create object subdirectories on demand
[git/debian.git] / update-index.c
blob01b4088ad661691c8a8b778071b12527bfc45c3c
1 /*
2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
5 */
6 #include "cache.h"
7 #include "strbuf.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 "git-update-index *" and suddenly having all the object
14 * files be revision controlled.
16 static int allow_add = 0, allow_remove = 0, allow_replace = 0, allow_unmerged = 0, not_new = 0, quiet = 0, info_only = 0;
17 static int force_remove;
19 /* Three functions to allow overloaded pointer return; see linux/err.h */
20 static inline void *ERR_PTR(long error)
22 return (void *) error;
25 static inline long PTR_ERR(const void *ptr)
27 return (long) ptr;
30 static inline long IS_ERR(const void *ptr)
32 return (unsigned long)ptr > (unsigned long)-1000L;
35 static int add_file_to_cache(const char *path)
37 int size, namelen, option, status;
38 struct cache_entry *ce;
39 struct stat st;
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-index --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.
51 if (status == 0 || (errno == ENOENT || errno == ENOTDIR)) {
52 if (allow_remove) {
53 if (remove_file_from_cache(path))
54 return error("%s: cannot remove from the index",
55 path);
56 else
57 return 0;
58 } else if (status < 0) {
59 return error("%s: does not exist and --remove not passed",
60 path);
63 if (0 == status)
64 return error("%s: is a directory - add files inside instead",
65 path);
66 else
67 return error("lstat(\"%s\"): %s", path,
68 strerror(errno));
70 namelen = strlen(path);
71 size = cache_entry_size(namelen);
72 ce = xmalloc(size);
73 memset(ce, 0, size);
74 memcpy(ce->name, path, namelen);
75 fill_stat_cache_info(ce, &st);
76 ce->ce_mode = create_ce_mode(st.st_mode);
77 ce->ce_flags = htons(namelen);
79 if (index_path(ce->sha1, path, &st, !info_only))
80 return -1;
81 option = allow_add ? ADD_CACHE_OK_TO_ADD : 0;
82 option |= allow_replace ? ADD_CACHE_OK_TO_REPLACE : 0;
83 if (add_cache_entry(ce, option))
84 return error("%s: cannot add to the index - missing --add option?",
85 path);
86 return 0;
90 * "refresh" does not calculate a new sha1 file or bring the
91 * cache up-to-date for mode/content changes. But what it
92 * _does_ do is to "re-match" the stat information of a file
93 * with the cache, so that you can refresh the cache for a
94 * file that hasn't been changed but where the stat entry is
95 * out of date.
97 * For example, you'd want to do this after doing a "git-read-tree",
98 * to link up the stat cache details with the proper files.
100 static struct cache_entry *refresh_entry(struct cache_entry *ce)
102 struct stat st;
103 struct cache_entry *updated;
104 int changed, size;
106 if (lstat(ce->name, &st) < 0)
107 return ERR_PTR(-errno);
109 changed = ce_match_stat(ce, &st);
110 if (!changed)
111 return NULL;
113 if (ce_modified(ce, &st))
114 return ERR_PTR(-EINVAL);
116 size = ce_size(ce);
117 updated = xmalloc(size);
118 memcpy(updated, ce, size);
119 fill_stat_cache_info(updated, &st);
120 return updated;
123 static int refresh_cache(void)
125 int i;
126 int has_errors = 0;
128 for (i = 0; i < active_nr; i++) {
129 struct cache_entry *ce, *new;
130 ce = active_cache[i];
131 if (ce_stage(ce)) {
132 while ((i < active_nr) &&
133 ! strcmp(active_cache[i]->name, ce->name))
134 i++;
135 i--;
136 if (allow_unmerged)
137 continue;
138 printf("%s: needs merge\n", ce->name);
139 has_errors = 1;
140 continue;
143 new = refresh_entry(ce);
144 if (!new)
145 continue;
146 if (IS_ERR(new)) {
147 if (not_new && PTR_ERR(new) == -ENOENT)
148 continue;
149 if (quiet)
150 continue;
151 printf("%s: needs update\n", ce->name);
152 has_errors = 1;
153 continue;
155 active_cache_changed = 1;
156 /* You can NOT just free active_cache[i] here, since it
157 * might not be necessarily malloc()ed but can also come
158 * from mmap(). */
159 active_cache[i] = new;
161 return has_errors;
165 * We fundamentally don't like some paths: we don't want
166 * dot or dot-dot anywhere, and for obvious reasons don't
167 * want to recurse into ".git" either.
169 * Also, we don't want double slashes or slashes at the
170 * end that can make pathnames ambiguous.
172 static int verify_dotfile(const char *rest)
175 * The first character was '.', but that
176 * has already been discarded, we now test
177 * the rest.
179 switch (*rest) {
180 /* "." is not allowed */
181 case '\0': case '/':
182 return 0;
185 * ".git" followed by NUL or slash is bad. This
186 * shares the path end test with the ".." case.
188 case 'g':
189 if (rest[1] != 'i')
190 break;
191 if (rest[2] != 't')
192 break;
193 rest += 2;
194 /* fallthrough */
195 case '.':
196 if (rest[1] == '\0' || rest[1] == '/')
197 return 0;
199 return 1;
202 static int verify_path(const char *path)
204 char c;
206 goto inside;
207 for (;;) {
208 if (!c)
209 return 1;
210 if (c == '/') {
211 inside:
212 c = *path++;
213 switch (c) {
214 default:
215 continue;
216 case '/': case '\0':
217 break;
218 case '.':
219 if (verify_dotfile(path))
220 continue;
222 return 0;
224 c = *path++;
228 static int add_cacheinfo(const char *arg1, const char *arg2, const char *arg3)
230 int size, len, option;
231 unsigned int mode;
232 unsigned char sha1[20];
233 struct cache_entry *ce;
235 if (sscanf(arg1, "%o", &mode) != 1)
236 return -1;
237 if (get_sha1_hex(arg2, sha1))
238 return -1;
239 if (!verify_path(arg3))
240 return -1;
242 len = strlen(arg3);
243 size = cache_entry_size(len);
244 ce = xmalloc(size);
245 memset(ce, 0, size);
247 memcpy(ce->sha1, sha1, 20);
248 memcpy(ce->name, arg3, len);
249 ce->ce_flags = htons(len);
250 ce->ce_mode = create_ce_mode(mode);
251 option = allow_add ? ADD_CACHE_OK_TO_ADD : 0;
252 option |= allow_replace ? ADD_CACHE_OK_TO_REPLACE : 0;
253 return add_cache_entry(ce, option);
256 static struct cache_file cache_file;
259 static void update_one(const char *path, const char *prefix, int prefix_length)
261 const char *p = prefix_path(prefix, prefix_length, path);
262 if (!verify_path(p)) {
263 fprintf(stderr, "Ignoring path %s\n", path);
264 return;
266 if (force_remove) {
267 if (remove_file_from_cache(p))
268 die("git-update-index: unable to remove %s", path);
269 return;
271 if (add_file_to_cache(p))
272 die("Unable to process file %s", path);
275 static void read_index_info(int line_termination)
277 struct strbuf buf;
278 strbuf_init(&buf);
279 while (1) {
280 char *ptr;
281 unsigned char sha1[20];
282 unsigned int mode;
284 read_line(&buf, stdin, line_termination);
285 if (buf.eof)
286 break;
288 mode = strtoul(buf.buf, &ptr, 8);
289 if (ptr == buf.buf || *ptr != ' ' ||
290 get_sha1_hex(ptr + 1, sha1) ||
291 ptr[41] != '\t')
292 goto bad_line;
294 ptr += 42;
295 if (!verify_path(ptr)) {
296 fprintf(stderr, "Ignoring path %s\n", ptr);
297 continue;
300 if (!mode) {
301 /* mode == 0 means there is no such path -- remove */
302 if (remove_file_from_cache(ptr))
303 die("git-update-index: unable to remove %s",
304 ptr);
306 else {
307 /* mode ' ' sha1 '\t' name
308 * ptr[-1] points at tab,
309 * ptr[-41] is at the beginning of sha1
311 ptr[-42] = ptr[-1] = 0;
312 if (add_cacheinfo(buf.buf, ptr-41, ptr))
313 die("git-update-index: unable to update %s",
314 ptr);
316 continue;
318 bad_line:
319 die("malformed index info %s", buf.buf);
323 int main(int argc, const char **argv)
325 int i, newfd, entries, has_errors = 0, line_termination = '\n';
326 int allow_options = 1;
327 int read_from_stdin = 0;
328 const char *prefix = setup_git_directory();
329 int prefix_length = prefix ? strlen(prefix) : 0;
331 newfd = hold_index_file_for_update(&cache_file, get_index_file());
332 if (newfd < 0)
333 die("unable to create new cachefile");
335 entries = read_cache();
336 if (entries < 0)
337 die("cache corrupted");
339 for (i = 1 ; i < argc; i++) {
340 const char *path = argv[i];
342 if (allow_options && *path == '-') {
343 if (!strcmp(path, "--")) {
344 allow_options = 0;
345 continue;
347 if (!strcmp(path, "-q")) {
348 quiet = 1;
349 continue;
351 if (!strcmp(path, "--add")) {
352 allow_add = 1;
353 continue;
355 if (!strcmp(path, "--replace")) {
356 allow_replace = 1;
357 continue;
359 if (!strcmp(path, "--remove")) {
360 allow_remove = 1;
361 continue;
363 if (!strcmp(path, "--unmerged")) {
364 allow_unmerged = 1;
365 continue;
367 if (!strcmp(path, "--refresh")) {
368 has_errors |= refresh_cache();
369 continue;
371 if (!strcmp(path, "--cacheinfo")) {
372 if (i+3 >= argc)
373 die("git-update-index: --cacheinfo <mode> <sha1> <path>");
374 if (add_cacheinfo(argv[i+1], argv[i+2], argv[i+3]))
375 die("git-update-index: --cacheinfo cannot add %s", argv[i+3]);
376 i += 3;
377 continue;
379 if (!strcmp(path, "--info-only")) {
380 info_only = 1;
381 continue;
383 if (!strcmp(path, "--force-remove")) {
384 force_remove = 1;
385 continue;
387 if (!strcmp(path, "-z")) {
388 line_termination = 0;
389 continue;
391 if (!strcmp(path, "--stdin")) {
392 if (i != argc - 1)
393 die("--stdin must be at the end");
394 read_from_stdin = 1;
395 break;
397 if (!strcmp(path, "--index-info")) {
398 allow_add = allow_replace = allow_remove = 1;
399 read_index_info(line_termination);
400 continue;
402 if (!strcmp(path, "--ignore-missing")) {
403 not_new = 1;
404 continue;
406 die("unknown option %s", path);
408 update_one(path, prefix, prefix_length);
410 if (read_from_stdin) {
411 struct strbuf buf;
412 strbuf_init(&buf);
413 while (1) {
414 read_line(&buf, stdin, line_termination);
415 if (buf.eof)
416 break;
417 update_one(buf.buf, prefix, prefix_length);
420 if (active_cache_changed) {
421 if (write_cache(newfd, active_cache, active_nr) ||
422 commit_index_file(&cache_file))
423 die("Unable to write new cachefile");
426 return has_errors ? 1 : 0;