index: make the index file format extensible.
[git/debian.git] / update-index.c
blobd6d3295e329ed2d0f1f46824a210b86c0c092b8f
1 /*
2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
5 */
6 #include "cache.h"
7 #include "strbuf.h"
8 #include "quote.h"
9 #include "cache-tree.h"
12 * Default to not allowing changes to the list of files. The
13 * tool doesn't actually care, but this makes it harder to add
14 * files to the revision control by mistake by doing something
15 * like "git-update-index *" and suddenly having all the object
16 * files be revision controlled.
18 static int allow_add;
19 static int allow_remove;
20 static int allow_replace;
21 static int allow_unmerged; /* --refresh needing merge is not error */
22 static int not_new; /* --refresh not having working tree files is not error */
23 static int quiet; /* --refresh needing update is not error */
24 static int info_only;
25 static int force_remove;
26 static int verbose;
27 static int mark_valid_only = 0;
28 #define MARK_VALID 1
29 #define UNMARK_VALID 2
32 /* Three functions to allow overloaded pointer return; see linux/err.h */
33 static inline void *ERR_PTR(long error)
35 return (void *) error;
38 static inline long PTR_ERR(const void *ptr)
40 return (long) ptr;
43 static inline long IS_ERR(const void *ptr)
45 return (unsigned long)ptr > (unsigned long)-1000L;
48 static void report(const char *fmt, ...)
50 va_list vp;
52 if (!verbose)
53 return;
55 va_start(vp, fmt);
56 vprintf(fmt, vp);
57 putchar('\n');
58 va_end(vp);
61 static int mark_valid(const char *path)
63 int namelen = strlen(path);
64 int pos = cache_name_pos(path, namelen);
65 if (0 <= pos) {
66 switch (mark_valid_only) {
67 case MARK_VALID:
68 active_cache[pos]->ce_flags |= htons(CE_VALID);
69 break;
70 case UNMARK_VALID:
71 active_cache[pos]->ce_flags &= ~htons(CE_VALID);
72 break;
74 cache_tree_invalidate_path(active_cache_tree, path);
75 active_cache_changed = 1;
76 return 0;
78 return -1;
81 static int add_file_to_cache(const char *path)
83 int size, namelen, option, status;
84 struct cache_entry *ce;
85 struct stat st;
87 status = lstat(path, &st);
89 /* We probably want to do this in remove_file_from_cache() and
90 * add_cache_entry() instead...
92 cache_tree_invalidate_path(active_cache_tree, path);
94 if (status < 0 || S_ISDIR(st.st_mode)) {
95 /* When we used to have "path" and now we want to add
96 * "path/file", we need a way to remove "path" before
97 * being able to add "path/file". However,
98 * "git-update-index --remove path" would not work.
99 * --force-remove can be used but this is more user
100 * friendly, especially since we can do the opposite
101 * case just fine without --force-remove.
103 if (status == 0 || (errno == ENOENT || errno == ENOTDIR)) {
104 if (allow_remove) {
105 if (remove_file_from_cache(path))
106 return error("%s: cannot remove from the index",
107 path);
108 else
109 return 0;
110 } else if (status < 0) {
111 return error("%s: does not exist and --remove not passed",
112 path);
115 if (0 == status)
116 return error("%s: is a directory - add files inside instead",
117 path);
118 else
119 return error("lstat(\"%s\"): %s", path,
120 strerror(errno));
123 namelen = strlen(path);
124 size = cache_entry_size(namelen);
125 ce = xcalloc(1, size);
126 memcpy(ce->name, path, namelen);
127 ce->ce_flags = htons(namelen);
128 fill_stat_cache_info(ce, &st);
130 ce->ce_mode = create_ce_mode(st.st_mode);
131 if (!trust_executable_bit) {
132 /* If there is an existing entry, pick the mode bits
133 * from it.
135 int pos = cache_name_pos(path, namelen);
136 if (0 <= pos)
137 ce->ce_mode = active_cache[pos]->ce_mode;
140 if (index_path(ce->sha1, path, &st, !info_only))
141 return -1;
142 option = allow_add ? ADD_CACHE_OK_TO_ADD : 0;
143 option |= allow_replace ? ADD_CACHE_OK_TO_REPLACE : 0;
144 if (add_cache_entry(ce, option))
145 return error("%s: cannot add to the index - missing --add option?",
146 path);
147 return 0;
151 * "refresh" does not calculate a new sha1 file or bring the
152 * cache up-to-date for mode/content changes. But what it
153 * _does_ do is to "re-match" the stat information of a file
154 * with the cache, so that you can refresh the cache for a
155 * file that hasn't been changed but where the stat entry is
156 * out of date.
158 * For example, you'd want to do this after doing a "git-read-tree",
159 * to link up the stat cache details with the proper files.
161 static struct cache_entry *refresh_entry(struct cache_entry *ce, int really)
163 struct stat st;
164 struct cache_entry *updated;
165 int changed, size;
167 if (lstat(ce->name, &st) < 0)
168 return ERR_PTR(-errno);
170 changed = ce_match_stat(ce, &st, really);
171 if (!changed) {
172 if (really && assume_unchanged &&
173 !(ce->ce_flags & htons(CE_VALID)))
174 ; /* mark this one VALID again */
175 else
176 return NULL;
179 if (ce_modified(ce, &st, really))
180 return ERR_PTR(-EINVAL);
182 size = ce_size(ce);
183 updated = xmalloc(size);
184 memcpy(updated, ce, size);
185 fill_stat_cache_info(updated, &st);
187 /* In this case, if really is not set, we should leave
188 * CE_VALID bit alone. Otherwise, paths marked with
189 * --no-assume-unchanged (i.e. things to be edited) will
190 * reacquire CE_VALID bit automatically, which is not
191 * really what we want.
193 if (!really && assume_unchanged && !(ce->ce_flags & htons(CE_VALID)))
194 updated->ce_flags &= ~htons(CE_VALID);
196 return updated;
199 static int refresh_cache(int really)
201 int i;
202 int has_errors = 0;
204 for (i = 0; i < active_nr; i++) {
205 struct cache_entry *ce, *new;
206 ce = active_cache[i];
207 if (ce_stage(ce)) {
208 while ((i < active_nr) &&
209 ! strcmp(active_cache[i]->name, ce->name))
210 i++;
211 i--;
212 if (allow_unmerged)
213 continue;
214 printf("%s: needs merge\n", ce->name);
215 has_errors = 1;
216 continue;
219 new = refresh_entry(ce, really);
220 if (!new)
221 continue;
222 if (IS_ERR(new)) {
223 if (not_new && PTR_ERR(new) == -ENOENT)
224 continue;
225 if (really && PTR_ERR(new) == -EINVAL) {
226 /* If we are doing --really-refresh that
227 * means the index is not valid anymore.
229 ce->ce_flags &= ~htons(CE_VALID);
230 active_cache_changed = 1;
232 if (quiet)
233 continue;
234 printf("%s: needs update\n", ce->name);
235 has_errors = 1;
236 continue;
238 active_cache_changed = 1;
239 /* You can NOT just free active_cache[i] here, since it
240 * might not be necessarily malloc()ed but can also come
241 * from mmap(). */
242 active_cache[i] = new;
244 return has_errors;
248 * We fundamentally don't like some paths: we don't want
249 * dot or dot-dot anywhere, and for obvious reasons don't
250 * want to recurse into ".git" either.
252 * Also, we don't want double slashes or slashes at the
253 * end that can make pathnames ambiguous.
255 static int verify_dotfile(const char *rest)
258 * The first character was '.', but that
259 * has already been discarded, we now test
260 * the rest.
262 switch (*rest) {
263 /* "." is not allowed */
264 case '\0': case '/':
265 return 0;
268 * ".git" followed by NUL or slash is bad. This
269 * shares the path end test with the ".." case.
271 case 'g':
272 if (rest[1] != 'i')
273 break;
274 if (rest[2] != 't')
275 break;
276 rest += 2;
277 /* fallthrough */
278 case '.':
279 if (rest[1] == '\0' || rest[1] == '/')
280 return 0;
282 return 1;
285 static int verify_path(const char *path)
287 char c;
289 goto inside;
290 for (;;) {
291 if (!c)
292 return 1;
293 if (c == '/') {
294 inside:
295 c = *path++;
296 switch (c) {
297 default:
298 continue;
299 case '/': case '\0':
300 break;
301 case '.':
302 if (verify_dotfile(path))
303 continue;
305 return 0;
307 c = *path++;
311 static int add_cacheinfo(unsigned int mode, const unsigned char *sha1,
312 const char *path, int stage)
314 int size, len, option;
315 struct cache_entry *ce;
317 if (!verify_path(path))
318 return -1;
320 len = strlen(path);
321 size = cache_entry_size(len);
322 ce = xcalloc(1, size);
324 memcpy(ce->sha1, sha1, 20);
325 memcpy(ce->name, path, len);
326 ce->ce_flags = create_ce_flags(len, stage);
327 ce->ce_mode = create_ce_mode(mode);
328 if (assume_unchanged)
329 ce->ce_flags |= htons(CE_VALID);
330 option = allow_add ? ADD_CACHE_OK_TO_ADD : 0;
331 option |= allow_replace ? ADD_CACHE_OK_TO_REPLACE : 0;
332 if (add_cache_entry(ce, option))
333 return error("%s: cannot add to the index - missing --add option?",
334 path);
335 report("add '%s'", path);
336 cache_tree_invalidate_path(active_cache_tree, path);
337 return 0;
340 static int chmod_path(int flip, const char *path)
342 int pos;
343 struct cache_entry *ce;
344 unsigned int mode;
346 pos = cache_name_pos(path, strlen(path));
347 if (pos < 0)
348 return -1;
349 ce = active_cache[pos];
350 mode = ntohl(ce->ce_mode);
351 if (!S_ISREG(mode))
352 return -1;
353 switch (flip) {
354 case '+':
355 ce->ce_mode |= htonl(0111); break;
356 case '-':
357 ce->ce_mode &= htonl(~0111); break;
358 default:
359 return -1;
361 cache_tree_invalidate_path(active_cache_tree, path);
362 active_cache_changed = 1;
363 return 0;
366 static struct cache_file cache_file;
368 static void update_one(const char *path, const char *prefix, int prefix_length)
370 const char *p = prefix_path(prefix, prefix_length, path);
371 if (!verify_path(p)) {
372 fprintf(stderr, "Ignoring path %s\n", path);
373 return;
375 if (mark_valid_only) {
376 if (mark_valid(p))
377 die("Unable to mark file %s", path);
378 return;
380 cache_tree_invalidate_path(active_cache_tree, path);
382 if (force_remove) {
383 if (remove_file_from_cache(p))
384 die("git-update-index: unable to remove %s", path);
385 report("remove '%s'", path);
386 return;
388 if (add_file_to_cache(p))
389 die("Unable to process file %s", path);
390 report("add '%s'", path);
393 static void read_index_info(int line_termination)
395 struct strbuf buf;
396 strbuf_init(&buf);
397 while (1) {
398 char *ptr, *tab;
399 char *path_name;
400 unsigned char sha1[20];
401 unsigned int mode;
402 int stage;
404 /* This reads lines formatted in one of three formats:
406 * (1) mode SP sha1 TAB path
407 * The first format is what "git-apply --index-info"
408 * reports, and used to reconstruct a partial tree
409 * that is used for phony merge base tree when falling
410 * back on 3-way merge.
412 * (2) mode SP type SP sha1 TAB path
413 * The second format is to stuff git-ls-tree output
414 * into the index file.
416 * (3) mode SP sha1 SP stage TAB path
417 * This format is to put higher order stages into the
418 * index file and matches git-ls-files --stage output.
420 read_line(&buf, stdin, line_termination);
421 if (buf.eof)
422 break;
424 mode = strtoul(buf.buf, &ptr, 8);
425 if (ptr == buf.buf || *ptr != ' ')
426 goto bad_line;
428 tab = strchr(ptr, '\t');
429 if (!tab || tab - ptr < 41)
430 goto bad_line;
432 if (tab[-2] == ' ' && '0' <= tab[-1] && tab[-1] <= '3') {
433 stage = tab[-1] - '0';
434 ptr = tab + 1; /* point at the head of path */
435 tab = tab - 2; /* point at tail of sha1 */
437 else {
438 stage = 0;
439 ptr = tab + 1; /* point at the head of path */
442 if (get_sha1_hex(tab - 40, sha1) || tab[-41] != ' ')
443 goto bad_line;
445 if (line_termination && ptr[0] == '"')
446 path_name = unquote_c_style(ptr, NULL);
447 else
448 path_name = ptr;
450 if (!verify_path(path_name)) {
451 fprintf(stderr, "Ignoring path %s\n", path_name);
452 if (path_name != ptr)
453 free(path_name);
454 continue;
456 cache_tree_invalidate_path(active_cache_tree, path_name);
458 if (!mode) {
459 /* mode == 0 means there is no such path -- remove */
460 if (remove_file_from_cache(path_name))
461 die("git-update-index: unable to remove %s",
462 ptr);
464 else {
465 /* mode ' ' sha1 '\t' name
466 * ptr[-1] points at tab,
467 * ptr[-41] is at the beginning of sha1
469 ptr[-42] = ptr[-1] = 0;
470 if (add_cacheinfo(mode, sha1, path_name, stage))
471 die("git-update-index: unable to update %s",
472 path_name);
474 if (path_name != ptr)
475 free(path_name);
476 continue;
478 bad_line:
479 die("malformed index info %s", buf.buf);
483 static const char update_index_usage[] =
484 "git-update-index [-q] [--add] [--replace] [--remove] [--unmerged] [--refresh] [--cacheinfo] [--chmod=(+|-)x] [--info-only] [--force-remove] [--stdin] [--index-info] [--ignore-missing] [-z] [--verbose] [--] <file>...";
486 int main(int argc, const char **argv)
488 int i, newfd, entries, has_errors = 0, line_termination = '\n';
489 int allow_options = 1;
490 int read_from_stdin = 0;
491 const char *prefix = setup_git_directory();
492 int prefix_length = prefix ? strlen(prefix) : 0;
494 git_config(git_default_config);
496 newfd = hold_index_file_for_update(&cache_file, get_index_file());
497 if (newfd < 0)
498 die("unable to create new cachefile");
500 entries = read_cache();
501 if (entries < 0)
502 die("cache corrupted");
504 for (i = 1 ; i < argc; i++) {
505 const char *path = argv[i];
507 if (allow_options && *path == '-') {
508 if (!strcmp(path, "--")) {
509 allow_options = 0;
510 continue;
512 if (!strcmp(path, "-q")) {
513 quiet = 1;
514 continue;
516 if (!strcmp(path, "--add")) {
517 allow_add = 1;
518 continue;
520 if (!strcmp(path, "--replace")) {
521 allow_replace = 1;
522 continue;
524 if (!strcmp(path, "--remove")) {
525 allow_remove = 1;
526 continue;
528 if (!strcmp(path, "--unmerged")) {
529 allow_unmerged = 1;
530 continue;
532 if (!strcmp(path, "--refresh")) {
533 has_errors |= refresh_cache(0);
534 continue;
536 if (!strcmp(path, "--really-refresh")) {
537 has_errors |= refresh_cache(1);
538 continue;
540 if (!strcmp(path, "--cacheinfo")) {
541 unsigned char sha1[20];
542 unsigned int mode;
544 if (i+3 >= argc)
545 die("git-update-index: --cacheinfo <mode> <sha1> <path>");
547 if ((sscanf(argv[i+1], "%o", &mode) != 1) ||
548 get_sha1_hex(argv[i+2], sha1) ||
549 add_cacheinfo(mode, sha1, argv[i+3], 0))
550 die("git-update-index: --cacheinfo"
551 " cannot add %s", argv[i+3]);
552 i += 3;
553 continue;
555 if (!strcmp(path, "--chmod=-x") ||
556 !strcmp(path, "--chmod=+x")) {
557 if (argc <= i+1)
558 die("git-update-index: %s <path>", path);
559 if (chmod_path(path[8], argv[++i]))
560 die("git-update-index: %s cannot chmod %s", path, argv[i]);
561 continue;
563 if (!strcmp(path, "--assume-unchanged")) {
564 mark_valid_only = MARK_VALID;
565 continue;
567 if (!strcmp(path, "--no-assume-unchanged")) {
568 mark_valid_only = UNMARK_VALID;
569 continue;
571 if (!strcmp(path, "--info-only")) {
572 info_only = 1;
573 continue;
575 if (!strcmp(path, "--force-remove")) {
576 force_remove = 1;
577 continue;
579 if (!strcmp(path, "-z")) {
580 line_termination = 0;
581 continue;
583 if (!strcmp(path, "--stdin")) {
584 if (i != argc - 1)
585 die("--stdin must be at the end");
586 read_from_stdin = 1;
587 break;
589 if (!strcmp(path, "--index-info")) {
590 if (i != argc - 1)
591 die("--index-info must be at the end");
592 allow_add = allow_replace = allow_remove = 1;
593 read_index_info(line_termination);
594 break;
596 if (!strcmp(path, "--ignore-missing")) {
597 not_new = 1;
598 continue;
600 if (!strcmp(path, "--verbose")) {
601 verbose = 1;
602 continue;
604 if (!strcmp(path, "-h") || !strcmp(path, "--help"))
605 usage(update_index_usage);
606 die("unknown option %s", path);
608 update_one(path, prefix, prefix_length);
610 if (read_from_stdin) {
611 struct strbuf buf;
612 strbuf_init(&buf);
613 while (1) {
614 char *path_name;
615 read_line(&buf, stdin, line_termination);
616 if (buf.eof)
617 break;
618 if (line_termination && buf.buf[0] == '"')
619 path_name = unquote_c_style(buf.buf, NULL);
620 else
621 path_name = buf.buf;
622 update_one(path_name, prefix, prefix_length);
623 if (path_name != buf.buf)
624 free(path_name);
627 if (active_cache_changed) {
628 if (write_cache(newfd, active_cache, active_nr) ||
629 commit_index_file(&cache_file))
630 die("Unable to write new cachefile");
633 return has_errors ? 1 : 0;