2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
6 #define USE_THE_INDEX_VARIABLE
8 #include "bulk-checkin.h"
10 #include "environment.h"
16 #include "cache-tree.h"
17 #include "tree-walk.h"
19 #include "object-file.h"
21 #include "resolve-undo.h"
22 #include "parse-options.h"
25 #include "read-cache.h"
26 #include "repository.h"
28 #include "sparse-index.h"
29 #include "split-index.h"
31 #include "fsmonitor.h"
32 #include "write-or-die.h"
35 * Default to not allowing changes to the list of files. The
36 * tool doesn't actually care, but this makes it harder to add
37 * files to the revision control by mistake by doing something
38 * like "git update-index *" and suddenly having all the object
39 * files be revision controlled.
42 static int allow_remove
;
43 static int allow_replace
;
45 static int force_remove
;
47 static int mark_valid_only
;
48 static int mark_skip_worktree_only
;
49 static int mark_fsmonitor_only
;
50 static int ignore_skip_worktree_entries
;
53 static struct strbuf mtime_dir
= STRBUF_INIT
;
55 /* Untracked cache mode */
64 __attribute__((format (printf
, 1, 2)))
65 static void report(const char *fmt
, ...)
73 * It is possible, though unlikely, that a caller could use the verbose
74 * output to synchronize with addition of objects to the object
75 * database. The current implementation of ODB transactions leaves
76 * objects invisible while a transaction is active, so flush the
77 * transaction here before reporting a change made by update-index.
79 flush_odb_transaction();
86 static void remove_test_directory(void)
89 remove_dir_recursively(&mtime_dir
, 0);
92 static const char *get_mtime_path(const char *path
)
94 static struct strbuf sb
= STRBUF_INIT
;
96 strbuf_addf(&sb
, "%s/%s", mtime_dir
.buf
, path
);
100 static void xmkdir(const char *path
)
102 path
= get_mtime_path(path
);
103 if (mkdir(path
, 0700))
104 die_errno(_("failed to create directory %s"), path
);
107 static int xstat_mtime_dir(struct stat
*st
)
109 if (stat(mtime_dir
.buf
, st
))
110 die_errno(_("failed to stat %s"), mtime_dir
.buf
);
114 static int create_file(const char *path
)
117 path
= get_mtime_path(path
);
118 fd
= xopen(path
, O_CREAT
| O_RDWR
, 0644);
122 static void xunlink(const char *path
)
124 path
= get_mtime_path(path
);
126 die_errno(_("failed to delete file %s"), path
);
129 static void xrmdir(const char *path
)
131 path
= get_mtime_path(path
);
133 die_errno(_("failed to delete directory %s"), path
);
136 static void avoid_racy(void)
139 * not use if we could usleep(10) if USE_NSEC is defined. The
140 * field nsec could be there, but the OS could choose to
146 static int test_if_untracked_cache_is_supported(void)
149 struct stat_data base
;
153 strbuf_addstr(&mtime_dir
, "mtime-test-XXXXXX");
154 if (!mkdtemp(mtime_dir
.buf
))
155 die_errno("Could not make temporary directory");
158 fprintf(stderr
, _("Testing mtime in '%s' "), cwd
);
161 atexit(remove_test_directory
);
162 xstat_mtime_dir(&st
);
163 fill_stat_data(&base
, &st
);
167 fd
= create_file("newfile");
168 xstat_mtime_dir(&st
);
169 if (!match_stat_data(&base
, &st
)) {
172 fprintf_ln(stderr
,_("directory stat info does not "
173 "change after adding a new file"));
176 fill_stat_data(&base
, &st
);
181 xstat_mtime_dir(&st
);
182 if (!match_stat_data(&base
, &st
)) {
185 fprintf_ln(stderr
, _("directory stat info does not change "
186 "after adding a new directory"));
189 fill_stat_data(&base
, &st
);
193 write_or_die(fd
, "data", 4);
195 xstat_mtime_dir(&st
);
196 if (match_stat_data(&base
, &st
)) {
198 fprintf_ln(stderr
, _("directory stat info changes "
199 "after updating a file"));
205 close(create_file("new-dir/new"));
206 xstat_mtime_dir(&st
);
207 if (match_stat_data(&base
, &st
)) {
209 fprintf_ln(stderr
, _("directory stat info changes after "
210 "adding a file inside subdirectory"));
217 xstat_mtime_dir(&st
);
218 if (!match_stat_data(&base
, &st
)) {
220 fprintf_ln(stderr
, _("directory stat info does not "
221 "change after deleting a file"));
224 fill_stat_data(&base
, &st
);
228 xunlink("new-dir/new");
230 xstat_mtime_dir(&st
);
231 if (!match_stat_data(&base
, &st
)) {
233 fprintf_ln(stderr
, _("directory stat info does not "
234 "change after deleting a directory"));
238 if (rmdir(mtime_dir
.buf
))
239 die_errno(_("failed to delete directory %s"), mtime_dir
.buf
);
240 fprintf_ln(stderr
, _(" OK"));
244 strbuf_release(&mtime_dir
);
248 static int mark_ce_flags(const char *path
, int flag
, int mark
)
250 int namelen
= strlen(path
);
251 int pos
= index_name_pos(&the_index
, path
, namelen
);
253 mark_fsmonitor_invalid(&the_index
, the_index
.cache
[pos
]);
255 the_index
.cache
[pos
]->ce_flags
|= flag
;
257 the_index
.cache
[pos
]->ce_flags
&= ~flag
;
258 the_index
.cache
[pos
]->ce_flags
|= CE_UPDATE_IN_BASE
;
259 cache_tree_invalidate_path(&the_index
, path
);
260 the_index
.cache_changed
|= CE_ENTRY_CHANGED
;
266 static int remove_one_path(const char *path
)
269 return error("%s: does not exist and --remove not passed", path
);
270 if (remove_file_from_index(&the_index
, path
))
271 return error("%s: cannot remove from the index", path
);
276 * Handle a path that couldn't be lstat'ed. It's either:
277 * - missing file (ENOENT or ENOTDIR). That's ok if we're
278 * supposed to be removing it and the removal actually
280 * - permission error. That's never ok.
282 static int process_lstat_error(const char *path
, int err
)
284 if (is_missing_file_error(err
))
285 return remove_one_path(path
);
286 return error("lstat(\"%s\"): %s", path
, strerror(err
));
289 static int add_one_path(const struct cache_entry
*old
, const char *path
, int len
, struct stat
*st
)
292 struct cache_entry
*ce
;
294 /* Was the old index entry already up-to-date? */
295 if (old
&& !ce_stage(old
) && !ie_match_stat(&the_index
, old
, st
, 0))
298 ce
= make_empty_cache_entry(&the_index
, len
);
299 memcpy(ce
->name
, path
, len
);
300 ce
->ce_flags
= create_ce_flags(0);
301 ce
->ce_namelen
= len
;
302 fill_stat_cache_info(&the_index
, ce
, st
);
303 ce
->ce_mode
= ce_mode_from_stat(old
, st
->st_mode
);
305 if (index_path(&the_index
, &ce
->oid
, path
, st
,
306 info_only
? 0 : HASH_WRITE_OBJECT
)) {
307 discard_cache_entry(ce
);
310 option
= allow_add
? ADD_CACHE_OK_TO_ADD
: 0;
311 option
|= allow_replace
? ADD_CACHE_OK_TO_REPLACE
: 0;
312 if (add_index_entry(&the_index
, ce
, option
)) {
313 discard_cache_entry(ce
);
314 return error("%s: cannot add to the index - missing --add option?", path
);
320 * Handle a path that was a directory. Four cases:
322 * - it's already a gitlink in the index, and we keep it that
323 * way, and update it if we can (if we cannot find the HEAD,
324 * we're going to keep it unchanged in the index!)
326 * - it's a *file* in the index, in which case it should be
327 * removed as a file if removal is allowed, since it doesn't
328 * exist as such any more. If removal isn't allowed, it's
331 * (NOTE! This is old and arguably fairly strange behaviour.
332 * We might want to make this an error unconditionally, and
333 * use "--force-remove" if you actually want to force removal).
335 * - it used to exist as a subdirectory (ie multiple files with
336 * this particular prefix) in the index, in which case it's wrong
337 * to try to update it as a directory.
339 * - it doesn't exist at all in the index, but it is a valid
340 * git directory, and it should be *added* as a gitlink.
342 static int process_directory(const char *path
, int len
, struct stat
*st
)
344 struct object_id oid
;
345 int pos
= index_name_pos(&the_index
, path
, len
);
347 /* Exact match: file or existing gitlink */
349 const struct cache_entry
*ce
= the_index
.cache
[pos
];
350 if (S_ISGITLINK(ce
->ce_mode
)) {
352 /* Do nothing to the index if there is no HEAD! */
353 if (resolve_gitlink_ref(path
, "HEAD", &oid
) < 0)
356 return add_one_path(ce
, path
, len
, st
);
358 /* Should this be an unconditional error? */
359 return remove_one_path(path
);
362 /* Inexact match: is there perhaps a subdirectory match? */
364 while (pos
< the_index
.cache_nr
) {
365 const struct cache_entry
*ce
= the_index
.cache
[pos
++];
367 if (strncmp(ce
->name
, path
, len
))
369 if (ce
->name
[len
] > '/')
371 if (ce
->name
[len
] < '/')
374 /* Subdirectory match - error out */
375 return error("%s: is a directory - add individual files instead", path
);
378 /* No match - should we add it as a gitlink? */
379 if (!resolve_gitlink_ref(path
, "HEAD", &oid
))
380 return add_one_path(NULL
, path
, len
, st
);
383 return error("%s: is a directory - add files inside instead", path
);
386 static int process_path(const char *path
, struct stat
*st
, int stat_errno
)
389 const struct cache_entry
*ce
;
392 if (has_symlink_leading_path(path
, len
))
393 return error("'%s' is beyond a symbolic link", path
);
395 pos
= index_name_pos(&the_index
, path
, len
);
396 ce
= pos
< 0 ? NULL
: the_index
.cache
[pos
];
397 if (ce
&& ce_skip_worktree(ce
)) {
399 * working directory version is assumed "good"
400 * so updating it does not make sense.
401 * On the other hand, removing it from index should work
403 if (!ignore_skip_worktree_entries
&& allow_remove
&&
404 remove_file_from_index(&the_index
, path
))
405 return error("%s: cannot remove from the index", path
);
410 * First things first: get the stat information, to decide
411 * what to do about the pathname!
414 return process_lstat_error(path
, stat_errno
);
416 if (S_ISDIR(st
->st_mode
))
417 return process_directory(path
, len
, st
);
419 return add_one_path(ce
, path
, len
, st
);
422 static int add_cacheinfo(unsigned int mode
, const struct object_id
*oid
,
423 const char *path
, int stage
)
426 struct cache_entry
*ce
;
428 if (!verify_path(path
, mode
))
429 return error("Invalid path '%s'", path
);
432 ce
= make_empty_cache_entry(&the_index
, len
);
434 oidcpy(&ce
->oid
, oid
);
435 memcpy(ce
->name
, path
, len
);
436 ce
->ce_flags
= create_ce_flags(stage
);
437 ce
->ce_namelen
= len
;
438 ce
->ce_mode
= create_ce_mode(mode
);
439 if (assume_unchanged
)
440 ce
->ce_flags
|= CE_VALID
;
441 option
= allow_add
? ADD_CACHE_OK_TO_ADD
: 0;
442 option
|= allow_replace
? ADD_CACHE_OK_TO_REPLACE
: 0;
443 if (add_index_entry(&the_index
, ce
, option
))
444 return error("%s: cannot add to the index - missing --add option?",
446 report("add '%s'", path
);
450 static void chmod_path(char flip
, const char *path
)
453 struct cache_entry
*ce
;
455 pos
= index_name_pos(&the_index
, path
, strlen(path
));
458 ce
= the_index
.cache
[pos
];
459 if (chmod_index_entry(&the_index
, ce
, flip
) < 0)
462 report("chmod %cx '%s'", flip
, path
);
465 die("git update-index: cannot chmod %cx '%s'", flip
, path
);
468 static void update_one(const char *path
)
473 if (mark_valid_only
|| mark_skip_worktree_only
|| force_remove
||
476 else if (lstat(path
, &st
) < 0) {
479 } /* else stat is valid */
481 if (!verify_path(path
, st
.st_mode
)) {
482 fprintf(stderr
, "Ignoring path %s\n", path
);
485 if (mark_valid_only
) {
486 if (mark_ce_flags(path
, CE_VALID
, mark_valid_only
== MARK_FLAG
))
487 die("Unable to mark file %s", path
);
490 if (mark_skip_worktree_only
) {
491 if (mark_ce_flags(path
, CE_SKIP_WORKTREE
, mark_skip_worktree_only
== MARK_FLAG
))
492 die("Unable to mark file %s", path
);
495 if (mark_fsmonitor_only
) {
496 if (mark_ce_flags(path
, CE_FSMONITOR_VALID
, mark_fsmonitor_only
== MARK_FLAG
))
497 die("Unable to mark file %s", path
);
502 if (remove_file_from_index(&the_index
, path
))
503 die("git update-index: unable to remove %s", path
);
504 report("remove '%s'", path
);
507 if (process_path(path
, &st
, stat_errno
))
508 die("Unable to process path %s", path
);
509 report("add '%s'", path
);
512 static void read_index_info(int nul_term_line
)
514 const int hexsz
= the_hash_algo
->hexsz
;
515 struct strbuf buf
= STRBUF_INIT
;
516 struct strbuf uq
= STRBUF_INIT
;
517 strbuf_getline_fn getline_fn
;
519 getline_fn
= nul_term_line
? strbuf_getline_nul
: strbuf_getline_lf
;
520 while (getline_fn(&buf
, stdin
) != EOF
) {
523 struct object_id oid
;
528 /* This reads lines formatted in one of three formats:
530 * (1) mode SP sha1 TAB path
531 * The first format is what "git apply --index-info"
532 * reports, and used to reconstruct a partial tree
533 * that is used for phony merge base tree when falling
534 * back on 3-way merge.
536 * (2) mode SP type SP sha1 TAB path
537 * The second format is to stuff "git ls-tree" output
538 * into the index file.
540 * (3) mode SP sha1 SP stage TAB path
541 * This format is to put higher order stages into the
542 * index file and matches "git ls-files --stage" output.
545 ul
= strtoul(buf
.buf
, &ptr
, 8);
546 if (ptr
== buf
.buf
|| *ptr
!= ' '
547 || errno
|| (unsigned int) ul
!= ul
)
551 tab
= strchr(ptr
, '\t');
552 if (!tab
|| tab
- ptr
< hexsz
+ 1)
555 if (tab
[-2] == ' ' && '0' <= tab
[-1] && tab
[-1] <= '3') {
556 stage
= tab
[-1] - '0';
557 ptr
= tab
+ 1; /* point at the head of path */
558 tab
= tab
- 2; /* point at tail of sha1 */
562 ptr
= tab
+ 1; /* point at the head of path */
565 if (get_oid_hex(tab
- hexsz
, &oid
) ||
566 tab
[-(hexsz
+ 1)] != ' ')
570 if (!nul_term_line
&& path_name
[0] == '"') {
572 if (unquote_c_style(&uq
, path_name
, NULL
)) {
573 die("git update-index: bad quoting of path name");
578 if (!verify_path(path_name
, mode
)) {
579 fprintf(stderr
, "Ignoring path %s\n", path_name
);
584 /* mode == 0 means there is no such path -- remove */
585 if (remove_file_from_index(&the_index
, path_name
))
586 die("git update-index: unable to remove %s",
590 /* mode ' ' sha1 '\t' name
591 * ptr[-1] points at tab,
592 * ptr[-41] is at the beginning of sha1
594 ptr
[-(hexsz
+ 2)] = ptr
[-1] = 0;
595 if (add_cacheinfo(mode
, &oid
, path_name
, stage
))
596 die("git update-index: unable to update %s",
602 die("malformed index info %s", buf
.buf
);
604 strbuf_release(&buf
);
608 static const char * const update_index_usage
[] = {
609 N_("git update-index [<options>] [--] [<file>...]"),
613 static struct object_id head_oid
;
614 static struct object_id merge_head_oid
;
616 static struct cache_entry
*read_one_ent(const char *which
,
617 struct object_id
*ent
, const char *path
,
618 int namelen
, int stage
)
621 struct object_id oid
;
622 struct cache_entry
*ce
;
624 if (get_tree_entry(the_repository
, ent
, path
, &oid
, &mode
)) {
626 error("%s: not in %s branch.", path
, which
);
629 if (!the_index
.sparse_index
&& mode
== S_IFDIR
) {
631 error("%s: not a blob in %s branch.", path
, which
);
634 ce
= make_empty_cache_entry(&the_index
, namelen
);
636 oidcpy(&ce
->oid
, &oid
);
637 memcpy(ce
->name
, path
, namelen
);
638 ce
->ce_flags
= create_ce_flags(stage
);
639 ce
->ce_namelen
= namelen
;
640 ce
->ce_mode
= create_ce_mode(mode
);
644 static int unresolve_one(const char *path
)
646 int namelen
= strlen(path
);
649 struct cache_entry
*ce_2
= NULL
, *ce_3
= NULL
;
651 /* See if there is such entry in the index. */
652 pos
= index_name_pos(&the_index
, path
, namelen
);
655 pos
= unmerge_index_entry_at(&the_index
, pos
);
656 if (pos
< the_index
.cache_nr
) {
657 const struct cache_entry
*ce
= the_index
.cache
[pos
];
659 ce_namelen(ce
) == namelen
&&
660 !memcmp(ce
->name
, path
, namelen
))
663 /* no resolve-undo information; fall back */
665 /* If there isn't, either it is unmerged, or
666 * resolved as "removed" by mistake. We do not
667 * want to do anything in the former case.
670 if (pos
< the_index
.cache_nr
) {
671 const struct cache_entry
*ce
= the_index
.cache
[pos
];
672 if (ce_namelen(ce
) == namelen
&&
673 !memcmp(ce
->name
, path
, namelen
)) {
675 "%s: skipping still unmerged path.\n",
682 /* Grab blobs from given path from HEAD and MERGE_HEAD,
683 * stuff HEAD version in stage #2,
684 * stuff MERGE_HEAD version in stage #3.
686 ce_2
= read_one_ent("our", &head_oid
, path
, namelen
, 2);
687 ce_3
= read_one_ent("their", &merge_head_oid
, path
, namelen
, 3);
689 if (!ce_2
|| !ce_3
) {
693 if (oideq(&ce_2
->oid
, &ce_3
->oid
) &&
694 ce_2
->ce_mode
== ce_3
->ce_mode
) {
695 fprintf(stderr
, "%s: identical in both, skipping.\n",
700 remove_file_from_index(&the_index
, path
);
701 if (add_index_entry(&the_index
, ce_2
, ADD_CACHE_OK_TO_ADD
)) {
702 error("%s: cannot add our version to the index.", path
);
706 if (!add_index_entry(&the_index
, ce_3
, ADD_CACHE_OK_TO_ADD
))
708 error("%s: cannot add their version to the index.", path
);
711 discard_cache_entry(ce_2
);
712 discard_cache_entry(ce_3
);
716 static void read_head_pointers(void)
718 if (read_ref("HEAD", &head_oid
))
719 die("No HEAD -- no initial commit yet?");
720 if (read_ref("MERGE_HEAD", &merge_head_oid
)) {
721 fprintf(stderr
, "Not in the middle of a merge.\n");
726 static int do_unresolve(int ac
, const char **av
,
727 const char *prefix
, int prefix_length
)
732 /* Read HEAD and MERGE_HEAD; if MERGE_HEAD does not exist, we
733 * are not doing a merge, so exit with success status.
735 read_head_pointers();
737 for (i
= 1; i
< ac
; i
++) {
738 const char *arg
= av
[i
];
739 char *p
= prefix_path(prefix
, prefix_length
, arg
);
740 err
|= unresolve_one(p
);
746 static int do_reupdate(const char **paths
,
749 /* Read HEAD and run update-index on paths that are
750 * merged and already different between index and HEAD.
754 struct pathspec pathspec
;
756 parse_pathspec(&pathspec
, 0,
760 if (read_ref("HEAD", &head_oid
))
761 /* If there is no HEAD, that means it is an initial
762 * commit. Update everything in the index.
766 for (pos
= 0; pos
< the_index
.cache_nr
; pos
++) {
767 const struct cache_entry
*ce
= the_index
.cache
[pos
];
768 struct cache_entry
*old
= NULL
;
772 if (ce_stage(ce
) || !ce_path_match(&the_index
, ce
, &pathspec
, NULL
))
775 old
= read_one_ent(NULL
, &head_oid
,
776 ce
->name
, ce_namelen(ce
), 0);
777 if (old
&& ce
->ce_mode
== old
->ce_mode
&&
778 oideq(&ce
->oid
, &old
->oid
)) {
779 discard_cache_entry(old
);
780 continue; /* unchanged */
783 /* At this point, we know the contents of the sparse directory are
784 * modified with respect to HEAD, so we expand the index and restart
785 * to process each path individually
787 if (S_ISSPARSEDIR(ce
->ce_mode
)) {
788 ensure_full_index(&the_index
);
792 /* Be careful. The working tree may not have the
793 * path anymore, in which case, under 'allow_remove',
794 * or worse yet 'allow_replace', active_nr may decrease.
796 save_nr
= the_index
.cache_nr
;
797 path
= xstrdup(ce
->name
);
800 discard_cache_entry(old
);
801 if (save_nr
!= the_index
.cache_nr
)
804 clear_pathspec(&pathspec
);
808 struct refresh_params
{
813 static int refresh(struct refresh_params
*o
, unsigned int flag
)
816 repo_read_index(the_repository
);
817 *o
->has_errors
|= refresh_index(&the_index
, o
->flags
| flag
, NULL
,
819 if (has_racy_timestamp(&the_index
)) {
821 * Even if nothing else has changed, updating the file
822 * increases the chance that racy timestamps become
823 * non-racy, helping future run-time performance.
824 * We do that even in case of "errors" returned by
825 * refresh_index() as these are no actual errors.
826 * cmd_status() does the same.
828 the_index
.cache_changed
|= SOMETHING_CHANGED
;
833 static int refresh_callback(const struct option
*opt
,
834 const char *arg
, int unset
)
836 BUG_ON_OPT_NEG(unset
);
838 return refresh(opt
->value
, 0);
841 static int really_refresh_callback(const struct option
*opt
,
842 const char *arg
, int unset
)
844 BUG_ON_OPT_NEG(unset
);
846 return refresh(opt
->value
, REFRESH_REALLY
);
849 static int chmod_callback(const struct option
*opt
,
850 const char *arg
, int unset
)
852 char *flip
= opt
->value
;
853 BUG_ON_OPT_NEG(unset
);
854 if ((arg
[0] != '-' && arg
[0] != '+') || arg
[1] != 'x' || arg
[2])
855 return error("option 'chmod' expects \"+x\" or \"-x\"");
860 static int resolve_undo_clear_callback(const struct option
*opt
,
861 const char *arg
, int unset
)
863 BUG_ON_OPT_NEG(unset
);
865 resolve_undo_clear_index(&the_index
);
869 static int parse_new_style_cacheinfo(const char *arg
,
871 struct object_id
*oid
,
882 ul
= strtoul(arg
, &endp
, 8);
883 if (errno
|| endp
== arg
|| *endp
!= ',' || (unsigned int) ul
!= ul
)
884 return -1; /* not a new-style cacheinfo */
887 if (parse_oid_hex(endp
, oid
, &p
) || *p
!= ',')
893 static enum parse_opt_result
cacheinfo_callback(
894 struct parse_opt_ctx_t
*ctx
, const struct option
*opt
,
895 const char *arg
, int unset
)
897 struct object_id oid
;
901 BUG_ON_OPT_NEG(unset
);
904 if (!parse_new_style_cacheinfo(ctx
->argv
[1], &mode
, &oid
, &path
)) {
905 if (add_cacheinfo(mode
, &oid
, path
, 0))
906 die("git update-index: --cacheinfo cannot add %s", path
);
912 return error("option 'cacheinfo' expects <mode>,<sha1>,<path>");
913 if (strtoul_ui(*++ctx
->argv
, 8, &mode
) ||
914 get_oid_hex(*++ctx
->argv
, &oid
) ||
915 add_cacheinfo(mode
, &oid
, *++ctx
->argv
, 0))
916 die("git update-index: --cacheinfo cannot add %s", *ctx
->argv
);
921 static enum parse_opt_result
stdin_cacheinfo_callback(
922 struct parse_opt_ctx_t
*ctx
, const struct option
*opt
,
923 const char *arg
, int unset
)
925 int *nul_term_line
= opt
->value
;
927 BUG_ON_OPT_NEG(unset
);
931 return error("option '%s' must be the last argument", opt
->long_name
);
932 allow_add
= allow_replace
= allow_remove
= 1;
933 read_index_info(*nul_term_line
);
937 static enum parse_opt_result
stdin_callback(
938 struct parse_opt_ctx_t
*ctx
, const struct option
*opt
,
939 const char *arg
, int unset
)
941 int *read_from_stdin
= opt
->value
;
943 BUG_ON_OPT_NEG(unset
);
947 return error("option '%s' must be the last argument", opt
->long_name
);
948 *read_from_stdin
= 1;
952 static enum parse_opt_result
unresolve_callback(
953 struct parse_opt_ctx_t
*ctx
, const struct option
*opt
,
954 const char *arg
, int unset
)
956 int *has_errors
= opt
->value
;
957 const char *prefix
= startup_info
->prefix
;
959 BUG_ON_OPT_NEG(unset
);
962 /* consume remaining arguments. */
963 *has_errors
= do_unresolve(ctx
->argc
, ctx
->argv
,
964 prefix
, prefix
? strlen(prefix
) : 0);
966 the_index
.cache_changed
= 0;
968 ctx
->argv
+= ctx
->argc
- 1;
973 static enum parse_opt_result
reupdate_callback(
974 struct parse_opt_ctx_t
*ctx
, const struct option
*opt
,
975 const char *arg
, int unset
)
977 int *has_errors
= opt
->value
;
978 const char *prefix
= startup_info
->prefix
;
980 BUG_ON_OPT_NEG(unset
);
983 /* consume remaining arguments. */
985 *has_errors
= do_reupdate(ctx
->argv
+ 1, prefix
);
987 the_index
.cache_changed
= 0;
989 ctx
->argv
+= ctx
->argc
- 1;
994 int cmd_update_index(int argc
, const char **argv
, const char *prefix
)
996 int newfd
, entries
, has_errors
= 0, nul_term_line
= 0;
997 enum uc_mode untracked_cache
= UC_UNSPECIFIED
;
998 int read_from_stdin
= 0;
999 int prefix_length
= prefix
? strlen(prefix
) : 0;
1000 int preferred_index_format
= 0;
1001 char set_executable_bit
= 0;
1002 struct refresh_params refresh_args
= {0, &has_errors
};
1004 int split_index
= -1;
1005 int force_write
= 0;
1007 struct lock_file lock_file
= LOCK_INIT
;
1008 struct parse_opt_ctx_t ctx
;
1009 strbuf_getline_fn getline_fn
;
1010 int parseopt_state
= PARSE_OPT_UNKNOWN
;
1011 struct repository
*r
= the_repository
;
1012 struct option options
[] = {
1013 OPT_BIT('q', NULL
, &refresh_args
.flags
,
1014 N_("continue refresh even when index needs update"),
1016 OPT_BIT(0, "ignore-submodules", &refresh_args
.flags
,
1017 N_("refresh: ignore submodules"),
1018 REFRESH_IGNORE_SUBMODULES
),
1019 OPT_SET_INT(0, "add", &allow_add
,
1020 N_("do not ignore new files"), 1),
1021 OPT_SET_INT(0, "replace", &allow_replace
,
1022 N_("let files replace directories and vice-versa"), 1),
1023 OPT_SET_INT(0, "remove", &allow_remove
,
1024 N_("notice files missing from worktree"), 1),
1025 OPT_BIT(0, "unmerged", &refresh_args
.flags
,
1026 N_("refresh even if index contains unmerged entries"),
1028 OPT_CALLBACK_F(0, "refresh", &refresh_args
, NULL
,
1029 N_("refresh stat information"),
1030 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
,
1032 OPT_CALLBACK_F(0, "really-refresh", &refresh_args
, NULL
,
1033 N_("like --refresh, but ignore assume-unchanged setting"),
1034 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
,
1035 really_refresh_callback
),
1036 {OPTION_LOWLEVEL_CALLBACK
, 0, "cacheinfo", NULL
,
1037 N_("<mode>,<object>,<path>"),
1038 N_("add the specified entry to the index"),
1039 PARSE_OPT_NOARG
| /* disallow --cacheinfo=<mode> form */
1040 PARSE_OPT_NONEG
| PARSE_OPT_LITERAL_ARGHELP
,
1042 cacheinfo_callback
},
1043 OPT_CALLBACK_F(0, "chmod", &set_executable_bit
, "(+|-)x",
1044 N_("override the executable bit of the listed files"),
1047 {OPTION_SET_INT
, 0, "assume-unchanged", &mark_valid_only
, NULL
,
1048 N_("mark files as \"not changing\""),
1049 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
, NULL
, MARK_FLAG
},
1050 {OPTION_SET_INT
, 0, "no-assume-unchanged", &mark_valid_only
, NULL
,
1051 N_("clear assumed-unchanged bit"),
1052 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
, NULL
, UNMARK_FLAG
},
1053 {OPTION_SET_INT
, 0, "skip-worktree", &mark_skip_worktree_only
, NULL
,
1054 N_("mark files as \"index-only\""),
1055 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
, NULL
, MARK_FLAG
},
1056 {OPTION_SET_INT
, 0, "no-skip-worktree", &mark_skip_worktree_only
, NULL
,
1057 N_("clear skip-worktree bit"),
1058 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
, NULL
, UNMARK_FLAG
},
1059 OPT_BOOL(0, "ignore-skip-worktree-entries", &ignore_skip_worktree_entries
,
1060 N_("do not touch index-only entries")),
1061 OPT_SET_INT(0, "info-only", &info_only
,
1062 N_("add to index only; do not add content to object database"), 1),
1063 OPT_SET_INT(0, "force-remove", &force_remove
,
1064 N_("remove named paths even if present in worktree"), 1),
1065 OPT_BOOL('z', NULL
, &nul_term_line
,
1066 N_("with --stdin: input lines are terminated by null bytes")),
1067 {OPTION_LOWLEVEL_CALLBACK
, 0, "stdin", &read_from_stdin
, NULL
,
1068 N_("read list of paths to be updated from standard input"),
1069 PARSE_OPT_NONEG
| PARSE_OPT_NOARG
,
1070 NULL
, 0, stdin_callback
},
1071 {OPTION_LOWLEVEL_CALLBACK
, 0, "index-info", &nul_term_line
, NULL
,
1072 N_("add entries from standard input to the index"),
1073 PARSE_OPT_NONEG
| PARSE_OPT_NOARG
,
1074 NULL
, 0, stdin_cacheinfo_callback
},
1075 {OPTION_LOWLEVEL_CALLBACK
, 0, "unresolve", &has_errors
, NULL
,
1076 N_("repopulate stages #2 and #3 for the listed paths"),
1077 PARSE_OPT_NONEG
| PARSE_OPT_NOARG
,
1078 NULL
, 0, unresolve_callback
},
1079 {OPTION_LOWLEVEL_CALLBACK
, 'g', "again", &has_errors
, NULL
,
1080 N_("only update entries that differ from HEAD"),
1081 PARSE_OPT_NONEG
| PARSE_OPT_NOARG
,
1082 NULL
, 0, reupdate_callback
},
1083 OPT_BIT(0, "ignore-missing", &refresh_args
.flags
,
1084 N_("ignore files missing from worktree"),
1085 REFRESH_IGNORE_MISSING
),
1086 OPT_SET_INT(0, "verbose", &verbose
,
1087 N_("report actions to standard output"), 1),
1088 OPT_CALLBACK_F(0, "clear-resolve-undo", NULL
, NULL
,
1089 N_("(for porcelains) forget saved unresolved conflicts"),
1090 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
,
1091 resolve_undo_clear_callback
),
1092 OPT_INTEGER(0, "index-version", &preferred_index_format
,
1093 N_("write index in this format")),
1094 OPT_BOOL(0, "split-index", &split_index
,
1095 N_("enable or disable split index")),
1096 OPT_BOOL(0, "untracked-cache", &untracked_cache
,
1097 N_("enable/disable untracked cache")),
1098 OPT_SET_INT(0, "test-untracked-cache", &untracked_cache
,
1099 N_("test if the filesystem supports untracked cache"), UC_TEST
),
1100 OPT_SET_INT(0, "force-untracked-cache", &untracked_cache
,
1101 N_("enable untracked cache without testing the filesystem"), UC_FORCE
),
1102 OPT_SET_INT(0, "force-write-index", &force_write
,
1103 N_("write out the index even if is not flagged as changed"), 1),
1104 OPT_BOOL(0, "fsmonitor", &fsmonitor
,
1105 N_("enable or disable file system monitor")),
1106 {OPTION_SET_INT
, 0, "fsmonitor-valid", &mark_fsmonitor_only
, NULL
,
1107 N_("mark files as fsmonitor valid"),
1108 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
, NULL
, MARK_FLAG
},
1109 {OPTION_SET_INT
, 0, "no-fsmonitor-valid", &mark_fsmonitor_only
, NULL
,
1110 N_("clear fsmonitor valid bit"),
1111 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
, NULL
, UNMARK_FLAG
},
1115 if (argc
== 2 && !strcmp(argv
[1], "-h"))
1116 usage_with_options(update_index_usage
, options
);
1118 git_config(git_default_config
, NULL
);
1120 prepare_repo_settings(r
);
1121 the_repository
->settings
.command_requires_full_index
= 0;
1123 /* we will diagnose later if it turns out that we need to update it */
1124 newfd
= repo_hold_locked_index(the_repository
, &lock_file
, 0);
1128 entries
= repo_read_index(the_repository
);
1130 die("cache corrupted");
1132 the_index
.updated_skipworktree
= 1;
1135 * Custom copy of parse_options() because we want to handle
1136 * filename arguments as they come.
1138 parse_options_start(&ctx
, argc
, argv
, prefix
,
1139 options
, PARSE_OPT_STOP_AT_NON_OPTION
);
1142 * Allow the object layer to optimize adding multiple objects in
1145 begin_odb_transaction();
1147 if (parseopt_state
!= PARSE_OPT_DONE
)
1148 parseopt_state
= parse_options_step(&ctx
, options
,
1149 update_index_usage
);
1152 switch (parseopt_state
) {
1153 case PARSE_OPT_HELP
:
1154 case PARSE_OPT_ERROR
:
1156 case PARSE_OPT_COMPLETE
:
1158 case PARSE_OPT_NON_OPTION
:
1159 case PARSE_OPT_DONE
:
1161 const char *path
= ctx
.argv
[0];
1165 p
= prefix_path(prefix
, prefix_length
, path
);
1167 if (set_executable_bit
)
1168 chmod_path(set_executable_bit
, p
);
1174 case PARSE_OPT_UNKNOWN
:
1175 if (ctx
.argv
[0][1] == '-')
1176 error("unknown option '%s'", ctx
.argv
[0] + 2);
1178 error("unknown switch '%c'", *ctx
.opt
);
1179 usage_with_options(update_index_usage
, options
);
1182 argc
= parse_options_end(&ctx
);
1184 getline_fn
= nul_term_line
? strbuf_getline_nul
: strbuf_getline_lf
;
1185 if (preferred_index_format
) {
1186 if (preferred_index_format
< INDEX_FORMAT_LB
||
1187 INDEX_FORMAT_UB
< preferred_index_format
)
1188 die("index-version %d not in range: %d..%d",
1189 preferred_index_format
,
1190 INDEX_FORMAT_LB
, INDEX_FORMAT_UB
);
1192 if (the_index
.version
!= preferred_index_format
)
1193 the_index
.cache_changed
|= SOMETHING_CHANGED
;
1194 the_index
.version
= preferred_index_format
;
1197 if (read_from_stdin
) {
1198 struct strbuf buf
= STRBUF_INIT
;
1199 struct strbuf unquoted
= STRBUF_INIT
;
1202 while (getline_fn(&buf
, stdin
) != EOF
) {
1204 if (!nul_term_line
&& buf
.buf
[0] == '"') {
1205 strbuf_reset(&unquoted
);
1206 if (unquote_c_style(&unquoted
, buf
.buf
, NULL
))
1207 die("line is badly quoted");
1208 strbuf_swap(&buf
, &unquoted
);
1210 p
= prefix_path(prefix
, prefix_length
, buf
.buf
);
1212 if (set_executable_bit
)
1213 chmod_path(set_executable_bit
, p
);
1216 strbuf_release(&unquoted
);
1217 strbuf_release(&buf
);
1221 * By now we have added all of the new objects
1223 end_odb_transaction();
1225 if (split_index
> 0) {
1226 if (git_config_get_split_index() == 0)
1227 warning(_("core.splitIndex is set to false; "
1228 "remove or change it, if you really want to "
1229 "enable split index"));
1230 if (the_index
.split_index
)
1231 the_index
.cache_changed
|= SPLIT_INDEX_ORDERED
;
1233 add_split_index(&the_index
);
1234 } else if (!split_index
) {
1235 if (git_config_get_split_index() == 1)
1236 warning(_("core.splitIndex is set to true; "
1237 "remove or change it, if you really want to "
1238 "disable split index"));
1239 remove_split_index(&the_index
);
1242 prepare_repo_settings(r
);
1243 switch (untracked_cache
) {
1244 case UC_UNSPECIFIED
:
1247 if (r
->settings
.core_untracked_cache
== UNTRACKED_CACHE_WRITE
)
1248 warning(_("core.untrackedCache is set to true; "
1249 "remove or change it, if you really want to "
1250 "disable the untracked cache"));
1251 remove_untracked_cache(&the_index
);
1252 report(_("Untracked cache disabled"));
1256 return !test_if_untracked_cache_is_supported();
1259 if (r
->settings
.core_untracked_cache
== UNTRACKED_CACHE_REMOVE
)
1260 warning(_("core.untrackedCache is set to false; "
1261 "remove or change it, if you really want to "
1262 "enable the untracked cache"));
1263 add_untracked_cache(&the_index
);
1264 report(_("Untracked cache enabled for '%s'"), get_git_work_tree());
1267 BUG("bad untracked_cache value: %d", untracked_cache
);
1270 if (fsmonitor
> 0) {
1271 enum fsmonitor_mode fsm_mode
= fsm_settings__get_mode(r
);
1272 enum fsmonitor_reason reason
= fsm_settings__get_reason(r
);
1275 * The user wants to turn on FSMonitor using the command
1276 * line argument. (We don't know (or care) whether that
1277 * is the IPC or HOOK version.)
1279 * Use one of the __get routines to force load the FSMonitor
1280 * config settings into the repo-settings. That will detect
1281 * whether the file system is compatible so that we can stop
1282 * here with a nice error message.
1284 if (reason
> FSMONITOR_REASON_OK
)
1286 fsm_settings__get_incompatible_msg(r
, reason
));
1288 if (fsm_mode
== FSMONITOR_MODE_DISABLED
) {
1289 warning(_("core.fsmonitor is unset; "
1290 "set it if you really want to "
1291 "enable fsmonitor"));
1293 add_fsmonitor(&the_index
);
1294 report(_("fsmonitor enabled"));
1295 } else if (!fsmonitor
) {
1296 enum fsmonitor_mode fsm_mode
= fsm_settings__get_mode(r
);
1297 if (fsm_mode
> FSMONITOR_MODE_DISABLED
)
1298 warning(_("core.fsmonitor is set; "
1299 "remove it if you really want to "
1300 "disable fsmonitor"));
1301 remove_fsmonitor(&the_index
);
1302 report(_("fsmonitor disabled"));
1305 if (the_index
.cache_changed
|| force_write
) {
1307 if (refresh_args
.flags
& REFRESH_QUIET
)
1309 unable_to_lock_die(get_index_file(), lock_error
);
1311 if (write_locked_index(&the_index
, &lock_file
, COMMIT_LOCK
))
1312 die("Unable to write new index file");
1315 rollback_lock_file(&lock_file
);
1317 return has_errors
? 1 : 0;