2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
6 #define USE_THE_INDEX_COMPATIBILITY_MACROS
8 #include "bulk-checkin.h"
12 #include "cache-tree.h"
13 #include "tree-walk.h"
16 #include "resolve-undo.h"
17 #include "parse-options.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.
31 static int allow_remove
;
32 static int allow_replace
;
34 static int force_remove
;
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
;
42 static struct strbuf mtime_dir
= STRBUF_INIT
;
44 /* Untracked cache mode */
53 __attribute__((format (printf
, 1, 2)))
54 static void report(const char *fmt
, ...)
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();
75 static void remove_test_directory(void)
78 remove_dir_recursively(&mtime_dir
, 0);
81 static const char *get_mtime_path(const char *path
)
83 static struct strbuf sb
= STRBUF_INIT
;
85 strbuf_addf(&sb
, "%s/%s", mtime_dir
.buf
, path
);
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
);
103 static int create_file(const char *path
)
106 path
= get_mtime_path(path
);
107 fd
= xopen(path
, O_CREAT
| O_RDWR
, 0644);
111 static void xunlink(const char *path
)
113 path
= get_mtime_path(path
);
115 die_errno(_("failed to delete file %s"), path
);
118 static void xrmdir(const char *path
)
120 path
= get_mtime_path(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
135 static int test_if_untracked_cache_is_supported(void)
138 struct stat_data base
;
142 strbuf_addstr(&mtime_dir
, "mtime-test-XXXXXX");
143 if (!mkdtemp(mtime_dir
.buf
))
144 die_errno("Could not make temporary directory");
147 fprintf(stderr
, _("Testing mtime in '%s' "), cwd
);
150 atexit(remove_test_directory
);
151 xstat_mtime_dir(&st
);
152 fill_stat_data(&base
, &st
);
156 fd
= create_file("newfile");
157 xstat_mtime_dir(&st
);
158 if (!match_stat_data(&base
, &st
)) {
161 fprintf_ln(stderr
,_("directory stat info does not "
162 "change after adding a new file"));
165 fill_stat_data(&base
, &st
);
170 xstat_mtime_dir(&st
);
171 if (!match_stat_data(&base
, &st
)) {
174 fprintf_ln(stderr
, _("directory stat info does not change "
175 "after adding a new directory"));
178 fill_stat_data(&base
, &st
);
182 write_or_die(fd
, "data", 4);
184 xstat_mtime_dir(&st
);
185 if (match_stat_data(&base
, &st
)) {
187 fprintf_ln(stderr
, _("directory stat info changes "
188 "after updating a file"));
194 close(create_file("new-dir/new"));
195 xstat_mtime_dir(&st
);
196 if (match_stat_data(&base
, &st
)) {
198 fprintf_ln(stderr
, _("directory stat info changes after "
199 "adding a file inside subdirectory"));
206 xstat_mtime_dir(&st
);
207 if (!match_stat_data(&base
, &st
)) {
209 fprintf_ln(stderr
, _("directory stat info does not "
210 "change after deleting a file"));
213 fill_stat_data(&base
, &st
);
217 xunlink("new-dir/new");
219 xstat_mtime_dir(&st
);
220 if (!match_stat_data(&base
, &st
)) {
222 fprintf_ln(stderr
, _("directory stat info does not "
223 "change after deleting a directory"));
227 if (rmdir(mtime_dir
.buf
))
228 die_errno(_("failed to delete directory %s"), mtime_dir
.buf
);
229 fprintf_ln(stderr
, _(" OK"));
233 strbuf_release(&mtime_dir
);
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
);
242 mark_fsmonitor_invalid(&the_index
, active_cache
[pos
]);
244 active_cache
[pos
]->ce_flags
|= flag
;
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
;
255 static int remove_one_path(const char *path
)
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
);
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
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
)
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))
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
);
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
);
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
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 */
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)
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? */
353 while (pos
< active_nr
) {
354 const struct cache_entry
*ce
= active_cache
[pos
++];
356 if (strncmp(ce
->name
, path
, len
))
358 if (ce
->name
[len
] > '/')
360 if (ce
->name
[len
] < '/')
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
);
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
)
378 const struct cache_entry
*ce
;
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
);
399 * First things first: get the stat information, to decide
400 * what to do about the pathname!
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
)
415 struct cache_entry
*ce
;
417 if (!verify_path(path
, mode
))
418 return error("Invalid path '%s'", 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?",
435 report("add '%s'", path
);
439 static void chmod_path(char flip
, const char *path
)
442 struct cache_entry
*ce
;
444 pos
= cache_name_pos(path
, strlen(path
));
447 ce
= active_cache
[pos
];
448 if (chmod_cache_entry(ce
, flip
) < 0)
451 report("chmod %cx '%s'", flip
, path
);
454 die("git update-index: cannot chmod %cx '%s'", flip
, path
);
457 static void update_one(const char *path
)
462 if (mark_valid_only
|| mark_skip_worktree_only
|| force_remove
||
465 else if (lstat(path
, &st
) < 0) {
468 } /* else stat is valid */
470 if (!verify_path(path
, st
.st_mode
)) {
471 fprintf(stderr
, "Ignoring path %s\n", path
);
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
);
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
);
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
);
491 if (remove_file_from_cache(path
))
492 die("git update-index: unable to remove %s", path
);
493 report("remove '%s'", path
);
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
) {
512 struct object_id oid
;
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.
534 ul
= strtoul(buf
.buf
, &ptr
, 8);
535 if (ptr
== buf
.buf
|| *ptr
!= ' '
536 || errno
|| (unsigned int) ul
!= ul
)
540 tab
= strchr(ptr
, '\t');
541 if (!tab
|| tab
- ptr
< hexsz
+ 1)
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 */
551 ptr
= tab
+ 1; /* point at the head of path */
554 if (get_oid_hex(tab
- hexsz
, &oid
) ||
555 tab
[-(hexsz
+ 1)] != ' ')
559 if (!nul_term_line
&& path_name
[0] == '"') {
561 if (unquote_c_style(&uq
, path_name
, NULL
)) {
562 die("git update-index: bad quoting of path name");
567 if (!verify_path(path_name
, mode
)) {
568 fprintf(stderr
, "Ignoring path %s\n", path_name
);
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",
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",
591 die("malformed index info %s", buf
.buf
);
593 strbuf_release(&buf
);
597 static const char * const update_index_usage
[] = {
598 N_("git update-index [<options>] [--] [<file>...]"),
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
)
610 struct object_id oid
;
611 struct cache_entry
*ce
;
613 if (get_tree_entry(the_repository
, ent
, path
, &oid
, &mode
)) {
615 error("%s: not in %s branch.", path
, which
);
618 if (!the_index
.sparse_index
&& mode
== S_IFDIR
) {
620 error("%s: not a blob in %s branch.", path
, which
);
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
);
633 static int unresolve_one(const char *path
)
635 int namelen
= strlen(path
);
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
);
644 pos
= unmerge_cache_entry_at(pos
);
645 if (pos
< active_nr
) {
646 const struct cache_entry
*ce
= active_cache
[pos
];
648 ce_namelen(ce
) == namelen
&&
649 !memcmp(ce
->name
, path
, namelen
))
652 /* no resolve-undo information; fall back */
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.
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
)) {
664 "%s: skipping still unmerged path.\n",
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
) {
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",
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
);
695 if (!add_cache_entry(ce_3
, ADD_CACHE_OK_TO_ADD
))
697 error("%s: cannot add their version to the index.", path
);
700 discard_cache_entry(ce_2
);
701 discard_cache_entry(ce_3
);
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");
715 static int do_unresolve(int ac
, const char **av
,
716 const char *prefix
, int prefix_length
)
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
);
735 static int do_reupdate(const char **paths
,
738 /* Read HEAD and run update-index on paths that are
739 * merged and already different between index and HEAD.
743 struct pathspec pathspec
;
745 parse_pathspec(&pathspec
, 0,
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.
755 for (pos
= 0; pos
< active_nr
; pos
++) {
756 const struct cache_entry
*ce
= active_cache
[pos
];
757 struct cache_entry
*old
= NULL
;
761 if (ce_stage(ce
) || !ce_path_match(&the_index
, ce
, &pathspec
, NULL
))
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
);
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.
786 path
= xstrdup(ce
->name
);
789 discard_cache_entry(old
);
790 if (save_nr
!= active_nr
)
793 clear_pathspec(&pathspec
);
797 struct refresh_params
{
802 static int refresh(struct refresh_params
*o
, unsigned int flag
)
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
;
821 static int refresh_callback(const struct option
*opt
,
822 const char *arg
, int unset
)
824 BUG_ON_OPT_NEG(unset
);
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
);
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\"");
848 static int resolve_undo_clear_callback(const struct option
*opt
,
849 const char *arg
, int unset
)
851 BUG_ON_OPT_NEG(unset
);
853 resolve_undo_clear();
857 static int parse_new_style_cacheinfo(const char *arg
,
859 struct object_id
*oid
,
870 ul
= strtoul(arg
, &endp
, 8);
871 if (errno
|| endp
== arg
|| *endp
!= ',' || (unsigned int) ul
!= ul
)
872 return -1; /* not a new-style cacheinfo */
875 if (parse_oid_hex(endp
, oid
, &p
) || *p
!= ',')
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
;
889 BUG_ON_OPT_NEG(unset
);
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
);
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
);
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
);
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
);
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
);
935 return error("option '%s' must be the last argument", opt
->long_name
);
936 *read_from_stdin
= 1;
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
);
950 /* consume remaining arguments. */
951 *has_errors
= do_unresolve(ctx
->argc
, ctx
->argv
,
952 prefix
, prefix
? strlen(prefix
) : 0);
954 active_cache_changed
= 0;
956 ctx
->argv
+= ctx
->argc
- 1;
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
);
971 /* consume remaining arguments. */
973 *has_errors
= do_reupdate(ctx
->argv
+ 1, prefix
);
975 active_cache_changed
= 0;
977 ctx
->argv
+= ctx
->argc
- 1;
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
};
992 int split_index
= -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"),
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"),
1016 OPT_CALLBACK_F(0, "refresh", &refresh_args
, NULL
,
1017 N_("refresh stat information"),
1018 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
,
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
,
1030 cacheinfo_callback
},
1031 OPT_CALLBACK_F(0, "chmod", &set_executable_bit
, "(+|-)x",
1032 N_("override the executable bit of the listed files"),
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
},
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);
1116 entries
= read_cache();
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
1133 begin_odb_transaction();
1135 if (parseopt_state
!= PARSE_OPT_DONE
)
1136 parseopt_state
= parse_options_step(&ctx
, options
,
1137 update_index_usage
);
1140 switch (parseopt_state
) {
1141 case PARSE_OPT_HELP
:
1142 case PARSE_OPT_ERROR
:
1144 case PARSE_OPT_COMPLETE
:
1146 case PARSE_OPT_NON_OPTION
:
1147 case PARSE_OPT_DONE
:
1149 const char *path
= ctx
.argv
[0];
1153 p
= prefix_path(prefix
, prefix_length
, path
);
1155 if (set_executable_bit
)
1156 chmod_path(set_executable_bit
, p
);
1162 case PARSE_OPT_UNKNOWN
:
1163 if (ctx
.argv
[0][1] == '-')
1164 error("unknown option '%s'", ctx
.argv
[0] + 2);
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
;
1190 while (getline_fn(&buf
, stdin
) != EOF
) {
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
);
1200 if (set_executable_bit
)
1201 chmod_path(set_executable_bit
, 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
;
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
:
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"));
1244 return !test_if_untracked_cache_is_supported();
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());
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
)
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
) {
1295 if (refresh_args
.flags
& REFRESH_QUIET
)
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;