chainlint: colorize problem annotations and test delimiters
[git/debian.git] / builtin / update-index.c
blobb62249905f1b808fe94a86494765b368836c7eec
1 /*
2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
5 */
6 #define USE_THE_INDEX_COMPATIBILITY_MACROS
7 #include "cache.h"
8 #include "bulk-checkin.h"
9 #include "config.h"
10 #include "lockfile.h"
11 #include "quote.h"
12 #include "cache-tree.h"
13 #include "tree-walk.h"
14 #include "builtin.h"
15 #include "refs.h"
16 #include "resolve-undo.h"
17 #include "parse-options.h"
18 #include "pathspec.h"
19 #include "dir.h"
20 #include "split-index.h"
21 #include "fsmonitor.h"
24 * Default to not allowing changes to the list of files. The
25 * tool doesn't actually care, but this makes it harder to add
26 * files to the revision control by mistake by doing something
27 * like "git update-index *" and suddenly having all the object
28 * files be revision controlled.
30 static int allow_add;
31 static int allow_remove;
32 static int allow_replace;
33 static int info_only;
34 static int force_remove;
35 static int verbose;
36 static int mark_valid_only;
37 static int mark_skip_worktree_only;
38 static int mark_fsmonitor_only;
39 static int ignore_skip_worktree_entries;
40 #define MARK_FLAG 1
41 #define UNMARK_FLAG 2
42 static struct strbuf mtime_dir = STRBUF_INIT;
44 /* Untracked cache mode */
45 enum uc_mode {
46 UC_UNSPECIFIED = -1,
47 UC_DISABLE = 0,
48 UC_ENABLE,
49 UC_TEST,
50 UC_FORCE
53 __attribute__((format (printf, 1, 2)))
54 static void report(const char *fmt, ...)
56 va_list vp;
58 if (!verbose)
59 return;
62 * It is possible, though unlikely, that a caller could use the verbose
63 * output to synchronize with addition of objects to the object
64 * database. The current implementation of ODB transactions leaves
65 * objects invisible while a transaction is active, so flush the
66 * transaction here before reporting a change made by update-index.
68 flush_odb_transaction();
69 va_start(vp, fmt);
70 vprintf(fmt, vp);
71 putchar('\n');
72 va_end(vp);
75 static void remove_test_directory(void)
77 if (mtime_dir.len)
78 remove_dir_recursively(&mtime_dir, 0);
81 static const char *get_mtime_path(const char *path)
83 static struct strbuf sb = STRBUF_INIT;
84 strbuf_reset(&sb);
85 strbuf_addf(&sb, "%s/%s", mtime_dir.buf, path);
86 return sb.buf;
89 static void xmkdir(const char *path)
91 path = get_mtime_path(path);
92 if (mkdir(path, 0700))
93 die_errno(_("failed to create directory %s"), path);
96 static int xstat_mtime_dir(struct stat *st)
98 if (stat(mtime_dir.buf, st))
99 die_errno(_("failed to stat %s"), mtime_dir.buf);
100 return 0;
103 static int create_file(const char *path)
105 int fd;
106 path = get_mtime_path(path);
107 fd = xopen(path, O_CREAT | O_RDWR, 0644);
108 return fd;
111 static void xunlink(const char *path)
113 path = get_mtime_path(path);
114 if (unlink(path))
115 die_errno(_("failed to delete file %s"), path);
118 static void xrmdir(const char *path)
120 path = get_mtime_path(path);
121 if (rmdir(path))
122 die_errno(_("failed to delete directory %s"), path);
125 static void avoid_racy(void)
128 * not use if we could usleep(10) if USE_NSEC is defined. The
129 * field nsec could be there, but the OS could choose to
130 * ignore it?
132 sleep(1);
135 static int test_if_untracked_cache_is_supported(void)
137 struct stat st;
138 struct stat_data base;
139 int fd, ret = 0;
140 char *cwd;
142 strbuf_addstr(&mtime_dir, "mtime-test-XXXXXX");
143 if (!mkdtemp(mtime_dir.buf))
144 die_errno("Could not make temporary directory");
146 cwd = xgetcwd();
147 fprintf(stderr, _("Testing mtime in '%s' "), cwd);
148 free(cwd);
150 atexit(remove_test_directory);
151 xstat_mtime_dir(&st);
152 fill_stat_data(&base, &st);
153 fputc('.', stderr);
155 avoid_racy();
156 fd = create_file("newfile");
157 xstat_mtime_dir(&st);
158 if (!match_stat_data(&base, &st)) {
159 close(fd);
160 fputc('\n', stderr);
161 fprintf_ln(stderr,_("directory stat info does not "
162 "change after adding a new file"));
163 goto done;
165 fill_stat_data(&base, &st);
166 fputc('.', stderr);
168 avoid_racy();
169 xmkdir("new-dir");
170 xstat_mtime_dir(&st);
171 if (!match_stat_data(&base, &st)) {
172 close(fd);
173 fputc('\n', stderr);
174 fprintf_ln(stderr, _("directory stat info does not change "
175 "after adding a new directory"));
176 goto done;
178 fill_stat_data(&base, &st);
179 fputc('.', stderr);
181 avoid_racy();
182 write_or_die(fd, "data", 4);
183 close(fd);
184 xstat_mtime_dir(&st);
185 if (match_stat_data(&base, &st)) {
186 fputc('\n', stderr);
187 fprintf_ln(stderr, _("directory stat info changes "
188 "after updating a file"));
189 goto done;
191 fputc('.', stderr);
193 avoid_racy();
194 close(create_file("new-dir/new"));
195 xstat_mtime_dir(&st);
196 if (match_stat_data(&base, &st)) {
197 fputc('\n', stderr);
198 fprintf_ln(stderr, _("directory stat info changes after "
199 "adding a file inside subdirectory"));
200 goto done;
202 fputc('.', stderr);
204 avoid_racy();
205 xunlink("newfile");
206 xstat_mtime_dir(&st);
207 if (!match_stat_data(&base, &st)) {
208 fputc('\n', stderr);
209 fprintf_ln(stderr, _("directory stat info does not "
210 "change after deleting a file"));
211 goto done;
213 fill_stat_data(&base, &st);
214 fputc('.', stderr);
216 avoid_racy();
217 xunlink("new-dir/new");
218 xrmdir("new-dir");
219 xstat_mtime_dir(&st);
220 if (!match_stat_data(&base, &st)) {
221 fputc('\n', stderr);
222 fprintf_ln(stderr, _("directory stat info does not "
223 "change after deleting a directory"));
224 goto done;
227 if (rmdir(mtime_dir.buf))
228 die_errno(_("failed to delete directory %s"), mtime_dir.buf);
229 fprintf_ln(stderr, _(" OK"));
230 ret = 1;
232 done:
233 strbuf_release(&mtime_dir);
234 return ret;
237 static int mark_ce_flags(const char *path, int flag, int mark)
239 int namelen = strlen(path);
240 int pos = cache_name_pos(path, namelen);
241 if (0 <= pos) {
242 mark_fsmonitor_invalid(&the_index, active_cache[pos]);
243 if (mark)
244 active_cache[pos]->ce_flags |= flag;
245 else
246 active_cache[pos]->ce_flags &= ~flag;
247 active_cache[pos]->ce_flags |= CE_UPDATE_IN_BASE;
248 cache_tree_invalidate_path(&the_index, path);
249 active_cache_changed |= CE_ENTRY_CHANGED;
250 return 0;
252 return -1;
255 static int remove_one_path(const char *path)
257 if (!allow_remove)
258 return error("%s: does not exist and --remove not passed", path);
259 if (remove_file_from_cache(path))
260 return error("%s: cannot remove from the index", path);
261 return 0;
265 * Handle a path that couldn't be lstat'ed. It's either:
266 * - missing file (ENOENT or ENOTDIR). That's ok if we're
267 * supposed to be removing it and the removal actually
268 * succeeds.
269 * - permission error. That's never ok.
271 static int process_lstat_error(const char *path, int err)
273 if (is_missing_file_error(err))
274 return remove_one_path(path);
275 return error("lstat(\"%s\"): %s", path, strerror(err));
278 static int add_one_path(const struct cache_entry *old, const char *path, int len, struct stat *st)
280 int option;
281 struct cache_entry *ce;
283 /* Was the old index entry already up-to-date? */
284 if (old && !ce_stage(old) && !ce_match_stat(old, st, 0))
285 return 0;
287 ce = make_empty_cache_entry(&the_index, len);
288 memcpy(ce->name, path, len);
289 ce->ce_flags = create_ce_flags(0);
290 ce->ce_namelen = len;
291 fill_stat_cache_info(&the_index, ce, st);
292 ce->ce_mode = ce_mode_from_stat(old, st->st_mode);
294 if (index_path(&the_index, &ce->oid, path, st,
295 info_only ? 0 : HASH_WRITE_OBJECT)) {
296 discard_cache_entry(ce);
297 return -1;
299 option = allow_add ? ADD_CACHE_OK_TO_ADD : 0;
300 option |= allow_replace ? ADD_CACHE_OK_TO_REPLACE : 0;
301 if (add_cache_entry(ce, option)) {
302 discard_cache_entry(ce);
303 return error("%s: cannot add to the index - missing --add option?", path);
305 return 0;
309 * Handle a path that was a directory. Four cases:
311 * - it's already a gitlink in the index, and we keep it that
312 * way, and update it if we can (if we cannot find the HEAD,
313 * we're going to keep it unchanged in the index!)
315 * - it's a *file* in the index, in which case it should be
316 * removed as a file if removal is allowed, since it doesn't
317 * exist as such any more. If removal isn't allowed, it's
318 * an error.
320 * (NOTE! This is old and arguably fairly strange behaviour.
321 * We might want to make this an error unconditionally, and
322 * use "--force-remove" if you actually want to force removal).
324 * - it used to exist as a subdirectory (ie multiple files with
325 * this particular prefix) in the index, in which case it's wrong
326 * to try to update it as a directory.
328 * - it doesn't exist at all in the index, but it is a valid
329 * git directory, and it should be *added* as a gitlink.
331 static int process_directory(const char *path, int len, struct stat *st)
333 struct object_id oid;
334 int pos = cache_name_pos(path, len);
336 /* Exact match: file or existing gitlink */
337 if (pos >= 0) {
338 const struct cache_entry *ce = active_cache[pos];
339 if (S_ISGITLINK(ce->ce_mode)) {
341 /* Do nothing to the index if there is no HEAD! */
342 if (resolve_gitlink_ref(path, "HEAD", &oid) < 0)
343 return 0;
345 return add_one_path(ce, path, len, st);
347 /* Should this be an unconditional error? */
348 return remove_one_path(path);
351 /* Inexact match: is there perhaps a subdirectory match? */
352 pos = -pos-1;
353 while (pos < active_nr) {
354 const struct cache_entry *ce = active_cache[pos++];
356 if (strncmp(ce->name, path, len))
357 break;
358 if (ce->name[len] > '/')
359 break;
360 if (ce->name[len] < '/')
361 continue;
363 /* Subdirectory match - error out */
364 return error("%s: is a directory - add individual files instead", path);
367 /* No match - should we add it as a gitlink? */
368 if (!resolve_gitlink_ref(path, "HEAD", &oid))
369 return add_one_path(NULL, path, len, st);
371 /* Error out. */
372 return error("%s: is a directory - add files inside instead", path);
375 static int process_path(const char *path, struct stat *st, int stat_errno)
377 int pos, len;
378 const struct cache_entry *ce;
380 len = strlen(path);
381 if (has_symlink_leading_path(path, len))
382 return error("'%s' is beyond a symbolic link", path);
384 pos = cache_name_pos(path, len);
385 ce = pos < 0 ? NULL : active_cache[pos];
386 if (ce && ce_skip_worktree(ce)) {
388 * working directory version is assumed "good"
389 * so updating it does not make sense.
390 * On the other hand, removing it from index should work
392 if (!ignore_skip_worktree_entries && allow_remove &&
393 remove_file_from_cache(path))
394 return error("%s: cannot remove from the index", path);
395 return 0;
399 * First things first: get the stat information, to decide
400 * what to do about the pathname!
402 if (stat_errno)
403 return process_lstat_error(path, stat_errno);
405 if (S_ISDIR(st->st_mode))
406 return process_directory(path, len, st);
408 return add_one_path(ce, path, len, st);
411 static int add_cacheinfo(unsigned int mode, const struct object_id *oid,
412 const char *path, int stage)
414 int len, option;
415 struct cache_entry *ce;
417 if (!verify_path(path, mode))
418 return error("Invalid path '%s'", path);
420 len = strlen(path);
421 ce = make_empty_cache_entry(&the_index, len);
423 oidcpy(&ce->oid, oid);
424 memcpy(ce->name, path, len);
425 ce->ce_flags = create_ce_flags(stage);
426 ce->ce_namelen = len;
427 ce->ce_mode = create_ce_mode(mode);
428 if (assume_unchanged)
429 ce->ce_flags |= CE_VALID;
430 option = allow_add ? ADD_CACHE_OK_TO_ADD : 0;
431 option |= allow_replace ? ADD_CACHE_OK_TO_REPLACE : 0;
432 if (add_cache_entry(ce, option))
433 return error("%s: cannot add to the index - missing --add option?",
434 path);
435 report("add '%s'", path);
436 return 0;
439 static void chmod_path(char flip, const char *path)
441 int pos;
442 struct cache_entry *ce;
444 pos = cache_name_pos(path, strlen(path));
445 if (pos < 0)
446 goto fail;
447 ce = active_cache[pos];
448 if (chmod_cache_entry(ce, flip) < 0)
449 goto fail;
451 report("chmod %cx '%s'", flip, path);
452 return;
453 fail:
454 die("git update-index: cannot chmod %cx '%s'", flip, path);
457 static void update_one(const char *path)
459 int stat_errno = 0;
460 struct stat st;
462 if (mark_valid_only || mark_skip_worktree_only || force_remove ||
463 mark_fsmonitor_only)
464 st.st_mode = 0;
465 else if (lstat(path, &st) < 0) {
466 st.st_mode = 0;
467 stat_errno = errno;
468 } /* else stat is valid */
470 if (!verify_path(path, st.st_mode)) {
471 fprintf(stderr, "Ignoring path %s\n", path);
472 return;
474 if (mark_valid_only) {
475 if (mark_ce_flags(path, CE_VALID, mark_valid_only == MARK_FLAG))
476 die("Unable to mark file %s", path);
477 return;
479 if (mark_skip_worktree_only) {
480 if (mark_ce_flags(path, CE_SKIP_WORKTREE, mark_skip_worktree_only == MARK_FLAG))
481 die("Unable to mark file %s", path);
482 return;
484 if (mark_fsmonitor_only) {
485 if (mark_ce_flags(path, CE_FSMONITOR_VALID, mark_fsmonitor_only == MARK_FLAG))
486 die("Unable to mark file %s", path);
487 return;
490 if (force_remove) {
491 if (remove_file_from_cache(path))
492 die("git update-index: unable to remove %s", path);
493 report("remove '%s'", path);
494 return;
496 if (process_path(path, &st, stat_errno))
497 die("Unable to process path %s", path);
498 report("add '%s'", path);
501 static void read_index_info(int nul_term_line)
503 const int hexsz = the_hash_algo->hexsz;
504 struct strbuf buf = STRBUF_INIT;
505 struct strbuf uq = STRBUF_INIT;
506 strbuf_getline_fn getline_fn;
508 getline_fn = nul_term_line ? strbuf_getline_nul : strbuf_getline_lf;
509 while (getline_fn(&buf, stdin) != EOF) {
510 char *ptr, *tab;
511 char *path_name;
512 struct object_id oid;
513 unsigned int mode;
514 unsigned long ul;
515 int stage;
517 /* This reads lines formatted in one of three formats:
519 * (1) mode SP sha1 TAB path
520 * The first format is what "git apply --index-info"
521 * reports, and used to reconstruct a partial tree
522 * that is used for phony merge base tree when falling
523 * back on 3-way merge.
525 * (2) mode SP type SP sha1 TAB path
526 * The second format is to stuff "git ls-tree" output
527 * into the index file.
529 * (3) mode SP sha1 SP stage TAB path
530 * This format is to put higher order stages into the
531 * index file and matches "git ls-files --stage" output.
533 errno = 0;
534 ul = strtoul(buf.buf, &ptr, 8);
535 if (ptr == buf.buf || *ptr != ' '
536 || errno || (unsigned int) ul != ul)
537 goto bad_line;
538 mode = ul;
540 tab = strchr(ptr, '\t');
541 if (!tab || tab - ptr < hexsz + 1)
542 goto bad_line;
544 if (tab[-2] == ' ' && '0' <= tab[-1] && tab[-1] <= '3') {
545 stage = tab[-1] - '0';
546 ptr = tab + 1; /* point at the head of path */
547 tab = tab - 2; /* point at tail of sha1 */
549 else {
550 stage = 0;
551 ptr = tab + 1; /* point at the head of path */
554 if (get_oid_hex(tab - hexsz, &oid) ||
555 tab[-(hexsz + 1)] != ' ')
556 goto bad_line;
558 path_name = ptr;
559 if (!nul_term_line && path_name[0] == '"') {
560 strbuf_reset(&uq);
561 if (unquote_c_style(&uq, path_name, NULL)) {
562 die("git update-index: bad quoting of path name");
564 path_name = uq.buf;
567 if (!verify_path(path_name, mode)) {
568 fprintf(stderr, "Ignoring path %s\n", path_name);
569 continue;
572 if (!mode) {
573 /* mode == 0 means there is no such path -- remove */
574 if (remove_file_from_cache(path_name))
575 die("git update-index: unable to remove %s",
576 ptr);
578 else {
579 /* mode ' ' sha1 '\t' name
580 * ptr[-1] points at tab,
581 * ptr[-41] is at the beginning of sha1
583 ptr[-(hexsz + 2)] = ptr[-1] = 0;
584 if (add_cacheinfo(mode, &oid, path_name, stage))
585 die("git update-index: unable to update %s",
586 path_name);
588 continue;
590 bad_line:
591 die("malformed index info %s", buf.buf);
593 strbuf_release(&buf);
594 strbuf_release(&uq);
597 static const char * const update_index_usage[] = {
598 N_("git update-index [<options>] [--] [<file>...]"),
599 NULL
602 static struct object_id head_oid;
603 static struct object_id merge_head_oid;
605 static struct cache_entry *read_one_ent(const char *which,
606 struct object_id *ent, const char *path,
607 int namelen, int stage)
609 unsigned short mode;
610 struct object_id oid;
611 struct cache_entry *ce;
613 if (get_tree_entry(the_repository, ent, path, &oid, &mode)) {
614 if (which)
615 error("%s: not in %s branch.", path, which);
616 return NULL;
618 if (!the_index.sparse_index && mode == S_IFDIR) {
619 if (which)
620 error("%s: not a blob in %s branch.", path, which);
621 return NULL;
623 ce = make_empty_cache_entry(&the_index, namelen);
625 oidcpy(&ce->oid, &oid);
626 memcpy(ce->name, path, namelen);
627 ce->ce_flags = create_ce_flags(stage);
628 ce->ce_namelen = namelen;
629 ce->ce_mode = create_ce_mode(mode);
630 return ce;
633 static int unresolve_one(const char *path)
635 int namelen = strlen(path);
636 int pos;
637 int ret = 0;
638 struct cache_entry *ce_2 = NULL, *ce_3 = NULL;
640 /* See if there is such entry in the index. */
641 pos = cache_name_pos(path, namelen);
642 if (0 <= pos) {
643 /* already merged */
644 pos = unmerge_cache_entry_at(pos);
645 if (pos < active_nr) {
646 const struct cache_entry *ce = active_cache[pos];
647 if (ce_stage(ce) &&
648 ce_namelen(ce) == namelen &&
649 !memcmp(ce->name, path, namelen))
650 return 0;
652 /* no resolve-undo information; fall back */
653 } else {
654 /* If there isn't, either it is unmerged, or
655 * resolved as "removed" by mistake. We do not
656 * want to do anything in the former case.
658 pos = -pos-1;
659 if (pos < active_nr) {
660 const struct cache_entry *ce = active_cache[pos];
661 if (ce_namelen(ce) == namelen &&
662 !memcmp(ce->name, path, namelen)) {
663 fprintf(stderr,
664 "%s: skipping still unmerged path.\n",
665 path);
666 goto free_return;
671 /* Grab blobs from given path from HEAD and MERGE_HEAD,
672 * stuff HEAD version in stage #2,
673 * stuff MERGE_HEAD version in stage #3.
675 ce_2 = read_one_ent("our", &head_oid, path, namelen, 2);
676 ce_3 = read_one_ent("their", &merge_head_oid, path, namelen, 3);
678 if (!ce_2 || !ce_3) {
679 ret = -1;
680 goto free_return;
682 if (oideq(&ce_2->oid, &ce_3->oid) &&
683 ce_2->ce_mode == ce_3->ce_mode) {
684 fprintf(stderr, "%s: identical in both, skipping.\n",
685 path);
686 goto free_return;
689 remove_file_from_cache(path);
690 if (add_cache_entry(ce_2, ADD_CACHE_OK_TO_ADD)) {
691 error("%s: cannot add our version to the index.", path);
692 ret = -1;
693 goto free_return;
695 if (!add_cache_entry(ce_3, ADD_CACHE_OK_TO_ADD))
696 return 0;
697 error("%s: cannot add their version to the index.", path);
698 ret = -1;
699 free_return:
700 discard_cache_entry(ce_2);
701 discard_cache_entry(ce_3);
702 return ret;
705 static void read_head_pointers(void)
707 if (read_ref("HEAD", &head_oid))
708 die("No HEAD -- no initial commit yet?");
709 if (read_ref("MERGE_HEAD", &merge_head_oid)) {
710 fprintf(stderr, "Not in the middle of a merge.\n");
711 exit(0);
715 static int do_unresolve(int ac, const char **av,
716 const char *prefix, int prefix_length)
718 int i;
719 int err = 0;
721 /* Read HEAD and MERGE_HEAD; if MERGE_HEAD does not exist, we
722 * are not doing a merge, so exit with success status.
724 read_head_pointers();
726 for (i = 1; i < ac; i++) {
727 const char *arg = av[i];
728 char *p = prefix_path(prefix, prefix_length, arg);
729 err |= unresolve_one(p);
730 free(p);
732 return err;
735 static int do_reupdate(int ac, const char **av,
736 const char *prefix)
738 /* Read HEAD and run update-index on paths that are
739 * merged and already different between index and HEAD.
741 int pos;
742 int has_head = 1;
743 struct pathspec pathspec;
745 parse_pathspec(&pathspec, 0,
746 PATHSPEC_PREFER_CWD,
747 prefix, av + 1);
749 if (read_ref("HEAD", &head_oid))
750 /* If there is no HEAD, that means it is an initial
751 * commit. Update everything in the index.
753 has_head = 0;
754 redo:
755 for (pos = 0; pos < active_nr; pos++) {
756 const struct cache_entry *ce = active_cache[pos];
757 struct cache_entry *old = NULL;
758 int save_nr;
759 char *path;
761 if (ce_stage(ce) || !ce_path_match(&the_index, ce, &pathspec, NULL))
762 continue;
763 if (has_head)
764 old = read_one_ent(NULL, &head_oid,
765 ce->name, ce_namelen(ce), 0);
766 if (old && ce->ce_mode == old->ce_mode &&
767 oideq(&ce->oid, &old->oid)) {
768 discard_cache_entry(old);
769 continue; /* unchanged */
772 /* At this point, we know the contents of the sparse directory are
773 * modified with respect to HEAD, so we expand the index and restart
774 * to process each path individually
776 if (S_ISSPARSEDIR(ce->ce_mode)) {
777 ensure_full_index(&the_index);
778 goto redo;
781 /* Be careful. The working tree may not have the
782 * path anymore, in which case, under 'allow_remove',
783 * or worse yet 'allow_replace', active_nr may decrease.
785 save_nr = active_nr;
786 path = xstrdup(ce->name);
787 update_one(path);
788 free(path);
789 discard_cache_entry(old);
790 if (save_nr != active_nr)
791 goto redo;
793 clear_pathspec(&pathspec);
794 return 0;
797 struct refresh_params {
798 unsigned int flags;
799 int *has_errors;
802 static int refresh(struct refresh_params *o, unsigned int flag)
804 setup_work_tree();
805 read_cache();
806 *o->has_errors |= refresh_cache(o->flags | flag);
807 if (has_racy_timestamp(&the_index)) {
809 * Even if nothing else has changed, updating the file
810 * increases the chance that racy timestamps become
811 * non-racy, helping future run-time performance.
812 * We do that even in case of "errors" returned by
813 * refresh_cache() as these are no actual errors.
814 * cmd_status() does the same.
816 active_cache_changed |= SOMETHING_CHANGED;
818 return 0;
821 static int refresh_callback(const struct option *opt,
822 const char *arg, int unset)
824 BUG_ON_OPT_NEG(unset);
825 BUG_ON_OPT_ARG(arg);
826 return refresh(opt->value, 0);
829 static int really_refresh_callback(const struct option *opt,
830 const char *arg, int unset)
832 BUG_ON_OPT_NEG(unset);
833 BUG_ON_OPT_ARG(arg);
834 return refresh(opt->value, REFRESH_REALLY);
837 static int chmod_callback(const struct option *opt,
838 const char *arg, int unset)
840 char *flip = opt->value;
841 BUG_ON_OPT_NEG(unset);
842 if ((arg[0] != '-' && arg[0] != '+') || arg[1] != 'x' || arg[2])
843 return error("option 'chmod' expects \"+x\" or \"-x\"");
844 *flip = arg[0];
845 return 0;
848 static int resolve_undo_clear_callback(const struct option *opt,
849 const char *arg, int unset)
851 BUG_ON_OPT_NEG(unset);
852 BUG_ON_OPT_ARG(arg);
853 resolve_undo_clear();
854 return 0;
857 static int parse_new_style_cacheinfo(const char *arg,
858 unsigned int *mode,
859 struct object_id *oid,
860 const char **path)
862 unsigned long ul;
863 char *endp;
864 const char *p;
866 if (!arg)
867 return -1;
869 errno = 0;
870 ul = strtoul(arg, &endp, 8);
871 if (errno || endp == arg || *endp != ',' || (unsigned int) ul != ul)
872 return -1; /* not a new-style cacheinfo */
873 *mode = ul;
874 endp++;
875 if (parse_oid_hex(endp, oid, &p) || *p != ',')
876 return -1;
877 *path = p + 1;
878 return 0;
881 static enum parse_opt_result cacheinfo_callback(
882 struct parse_opt_ctx_t *ctx, const struct option *opt,
883 const char *arg, int unset)
885 struct object_id oid;
886 unsigned int mode;
887 const char *path;
889 BUG_ON_OPT_NEG(unset);
890 BUG_ON_OPT_ARG(arg);
892 if (!parse_new_style_cacheinfo(ctx->argv[1], &mode, &oid, &path)) {
893 if (add_cacheinfo(mode, &oid, path, 0))
894 die("git update-index: --cacheinfo cannot add %s", path);
895 ctx->argv++;
896 ctx->argc--;
897 return 0;
899 if (ctx->argc <= 3)
900 return error("option 'cacheinfo' expects <mode>,<sha1>,<path>");
901 if (strtoul_ui(*++ctx->argv, 8, &mode) ||
902 get_oid_hex(*++ctx->argv, &oid) ||
903 add_cacheinfo(mode, &oid, *++ctx->argv, 0))
904 die("git update-index: --cacheinfo cannot add %s", *ctx->argv);
905 ctx->argc -= 3;
906 return 0;
909 static enum parse_opt_result stdin_cacheinfo_callback(
910 struct parse_opt_ctx_t *ctx, const struct option *opt,
911 const char *arg, int unset)
913 int *nul_term_line = opt->value;
915 BUG_ON_OPT_NEG(unset);
916 BUG_ON_OPT_ARG(arg);
918 if (ctx->argc != 1)
919 return error("option '%s' must be the last argument", opt->long_name);
920 allow_add = allow_replace = allow_remove = 1;
921 read_index_info(*nul_term_line);
922 return 0;
925 static enum parse_opt_result stdin_callback(
926 struct parse_opt_ctx_t *ctx, const struct option *opt,
927 const char *arg, int unset)
929 int *read_from_stdin = opt->value;
931 BUG_ON_OPT_NEG(unset);
932 BUG_ON_OPT_ARG(arg);
934 if (ctx->argc != 1)
935 return error("option '%s' must be the last argument", opt->long_name);
936 *read_from_stdin = 1;
937 return 0;
940 static enum parse_opt_result unresolve_callback(
941 struct parse_opt_ctx_t *ctx, const struct option *opt,
942 const char *arg, int unset)
944 int *has_errors = opt->value;
945 const char *prefix = startup_info->prefix;
947 BUG_ON_OPT_NEG(unset);
948 BUG_ON_OPT_ARG(arg);
950 /* consume remaining arguments. */
951 *has_errors = do_unresolve(ctx->argc, ctx->argv,
952 prefix, prefix ? strlen(prefix) : 0);
953 if (*has_errors)
954 active_cache_changed = 0;
956 ctx->argv += ctx->argc - 1;
957 ctx->argc = 1;
958 return 0;
961 static enum parse_opt_result reupdate_callback(
962 struct parse_opt_ctx_t *ctx, const struct option *opt,
963 const char *arg, int unset)
965 int *has_errors = opt->value;
966 const char *prefix = startup_info->prefix;
968 BUG_ON_OPT_NEG(unset);
969 BUG_ON_OPT_ARG(arg);
971 /* consume remaining arguments. */
972 setup_work_tree();
973 *has_errors = do_reupdate(ctx->argc, ctx->argv, prefix);
974 if (*has_errors)
975 active_cache_changed = 0;
977 ctx->argv += ctx->argc - 1;
978 ctx->argc = 1;
979 return 0;
982 int cmd_update_index(int argc, const char **argv, const char *prefix)
984 int newfd, entries, has_errors = 0, nul_term_line = 0;
985 enum uc_mode untracked_cache = UC_UNSPECIFIED;
986 int read_from_stdin = 0;
987 int prefix_length = prefix ? strlen(prefix) : 0;
988 int preferred_index_format = 0;
989 char set_executable_bit = 0;
990 struct refresh_params refresh_args = {0, &has_errors};
991 int lock_error = 0;
992 int split_index = -1;
993 int force_write = 0;
994 int fsmonitor = -1;
995 struct lock_file lock_file = LOCK_INIT;
996 struct parse_opt_ctx_t ctx;
997 strbuf_getline_fn getline_fn;
998 int parseopt_state = PARSE_OPT_UNKNOWN;
999 struct repository *r = the_repository;
1000 struct option options[] = {
1001 OPT_BIT('q', NULL, &refresh_args.flags,
1002 N_("continue refresh even when index needs update"),
1003 REFRESH_QUIET),
1004 OPT_BIT(0, "ignore-submodules", &refresh_args.flags,
1005 N_("refresh: ignore submodules"),
1006 REFRESH_IGNORE_SUBMODULES),
1007 OPT_SET_INT(0, "add", &allow_add,
1008 N_("do not ignore new files"), 1),
1009 OPT_SET_INT(0, "replace", &allow_replace,
1010 N_("let files replace directories and vice-versa"), 1),
1011 OPT_SET_INT(0, "remove", &allow_remove,
1012 N_("notice files missing from worktree"), 1),
1013 OPT_BIT(0, "unmerged", &refresh_args.flags,
1014 N_("refresh even if index contains unmerged entries"),
1015 REFRESH_UNMERGED),
1016 OPT_CALLBACK_F(0, "refresh", &refresh_args, NULL,
1017 N_("refresh stat information"),
1018 PARSE_OPT_NOARG | PARSE_OPT_NONEG,
1019 refresh_callback),
1020 OPT_CALLBACK_F(0, "really-refresh", &refresh_args, NULL,
1021 N_("like --refresh, but ignore assume-unchanged setting"),
1022 PARSE_OPT_NOARG | PARSE_OPT_NONEG,
1023 really_refresh_callback),
1024 {OPTION_LOWLEVEL_CALLBACK, 0, "cacheinfo", NULL,
1025 N_("<mode>,<object>,<path>"),
1026 N_("add the specified entry to the index"),
1027 PARSE_OPT_NOARG | /* disallow --cacheinfo=<mode> form */
1028 PARSE_OPT_NONEG | PARSE_OPT_LITERAL_ARGHELP,
1029 NULL, 0,
1030 cacheinfo_callback},
1031 OPT_CALLBACK_F(0, "chmod", &set_executable_bit, "(+|-)x",
1032 N_("override the executable bit of the listed files"),
1033 PARSE_OPT_NONEG,
1034 chmod_callback),
1035 {OPTION_SET_INT, 0, "assume-unchanged", &mark_valid_only, NULL,
1036 N_("mark files as \"not changing\""),
1037 PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL, MARK_FLAG},
1038 {OPTION_SET_INT, 0, "no-assume-unchanged", &mark_valid_only, NULL,
1039 N_("clear assumed-unchanged bit"),
1040 PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL, UNMARK_FLAG},
1041 {OPTION_SET_INT, 0, "skip-worktree", &mark_skip_worktree_only, NULL,
1042 N_("mark files as \"index-only\""),
1043 PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL, MARK_FLAG},
1044 {OPTION_SET_INT, 0, "no-skip-worktree", &mark_skip_worktree_only, NULL,
1045 N_("clear skip-worktree bit"),
1046 PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL, UNMARK_FLAG},
1047 OPT_BOOL(0, "ignore-skip-worktree-entries", &ignore_skip_worktree_entries,
1048 N_("do not touch index-only entries")),
1049 OPT_SET_INT(0, "info-only", &info_only,
1050 N_("add to index only; do not add content to object database"), 1),
1051 OPT_SET_INT(0, "force-remove", &force_remove,
1052 N_("remove named paths even if present in worktree"), 1),
1053 OPT_BOOL('z', NULL, &nul_term_line,
1054 N_("with --stdin: input lines are terminated by null bytes")),
1055 {OPTION_LOWLEVEL_CALLBACK, 0, "stdin", &read_from_stdin, NULL,
1056 N_("read list of paths to be updated from standard input"),
1057 PARSE_OPT_NONEG | PARSE_OPT_NOARG,
1058 NULL, 0, stdin_callback},
1059 {OPTION_LOWLEVEL_CALLBACK, 0, "index-info", &nul_term_line, NULL,
1060 N_("add entries from standard input to the index"),
1061 PARSE_OPT_NONEG | PARSE_OPT_NOARG,
1062 NULL, 0, stdin_cacheinfo_callback},
1063 {OPTION_LOWLEVEL_CALLBACK, 0, "unresolve", &has_errors, NULL,
1064 N_("repopulate stages #2 and #3 for the listed paths"),
1065 PARSE_OPT_NONEG | PARSE_OPT_NOARG,
1066 NULL, 0, unresolve_callback},
1067 {OPTION_LOWLEVEL_CALLBACK, 'g', "again", &has_errors, NULL,
1068 N_("only update entries that differ from HEAD"),
1069 PARSE_OPT_NONEG | PARSE_OPT_NOARG,
1070 NULL, 0, reupdate_callback},
1071 OPT_BIT(0, "ignore-missing", &refresh_args.flags,
1072 N_("ignore files missing from worktree"),
1073 REFRESH_IGNORE_MISSING),
1074 OPT_SET_INT(0, "verbose", &verbose,
1075 N_("report actions to standard output"), 1),
1076 OPT_CALLBACK_F(0, "clear-resolve-undo", NULL, NULL,
1077 N_("(for porcelains) forget saved unresolved conflicts"),
1078 PARSE_OPT_NOARG | PARSE_OPT_NONEG,
1079 resolve_undo_clear_callback),
1080 OPT_INTEGER(0, "index-version", &preferred_index_format,
1081 N_("write index in this format")),
1082 OPT_BOOL(0, "split-index", &split_index,
1083 N_("enable or disable split index")),
1084 OPT_BOOL(0, "untracked-cache", &untracked_cache,
1085 N_("enable/disable untracked cache")),
1086 OPT_SET_INT(0, "test-untracked-cache", &untracked_cache,
1087 N_("test if the filesystem supports untracked cache"), UC_TEST),
1088 OPT_SET_INT(0, "force-untracked-cache", &untracked_cache,
1089 N_("enable untracked cache without testing the filesystem"), UC_FORCE),
1090 OPT_SET_INT(0, "force-write-index", &force_write,
1091 N_("write out the index even if is not flagged as changed"), 1),
1092 OPT_BOOL(0, "fsmonitor", &fsmonitor,
1093 N_("enable or disable file system monitor")),
1094 {OPTION_SET_INT, 0, "fsmonitor-valid", &mark_fsmonitor_only, NULL,
1095 N_("mark files as fsmonitor valid"),
1096 PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL, MARK_FLAG},
1097 {OPTION_SET_INT, 0, "no-fsmonitor-valid", &mark_fsmonitor_only, NULL,
1098 N_("clear fsmonitor valid bit"),
1099 PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL, UNMARK_FLAG},
1100 OPT_END()
1103 if (argc == 2 && !strcmp(argv[1], "-h"))
1104 usage_with_options(update_index_usage, options);
1106 git_config(git_default_config, NULL);
1108 prepare_repo_settings(r);
1109 the_repository->settings.command_requires_full_index = 0;
1111 /* we will diagnose later if it turns out that we need to update it */
1112 newfd = hold_locked_index(&lock_file, 0);
1113 if (newfd < 0)
1114 lock_error = errno;
1116 entries = read_cache();
1117 if (entries < 0)
1118 die("cache corrupted");
1120 the_index.updated_skipworktree = 1;
1123 * Custom copy of parse_options() because we want to handle
1124 * filename arguments as they come.
1126 parse_options_start(&ctx, argc, argv, prefix,
1127 options, PARSE_OPT_STOP_AT_NON_OPTION);
1130 * Allow the object layer to optimize adding multiple objects in
1131 * a batch.
1133 begin_odb_transaction();
1134 while (ctx.argc) {
1135 if (parseopt_state != PARSE_OPT_DONE)
1136 parseopt_state = parse_options_step(&ctx, options,
1137 update_index_usage);
1138 if (!ctx.argc)
1139 break;
1140 switch (parseopt_state) {
1141 case PARSE_OPT_HELP:
1142 case PARSE_OPT_ERROR:
1143 exit(129);
1144 case PARSE_OPT_COMPLETE:
1145 exit(0);
1146 case PARSE_OPT_NON_OPTION:
1147 case PARSE_OPT_DONE:
1149 const char *path = ctx.argv[0];
1150 char *p;
1152 setup_work_tree();
1153 p = prefix_path(prefix, prefix_length, path);
1154 update_one(p);
1155 if (set_executable_bit)
1156 chmod_path(set_executable_bit, p);
1157 free(p);
1158 ctx.argc--;
1159 ctx.argv++;
1160 break;
1162 case PARSE_OPT_UNKNOWN:
1163 if (ctx.argv[0][1] == '-')
1164 error("unknown option '%s'", ctx.argv[0] + 2);
1165 else
1166 error("unknown switch '%c'", *ctx.opt);
1167 usage_with_options(update_index_usage, options);
1170 argc = parse_options_end(&ctx);
1172 getline_fn = nul_term_line ? strbuf_getline_nul : strbuf_getline_lf;
1173 if (preferred_index_format) {
1174 if (preferred_index_format < INDEX_FORMAT_LB ||
1175 INDEX_FORMAT_UB < preferred_index_format)
1176 die("index-version %d not in range: %d..%d",
1177 preferred_index_format,
1178 INDEX_FORMAT_LB, INDEX_FORMAT_UB);
1180 if (the_index.version != preferred_index_format)
1181 active_cache_changed |= SOMETHING_CHANGED;
1182 the_index.version = preferred_index_format;
1185 if (read_from_stdin) {
1186 struct strbuf buf = STRBUF_INIT;
1187 struct strbuf unquoted = STRBUF_INIT;
1189 setup_work_tree();
1190 while (getline_fn(&buf, stdin) != EOF) {
1191 char *p;
1192 if (!nul_term_line && buf.buf[0] == '"') {
1193 strbuf_reset(&unquoted);
1194 if (unquote_c_style(&unquoted, buf.buf, NULL))
1195 die("line is badly quoted");
1196 strbuf_swap(&buf, &unquoted);
1198 p = prefix_path(prefix, prefix_length, buf.buf);
1199 update_one(p);
1200 if (set_executable_bit)
1201 chmod_path(set_executable_bit, p);
1202 free(p);
1204 strbuf_release(&unquoted);
1205 strbuf_release(&buf);
1209 * By now we have added all of the new objects
1211 end_odb_transaction();
1213 if (split_index > 0) {
1214 if (git_config_get_split_index() == 0)
1215 warning(_("core.splitIndex is set to false; "
1216 "remove or change it, if you really want to "
1217 "enable split index"));
1218 if (the_index.split_index)
1219 the_index.cache_changed |= SPLIT_INDEX_ORDERED;
1220 else
1221 add_split_index(&the_index);
1222 } else if (!split_index) {
1223 if (git_config_get_split_index() == 1)
1224 warning(_("core.splitIndex is set to true; "
1225 "remove or change it, if you really want to "
1226 "disable split index"));
1227 remove_split_index(&the_index);
1230 prepare_repo_settings(r);
1231 switch (untracked_cache) {
1232 case UC_UNSPECIFIED:
1233 break;
1234 case UC_DISABLE:
1235 if (r->settings.core_untracked_cache == UNTRACKED_CACHE_WRITE)
1236 warning(_("core.untrackedCache is set to true; "
1237 "remove or change it, if you really want to "
1238 "disable the untracked cache"));
1239 remove_untracked_cache(&the_index);
1240 report(_("Untracked cache disabled"));
1241 break;
1242 case UC_TEST:
1243 setup_work_tree();
1244 return !test_if_untracked_cache_is_supported();
1245 case UC_ENABLE:
1246 case UC_FORCE:
1247 if (r->settings.core_untracked_cache == UNTRACKED_CACHE_REMOVE)
1248 warning(_("core.untrackedCache is set to false; "
1249 "remove or change it, if you really want to "
1250 "enable the untracked cache"));
1251 add_untracked_cache(&the_index);
1252 report(_("Untracked cache enabled for '%s'"), get_git_work_tree());
1253 break;
1254 default:
1255 BUG("bad untracked_cache value: %d", untracked_cache);
1258 if (fsmonitor > 0) {
1259 enum fsmonitor_mode fsm_mode = fsm_settings__get_mode(r);
1260 enum fsmonitor_reason reason = fsm_settings__get_reason(r);
1263 * The user wants to turn on FSMonitor using the command
1264 * line argument. (We don't know (or care) whether that
1265 * is the IPC or HOOK version.)
1267 * Use one of the __get routines to force load the FSMonitor
1268 * config settings into the repo-settings. That will detect
1269 * whether the file system is compatible so that we can stop
1270 * here with a nice error message.
1272 if (reason > FSMONITOR_REASON_OK)
1273 die("%s",
1274 fsm_settings__get_incompatible_msg(r, reason));
1276 if (fsm_mode == FSMONITOR_MODE_DISABLED) {
1277 warning(_("core.fsmonitor is unset; "
1278 "set it if you really want to "
1279 "enable fsmonitor"));
1281 add_fsmonitor(&the_index);
1282 report(_("fsmonitor enabled"));
1283 } else if (!fsmonitor) {
1284 enum fsmonitor_mode fsm_mode = fsm_settings__get_mode(r);
1285 if (fsm_mode > FSMONITOR_MODE_DISABLED)
1286 warning(_("core.fsmonitor is set; "
1287 "remove it if you really want to "
1288 "disable fsmonitor"));
1289 remove_fsmonitor(&the_index);
1290 report(_("fsmonitor disabled"));
1293 if (active_cache_changed || force_write) {
1294 if (newfd < 0) {
1295 if (refresh_args.flags & REFRESH_QUIET)
1296 exit(128);
1297 unable_to_lock_die(get_index_file(), lock_error);
1299 if (write_locked_index(&the_index, &lock_file, COMMIT_LOCK))
1300 die("Unable to write new index file");
1303 rollback_lock_file(&lock_file);
1305 return has_errors ? 1 : 0;