Merge branch 'mh/rebase-fixup' (early part)
[git/dkf.git] / builtin-update-index.c
bloba64a752990b85b69590cdbeed3901154642007b8
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"
14 * Default to not allowing changes to the list of files. The
15 * tool doesn't actually care, but this makes it harder to add
16 * files to the revision control by mistake by doing something
17 * like "git update-index *" and suddenly having all the object
18 * files be revision controlled.
20 static int allow_add;
21 static int allow_remove;
22 static int allow_replace;
23 static int info_only;
24 static int force_remove;
25 static int verbose;
26 static int mark_valid_only;
27 static int mark_skip_worktree_only;
28 #define MARK_FLAG 1
29 #define UNMARK_FLAG 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_ce_flags(const char *path, int flag, int mark)
47 int namelen = strlen(path);
48 int pos = cache_name_pos(path, namelen);
49 if (0 <= pos) {
50 if (mark)
51 active_cache[pos]->ce_flags |= flag;
52 else
53 active_cache[pos]->ce_flags &= ~flag;
54 cache_tree_invalidate_path(active_cache_tree, path);
55 active_cache_changed = 1;
56 return 0;
58 return -1;
61 static int remove_one_path(const char *path)
63 if (!allow_remove)
64 return error("%s: does not exist and --remove not passed", path);
65 if (remove_file_from_cache(path))
66 return error("%s: cannot remove from the index", path);
67 return 0;
71 * Handle a path that couldn't be lstat'ed. It's either:
72 * - missing file (ENOENT or ENOTDIR). That's ok if we're
73 * supposed to be removing it and the removal actually
74 * succeeds.
75 * - permission error. That's never ok.
77 static int process_lstat_error(const char *path, int err)
79 if (err == ENOENT || err == ENOTDIR)
80 return remove_one_path(path);
81 return error("lstat(\"%s\"): %s", path, strerror(errno));
84 static int add_one_path(struct cache_entry *old, const char *path, int len, struct stat *st)
86 int option, size;
87 struct cache_entry *ce;
89 /* Was the old index entry already up-to-date? */
90 if (old && !ce_stage(old) && !ce_match_stat(old, st, 0))
91 return 0;
93 size = cache_entry_size(len);
94 ce = xcalloc(1, size);
95 memcpy(ce->name, path, len);
96 ce->ce_flags = len;
97 fill_stat_cache_info(ce, st);
98 ce->ce_mode = ce_mode_from_stat(old, st->st_mode);
100 if (index_path(ce->sha1, path, st, !info_only))
101 return -1;
102 option = allow_add ? ADD_CACHE_OK_TO_ADD : 0;
103 option |= allow_replace ? ADD_CACHE_OK_TO_REPLACE : 0;
104 if (add_cache_entry(ce, option))
105 return error("%s: cannot add to the index - missing --add option?", path);
106 return 0;
110 * Handle a path that was a directory. Four cases:
112 * - it's already a gitlink in the index, and we keep it that
113 * way, and update it if we can (if we cannot find the HEAD,
114 * we're going to keep it unchanged in the index!)
116 * - it's a *file* in the index, in which case it should be
117 * removed as a file if removal is allowed, since it doesn't
118 * exist as such any more. If removal isn't allowed, it's
119 * an error.
121 * (NOTE! This is old and arguably fairly strange behaviour.
122 * We might want to make this an error unconditionally, and
123 * use "--force-remove" if you actually want to force removal).
125 * - it used to exist as a subdirectory (ie multiple files with
126 * this particular prefix) in the index, in which case it's wrong
127 * to try to update it as a directory.
129 * - it doesn't exist at all in the index, but it is a valid
130 * git directory, and it should be *added* as a gitlink.
132 static int process_directory(const char *path, int len, struct stat *st)
134 unsigned char sha1[20];
135 int pos = cache_name_pos(path, len);
137 /* Exact match: file or existing gitlink */
138 if (pos >= 0) {
139 struct cache_entry *ce = active_cache[pos];
140 if (S_ISGITLINK(ce->ce_mode)) {
142 /* Do nothing to the index if there is no HEAD! */
143 if (resolve_gitlink_ref(path, "HEAD", sha1) < 0)
144 return 0;
146 return add_one_path(ce, path, len, st);
148 /* Should this be an unconditional error? */
149 return remove_one_path(path);
152 /* Inexact match: is there perhaps a subdirectory match? */
153 pos = -pos-1;
154 while (pos < active_nr) {
155 struct cache_entry *ce = active_cache[pos++];
157 if (strncmp(ce->name, path, len))
158 break;
159 if (ce->name[len] > '/')
160 break;
161 if (ce->name[len] < '/')
162 continue;
164 /* Subdirectory match - error out */
165 return error("%s: is a directory - add individual files instead", path);
168 /* No match - should we add it as a gitlink? */
169 if (!resolve_gitlink_ref(path, "HEAD", sha1))
170 return add_one_path(NULL, path, len, st);
172 /* Error out. */
173 return error("%s: is a directory - add files inside instead", path);
176 static int process_path(const char *path)
178 int pos, len;
179 struct stat st;
180 struct cache_entry *ce;
182 len = strlen(path);
183 if (has_symlink_leading_path(path, len))
184 return error("'%s' is beyond a symbolic link", path);
186 pos = cache_name_pos(path, len);
187 ce = pos < 0 ? NULL : active_cache[pos];
188 if (ce && ce_skip_worktree(ce)) {
190 * working directory version is assumed "good"
191 * so updating it does not make sense.
192 * On the other hand, removing it from index should work
194 if (allow_remove && remove_file_from_cache(path))
195 return error("%s: cannot remove from the index", path);
196 return 0;
200 * First things first: get the stat information, to decide
201 * what to do about the pathname!
203 if (lstat(path, &st) < 0)
204 return process_lstat_error(path, errno);
206 if (S_ISDIR(st.st_mode))
207 return process_directory(path, len, &st);
210 * Process a regular file
212 if (ce && S_ISGITLINK(ce->ce_mode))
213 return error("%s is already a gitlink, not replacing", path);
215 return add_one_path(ce, path, len, &st);
218 static int add_cacheinfo(unsigned int mode, const unsigned char *sha1,
219 const char *path, int stage)
221 int size, len, option;
222 struct cache_entry *ce;
224 if (!verify_path(path))
225 return error("Invalid path '%s'", path);
227 len = strlen(path);
228 size = cache_entry_size(len);
229 ce = xcalloc(1, size);
231 hashcpy(ce->sha1, sha1);
232 memcpy(ce->name, path, len);
233 ce->ce_flags = create_ce_flags(len, stage);
234 ce->ce_mode = create_ce_mode(mode);
235 if (assume_unchanged)
236 ce->ce_flags |= CE_VALID;
237 option = allow_add ? ADD_CACHE_OK_TO_ADD : 0;
238 option |= allow_replace ? ADD_CACHE_OK_TO_REPLACE : 0;
239 if (add_cache_entry(ce, option))
240 return error("%s: cannot add to the index - missing --add option?",
241 path);
242 report("add '%s'", path);
243 return 0;
246 static void chmod_path(int flip, const char *path)
248 int pos;
249 struct cache_entry *ce;
250 unsigned int mode;
252 pos = cache_name_pos(path, strlen(path));
253 if (pos < 0)
254 goto fail;
255 ce = active_cache[pos];
256 mode = ce->ce_mode;
257 if (!S_ISREG(mode))
258 goto fail;
259 switch (flip) {
260 case '+':
261 ce->ce_mode |= 0111; break;
262 case '-':
263 ce->ce_mode &= ~0111; break;
264 default:
265 goto fail;
267 cache_tree_invalidate_path(active_cache_tree, path);
268 active_cache_changed = 1;
269 report("chmod %cx '%s'", flip, path);
270 return;
271 fail:
272 die("git update-index: cannot chmod %cx '%s'", flip, path);
275 static void update_one(const char *path, const char *prefix, int prefix_length)
277 const char *p = prefix_path(prefix, prefix_length, path);
278 if (!verify_path(p)) {
279 fprintf(stderr, "Ignoring path %s\n", path);
280 goto free_return;
282 if (mark_valid_only) {
283 if (mark_ce_flags(p, CE_VALID, mark_valid_only == MARK_FLAG))
284 die("Unable to mark file %s", path);
285 goto free_return;
287 if (mark_skip_worktree_only) {
288 if (mark_ce_flags(p, CE_SKIP_WORKTREE, mark_skip_worktree_only == MARK_FLAG))
289 die("Unable to mark file %s", path);
290 goto free_return;
293 if (force_remove) {
294 if (remove_file_from_cache(p))
295 die("git update-index: unable to remove %s", path);
296 report("remove '%s'", path);
297 goto free_return;
299 if (process_path(p))
300 die("Unable to process path %s", path);
301 report("add '%s'", path);
302 free_return:
303 if (p < path || p > path + strlen(path))
304 free((char *)p);
307 static void read_index_info(int line_termination)
309 struct strbuf buf = STRBUF_INIT;
310 struct strbuf uq = STRBUF_INIT;
312 while (strbuf_getline(&buf, stdin, line_termination) != EOF) {
313 char *ptr, *tab;
314 char *path_name;
315 unsigned char sha1[20];
316 unsigned int mode;
317 unsigned long ul;
318 int stage;
320 /* This reads lines formatted in one of three formats:
322 * (1) mode SP sha1 TAB path
323 * The first format is what "git apply --index-info"
324 * reports, and used to reconstruct a partial tree
325 * that is used for phony merge base tree when falling
326 * back on 3-way merge.
328 * (2) mode SP type SP sha1 TAB path
329 * The second format is to stuff "git ls-tree" output
330 * into the index file.
332 * (3) mode SP sha1 SP stage TAB path
333 * This format is to put higher order stages into the
334 * index file and matches "git ls-files --stage" output.
336 errno = 0;
337 ul = strtoul(buf.buf, &ptr, 8);
338 if (ptr == buf.buf || *ptr != ' '
339 || errno || (unsigned int) ul != ul)
340 goto bad_line;
341 mode = ul;
343 tab = strchr(ptr, '\t');
344 if (!tab || tab - ptr < 41)
345 goto bad_line;
347 if (tab[-2] == ' ' && '0' <= tab[-1] && tab[-1] <= '3') {
348 stage = tab[-1] - '0';
349 ptr = tab + 1; /* point at the head of path */
350 tab = tab - 2; /* point at tail of sha1 */
352 else {
353 stage = 0;
354 ptr = tab + 1; /* point at the head of path */
357 if (get_sha1_hex(tab - 40, sha1) || tab[-41] != ' ')
358 goto bad_line;
360 path_name = ptr;
361 if (line_termination && path_name[0] == '"') {
362 strbuf_reset(&uq);
363 if (unquote_c_style(&uq, path_name, NULL)) {
364 die("git update-index: bad quoting of path name");
366 path_name = uq.buf;
369 if (!verify_path(path_name)) {
370 fprintf(stderr, "Ignoring path %s\n", path_name);
371 continue;
374 if (!mode) {
375 /* mode == 0 means there is no such path -- remove */
376 if (remove_file_from_cache(path_name))
377 die("git update-index: unable to remove %s",
378 ptr);
380 else {
381 /* mode ' ' sha1 '\t' name
382 * ptr[-1] points at tab,
383 * ptr[-41] is at the beginning of sha1
385 ptr[-42] = ptr[-1] = 0;
386 if (add_cacheinfo(mode, sha1, path_name, stage))
387 die("git update-index: unable to update %s",
388 path_name);
390 continue;
392 bad_line:
393 die("malformed index info %s", buf.buf);
395 strbuf_release(&buf);
396 strbuf_release(&uq);
399 static const char update_index_usage[] =
400 "git update-index [-q] [--add] [--replace] [--remove] [--unmerged] [--refresh] [--really-refresh] [--cacheinfo] [--chmod=(+|-)x] [--assume-unchanged] [--skip-worktree|--no-skip-worktree] [--info-only] [--force-remove] [--stdin] [--index-info] [--unresolve] [--again | -g] [--ignore-missing] [-z] [--verbose] [--] <file>...";
402 static unsigned char head_sha1[20];
403 static unsigned char merge_head_sha1[20];
405 static struct cache_entry *read_one_ent(const char *which,
406 unsigned char *ent, const char *path,
407 int namelen, int stage)
409 unsigned mode;
410 unsigned char sha1[20];
411 int size;
412 struct cache_entry *ce;
414 if (get_tree_entry(ent, path, sha1, &mode)) {
415 if (which)
416 error("%s: not in %s branch.", path, which);
417 return NULL;
419 if (mode == S_IFDIR) {
420 if (which)
421 error("%s: not a blob in %s branch.", path, which);
422 return NULL;
424 size = cache_entry_size(namelen);
425 ce = xcalloc(1, size);
427 hashcpy(ce->sha1, sha1);
428 memcpy(ce->name, path, namelen);
429 ce->ce_flags = create_ce_flags(namelen, stage);
430 ce->ce_mode = create_ce_mode(mode);
431 return ce;
434 static int unresolve_one(const char *path)
436 int namelen = strlen(path);
437 int pos;
438 int ret = 0;
439 struct cache_entry *ce_2 = NULL, *ce_3 = NULL;
441 /* See if there is such entry in the index. */
442 pos = cache_name_pos(path, namelen);
443 if (pos < 0) {
444 /* If there isn't, either it is unmerged, or
445 * resolved as "removed" by mistake. We do not
446 * want to do anything in the former case.
448 pos = -pos-1;
449 if (pos < active_nr) {
450 struct cache_entry *ce = active_cache[pos];
451 if (ce_namelen(ce) == namelen &&
452 !memcmp(ce->name, path, namelen)) {
453 fprintf(stderr,
454 "%s: skipping still unmerged path.\n",
455 path);
456 goto free_return;
461 /* Grab blobs from given path from HEAD and MERGE_HEAD,
462 * stuff HEAD version in stage #2,
463 * stuff MERGE_HEAD version in stage #3.
465 ce_2 = read_one_ent("our", head_sha1, path, namelen, 2);
466 ce_3 = read_one_ent("their", merge_head_sha1, path, namelen, 3);
468 if (!ce_2 || !ce_3) {
469 ret = -1;
470 goto free_return;
472 if (!hashcmp(ce_2->sha1, ce_3->sha1) &&
473 ce_2->ce_mode == ce_3->ce_mode) {
474 fprintf(stderr, "%s: identical in both, skipping.\n",
475 path);
476 goto free_return;
479 remove_file_from_cache(path);
480 if (add_cache_entry(ce_2, ADD_CACHE_OK_TO_ADD)) {
481 error("%s: cannot add our version to the index.", path);
482 ret = -1;
483 goto free_return;
485 if (!add_cache_entry(ce_3, ADD_CACHE_OK_TO_ADD))
486 return 0;
487 error("%s: cannot add their version to the index.", path);
488 ret = -1;
489 free_return:
490 free(ce_2);
491 free(ce_3);
492 return ret;
495 static void read_head_pointers(void)
497 if (read_ref("HEAD", head_sha1))
498 die("No HEAD -- no initial commit yet?");
499 if (read_ref("MERGE_HEAD", merge_head_sha1)) {
500 fprintf(stderr, "Not in the middle of a merge.\n");
501 exit(0);
505 static int do_unresolve(int ac, const char **av,
506 const char *prefix, int prefix_length)
508 int i;
509 int err = 0;
511 /* Read HEAD and MERGE_HEAD; if MERGE_HEAD does not exist, we
512 * are not doing a merge, so exit with success status.
514 read_head_pointers();
516 for (i = 1; i < ac; i++) {
517 const char *arg = av[i];
518 const char *p = prefix_path(prefix, prefix_length, arg);
519 err |= unresolve_one(p);
520 if (p < arg || p > arg + strlen(arg))
521 free((char *)p);
523 return err;
526 static int do_reupdate(int ac, const char **av,
527 const char *prefix, int prefix_length)
529 /* Read HEAD and run update-index on paths that are
530 * merged and already different between index and HEAD.
532 int pos;
533 int has_head = 1;
534 const char **pathspec = get_pathspec(prefix, av + 1);
536 if (read_ref("HEAD", head_sha1))
537 /* If there is no HEAD, that means it is an initial
538 * commit. Update everything in the index.
540 has_head = 0;
541 redo:
542 for (pos = 0; pos < active_nr; pos++) {
543 struct cache_entry *ce = active_cache[pos];
544 struct cache_entry *old = NULL;
545 int save_nr;
547 if (ce_stage(ce) || !ce_path_match(ce, pathspec))
548 continue;
549 if (has_head)
550 old = read_one_ent(NULL, head_sha1,
551 ce->name, ce_namelen(ce), 0);
552 if (old && ce->ce_mode == old->ce_mode &&
553 !hashcmp(ce->sha1, old->sha1)) {
554 free(old);
555 continue; /* unchanged */
557 /* Be careful. The working tree may not have the
558 * path anymore, in which case, under 'allow_remove',
559 * or worse yet 'allow_replace', active_nr may decrease.
561 save_nr = active_nr;
562 update_one(ce->name + prefix_length, prefix, prefix_length);
563 if (save_nr != active_nr)
564 goto redo;
566 return 0;
569 int cmd_update_index(int argc, const char **argv, const char *prefix)
571 int i, newfd, entries, has_errors = 0, line_termination = '\n';
572 int allow_options = 1;
573 int read_from_stdin = 0;
574 int prefix_length = prefix ? strlen(prefix) : 0;
575 char set_executable_bit = 0;
576 unsigned int refresh_flags = 0;
577 int lock_error = 0;
578 struct lock_file *lock_file;
580 git_config(git_default_config, NULL);
582 /* We can't free this memory, it becomes part of a linked list parsed atexit() */
583 lock_file = xcalloc(1, sizeof(struct lock_file));
585 newfd = hold_locked_index(lock_file, 0);
586 if (newfd < 0)
587 lock_error = errno;
589 entries = read_cache();
590 if (entries < 0)
591 die("cache corrupted");
593 for (i = 1 ; i < argc; i++) {
594 const char *path = argv[i];
595 const char *p;
597 if (allow_options && *path == '-') {
598 if (!strcmp(path, "--")) {
599 allow_options = 0;
600 continue;
602 if (!strcmp(path, "-q")) {
603 refresh_flags |= REFRESH_QUIET;
604 continue;
606 if (!strcmp(path, "--ignore-submodules")) {
607 refresh_flags |= REFRESH_IGNORE_SUBMODULES;
608 continue;
610 if (!strcmp(path, "--add")) {
611 allow_add = 1;
612 continue;
614 if (!strcmp(path, "--replace")) {
615 allow_replace = 1;
616 continue;
618 if (!strcmp(path, "--remove")) {
619 allow_remove = 1;
620 continue;
622 if (!strcmp(path, "--unmerged")) {
623 refresh_flags |= REFRESH_UNMERGED;
624 continue;
626 if (!strcmp(path, "--refresh")) {
627 setup_work_tree();
628 has_errors |= refresh_cache(refresh_flags);
629 continue;
631 if (!strcmp(path, "--really-refresh")) {
632 setup_work_tree();
633 has_errors |= refresh_cache(REFRESH_REALLY | refresh_flags);
634 continue;
636 if (!strcmp(path, "--cacheinfo")) {
637 unsigned char sha1[20];
638 unsigned int mode;
640 if (i+3 >= argc)
641 die("git update-index: --cacheinfo <mode> <sha1> <path>");
643 if (strtoul_ui(argv[i+1], 8, &mode) ||
644 get_sha1_hex(argv[i+2], sha1) ||
645 add_cacheinfo(mode, sha1, argv[i+3], 0))
646 die("git update-index: --cacheinfo"
647 " cannot add %s", argv[i+3]);
648 i += 3;
649 continue;
651 if (!strcmp(path, "--chmod=-x") ||
652 !strcmp(path, "--chmod=+x")) {
653 if (argc <= i+1)
654 die("git update-index: %s <path>", path);
655 set_executable_bit = path[8];
656 continue;
658 if (!strcmp(path, "--assume-unchanged")) {
659 mark_valid_only = MARK_FLAG;
660 continue;
662 if (!strcmp(path, "--no-assume-unchanged")) {
663 mark_valid_only = UNMARK_FLAG;
664 continue;
666 if (!strcmp(path, "--no-skip-worktree")) {
667 mark_skip_worktree_only = UNMARK_FLAG;
668 continue;
670 if (!strcmp(path, "--skip-worktree")) {
671 mark_skip_worktree_only = MARK_FLAG;
672 continue;
674 if (!strcmp(path, "--info-only")) {
675 info_only = 1;
676 continue;
678 if (!strcmp(path, "--force-remove")) {
679 force_remove = 1;
680 continue;
682 if (!strcmp(path, "-z")) {
683 line_termination = 0;
684 continue;
686 if (!strcmp(path, "--stdin")) {
687 if (i != argc - 1)
688 die("--stdin must be at the end");
689 read_from_stdin = 1;
690 break;
692 if (!strcmp(path, "--index-info")) {
693 if (i != argc - 1)
694 die("--index-info must be at the end");
695 allow_add = allow_replace = allow_remove = 1;
696 read_index_info(line_termination);
697 break;
699 if (!strcmp(path, "--unresolve")) {
700 has_errors = do_unresolve(argc - i, argv + i,
701 prefix, prefix_length);
702 if (has_errors)
703 active_cache_changed = 0;
704 goto finish;
706 if (!strcmp(path, "--again") || !strcmp(path, "-g")) {
707 setup_work_tree();
708 has_errors = do_reupdate(argc - i, argv + i,
709 prefix, prefix_length);
710 if (has_errors)
711 active_cache_changed = 0;
712 goto finish;
714 if (!strcmp(path, "--ignore-missing")) {
715 refresh_flags |= REFRESH_IGNORE_MISSING;
716 continue;
718 if (!strcmp(path, "--verbose")) {
719 verbose = 1;
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;