rerere: remove silly 1024-byte line limit
[git/gitweb.git] / builtin-update-index.c
blob750db163b93be19e1fe5648af49f3302cacecb2f
1 /*
2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
5 */
6 #include "cache.h"
7 #include "quote.h"
8 #include "cache-tree.h"
9 #include "tree-walk.h"
10 #include "builtin.h"
11 #include "refs.h"
12 #include "resolve-undo.h"
15 * Default to not allowing changes to the list of files. The
16 * tool doesn't actually care, but this makes it harder to add
17 * files to the revision control by mistake by doing something
18 * like "git update-index *" and suddenly having all the object
19 * files be revision controlled.
21 static int allow_add;
22 static int allow_remove;
23 static int allow_replace;
24 static int info_only;
25 static int force_remove;
26 static int verbose;
27 static int mark_valid_only;
28 #define MARK_VALID 1
29 #define UNMARK_VALID 2
31 __attribute__((format (printf, 1, 2)))
32 static void report(const char *fmt, ...)
34 va_list vp;
36 if (!verbose)
37 return;
39 va_start(vp, fmt);
40 vprintf(fmt, vp);
41 putchar('\n');
42 va_end(vp);
45 static int mark_valid(const char *path)
47 int namelen = strlen(path);
48 int pos = cache_name_pos(path, namelen);
49 if (0 <= pos) {
50 switch (mark_valid_only) {
51 case MARK_VALID:
52 active_cache[pos]->ce_flags |= CE_VALID;
53 break;
54 case UNMARK_VALID:
55 active_cache[pos]->ce_flags &= ~CE_VALID;
56 break;
58 cache_tree_invalidate_path(active_cache_tree, path);
59 active_cache_changed = 1;
60 return 0;
62 return -1;
65 static int remove_one_path(const char *path)
67 if (!allow_remove)
68 return error("%s: does not exist and --remove not passed", path);
69 if (remove_file_from_cache(path))
70 return error("%s: cannot remove from the index", path);
71 return 0;
75 * Handle a path that couldn't be lstat'ed. It's either:
76 * - missing file (ENOENT or ENOTDIR). That's ok if we're
77 * supposed to be removing it and the removal actually
78 * succeeds.
79 * - permission error. That's never ok.
81 static int process_lstat_error(const char *path, int err)
83 if (err == ENOENT || err == ENOTDIR)
84 return remove_one_path(path);
85 return error("lstat(\"%s\"): %s", path, strerror(errno));
88 static int add_one_path(struct cache_entry *old, const char *path, int len, struct stat *st)
90 int option, size;
91 struct cache_entry *ce;
93 /* Was the old index entry already up-to-date? */
94 if (old && !ce_stage(old) && !ce_match_stat(old, st, 0))
95 return 0;
97 size = cache_entry_size(len);
98 ce = xcalloc(1, size);
99 memcpy(ce->name, path, len);
100 ce->ce_flags = len;
101 fill_stat_cache_info(ce, st);
102 ce->ce_mode = ce_mode_from_stat(old, st->st_mode);
104 if (index_path(ce->sha1, path, st, !info_only))
105 return -1;
106 option = allow_add ? ADD_CACHE_OK_TO_ADD : 0;
107 option |= allow_replace ? ADD_CACHE_OK_TO_REPLACE : 0;
108 if (add_cache_entry(ce, option))
109 return error("%s: cannot add to the index - missing --add option?", path);
110 return 0;
114 * Handle a path that was a directory. Four cases:
116 * - it's already a gitlink in the index, and we keep it that
117 * way, and update it if we can (if we cannot find the HEAD,
118 * we're going to keep it unchanged in the index!)
120 * - it's a *file* in the index, in which case it should be
121 * removed as a file if removal is allowed, since it doesn't
122 * exist as such any more. If removal isn't allowed, it's
123 * an error.
125 * (NOTE! This is old and arguably fairly strange behaviour.
126 * We might want to make this an error unconditionally, and
127 * use "--force-remove" if you actually want to force removal).
129 * - it used to exist as a subdirectory (ie multiple files with
130 * this particular prefix) in the index, in which case it's wrong
131 * to try to update it as a directory.
133 * - it doesn't exist at all in the index, but it is a valid
134 * git directory, and it should be *added* as a gitlink.
136 static int process_directory(const char *path, int len, struct stat *st)
138 unsigned char sha1[20];
139 int pos = cache_name_pos(path, len);
141 /* Exact match: file or existing gitlink */
142 if (pos >= 0) {
143 struct cache_entry *ce = active_cache[pos];
144 if (S_ISGITLINK(ce->ce_mode)) {
146 /* Do nothing to the index if there is no HEAD! */
147 if (resolve_gitlink_ref(path, "HEAD", sha1) < 0)
148 return 0;
150 return add_one_path(ce, path, len, st);
152 /* Should this be an unconditional error? */
153 return remove_one_path(path);
156 /* Inexact match: is there perhaps a subdirectory match? */
157 pos = -pos-1;
158 while (pos < active_nr) {
159 struct cache_entry *ce = active_cache[pos++];
161 if (strncmp(ce->name, path, len))
162 break;
163 if (ce->name[len] > '/')
164 break;
165 if (ce->name[len] < '/')
166 continue;
168 /* Subdirectory match - error out */
169 return error("%s: is a directory - add individual files instead", path);
172 /* No match - should we add it as a gitlink? */
173 if (!resolve_gitlink_ref(path, "HEAD", sha1))
174 return add_one_path(NULL, path, len, st);
176 /* Error out. */
177 return error("%s: is a directory - add files inside instead", path);
181 * Process a regular file
183 static int process_file(const char *path, int len, struct stat *st)
185 int pos = cache_name_pos(path, len);
186 struct cache_entry *ce = pos < 0 ? NULL : active_cache[pos];
188 if (ce && S_ISGITLINK(ce->ce_mode))
189 return error("%s is already a gitlink, not replacing", path);
191 return add_one_path(ce, path, len, st);
194 static int process_path(const char *path)
196 int len;
197 struct stat st;
199 len = strlen(path);
200 if (has_symlink_leading_path(path, len))
201 return error("'%s' is beyond a symbolic link", path);
204 * First things first: get the stat information, to decide
205 * what to do about the pathname!
207 if (lstat(path, &st) < 0)
208 return process_lstat_error(path, errno);
210 if (S_ISDIR(st.st_mode))
211 return process_directory(path, len, &st);
213 return process_file(path, len, &st);
216 static int add_cacheinfo(unsigned int mode, const unsigned char *sha1,
217 const char *path, int stage)
219 int size, len, option;
220 struct cache_entry *ce;
222 if (!verify_path(path))
223 return error("Invalid path '%s'", path);
225 len = strlen(path);
226 size = cache_entry_size(len);
227 ce = xcalloc(1, size);
229 hashcpy(ce->sha1, sha1);
230 memcpy(ce->name, path, len);
231 ce->ce_flags = create_ce_flags(len, stage);
232 ce->ce_mode = create_ce_mode(mode);
233 if (assume_unchanged)
234 ce->ce_flags |= CE_VALID;
235 option = allow_add ? ADD_CACHE_OK_TO_ADD : 0;
236 option |= allow_replace ? ADD_CACHE_OK_TO_REPLACE : 0;
237 if (add_cache_entry(ce, option))
238 return error("%s: cannot add to the index - missing --add option?",
239 path);
240 report("add '%s'", path);
241 return 0;
244 static void chmod_path(int flip, const char *path)
246 int pos;
247 struct cache_entry *ce;
248 unsigned int mode;
250 pos = cache_name_pos(path, strlen(path));
251 if (pos < 0)
252 goto fail;
253 ce = active_cache[pos];
254 mode = ce->ce_mode;
255 if (!S_ISREG(mode))
256 goto fail;
257 switch (flip) {
258 case '+':
259 ce->ce_mode |= 0111; break;
260 case '-':
261 ce->ce_mode &= ~0111; break;
262 default:
263 goto fail;
265 cache_tree_invalidate_path(active_cache_tree, path);
266 active_cache_changed = 1;
267 report("chmod %cx '%s'", flip, path);
268 return;
269 fail:
270 die("git update-index: cannot chmod %cx '%s'", flip, path);
273 static void update_one(const char *path, const char *prefix, int prefix_length)
275 const char *p = prefix_path(prefix, prefix_length, path);
276 if (!verify_path(p)) {
277 fprintf(stderr, "Ignoring path %s\n", path);
278 goto free_return;
280 if (mark_valid_only) {
281 if (mark_valid(p))
282 die("Unable to mark file %s", path);
283 goto free_return;
286 if (force_remove) {
287 if (remove_file_from_cache(p))
288 die("git update-index: unable to remove %s", path);
289 report("remove '%s'", path);
290 goto free_return;
292 if (process_path(p))
293 die("Unable to process path %s", path);
294 report("add '%s'", path);
295 free_return:
296 if (p < path || p > path + strlen(path))
297 free((char *)p);
300 static void read_index_info(int line_termination)
302 struct strbuf buf = STRBUF_INIT;
303 struct strbuf uq = STRBUF_INIT;
305 while (strbuf_getline(&buf, stdin, line_termination) != EOF) {
306 char *ptr, *tab;
307 char *path_name;
308 unsigned char sha1[20];
309 unsigned int mode;
310 unsigned long ul;
311 int stage;
313 /* This reads lines formatted in one of three formats:
315 * (1) mode SP sha1 TAB path
316 * The first format is what "git apply --index-info"
317 * reports, and used to reconstruct a partial tree
318 * that is used for phony merge base tree when falling
319 * back on 3-way merge.
321 * (2) mode SP type SP sha1 TAB path
322 * The second format is to stuff "git ls-tree" output
323 * into the index file.
325 * (3) mode SP sha1 SP stage TAB path
326 * This format is to put higher order stages into the
327 * index file and matches "git ls-files --stage" output.
329 errno = 0;
330 ul = strtoul(buf.buf, &ptr, 8);
331 if (ptr == buf.buf || *ptr != ' '
332 || errno || (unsigned int) ul != ul)
333 goto bad_line;
334 mode = ul;
336 tab = strchr(ptr, '\t');
337 if (!tab || tab - ptr < 41)
338 goto bad_line;
340 if (tab[-2] == ' ' && '0' <= tab[-1] && tab[-1] <= '3') {
341 stage = tab[-1] - '0';
342 ptr = tab + 1; /* point at the head of path */
343 tab = tab - 2; /* point at tail of sha1 */
345 else {
346 stage = 0;
347 ptr = tab + 1; /* point at the head of path */
350 if (get_sha1_hex(tab - 40, sha1) || tab[-41] != ' ')
351 goto bad_line;
353 path_name = ptr;
354 if (line_termination && path_name[0] == '"') {
355 strbuf_reset(&uq);
356 if (unquote_c_style(&uq, path_name, NULL)) {
357 die("git update-index: bad quoting of path name");
359 path_name = uq.buf;
362 if (!verify_path(path_name)) {
363 fprintf(stderr, "Ignoring path %s\n", path_name);
364 continue;
367 if (!mode) {
368 /* mode == 0 means there is no such path -- remove */
369 if (remove_file_from_cache(path_name))
370 die("git update-index: unable to remove %s",
371 ptr);
373 else {
374 /* mode ' ' sha1 '\t' name
375 * ptr[-1] points at tab,
376 * ptr[-41] is at the beginning of sha1
378 ptr[-42] = ptr[-1] = 0;
379 if (add_cacheinfo(mode, sha1, path_name, stage))
380 die("git update-index: unable to update %s",
381 path_name);
383 continue;
385 bad_line:
386 die("malformed index info %s", buf.buf);
388 strbuf_release(&buf);
389 strbuf_release(&uq);
392 static const char update_index_usage[] =
393 "git update-index [-q] [--add] [--replace] [--remove] [--unmerged] [--refresh] [--really-refresh] [--cacheinfo] [--chmod=(+|-)x] [--assume-unchanged] [--info-only] [--force-remove] [--stdin] [--index-info] [--unresolve] [--again | -g] [--ignore-missing] [-z] [--verbose] [--] <file>...";
395 static unsigned char head_sha1[20];
396 static unsigned char merge_head_sha1[20];
398 static struct cache_entry *read_one_ent(const char *which,
399 unsigned char *ent, const char *path,
400 int namelen, int stage)
402 unsigned mode;
403 unsigned char sha1[20];
404 int size;
405 struct cache_entry *ce;
407 if (get_tree_entry(ent, path, sha1, &mode)) {
408 if (which)
409 error("%s: not in %s branch.", path, which);
410 return NULL;
412 if (mode == S_IFDIR) {
413 if (which)
414 error("%s: not a blob in %s branch.", path, which);
415 return NULL;
417 size = cache_entry_size(namelen);
418 ce = xcalloc(1, size);
420 hashcpy(ce->sha1, sha1);
421 memcpy(ce->name, path, namelen);
422 ce->ce_flags = create_ce_flags(namelen, stage);
423 ce->ce_mode = create_ce_mode(mode);
424 return ce;
427 static int unresolve_one(const char *path)
429 int namelen = strlen(path);
430 int pos;
431 int ret = 0;
432 struct cache_entry *ce_2 = NULL, *ce_3 = NULL;
434 /* See if there is such entry in the index. */
435 pos = cache_name_pos(path, namelen);
436 if (0 <= pos) {
437 /* already merged */
438 pos = unmerge_cache_entry_at(pos);
439 if (pos < active_nr) {
440 struct cache_entry *ce = active_cache[pos];
441 if (ce_stage(ce) &&
442 ce_namelen(ce) == namelen &&
443 !memcmp(ce->name, path, namelen))
444 return 0;
446 /* no resolve-undo information; fall back */
447 } else {
448 /* If there isn't, either it is unmerged, or
449 * resolved as "removed" by mistake. We do not
450 * want to do anything in the former case.
452 pos = -pos-1;
453 if (pos < active_nr) {
454 struct cache_entry *ce = active_cache[pos];
455 if (ce_namelen(ce) == namelen &&
456 !memcmp(ce->name, path, namelen)) {
457 fprintf(stderr,
458 "%s: skipping still unmerged path.\n",
459 path);
460 goto free_return;
465 /* Grab blobs from given path from HEAD and MERGE_HEAD,
466 * stuff HEAD version in stage #2,
467 * stuff MERGE_HEAD version in stage #3.
469 ce_2 = read_one_ent("our", head_sha1, path, namelen, 2);
470 ce_3 = read_one_ent("their", merge_head_sha1, path, namelen, 3);
472 if (!ce_2 || !ce_3) {
473 ret = -1;
474 goto free_return;
476 if (!hashcmp(ce_2->sha1, ce_3->sha1) &&
477 ce_2->ce_mode == ce_3->ce_mode) {
478 fprintf(stderr, "%s: identical in both, skipping.\n",
479 path);
480 goto free_return;
483 remove_file_from_cache(path);
484 if (add_cache_entry(ce_2, ADD_CACHE_OK_TO_ADD)) {
485 error("%s: cannot add our version to the index.", path);
486 ret = -1;
487 goto free_return;
489 if (!add_cache_entry(ce_3, ADD_CACHE_OK_TO_ADD))
490 return 0;
491 error("%s: cannot add their version to the index.", path);
492 ret = -1;
493 free_return:
494 free(ce_2);
495 free(ce_3);
496 return ret;
499 static void read_head_pointers(void)
501 if (read_ref("HEAD", head_sha1))
502 die("No HEAD -- no initial commit yet?");
503 if (read_ref("MERGE_HEAD", merge_head_sha1)) {
504 fprintf(stderr, "Not in the middle of a merge.\n");
505 exit(0);
509 static int do_unresolve(int ac, const char **av,
510 const char *prefix, int prefix_length)
512 int i;
513 int err = 0;
515 /* Read HEAD and MERGE_HEAD; if MERGE_HEAD does not exist, we
516 * are not doing a merge, so exit with success status.
518 read_head_pointers();
520 for (i = 1; i < ac; i++) {
521 const char *arg = av[i];
522 const char *p = prefix_path(prefix, prefix_length, arg);
523 err |= unresolve_one(p);
524 if (p < arg || p > arg + strlen(arg))
525 free((char *)p);
527 return err;
530 static int do_reupdate(int ac, const char **av,
531 const char *prefix, int prefix_length)
533 /* Read HEAD and run update-index on paths that are
534 * merged and already different between index and HEAD.
536 int pos;
537 int has_head = 1;
538 const char **pathspec = get_pathspec(prefix, av + 1);
540 if (read_ref("HEAD", head_sha1))
541 /* If there is no HEAD, that means it is an initial
542 * commit. Update everything in the index.
544 has_head = 0;
545 redo:
546 for (pos = 0; pos < active_nr; pos++) {
547 struct cache_entry *ce = active_cache[pos];
548 struct cache_entry *old = NULL;
549 int save_nr;
551 if (ce_stage(ce) || !ce_path_match(ce, pathspec))
552 continue;
553 if (has_head)
554 old = read_one_ent(NULL, head_sha1,
555 ce->name, ce_namelen(ce), 0);
556 if (old && ce->ce_mode == old->ce_mode &&
557 !hashcmp(ce->sha1, old->sha1)) {
558 free(old);
559 continue; /* unchanged */
561 /* Be careful. The working tree may not have the
562 * path anymore, in which case, under 'allow_remove',
563 * or worse yet 'allow_replace', active_nr may decrease.
565 save_nr = active_nr;
566 update_one(ce->name + prefix_length, prefix, prefix_length);
567 if (save_nr != active_nr)
568 goto redo;
570 return 0;
573 int cmd_update_index(int argc, const char **argv, const char *prefix)
575 int i, newfd, entries, has_errors = 0, line_termination = '\n';
576 int allow_options = 1;
577 int read_from_stdin = 0;
578 int prefix_length = prefix ? strlen(prefix) : 0;
579 char set_executable_bit = 0;
580 unsigned int refresh_flags = 0;
581 int lock_error = 0;
582 struct lock_file *lock_file;
584 git_config(git_default_config, NULL);
586 /* We can't free this memory, it becomes part of a linked list parsed atexit() */
587 lock_file = xcalloc(1, sizeof(struct lock_file));
589 newfd = hold_locked_index(lock_file, 0);
590 if (newfd < 0)
591 lock_error = errno;
593 entries = read_cache();
594 if (entries < 0)
595 die("cache corrupted");
597 for (i = 1 ; i < argc; i++) {
598 const char *path = argv[i];
599 const char *p;
601 if (allow_options && *path == '-') {
602 if (!strcmp(path, "--")) {
603 allow_options = 0;
604 continue;
606 if (!strcmp(path, "-q")) {
607 refresh_flags |= REFRESH_QUIET;
608 continue;
610 if (!strcmp(path, "--ignore-submodules")) {
611 refresh_flags |= REFRESH_IGNORE_SUBMODULES;
612 continue;
614 if (!strcmp(path, "--add")) {
615 allow_add = 1;
616 continue;
618 if (!strcmp(path, "--replace")) {
619 allow_replace = 1;
620 continue;
622 if (!strcmp(path, "--remove")) {
623 allow_remove = 1;
624 continue;
626 if (!strcmp(path, "--unmerged")) {
627 refresh_flags |= REFRESH_UNMERGED;
628 continue;
630 if (!strcmp(path, "--refresh")) {
631 setup_work_tree();
632 has_errors |= refresh_cache(refresh_flags);
633 continue;
635 if (!strcmp(path, "--really-refresh")) {
636 setup_work_tree();
637 has_errors |= refresh_cache(REFRESH_REALLY | refresh_flags);
638 continue;
640 if (!strcmp(path, "--cacheinfo")) {
641 unsigned char sha1[20];
642 unsigned int mode;
644 if (i+3 >= argc)
645 die("git update-index: --cacheinfo <mode> <sha1> <path>");
647 if (strtoul_ui(argv[i+1], 8, &mode) ||
648 get_sha1_hex(argv[i+2], sha1) ||
649 add_cacheinfo(mode, sha1, argv[i+3], 0))
650 die("git update-index: --cacheinfo"
651 " cannot add %s", argv[i+3]);
652 i += 3;
653 continue;
655 if (!strcmp(path, "--chmod=-x") ||
656 !strcmp(path, "--chmod=+x")) {
657 if (argc <= i+1)
658 die("git update-index: %s <path>", path);
659 set_executable_bit = path[8];
660 continue;
662 if (!strcmp(path, "--assume-unchanged")) {
663 mark_valid_only = MARK_VALID;
664 continue;
666 if (!strcmp(path, "--no-assume-unchanged")) {
667 mark_valid_only = UNMARK_VALID;
668 continue;
670 if (!strcmp(path, "--info-only")) {
671 info_only = 1;
672 continue;
674 if (!strcmp(path, "--force-remove")) {
675 force_remove = 1;
676 continue;
678 if (!strcmp(path, "-z")) {
679 line_termination = 0;
680 continue;
682 if (!strcmp(path, "--stdin")) {
683 if (i != argc - 1)
684 die("--stdin must be at the end");
685 read_from_stdin = 1;
686 break;
688 if (!strcmp(path, "--index-info")) {
689 if (i != argc - 1)
690 die("--index-info must be at the end");
691 allow_add = allow_replace = allow_remove = 1;
692 read_index_info(line_termination);
693 break;
695 if (!strcmp(path, "--unresolve")) {
696 has_errors = do_unresolve(argc - i, argv + i,
697 prefix, prefix_length);
698 if (has_errors)
699 active_cache_changed = 0;
700 goto finish;
702 if (!strcmp(path, "--again") || !strcmp(path, "-g")) {
703 setup_work_tree();
704 has_errors = do_reupdate(argc - i, argv + i,
705 prefix, prefix_length);
706 if (has_errors)
707 active_cache_changed = 0;
708 goto finish;
710 if (!strcmp(path, "--ignore-missing")) {
711 refresh_flags |= REFRESH_IGNORE_MISSING;
712 continue;
714 if (!strcmp(path, "--verbose")) {
715 verbose = 1;
716 continue;
718 if (!strcmp(path, "--clear-resolve-undo")) {
719 resolve_undo_clear();
720 continue;
722 if (!strcmp(path, "-h") || !strcmp(path, "--help"))
723 usage(update_index_usage);
724 die("unknown option %s", path);
726 setup_work_tree();
727 p = prefix_path(prefix, prefix_length, path);
728 update_one(p, NULL, 0);
729 if (set_executable_bit)
730 chmod_path(set_executable_bit, p);
731 if (p < path || p > path + strlen(path))
732 free((char *)p);
734 if (read_from_stdin) {
735 struct strbuf buf = STRBUF_INIT, nbuf = STRBUF_INIT;
737 setup_work_tree();
738 while (strbuf_getline(&buf, stdin, line_termination) != EOF) {
739 const char *p;
740 if (line_termination && buf.buf[0] == '"') {
741 strbuf_reset(&nbuf);
742 if (unquote_c_style(&nbuf, buf.buf, NULL))
743 die("line is badly quoted");
744 strbuf_swap(&buf, &nbuf);
746 p = prefix_path(prefix, prefix_length, buf.buf);
747 update_one(p, NULL, 0);
748 if (set_executable_bit)
749 chmod_path(set_executable_bit, p);
750 if (p < buf.buf || p > buf.buf + buf.len)
751 free((char *)p);
753 strbuf_release(&nbuf);
754 strbuf_release(&buf);
757 finish:
758 if (active_cache_changed) {
759 if (newfd < 0) {
760 if (refresh_flags & REFRESH_QUIET)
761 exit(128);
762 unable_to_lock_index_die(get_index_file(), lock_error);
764 if (write_cache(newfd, active_cache, active_nr) ||
765 commit_locked_index(lock_file))
766 die("Unable to write new index file");
769 rollback_lock_file(lock_file);
771 return has_errors ? 1 : 0;