git-merge-tree.txt: replace spurious HTML entity
[git.git] / builtin / update-index.c
blob82d5902cc8b15bafaeb09813ae99a45c54800b20
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 = index_name_pos(&the_index, path, namelen);
241 if (0 <= pos) {
242 mark_fsmonitor_invalid(&the_index, the_index.cache[pos]);
243 if (mark)
244 the_index.cache[pos]->ce_flags |= flag;
245 else
246 the_index.cache[pos]->ce_flags &= ~flag;
247 the_index.cache[pos]->ce_flags |= CE_UPDATE_IN_BASE;
248 cache_tree_invalidate_path(&the_index, path);
249 the_index.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_index(&the_index, 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) && !ie_match_stat(&the_index, 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_index_entry(&the_index, 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 = index_name_pos(&the_index, path, len);
336 /* Exact match: file or existing gitlink */
337 if (pos >= 0) {
338 const struct cache_entry *ce = the_index.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 < the_index.cache_nr) {
354 const struct cache_entry *ce = the_index.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 : the_index.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_index(&the_index, 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_index_entry(&the_index, 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 = index_name_pos(&the_index, path, strlen(path));
445 if (pos < 0)
446 goto fail;
447 ce = the_index.cache[pos];
448 if (chmod_index_entry(&the_index, 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_index(&the_index, 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_index(&the_index, 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 = index_name_pos(&the_index, path, namelen);
642 if (0 <= pos) {
643 /* already merged */
644 pos = unmerge_index_entry_at(&the_index, pos);
645 if (pos < the_index.cache_nr) {
646 const struct cache_entry *ce = the_index.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 < the_index.cache_nr) {
660 const struct cache_entry *ce = the_index.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_index(&the_index, path);
690 if (add_index_entry(&the_index, 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_index_entry(&the_index, 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(const char **paths,
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, paths);
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 < the_index.cache_nr; pos++) {
756 const struct cache_entry *ce = the_index.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 = the_index.cache_nr;
786 path = xstrdup(ce->name);
787 update_one(path);
788 free(path);
789 discard_cache_entry(old);
790 if (save_nr != the_index.cache_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 repo_read_index(the_repository);
806 *o->has_errors |= refresh_index(&the_index, o->flags | flag, NULL,
807 NULL, NULL);
808 if (has_racy_timestamp(&the_index)) {
810 * Even if nothing else has changed, updating the file
811 * increases the chance that racy timestamps become
812 * non-racy, helping future run-time performance.
813 * We do that even in case of "errors" returned by
814 * refresh_index() as these are no actual errors.
815 * cmd_status() does the same.
817 the_index.cache_changed |= SOMETHING_CHANGED;
819 return 0;
822 static int refresh_callback(const struct option *opt,
823 const char *arg, int unset)
825 BUG_ON_OPT_NEG(unset);
826 BUG_ON_OPT_ARG(arg);
827 return refresh(opt->value, 0);
830 static int really_refresh_callback(const struct option *opt,
831 const char *arg, int unset)
833 BUG_ON_OPT_NEG(unset);
834 BUG_ON_OPT_ARG(arg);
835 return refresh(opt->value, REFRESH_REALLY);
838 static int chmod_callback(const struct option *opt,
839 const char *arg, int unset)
841 char *flip = opt->value;
842 BUG_ON_OPT_NEG(unset);
843 if ((arg[0] != '-' && arg[0] != '+') || arg[1] != 'x' || arg[2])
844 return error("option 'chmod' expects \"+x\" or \"-x\"");
845 *flip = arg[0];
846 return 0;
849 static int resolve_undo_clear_callback(const struct option *opt,
850 const char *arg, int unset)
852 BUG_ON_OPT_NEG(unset);
853 BUG_ON_OPT_ARG(arg);
854 resolve_undo_clear_index(&the_index);
855 return 0;
858 static int parse_new_style_cacheinfo(const char *arg,
859 unsigned int *mode,
860 struct object_id *oid,
861 const char **path)
863 unsigned long ul;
864 char *endp;
865 const char *p;
867 if (!arg)
868 return -1;
870 errno = 0;
871 ul = strtoul(arg, &endp, 8);
872 if (errno || endp == arg || *endp != ',' || (unsigned int) ul != ul)
873 return -1; /* not a new-style cacheinfo */
874 *mode = ul;
875 endp++;
876 if (parse_oid_hex(endp, oid, &p) || *p != ',')
877 return -1;
878 *path = p + 1;
879 return 0;
882 static enum parse_opt_result cacheinfo_callback(
883 struct parse_opt_ctx_t *ctx, const struct option *opt,
884 const char *arg, int unset)
886 struct object_id oid;
887 unsigned int mode;
888 const char *path;
890 BUG_ON_OPT_NEG(unset);
891 BUG_ON_OPT_ARG(arg);
893 if (!parse_new_style_cacheinfo(ctx->argv[1], &mode, &oid, &path)) {
894 if (add_cacheinfo(mode, &oid, path, 0))
895 die("git update-index: --cacheinfo cannot add %s", path);
896 ctx->argv++;
897 ctx->argc--;
898 return 0;
900 if (ctx->argc <= 3)
901 return error("option 'cacheinfo' expects <mode>,<sha1>,<path>");
902 if (strtoul_ui(*++ctx->argv, 8, &mode) ||
903 get_oid_hex(*++ctx->argv, &oid) ||
904 add_cacheinfo(mode, &oid, *++ctx->argv, 0))
905 die("git update-index: --cacheinfo cannot add %s", *ctx->argv);
906 ctx->argc -= 3;
907 return 0;
910 static enum parse_opt_result stdin_cacheinfo_callback(
911 struct parse_opt_ctx_t *ctx, const struct option *opt,
912 const char *arg, int unset)
914 int *nul_term_line = opt->value;
916 BUG_ON_OPT_NEG(unset);
917 BUG_ON_OPT_ARG(arg);
919 if (ctx->argc != 1)
920 return error("option '%s' must be the last argument", opt->long_name);
921 allow_add = allow_replace = allow_remove = 1;
922 read_index_info(*nul_term_line);
923 return 0;
926 static enum parse_opt_result stdin_callback(
927 struct parse_opt_ctx_t *ctx, const struct option *opt,
928 const char *arg, int unset)
930 int *read_from_stdin = opt->value;
932 BUG_ON_OPT_NEG(unset);
933 BUG_ON_OPT_ARG(arg);
935 if (ctx->argc != 1)
936 return error("option '%s' must be the last argument", opt->long_name);
937 *read_from_stdin = 1;
938 return 0;
941 static enum parse_opt_result unresolve_callback(
942 struct parse_opt_ctx_t *ctx, const struct option *opt,
943 const char *arg, int unset)
945 int *has_errors = opt->value;
946 const char *prefix = startup_info->prefix;
948 BUG_ON_OPT_NEG(unset);
949 BUG_ON_OPT_ARG(arg);
951 /* consume remaining arguments. */
952 *has_errors = do_unresolve(ctx->argc, ctx->argv,
953 prefix, prefix ? strlen(prefix) : 0);
954 if (*has_errors)
955 the_index.cache_changed = 0;
957 ctx->argv += ctx->argc - 1;
958 ctx->argc = 1;
959 return 0;
962 static enum parse_opt_result reupdate_callback(
963 struct parse_opt_ctx_t *ctx, const struct option *opt,
964 const char *arg, int unset)
966 int *has_errors = opt->value;
967 const char *prefix = startup_info->prefix;
969 BUG_ON_OPT_NEG(unset);
970 BUG_ON_OPT_ARG(arg);
972 /* consume remaining arguments. */
973 setup_work_tree();
974 *has_errors = do_reupdate(ctx->argv + 1, prefix);
975 if (*has_errors)
976 the_index.cache_changed = 0;
978 ctx->argv += ctx->argc - 1;
979 ctx->argc = 1;
980 return 0;
983 int cmd_update_index(int argc, const char **argv, const char *prefix)
985 int newfd, entries, has_errors = 0, nul_term_line = 0;
986 enum uc_mode untracked_cache = UC_UNSPECIFIED;
987 int read_from_stdin = 0;
988 int prefix_length = prefix ? strlen(prefix) : 0;
989 int preferred_index_format = 0;
990 char set_executable_bit = 0;
991 struct refresh_params refresh_args = {0, &has_errors};
992 int lock_error = 0;
993 int split_index = -1;
994 int force_write = 0;
995 int fsmonitor = -1;
996 struct lock_file lock_file = LOCK_INIT;
997 struct parse_opt_ctx_t ctx;
998 strbuf_getline_fn getline_fn;
999 int parseopt_state = PARSE_OPT_UNKNOWN;
1000 struct repository *r = the_repository;
1001 struct option options[] = {
1002 OPT_BIT('q', NULL, &refresh_args.flags,
1003 N_("continue refresh even when index needs update"),
1004 REFRESH_QUIET),
1005 OPT_BIT(0, "ignore-submodules", &refresh_args.flags,
1006 N_("refresh: ignore submodules"),
1007 REFRESH_IGNORE_SUBMODULES),
1008 OPT_SET_INT(0, "add", &allow_add,
1009 N_("do not ignore new files"), 1),
1010 OPT_SET_INT(0, "replace", &allow_replace,
1011 N_("let files replace directories and vice-versa"), 1),
1012 OPT_SET_INT(0, "remove", &allow_remove,
1013 N_("notice files missing from worktree"), 1),
1014 OPT_BIT(0, "unmerged", &refresh_args.flags,
1015 N_("refresh even if index contains unmerged entries"),
1016 REFRESH_UNMERGED),
1017 OPT_CALLBACK_F(0, "refresh", &refresh_args, NULL,
1018 N_("refresh stat information"),
1019 PARSE_OPT_NOARG | PARSE_OPT_NONEG,
1020 refresh_callback),
1021 OPT_CALLBACK_F(0, "really-refresh", &refresh_args, NULL,
1022 N_("like --refresh, but ignore assume-unchanged setting"),
1023 PARSE_OPT_NOARG | PARSE_OPT_NONEG,
1024 really_refresh_callback),
1025 {OPTION_LOWLEVEL_CALLBACK, 0, "cacheinfo", NULL,
1026 N_("<mode>,<object>,<path>"),
1027 N_("add the specified entry to the index"),
1028 PARSE_OPT_NOARG | /* disallow --cacheinfo=<mode> form */
1029 PARSE_OPT_NONEG | PARSE_OPT_LITERAL_ARGHELP,
1030 NULL, 0,
1031 cacheinfo_callback},
1032 OPT_CALLBACK_F(0, "chmod", &set_executable_bit, "(+|-)x",
1033 N_("override the executable bit of the listed files"),
1034 PARSE_OPT_NONEG,
1035 chmod_callback),
1036 {OPTION_SET_INT, 0, "assume-unchanged", &mark_valid_only, NULL,
1037 N_("mark files as \"not changing\""),
1038 PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL, MARK_FLAG},
1039 {OPTION_SET_INT, 0, "no-assume-unchanged", &mark_valid_only, NULL,
1040 N_("clear assumed-unchanged bit"),
1041 PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL, UNMARK_FLAG},
1042 {OPTION_SET_INT, 0, "skip-worktree", &mark_skip_worktree_only, NULL,
1043 N_("mark files as \"index-only\""),
1044 PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL, MARK_FLAG},
1045 {OPTION_SET_INT, 0, "no-skip-worktree", &mark_skip_worktree_only, NULL,
1046 N_("clear skip-worktree bit"),
1047 PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL, UNMARK_FLAG},
1048 OPT_BOOL(0, "ignore-skip-worktree-entries", &ignore_skip_worktree_entries,
1049 N_("do not touch index-only entries")),
1050 OPT_SET_INT(0, "info-only", &info_only,
1051 N_("add to index only; do not add content to object database"), 1),
1052 OPT_SET_INT(0, "force-remove", &force_remove,
1053 N_("remove named paths even if present in worktree"), 1),
1054 OPT_BOOL('z', NULL, &nul_term_line,
1055 N_("with --stdin: input lines are terminated by null bytes")),
1056 {OPTION_LOWLEVEL_CALLBACK, 0, "stdin", &read_from_stdin, NULL,
1057 N_("read list of paths to be updated from standard input"),
1058 PARSE_OPT_NONEG | PARSE_OPT_NOARG,
1059 NULL, 0, stdin_callback},
1060 {OPTION_LOWLEVEL_CALLBACK, 0, "index-info", &nul_term_line, NULL,
1061 N_("add entries from standard input to the index"),
1062 PARSE_OPT_NONEG | PARSE_OPT_NOARG,
1063 NULL, 0, stdin_cacheinfo_callback},
1064 {OPTION_LOWLEVEL_CALLBACK, 0, "unresolve", &has_errors, NULL,
1065 N_("repopulate stages #2 and #3 for the listed paths"),
1066 PARSE_OPT_NONEG | PARSE_OPT_NOARG,
1067 NULL, 0, unresolve_callback},
1068 {OPTION_LOWLEVEL_CALLBACK, 'g', "again", &has_errors, NULL,
1069 N_("only update entries that differ from HEAD"),
1070 PARSE_OPT_NONEG | PARSE_OPT_NOARG,
1071 NULL, 0, reupdate_callback},
1072 OPT_BIT(0, "ignore-missing", &refresh_args.flags,
1073 N_("ignore files missing from worktree"),
1074 REFRESH_IGNORE_MISSING),
1075 OPT_SET_INT(0, "verbose", &verbose,
1076 N_("report actions to standard output"), 1),
1077 OPT_CALLBACK_F(0, "clear-resolve-undo", NULL, NULL,
1078 N_("(for porcelains) forget saved unresolved conflicts"),
1079 PARSE_OPT_NOARG | PARSE_OPT_NONEG,
1080 resolve_undo_clear_callback),
1081 OPT_INTEGER(0, "index-version", &preferred_index_format,
1082 N_("write index in this format")),
1083 OPT_BOOL(0, "split-index", &split_index,
1084 N_("enable or disable split index")),
1085 OPT_BOOL(0, "untracked-cache", &untracked_cache,
1086 N_("enable/disable untracked cache")),
1087 OPT_SET_INT(0, "test-untracked-cache", &untracked_cache,
1088 N_("test if the filesystem supports untracked cache"), UC_TEST),
1089 OPT_SET_INT(0, "force-untracked-cache", &untracked_cache,
1090 N_("enable untracked cache without testing the filesystem"), UC_FORCE),
1091 OPT_SET_INT(0, "force-write-index", &force_write,
1092 N_("write out the index even if is not flagged as changed"), 1),
1093 OPT_BOOL(0, "fsmonitor", &fsmonitor,
1094 N_("enable or disable file system monitor")),
1095 {OPTION_SET_INT, 0, "fsmonitor-valid", &mark_fsmonitor_only, NULL,
1096 N_("mark files as fsmonitor valid"),
1097 PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL, MARK_FLAG},
1098 {OPTION_SET_INT, 0, "no-fsmonitor-valid", &mark_fsmonitor_only, NULL,
1099 N_("clear fsmonitor valid bit"),
1100 PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL, UNMARK_FLAG},
1101 OPT_END()
1104 if (argc == 2 && !strcmp(argv[1], "-h"))
1105 usage_with_options(update_index_usage, options);
1107 git_config(git_default_config, NULL);
1109 prepare_repo_settings(r);
1110 the_repository->settings.command_requires_full_index = 0;
1112 /* we will diagnose later if it turns out that we need to update it */
1113 newfd = repo_hold_locked_index(the_repository, &lock_file, 0);
1114 if (newfd < 0)
1115 lock_error = errno;
1117 entries = repo_read_index(the_repository);
1118 if (entries < 0)
1119 die("cache corrupted");
1121 the_index.updated_skipworktree = 1;
1124 * Custom copy of parse_options() because we want to handle
1125 * filename arguments as they come.
1127 parse_options_start(&ctx, argc, argv, prefix,
1128 options, PARSE_OPT_STOP_AT_NON_OPTION);
1131 * Allow the object layer to optimize adding multiple objects in
1132 * a batch.
1134 begin_odb_transaction();
1135 while (ctx.argc) {
1136 if (parseopt_state != PARSE_OPT_DONE)
1137 parseopt_state = parse_options_step(&ctx, options,
1138 update_index_usage);
1139 if (!ctx.argc)
1140 break;
1141 switch (parseopt_state) {
1142 case PARSE_OPT_HELP:
1143 case PARSE_OPT_ERROR:
1144 exit(129);
1145 case PARSE_OPT_COMPLETE:
1146 exit(0);
1147 case PARSE_OPT_NON_OPTION:
1148 case PARSE_OPT_DONE:
1150 const char *path = ctx.argv[0];
1151 char *p;
1153 setup_work_tree();
1154 p = prefix_path(prefix, prefix_length, path);
1155 update_one(p);
1156 if (set_executable_bit)
1157 chmod_path(set_executable_bit, p);
1158 free(p);
1159 ctx.argc--;
1160 ctx.argv++;
1161 break;
1163 case PARSE_OPT_UNKNOWN:
1164 if (ctx.argv[0][1] == '-')
1165 error("unknown option '%s'", ctx.argv[0] + 2);
1166 else
1167 error("unknown switch '%c'", *ctx.opt);
1168 usage_with_options(update_index_usage, options);
1171 argc = parse_options_end(&ctx);
1173 getline_fn = nul_term_line ? strbuf_getline_nul : strbuf_getline_lf;
1174 if (preferred_index_format) {
1175 if (preferred_index_format < INDEX_FORMAT_LB ||
1176 INDEX_FORMAT_UB < preferred_index_format)
1177 die("index-version %d not in range: %d..%d",
1178 preferred_index_format,
1179 INDEX_FORMAT_LB, INDEX_FORMAT_UB);
1181 if (the_index.version != preferred_index_format)
1182 the_index.cache_changed |= SOMETHING_CHANGED;
1183 the_index.version = preferred_index_format;
1186 if (read_from_stdin) {
1187 struct strbuf buf = STRBUF_INIT;
1188 struct strbuf unquoted = STRBUF_INIT;
1190 setup_work_tree();
1191 while (getline_fn(&buf, stdin) != EOF) {
1192 char *p;
1193 if (!nul_term_line && buf.buf[0] == '"') {
1194 strbuf_reset(&unquoted);
1195 if (unquote_c_style(&unquoted, buf.buf, NULL))
1196 die("line is badly quoted");
1197 strbuf_swap(&buf, &unquoted);
1199 p = prefix_path(prefix, prefix_length, buf.buf);
1200 update_one(p);
1201 if (set_executable_bit)
1202 chmod_path(set_executable_bit, p);
1203 free(p);
1205 strbuf_release(&unquoted);
1206 strbuf_release(&buf);
1210 * By now we have added all of the new objects
1212 end_odb_transaction();
1214 if (split_index > 0) {
1215 if (git_config_get_split_index() == 0)
1216 warning(_("core.splitIndex is set to false; "
1217 "remove or change it, if you really want to "
1218 "enable split index"));
1219 if (the_index.split_index)
1220 the_index.cache_changed |= SPLIT_INDEX_ORDERED;
1221 else
1222 add_split_index(&the_index);
1223 } else if (!split_index) {
1224 if (git_config_get_split_index() == 1)
1225 warning(_("core.splitIndex is set to true; "
1226 "remove or change it, if you really want to "
1227 "disable split index"));
1228 remove_split_index(&the_index);
1231 prepare_repo_settings(r);
1232 switch (untracked_cache) {
1233 case UC_UNSPECIFIED:
1234 break;
1235 case UC_DISABLE:
1236 if (r->settings.core_untracked_cache == UNTRACKED_CACHE_WRITE)
1237 warning(_("core.untrackedCache is set to true; "
1238 "remove or change it, if you really want to "
1239 "disable the untracked cache"));
1240 remove_untracked_cache(&the_index);
1241 report(_("Untracked cache disabled"));
1242 break;
1243 case UC_TEST:
1244 setup_work_tree();
1245 return !test_if_untracked_cache_is_supported();
1246 case UC_ENABLE:
1247 case UC_FORCE:
1248 if (r->settings.core_untracked_cache == UNTRACKED_CACHE_REMOVE)
1249 warning(_("core.untrackedCache is set to false; "
1250 "remove or change it, if you really want to "
1251 "enable the untracked cache"));
1252 add_untracked_cache(&the_index);
1253 report(_("Untracked cache enabled for '%s'"), get_git_work_tree());
1254 break;
1255 default:
1256 BUG("bad untracked_cache value: %d", untracked_cache);
1259 if (fsmonitor > 0) {
1260 enum fsmonitor_mode fsm_mode = fsm_settings__get_mode(r);
1261 enum fsmonitor_reason reason = fsm_settings__get_reason(r);
1264 * The user wants to turn on FSMonitor using the command
1265 * line argument. (We don't know (or care) whether that
1266 * is the IPC or HOOK version.)
1268 * Use one of the __get routines to force load the FSMonitor
1269 * config settings into the repo-settings. That will detect
1270 * whether the file system is compatible so that we can stop
1271 * here with a nice error message.
1273 if (reason > FSMONITOR_REASON_OK)
1274 die("%s",
1275 fsm_settings__get_incompatible_msg(r, reason));
1277 if (fsm_mode == FSMONITOR_MODE_DISABLED) {
1278 warning(_("core.fsmonitor is unset; "
1279 "set it if you really want to "
1280 "enable fsmonitor"));
1282 add_fsmonitor(&the_index);
1283 report(_("fsmonitor enabled"));
1284 } else if (!fsmonitor) {
1285 enum fsmonitor_mode fsm_mode = fsm_settings__get_mode(r);
1286 if (fsm_mode > FSMONITOR_MODE_DISABLED)
1287 warning(_("core.fsmonitor is set; "
1288 "remove it if you really want to "
1289 "disable fsmonitor"));
1290 remove_fsmonitor(&the_index);
1291 report(_("fsmonitor disabled"));
1294 if (the_index.cache_changed || force_write) {
1295 if (newfd < 0) {
1296 if (refresh_args.flags & REFRESH_QUIET)
1297 exit(128);
1298 unable_to_lock_die(get_index_file(), lock_error);
1300 if (write_locked_index(&the_index, &lock_file, COMMIT_LOCK))
1301 die("Unable to write new index file");
1304 rollback_lock_file(&lock_file);
1306 return has_errors ? 1 : 0;