mergetool: Fix typo in options passed to kdiff3
[git/mergetool.git] / builtin-update-index.c
bloba7a4574f2bff2a7db4a1c25aa4a514ad99760381
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"
10 #include "tree-walk.h"
11 #include "builtin.h"
12 #include "refs.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 static void report(const char *fmt, ...)
33 va_list vp;
35 if (!verbose)
36 return;
38 va_start(vp, fmt);
39 vprintf(fmt, vp);
40 putchar('\n');
41 va_end(vp);
44 static int mark_valid(const char *path)
46 int namelen = strlen(path);
47 int pos = cache_name_pos(path, namelen);
48 if (0 <= pos) {
49 switch (mark_valid_only) {
50 case MARK_VALID:
51 active_cache[pos]->ce_flags |= htons(CE_VALID);
52 break;
53 case UNMARK_VALID:
54 active_cache[pos]->ce_flags &= ~htons(CE_VALID);
55 break;
57 cache_tree_invalidate_path(active_cache_tree, path);
58 active_cache_changed = 1;
59 return 0;
61 return -1;
64 static int remove_one_path(const char *path)
66 if (!allow_remove)
67 return error("%s: does not exist and --remove not passed", path);
68 if (remove_file_from_cache(path))
69 return error("%s: cannot remove from the index", path);
70 return 0;
74 * Handle a path that couldn't be lstat'ed. It's either:
75 * - missing file (ENOENT or ENOTDIR). That's ok if we're
76 * supposed to be removing it and the removal actually
77 * succeeds.
78 * - permission error. That's never ok.
80 static int process_lstat_error(const char *path, int err)
82 if (err == ENOENT || err == ENOTDIR)
83 return remove_one_path(path);
84 return error("lstat(\"%s\"): %s", path, strerror(errno));
87 static int add_one_path(struct cache_entry *old, const char *path, int len, struct stat *st)
89 int option, size;
90 struct cache_entry *ce;
92 /* Was the old index entry already up-to-date? */
93 if (old && !ce_stage(old) && !ce_match_stat(old, st, 0))
94 return 0;
96 size = cache_entry_size(len);
97 ce = xcalloc(1, size);
98 memcpy(ce->name, path, len);
99 ce->ce_flags = htons(len);
100 fill_stat_cache_info(ce, st);
101 ce->ce_mode = ce_mode_from_stat(old, st->st_mode);
103 if (index_path(ce->sha1, path, st, !info_only))
104 return -1;
105 option = allow_add ? ADD_CACHE_OK_TO_ADD : 0;
106 option |= allow_replace ? ADD_CACHE_OK_TO_REPLACE : 0;
107 if (add_cache_entry(ce, option))
108 return error("%s: cannot add to the index - missing --add option?", path);
109 return 0;
113 * Handle a path that was a directory. Four cases:
115 * - it's already a gitlink in the index, and we keep it that
116 * way, and update it if we can (if we cannot find the HEAD,
117 * we're going to keep it unchanged in the index!)
119 * - it's a *file* in the index, in which case it should be
120 * removed as a file if removal is allowed, since it doesn't
121 * exist as such any more. If removal isn't allowed, it's
122 * an error.
124 * (NOTE! This is old and arguably fairly strange behaviour.
125 * We might want to make this an error unconditionally, and
126 * use "--force-remove" if you actually want to force removal).
128 * - it used to exist as a subdirectory (ie multiple files with
129 * this particular prefix) in the index, in which case it's wrong
130 * to try to update it as a directory.
132 * - it doesn't exist at all in the index, but it is a valid
133 * git directory, and it should be *added* as a gitlink.
135 static int process_directory(const char *path, int len, struct stat *st)
137 unsigned char sha1[20];
138 int pos = cache_name_pos(path, len);
140 /* Exact match: file or existing gitlink */
141 if (pos >= 0) {
142 struct cache_entry *ce = active_cache[pos];
143 if (S_ISGITLINK(ntohl(ce->ce_mode))) {
145 /* Do nothing to the index if there is no HEAD! */
146 if (resolve_gitlink_ref(path, "HEAD", sha1) < 0)
147 return 0;
149 return add_one_path(ce, path, len, st);
151 /* Should this be an unconditional error? */
152 return remove_one_path(path);
155 /* Inexact match: is there perhaps a subdirectory match? */
156 pos = -pos-1;
157 while (pos < active_nr) {
158 struct cache_entry *ce = active_cache[pos++];
160 if (strncmp(ce->name, path, len))
161 break;
162 if (ce->name[len] > '/')
163 break;
164 if (ce->name[len] < '/')
165 continue;
167 /* Subdirectory match - error out */
168 return error("%s: is a directory - add individual files instead", path);
171 /* No match - should we add it as a gitlink? */
172 if (!resolve_gitlink_ref(path, "HEAD", sha1))
173 return add_one_path(NULL, path, len, st);
175 /* Error out. */
176 return error("%s: is a directory - add files inside instead", path);
180 * Process a regular file
182 static int process_file(const char *path, int len, struct stat *st)
184 int pos = cache_name_pos(path, len);
185 struct cache_entry *ce = pos < 0 ? NULL : active_cache[pos];
187 if (ce && S_ISGITLINK(ntohl(ce->ce_mode)))
188 return error("%s is already a gitlink, not replacing", path);
190 return add_one_path(ce, path, len, st);
193 static int process_path(const char *path)
195 int len;
196 struct stat st;
198 /* We probably want to do this in remove_file_from_cache() and
199 * add_cache_entry() instead...
201 cache_tree_invalidate_path(active_cache_tree, 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 len = strlen(path);
211 if (S_ISDIR(st.st_mode))
212 return process_directory(path, len, &st);
214 return process_file(path, len, &st);
217 static int add_cacheinfo(unsigned int mode, const unsigned char *sha1,
218 const char *path, int stage)
220 int size, len, option;
221 struct cache_entry *ce;
223 if (!verify_path(path))
224 return -1;
226 len = strlen(path);
227 size = cache_entry_size(len);
228 ce = xcalloc(1, size);
230 hashcpy(ce->sha1, sha1);
231 memcpy(ce->name, path, len);
232 ce->ce_flags = create_ce_flags(len, stage);
233 ce->ce_mode = create_ce_mode(mode);
234 if (assume_unchanged)
235 ce->ce_flags |= htons(CE_VALID);
236 option = allow_add ? ADD_CACHE_OK_TO_ADD : 0;
237 option |= allow_replace ? ADD_CACHE_OK_TO_REPLACE : 0;
238 if (add_cache_entry(ce, option))
239 return error("%s: cannot add to the index - missing --add option?",
240 path);
241 report("add '%s'", path);
242 cache_tree_invalidate_path(active_cache_tree, 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 = ntohl(ce->ce_mode);
257 if (!S_ISREG(mode))
258 goto fail;
259 switch (flip) {
260 case '+':
261 ce->ce_mode |= htonl(0111); break;
262 case '-':
263 ce->ce_mode &= htonl(~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_valid(p))
284 die("Unable to mark file %s", path);
285 goto free_return;
287 cache_tree_invalidate_path(active_cache_tree, path);
289 if (force_remove) {
290 if (remove_file_from_cache(p))
291 die("git-update-index: unable to remove %s", path);
292 report("remove '%s'", path);
293 goto free_return;
295 if (process_path(p))
296 die("Unable to process path %s", path);
297 report("add '%s'", path);
298 free_return:
299 if (p < path || p > path + strlen(path))
300 free((char*)p);
303 static void read_index_info(int line_termination)
305 struct strbuf buf;
306 strbuf_init(&buf);
307 while (1) {
308 char *ptr, *tab;
309 char *path_name;
310 unsigned char sha1[20];
311 unsigned int mode;
312 unsigned long ul;
313 int stage;
315 /* This reads lines formatted in one of three formats:
317 * (1) mode SP sha1 TAB path
318 * The first format is what "git-apply --index-info"
319 * reports, and used to reconstruct a partial tree
320 * that is used for phony merge base tree when falling
321 * back on 3-way merge.
323 * (2) mode SP type SP sha1 TAB path
324 * The second format is to stuff git-ls-tree output
325 * into the index file.
327 * (3) mode SP sha1 SP stage TAB path
328 * This format is to put higher order stages into the
329 * index file and matches git-ls-files --stage output.
331 read_line(&buf, stdin, line_termination);
332 if (buf.eof)
333 break;
335 errno = 0;
336 ul = strtoul(buf.buf, &ptr, 8);
337 if (ptr == buf.buf || *ptr != ' '
338 || errno || (unsigned int) ul != ul)
339 goto bad_line;
340 mode = ul;
342 tab = strchr(ptr, '\t');
343 if (!tab || tab - ptr < 41)
344 goto bad_line;
346 if (tab[-2] == ' ' && '0' <= tab[-1] && tab[-1] <= '3') {
347 stage = tab[-1] - '0';
348 ptr = tab + 1; /* point at the head of path */
349 tab = tab - 2; /* point at tail of sha1 */
351 else {
352 stage = 0;
353 ptr = tab + 1; /* point at the head of path */
356 if (get_sha1_hex(tab - 40, sha1) || tab[-41] != ' ')
357 goto bad_line;
359 if (line_termination && ptr[0] == '"')
360 path_name = unquote_c_style(ptr, NULL);
361 else
362 path_name = ptr;
364 if (!verify_path(path_name)) {
365 fprintf(stderr, "Ignoring path %s\n", path_name);
366 if (path_name != ptr)
367 free(path_name);
368 continue;
370 cache_tree_invalidate_path(active_cache_tree, path_name);
372 if (!mode) {
373 /* mode == 0 means there is no such path -- remove */
374 if (remove_file_from_cache(path_name))
375 die("git-update-index: unable to remove %s",
376 ptr);
378 else {
379 /* mode ' ' sha1 '\t' name
380 * ptr[-1] points at tab,
381 * ptr[-41] is at the beginning of sha1
383 ptr[-42] = ptr[-1] = 0;
384 if (add_cacheinfo(mode, sha1, path_name, stage))
385 die("git-update-index: unable to update %s",
386 path_name);
388 if (path_name != ptr)
389 free(path_name);
390 continue;
392 bad_line:
393 die("malformed index info %s", buf.buf);
397 static const char update_index_usage[] =
398 "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>...";
400 static unsigned char head_sha1[20];
401 static unsigned char merge_head_sha1[20];
403 static struct cache_entry *read_one_ent(const char *which,
404 unsigned char *ent, const char *path,
405 int namelen, int stage)
407 unsigned mode;
408 unsigned char sha1[20];
409 int size;
410 struct cache_entry *ce;
412 if (get_tree_entry(ent, path, sha1, &mode)) {
413 if (which)
414 error("%s: not in %s branch.", path, which);
415 return NULL;
417 if (mode == S_IFDIR) {
418 if (which)
419 error("%s: not a blob in %s branch.", path, which);
420 return NULL;
422 size = cache_entry_size(namelen);
423 ce = xcalloc(1, size);
425 hashcpy(ce->sha1, sha1);
426 memcpy(ce->name, path, namelen);
427 ce->ce_flags = create_ce_flags(namelen, stage);
428 ce->ce_mode = create_ce_mode(mode);
429 return ce;
432 static int unresolve_one(const char *path)
434 int namelen = strlen(path);
435 int pos;
436 int ret = 0;
437 struct cache_entry *ce_2 = NULL, *ce_3 = NULL;
439 /* See if there is such entry in the index. */
440 pos = cache_name_pos(path, namelen);
441 if (pos < 0) {
442 /* If there isn't, either it is unmerged, or
443 * resolved as "removed" by mistake. We do not
444 * want to do anything in the former case.
446 pos = -pos-1;
447 if (pos < active_nr) {
448 struct cache_entry *ce = active_cache[pos];
449 if (ce_namelen(ce) == namelen &&
450 !memcmp(ce->name, path, namelen)) {
451 fprintf(stderr,
452 "%s: skipping still unmerged path.\n",
453 path);
454 goto free_return;
459 /* Grab blobs from given path from HEAD and MERGE_HEAD,
460 * stuff HEAD version in stage #2,
461 * stuff MERGE_HEAD version in stage #3.
463 ce_2 = read_one_ent("our", head_sha1, path, namelen, 2);
464 ce_3 = read_one_ent("their", merge_head_sha1, path, namelen, 3);
466 if (!ce_2 || !ce_3) {
467 ret = -1;
468 goto free_return;
470 if (!hashcmp(ce_2->sha1, ce_3->sha1) &&
471 ce_2->ce_mode == ce_3->ce_mode) {
472 fprintf(stderr, "%s: identical in both, skipping.\n",
473 path);
474 goto free_return;
477 cache_tree_invalidate_path(active_cache_tree, path);
478 remove_file_from_cache(path);
479 if (add_cache_entry(ce_2, ADD_CACHE_OK_TO_ADD)) {
480 error("%s: cannot add our version to the index.", path);
481 ret = -1;
482 goto free_return;
484 if (!add_cache_entry(ce_3, ADD_CACHE_OK_TO_ADD))
485 return 0;
486 error("%s: cannot add their version to the index.", path);
487 ret = -1;
488 free_return:
489 free(ce_2);
490 free(ce_3);
491 return ret;
494 static void read_head_pointers(void)
496 if (read_ref("HEAD", head_sha1))
497 die("No HEAD -- no initial commit yet?\n");
498 if (read_ref("MERGE_HEAD", merge_head_sha1)) {
499 fprintf(stderr, "Not in the middle of a merge.\n");
500 exit(0);
504 static int do_unresolve(int ac, const char **av,
505 const char *prefix, int prefix_length)
507 int i;
508 int err = 0;
510 /* Read HEAD and MERGE_HEAD; if MERGE_HEAD does not exist, we
511 * are not doing a merge, so exit with success status.
513 read_head_pointers();
515 for (i = 1; i < ac; i++) {
516 const char *arg = av[i];
517 const char *p = prefix_path(prefix, prefix_length, arg);
518 err |= unresolve_one(p);
519 if (p < arg || p > arg + strlen(arg))
520 free((char*)p);
522 return err;
525 static int do_reupdate(int ac, const char **av,
526 const char *prefix, int prefix_length)
528 /* Read HEAD and run update-index on paths that are
529 * merged and already different between index and HEAD.
531 int pos;
532 int has_head = 1;
533 const char **pathspec = get_pathspec(prefix, av + 1);
535 if (read_ref("HEAD", head_sha1))
536 /* If there is no HEAD, that means it is an initial
537 * commit. Update everything in the index.
539 has_head = 0;
540 redo:
541 for (pos = 0; pos < active_nr; pos++) {
542 struct cache_entry *ce = active_cache[pos];
543 struct cache_entry *old = NULL;
544 int save_nr;
546 if (ce_stage(ce) || !ce_path_match(ce, pathspec))
547 continue;
548 if (has_head)
549 old = read_one_ent(NULL, head_sha1,
550 ce->name, ce_namelen(ce), 0);
551 if (old && ce->ce_mode == old->ce_mode &&
552 !hashcmp(ce->sha1, old->sha1)) {
553 free(old);
554 continue; /* unchanged */
556 /* Be careful. The working tree may not have the
557 * path anymore, in which case, under 'allow_remove',
558 * or worse yet 'allow_replace', active_nr may decrease.
560 save_nr = active_nr;
561 update_one(ce->name + prefix_length, prefix, prefix_length);
562 if (save_nr != active_nr)
563 goto redo;
565 return 0;
568 int cmd_update_index(int argc, const char **argv, const char *prefix)
570 int i, newfd, entries, has_errors = 0, line_termination = '\n';
571 int allow_options = 1;
572 int read_from_stdin = 0;
573 int prefix_length = prefix ? strlen(prefix) : 0;
574 char set_executable_bit = 0;
575 unsigned int refresh_flags = 0;
576 int lock_error = 0;
577 struct lock_file *lock_file;
579 git_config(git_default_config);
581 /* We can't free this memory, it becomes part of a linked list parsed atexit() */
582 lock_file = xcalloc(1, sizeof(struct lock_file));
584 newfd = hold_locked_index(lock_file, 0);
585 if (newfd < 0)
586 lock_error = errno;
588 entries = read_cache();
589 if (entries < 0)
590 die("cache corrupted");
592 for (i = 1 ; i < argc; i++) {
593 const char *path = argv[i];
594 const char *p;
596 if (allow_options && *path == '-') {
597 if (!strcmp(path, "--")) {
598 allow_options = 0;
599 continue;
601 if (!strcmp(path, "-q")) {
602 refresh_flags |= REFRESH_QUIET;
603 continue;
605 if (!strcmp(path, "--add")) {
606 allow_add = 1;
607 continue;
609 if (!strcmp(path, "--replace")) {
610 allow_replace = 1;
611 continue;
613 if (!strcmp(path, "--remove")) {
614 allow_remove = 1;
615 continue;
617 if (!strcmp(path, "--unmerged")) {
618 refresh_flags |= REFRESH_UNMERGED;
619 continue;
621 if (!strcmp(path, "--refresh")) {
622 has_errors |= refresh_cache(refresh_flags);
623 continue;
625 if (!strcmp(path, "--really-refresh")) {
626 has_errors |= refresh_cache(REFRESH_REALLY | refresh_flags);
627 continue;
629 if (!strcmp(path, "--cacheinfo")) {
630 unsigned char sha1[20];
631 unsigned int mode;
633 if (i+3 >= argc)
634 die("git-update-index: --cacheinfo <mode> <sha1> <path>");
636 if (strtoul_ui(argv[i+1], 8, &mode) ||
637 get_sha1_hex(argv[i+2], sha1) ||
638 add_cacheinfo(mode, sha1, argv[i+3], 0))
639 die("git-update-index: --cacheinfo"
640 " cannot add %s", argv[i+3]);
641 i += 3;
642 continue;
644 if (!strcmp(path, "--chmod=-x") ||
645 !strcmp(path, "--chmod=+x")) {
646 if (argc <= i+1)
647 die("git-update-index: %s <path>", path);
648 set_executable_bit = path[8];
649 continue;
651 if (!strcmp(path, "--assume-unchanged")) {
652 mark_valid_only = MARK_VALID;
653 continue;
655 if (!strcmp(path, "--no-assume-unchanged")) {
656 mark_valid_only = UNMARK_VALID;
657 continue;
659 if (!strcmp(path, "--info-only")) {
660 info_only = 1;
661 continue;
663 if (!strcmp(path, "--force-remove")) {
664 force_remove = 1;
665 continue;
667 if (!strcmp(path, "-z")) {
668 line_termination = 0;
669 continue;
671 if (!strcmp(path, "--stdin")) {
672 if (i != argc - 1)
673 die("--stdin must be at the end");
674 read_from_stdin = 1;
675 break;
677 if (!strcmp(path, "--index-info")) {
678 if (i != argc - 1)
679 die("--index-info must be at the end");
680 allow_add = allow_replace = allow_remove = 1;
681 read_index_info(line_termination);
682 break;
684 if (!strcmp(path, "--unresolve")) {
685 has_errors = do_unresolve(argc - i, argv + i,
686 prefix, prefix_length);
687 if (has_errors)
688 active_cache_changed = 0;
689 goto finish;
691 if (!strcmp(path, "--again") || !strcmp(path, "-g")) {
692 has_errors = do_reupdate(argc - i, argv + i,
693 prefix, prefix_length);
694 if (has_errors)
695 active_cache_changed = 0;
696 goto finish;
698 if (!strcmp(path, "--ignore-missing")) {
699 refresh_flags |= REFRESH_IGNORE_MISSING;
700 continue;
702 if (!strcmp(path, "--verbose")) {
703 verbose = 1;
704 continue;
706 if (!strcmp(path, "-h") || !strcmp(path, "--help"))
707 usage(update_index_usage);
708 die("unknown option %s", path);
710 p = prefix_path(prefix, prefix_length, path);
711 update_one(p, NULL, 0);
712 if (set_executable_bit)
713 chmod_path(set_executable_bit, p);
714 if (p < path || p > path + strlen(path))
715 free((char*)p);
717 if (read_from_stdin) {
718 struct strbuf buf;
719 strbuf_init(&buf);
720 while (1) {
721 char *path_name;
722 const char *p;
723 read_line(&buf, stdin, line_termination);
724 if (buf.eof)
725 break;
726 if (line_termination && buf.buf[0] == '"')
727 path_name = unquote_c_style(buf.buf, NULL);
728 else
729 path_name = buf.buf;
730 p = prefix_path(prefix, prefix_length, path_name);
731 update_one(p, NULL, 0);
732 if (set_executable_bit)
733 chmod_path(set_executable_bit, p);
734 if (p < path_name || p > path_name + strlen(path_name))
735 free((char*) p);
736 if (path_name != buf.buf)
737 free(path_name);
741 finish:
742 if (active_cache_changed) {
743 if (newfd < 0) {
744 if (refresh_flags & REFRESH_QUIET)
745 exit(128);
746 die("unable to create '%s.lock': %s",
747 get_index_file(), strerror(lock_error));
749 if (write_cache(newfd, active_cache, active_nr) ||
750 close(newfd) || commit_locked_index(lock_file))
751 die("Unable to write new index file");
754 rollback_lock_file(lock_file);
756 return has_errors ? 1 : 0;