2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
7 #define USE_THE_REPOSITORY_VARIABLE
8 #define DISABLE_SIGN_COMPARE_WARNINGS
11 #include "bulk-checkin.h"
13 #include "environment.h"
19 #include "cache-tree.h"
20 #include "tree-walk.h"
21 #include "object-file.h"
23 #include "resolve-undo.h"
24 #include "parse-options.h"
27 #include "read-cache.h"
29 #include "sparse-index.h"
30 #include "split-index.h"
32 #include "fsmonitor.h"
33 #include "write-or-die.h"
36 * Default to not allowing changes to the list of files. The
37 * tool doesn't actually care, but this makes it harder to add
38 * files to the revision control by mistake by doing something
39 * like "git update-index *" and suddenly having all the object
40 * files be revision controlled.
43 static int allow_remove
;
44 static int allow_replace
;
46 static int force_remove
;
48 static int mark_valid_only
;
49 static int mark_skip_worktree_only
;
50 static int mark_fsmonitor_only
;
51 static int ignore_skip_worktree_entries
;
54 static struct strbuf mtime_dir
= STRBUF_INIT
;
56 /* Untracked cache mode */
65 __attribute__((format (printf
, 1, 2)))
66 static void report(const char *fmt
, ...)
74 * It is possible, though unlikely, that a caller could use the verbose
75 * output to synchronize with addition of objects to the object
76 * database. The current implementation of ODB transactions leaves
77 * objects invisible while a transaction is active, so flush the
78 * transaction here before reporting a change made by update-index.
80 flush_odb_transaction();
87 static void remove_test_directory(void)
90 remove_dir_recursively(&mtime_dir
, 0);
93 static const char *get_mtime_path(const char *path
)
95 static struct strbuf sb
= STRBUF_INIT
;
97 strbuf_addf(&sb
, "%s/%s", mtime_dir
.buf
, path
);
101 static void xmkdir(const char *path
)
103 path
= get_mtime_path(path
);
104 if (mkdir(path
, 0700))
105 die_errno(_("failed to create directory %s"), path
);
108 static int xstat_mtime_dir(struct stat
*st
)
110 if (stat(mtime_dir
.buf
, st
))
111 die_errno(_("failed to stat %s"), mtime_dir
.buf
);
115 static int create_file(const char *path
)
118 path
= get_mtime_path(path
);
119 fd
= xopen(path
, O_CREAT
| O_RDWR
, 0644);
123 static void xunlink(const char *path
)
125 path
= get_mtime_path(path
);
127 die_errno(_("failed to delete file %s"), path
);
130 static void xrmdir(const char *path
)
132 path
= get_mtime_path(path
);
134 die_errno(_("failed to delete directory %s"), path
);
137 static void avoid_racy(void)
140 * not use if we could usleep(10) if USE_NSEC is defined. The
141 * field nsec could be there, but the OS could choose to
147 static int test_if_untracked_cache_is_supported(void)
150 struct stat_data base
;
154 strbuf_addstr(&mtime_dir
, "mtime-test-XXXXXX");
155 if (!mkdtemp(mtime_dir
.buf
))
156 die_errno("Could not make temporary directory");
159 fprintf(stderr
, _("Testing mtime in '%s' "), cwd
);
162 atexit(remove_test_directory
);
163 xstat_mtime_dir(&st
);
164 fill_stat_data(&base
, &st
);
168 fd
= create_file("newfile");
169 xstat_mtime_dir(&st
);
170 if (!match_stat_data(&base
, &st
)) {
173 fprintf_ln(stderr
,_("directory stat info does not "
174 "change after adding a new file"));
177 fill_stat_data(&base
, &st
);
182 xstat_mtime_dir(&st
);
183 if (!match_stat_data(&base
, &st
)) {
186 fprintf_ln(stderr
, _("directory stat info does not change "
187 "after adding a new directory"));
190 fill_stat_data(&base
, &st
);
194 write_or_die(fd
, "data", 4);
196 xstat_mtime_dir(&st
);
197 if (match_stat_data(&base
, &st
)) {
199 fprintf_ln(stderr
, _("directory stat info changes "
200 "after updating a file"));
206 close(create_file("new-dir/new"));
207 xstat_mtime_dir(&st
);
208 if (match_stat_data(&base
, &st
)) {
210 fprintf_ln(stderr
, _("directory stat info changes after "
211 "adding a file inside subdirectory"));
218 xstat_mtime_dir(&st
);
219 if (!match_stat_data(&base
, &st
)) {
221 fprintf_ln(stderr
, _("directory stat info does not "
222 "change after deleting a file"));
225 fill_stat_data(&base
, &st
);
229 xunlink("new-dir/new");
231 xstat_mtime_dir(&st
);
232 if (!match_stat_data(&base
, &st
)) {
234 fprintf_ln(stderr
, _("directory stat info does not "
235 "change after deleting a directory"));
239 if (rmdir(mtime_dir
.buf
))
240 die_errno(_("failed to delete directory %s"), mtime_dir
.buf
);
241 fprintf_ln(stderr
, _(" OK"));
245 strbuf_release(&mtime_dir
);
249 static int mark_ce_flags(const char *path
, int flag
, int mark
)
251 int namelen
= strlen(path
);
252 int pos
= index_name_pos(the_repository
->index
, path
, namelen
);
254 mark_fsmonitor_invalid(the_repository
->index
, the_repository
->index
->cache
[pos
]);
256 the_repository
->index
->cache
[pos
]->ce_flags
|= flag
;
258 the_repository
->index
->cache
[pos
]->ce_flags
&= ~flag
;
259 the_repository
->index
->cache
[pos
]->ce_flags
|= CE_UPDATE_IN_BASE
;
260 cache_tree_invalidate_path(the_repository
->index
, path
);
261 the_repository
->index
->cache_changed
|= CE_ENTRY_CHANGED
;
267 static int remove_one_path(const char *path
)
270 return error("%s: does not exist and --remove not passed", path
);
271 if (remove_file_from_index(the_repository
->index
, path
))
272 return error("%s: cannot remove from the index", path
);
277 * Handle a path that couldn't be lstat'ed. It's either:
278 * - missing file (ENOENT or ENOTDIR). That's ok if we're
279 * supposed to be removing it and the removal actually
281 * - permission error. That's never ok.
283 static int process_lstat_error(const char *path
, int err
)
285 if (is_missing_file_error(err
))
286 return remove_one_path(path
);
287 return error("lstat(\"%s\"): %s", path
, strerror(err
));
290 static int add_one_path(const struct cache_entry
*old
, const char *path
, int len
, struct stat
*st
)
293 struct cache_entry
*ce
;
295 /* Was the old index entry already up-to-date? */
296 if (old
&& !ce_stage(old
) && !ie_match_stat(the_repository
->index
, old
, st
, 0))
299 ce
= make_empty_cache_entry(the_repository
->index
, len
);
300 memcpy(ce
->name
, path
, len
);
301 ce
->ce_flags
= create_ce_flags(0);
302 ce
->ce_namelen
= len
;
303 fill_stat_cache_info(the_repository
->index
, ce
, st
);
304 ce
->ce_mode
= ce_mode_from_stat(old
, st
->st_mode
);
306 if (index_path(the_repository
->index
, &ce
->oid
, path
, st
,
307 info_only
? 0 : INDEX_WRITE_OBJECT
)) {
308 discard_cache_entry(ce
);
311 option
= allow_add
? ADD_CACHE_OK_TO_ADD
: 0;
312 option
|= allow_replace
? ADD_CACHE_OK_TO_REPLACE
: 0;
313 if (add_index_entry(the_repository
->index
, ce
, option
)) {
314 discard_cache_entry(ce
);
315 return error("%s: cannot add to the index - missing --add option?", path
);
321 * Handle a path that was a directory. Four cases:
323 * - it's already a gitlink in the index, and we keep it that
324 * way, and update it if we can (if we cannot find the HEAD,
325 * we're going to keep it unchanged in the index!)
327 * - it's a *file* in the index, in which case it should be
328 * removed as a file if removal is allowed, since it doesn't
329 * exist as such any more. If removal isn't allowed, it's
332 * (NOTE! This is old and arguably fairly strange behaviour.
333 * We might want to make this an error unconditionally, and
334 * use "--force-remove" if you actually want to force removal).
336 * - it used to exist as a subdirectory (ie multiple files with
337 * this particular prefix) in the index, in which case it's wrong
338 * to try to update it as a directory.
340 * - it doesn't exist at all in the index, but it is a valid
341 * git directory, and it should be *added* as a gitlink.
343 static int process_directory(const char *path
, int len
, struct stat
*st
)
345 struct object_id oid
;
346 int pos
= index_name_pos(the_repository
->index
, path
, len
);
348 /* Exact match: file or existing gitlink */
350 const struct cache_entry
*ce
= the_repository
->index
->cache
[pos
];
351 if (S_ISGITLINK(ce
->ce_mode
)) {
353 /* Do nothing to the index if there is no HEAD! */
354 if (repo_resolve_gitlink_ref(the_repository
, path
,
358 return add_one_path(ce
, path
, len
, st
);
360 /* Should this be an unconditional error? */
361 return remove_one_path(path
);
364 /* Inexact match: is there perhaps a subdirectory match? */
366 while (pos
< the_repository
->index
->cache_nr
) {
367 const struct cache_entry
*ce
= the_repository
->index
->cache
[pos
++];
369 if (strncmp(ce
->name
, path
, len
))
371 if (ce
->name
[len
] > '/')
373 if (ce
->name
[len
] < '/')
376 /* Subdirectory match - error out */
377 return error("%s: is a directory - add individual files instead", path
);
380 /* No match - should we add it as a gitlink? */
381 if (!repo_resolve_gitlink_ref(the_repository
, path
, "HEAD", &oid
))
382 return add_one_path(NULL
, path
, len
, st
);
385 return error("%s: is a directory - add files inside instead", path
);
388 static int process_path(const char *path
, struct stat
*st
, int stat_errno
)
391 const struct cache_entry
*ce
;
394 if (has_symlink_leading_path(path
, len
))
395 return error("'%s' is beyond a symbolic link", path
);
397 pos
= index_name_pos(the_repository
->index
, path
, len
);
398 ce
= pos
< 0 ? NULL
: the_repository
->index
->cache
[pos
];
399 if (ce
&& ce_skip_worktree(ce
)) {
401 * working directory version is assumed "good"
402 * so updating it does not make sense.
403 * On the other hand, removing it from index should work
405 if (!ignore_skip_worktree_entries
&& allow_remove
&&
406 remove_file_from_index(the_repository
->index
, path
))
407 return error("%s: cannot remove from the index", path
);
412 * First things first: get the stat information, to decide
413 * what to do about the pathname!
416 return process_lstat_error(path
, stat_errno
);
418 if (S_ISDIR(st
->st_mode
))
419 return process_directory(path
, len
, st
);
421 return add_one_path(ce
, path
, len
, st
);
424 static int add_cacheinfo(unsigned int mode
, const struct object_id
*oid
,
425 const char *path
, int stage
)
428 struct cache_entry
*ce
;
430 if (!verify_path(path
, mode
))
431 return error("Invalid path '%s'", path
);
434 ce
= make_empty_cache_entry(the_repository
->index
, len
);
436 oidcpy(&ce
->oid
, oid
);
437 memcpy(ce
->name
, path
, len
);
438 ce
->ce_flags
= create_ce_flags(stage
);
439 ce
->ce_namelen
= len
;
440 ce
->ce_mode
= create_ce_mode(mode
);
441 if (assume_unchanged
)
442 ce
->ce_flags
|= CE_VALID
;
443 option
= allow_add
? ADD_CACHE_OK_TO_ADD
: 0;
444 option
|= allow_replace
? ADD_CACHE_OK_TO_REPLACE
: 0;
445 if (add_index_entry(the_repository
->index
, ce
, option
))
446 return error("%s: cannot add to the index - missing --add option?",
448 report("add '%s'", path
);
452 static void chmod_path(char flip
, const char *path
)
455 struct cache_entry
*ce
;
457 pos
= index_name_pos(the_repository
->index
, path
, strlen(path
));
460 ce
= the_repository
->index
->cache
[pos
];
461 if (chmod_index_entry(the_repository
->index
, ce
, flip
) < 0)
464 report("chmod %cx '%s'", flip
, path
);
467 die("git update-index: cannot chmod %cx '%s'", flip
, path
);
470 static void update_one(const char *path
)
475 if (mark_valid_only
|| mark_skip_worktree_only
|| force_remove
||
478 else if (lstat(path
, &st
) < 0) {
481 } /* else stat is valid */
483 if (!verify_path(path
, st
.st_mode
)) {
484 fprintf(stderr
, "Ignoring path %s\n", path
);
487 if (mark_valid_only
) {
488 if (mark_ce_flags(path
, CE_VALID
, mark_valid_only
== MARK_FLAG
))
489 die("Unable to mark file %s", path
);
492 if (mark_skip_worktree_only
) {
493 if (mark_ce_flags(path
, CE_SKIP_WORKTREE
, mark_skip_worktree_only
== MARK_FLAG
))
494 die("Unable to mark file %s", path
);
497 if (mark_fsmonitor_only
) {
498 if (mark_ce_flags(path
, CE_FSMONITOR_VALID
, mark_fsmonitor_only
== MARK_FLAG
))
499 die("Unable to mark file %s", path
);
504 if (remove_file_from_index(the_repository
->index
, path
))
505 die("git update-index: unable to remove %s", path
);
506 report("remove '%s'", path
);
509 if (process_path(path
, &st
, stat_errno
))
510 die("Unable to process path %s", path
);
511 report("add '%s'", path
);
514 static void read_index_info(int nul_term_line
)
516 const int hexsz
= the_hash_algo
->hexsz
;
517 struct strbuf buf
= STRBUF_INIT
;
518 struct strbuf uq
= STRBUF_INIT
;
519 strbuf_getline_fn getline_fn
;
521 getline_fn
= nul_term_line
? strbuf_getline_nul
: strbuf_getline_lf
;
522 while (getline_fn(&buf
, stdin
) != EOF
) {
525 struct object_id oid
;
530 /* This reads lines formatted in one of three formats:
532 * (1) mode SP sha1 TAB path
533 * The first format is what "git apply --index-info"
534 * reports, and used to reconstruct a partial tree
535 * that is used for phony merge base tree when falling
536 * back on 3-way merge.
538 * (2) mode SP type SP sha1 TAB path
539 * The second format is to stuff "git ls-tree" output
540 * into the index file.
542 * (3) mode SP sha1 SP stage TAB path
543 * This format is to put higher order stages into the
544 * index file and matches "git ls-files --stage" output.
547 ul
= strtoul(buf
.buf
, &ptr
, 8);
548 if (ptr
== buf
.buf
|| *ptr
!= ' '
549 || errno
|| (unsigned int) ul
!= ul
)
553 tab
= strchr(ptr
, '\t');
554 if (!tab
|| tab
- ptr
< hexsz
+ 1)
557 if (tab
[-2] == ' ' && '0' <= tab
[-1] && tab
[-1] <= '3') {
558 stage
= tab
[-1] - '0';
559 ptr
= tab
+ 1; /* point at the head of path */
560 tab
= tab
- 2; /* point at tail of sha1 */
564 ptr
= tab
+ 1; /* point at the head of path */
567 if (get_oid_hex(tab
- hexsz
, &oid
) ||
568 tab
[-(hexsz
+ 1)] != ' ')
572 if (!nul_term_line
&& path_name
[0] == '"') {
574 if (unquote_c_style(&uq
, path_name
, NULL
)) {
575 die("git update-index: bad quoting of path name");
580 if (!verify_path(path_name
, mode
)) {
581 fprintf(stderr
, "Ignoring path %s\n", path_name
);
586 /* mode == 0 means there is no such path -- remove */
587 if (remove_file_from_index(the_repository
->index
, path_name
))
588 die("git update-index: unable to remove %s",
592 /* mode ' ' sha1 '\t' name
593 * ptr[-1] points at tab,
594 * ptr[-41] is at the beginning of sha1
596 ptr
[-(hexsz
+ 2)] = ptr
[-1] = 0;
597 if (add_cacheinfo(mode
, &oid
, path_name
, stage
))
598 die("git update-index: unable to update %s",
604 die("malformed index info %s", buf
.buf
);
606 strbuf_release(&buf
);
610 static const char * const update_index_usage
[] = {
611 N_("git update-index [<options>] [--] [<file>...]"),
615 static struct cache_entry
*read_one_ent(const char *which
,
616 struct object_id
*ent
, const char *path
,
617 int namelen
, int stage
)
620 struct object_id oid
;
621 struct cache_entry
*ce
;
623 if (get_tree_entry(the_repository
, ent
, path
, &oid
, &mode
)) {
625 error("%s: not in %s branch.", path
, which
);
628 if (!the_repository
->index
->sparse_index
&& mode
== S_IFDIR
) {
630 error("%s: not a blob in %s branch.", path
, which
);
633 ce
= make_empty_cache_entry(the_repository
->index
, namelen
);
635 oidcpy(&ce
->oid
, &oid
);
636 memcpy(ce
->name
, path
, namelen
);
637 ce
->ce_flags
= create_ce_flags(stage
);
638 ce
->ce_namelen
= namelen
;
639 ce
->ce_mode
= create_ce_mode(mode
);
643 static int unresolve_one(const char *path
)
645 struct string_list_item
*item
;
648 if (!the_repository
->index
->resolve_undo
)
650 item
= string_list_lookup(the_repository
->index
->resolve_undo
, path
);
652 return res
; /* no resolve-undo record for the path */
653 res
= unmerge_index_entry(the_repository
->index
, path
, item
->util
, 0);
654 FREE_AND_NULL(item
->util
);
658 static int do_unresolve(int ac
, const char **av
,
659 const char *prefix
, int prefix_length
)
664 for (i
= 1; i
< ac
; i
++) {
665 const char *arg
= av
[i
];
666 char *p
= prefix_path(prefix
, prefix_length
, arg
);
667 err
|= unresolve_one(p
);
673 static int do_reupdate(const char **paths
,
676 /* Read HEAD and run update-index on paths that are
677 * merged and already different between index and HEAD.
681 struct pathspec pathspec
;
682 struct object_id head_oid
;
684 parse_pathspec(&pathspec
, 0,
688 if (refs_read_ref(get_main_ref_store(the_repository
), "HEAD", &head_oid
))
689 /* If there is no HEAD, that means it is an initial
690 * commit. Update everything in the index.
694 for (pos
= 0; pos
< the_repository
->index
->cache_nr
; pos
++) {
695 const struct cache_entry
*ce
= the_repository
->index
->cache
[pos
];
696 struct cache_entry
*old
= NULL
;
700 if (ce_stage(ce
) || !ce_path_match(the_repository
->index
, ce
, &pathspec
, NULL
))
703 old
= read_one_ent(NULL
, &head_oid
,
704 ce
->name
, ce_namelen(ce
), 0);
705 if (old
&& ce
->ce_mode
== old
->ce_mode
&&
706 oideq(&ce
->oid
, &old
->oid
)) {
707 discard_cache_entry(old
);
708 continue; /* unchanged */
711 /* At this point, we know the contents of the sparse directory are
712 * modified with respect to HEAD, so we expand the index and restart
713 * to process each path individually
715 if (S_ISSPARSEDIR(ce
->ce_mode
)) {
716 ensure_full_index(the_repository
->index
);
720 /* Be careful. The working tree may not have the
721 * path anymore, in which case, under 'allow_remove',
722 * or worse yet 'allow_replace', active_nr may decrease.
724 save_nr
= the_repository
->index
->cache_nr
;
725 path
= xstrdup(ce
->name
);
728 discard_cache_entry(old
);
729 if (save_nr
!= the_repository
->index
->cache_nr
)
732 clear_pathspec(&pathspec
);
736 struct refresh_params
{
741 static int refresh(struct refresh_params
*o
, unsigned int flag
)
744 repo_read_index(the_repository
);
745 *o
->has_errors
|= refresh_index(the_repository
->index
, o
->flags
| flag
, NULL
,
747 if (has_racy_timestamp(the_repository
->index
)) {
749 * Even if nothing else has changed, updating the file
750 * increases the chance that racy timestamps become
751 * non-racy, helping future run-time performance.
752 * We do that even in case of "errors" returned by
753 * refresh_index() as these are no actual errors.
754 * cmd_status() does the same.
756 the_repository
->index
->cache_changed
|= SOMETHING_CHANGED
;
761 static int refresh_callback(const struct option
*opt
,
762 const char *arg
, int unset
)
764 BUG_ON_OPT_NEG(unset
);
766 return refresh(opt
->value
, 0);
769 static int really_refresh_callback(const struct option
*opt
,
770 const char *arg
, int unset
)
772 BUG_ON_OPT_NEG(unset
);
774 return refresh(opt
->value
, REFRESH_REALLY
);
777 static int chmod_callback(const struct option
*opt
,
778 const char *arg
, int unset
)
780 char *flip
= opt
->value
;
781 BUG_ON_OPT_NEG(unset
);
782 if ((arg
[0] != '-' && arg
[0] != '+') || arg
[1] != 'x' || arg
[2])
783 return error("option 'chmod' expects \"+x\" or \"-x\"");
788 static int resolve_undo_clear_callback(const struct option
*opt UNUSED
,
789 const char *arg
, int unset
)
791 BUG_ON_OPT_NEG(unset
);
793 resolve_undo_clear_index(the_repository
->index
);
797 static int parse_new_style_cacheinfo(const char *arg
,
799 struct object_id
*oid
,
810 ul
= strtoul(arg
, &endp
, 8);
811 if (errno
|| endp
== arg
|| *endp
!= ',' || (unsigned int) ul
!= ul
)
812 return -1; /* not a new-style cacheinfo */
815 if (parse_oid_hex(endp
, oid
, &p
) || *p
!= ',')
821 static enum parse_opt_result
cacheinfo_callback(
822 struct parse_opt_ctx_t
*ctx
, const struct option
*opt UNUSED
,
823 const char *arg
, int unset
)
825 struct object_id oid
;
829 BUG_ON_OPT_NEG(unset
);
832 if (!parse_new_style_cacheinfo(ctx
->argv
[1], &mode
, &oid
, &path
)) {
833 if (add_cacheinfo(mode
, &oid
, path
, 0))
834 die("git update-index: --cacheinfo cannot add %s", path
);
840 return error("option 'cacheinfo' expects <mode>,<sha1>,<path>");
841 if (strtoul_ui(*++ctx
->argv
, 8, &mode
) ||
842 get_oid_hex(*++ctx
->argv
, &oid
) ||
843 add_cacheinfo(mode
, &oid
, *++ctx
->argv
, 0))
844 die("git update-index: --cacheinfo cannot add %s", *ctx
->argv
);
849 static enum parse_opt_result
stdin_cacheinfo_callback(
850 struct parse_opt_ctx_t
*ctx
, const struct option
*opt
,
851 const char *arg
, int unset
)
853 int *nul_term_line
= opt
->value
;
855 BUG_ON_OPT_NEG(unset
);
859 return error("option '%s' must be the last argument", opt
->long_name
);
860 allow_add
= allow_replace
= allow_remove
= 1;
861 read_index_info(*nul_term_line
);
865 static enum parse_opt_result
stdin_callback(
866 struct parse_opt_ctx_t
*ctx
, const struct option
*opt
,
867 const char *arg
, int unset
)
869 int *read_from_stdin
= opt
->value
;
871 BUG_ON_OPT_NEG(unset
);
875 return error("option '%s' must be the last argument", opt
->long_name
);
876 *read_from_stdin
= 1;
880 static enum parse_opt_result
unresolve_callback(
881 struct parse_opt_ctx_t
*ctx
, const struct option
*opt
,
882 const char *arg
, int unset
)
884 int *has_errors
= opt
->value
;
885 const char *prefix
= startup_info
->prefix
;
887 BUG_ON_OPT_NEG(unset
);
890 /* consume remaining arguments. */
891 *has_errors
= do_unresolve(ctx
->argc
, ctx
->argv
,
892 prefix
, prefix
? strlen(prefix
) : 0);
894 the_repository
->index
->cache_changed
= 0;
896 ctx
->argv
+= ctx
->argc
- 1;
901 static enum parse_opt_result
reupdate_callback(
902 struct parse_opt_ctx_t
*ctx
, const struct option
*opt
,
903 const char *arg
, int unset
)
905 int *has_errors
= opt
->value
;
906 const char *prefix
= startup_info
->prefix
;
908 BUG_ON_OPT_NEG(unset
);
911 /* consume remaining arguments. */
913 *has_errors
= do_reupdate(ctx
->argv
+ 1, prefix
);
915 the_repository
->index
->cache_changed
= 0;
917 ctx
->argv
+= ctx
->argc
- 1;
922 int cmd_update_index(int argc
,
925 struct repository
*repo UNUSED
)
927 int newfd
, entries
, has_errors
= 0, nul_term_line
= 0;
928 enum uc_mode untracked_cache
= UC_UNSPECIFIED
;
929 int read_from_stdin
= 0;
930 int prefix_length
= prefix
? strlen(prefix
) : 0;
931 int preferred_index_format
= 0;
932 char set_executable_bit
= 0;
933 struct refresh_params refresh_args
= {0, &has_errors
};
935 int split_index
= -1;
938 struct lock_file lock_file
= LOCK_INIT
;
939 struct parse_opt_ctx_t ctx
;
940 strbuf_getline_fn getline_fn
;
941 int parseopt_state
= PARSE_OPT_UNKNOWN
;
942 struct repository
*r
= the_repository
;
943 struct option options
[] = {
944 OPT_BIT('q', NULL
, &refresh_args
.flags
,
945 N_("continue refresh even when index needs update"),
947 OPT_BIT(0, "ignore-submodules", &refresh_args
.flags
,
948 N_("refresh: ignore submodules"),
949 REFRESH_IGNORE_SUBMODULES
),
950 OPT_SET_INT(0, "add", &allow_add
,
951 N_("do not ignore new files"), 1),
952 OPT_SET_INT(0, "replace", &allow_replace
,
953 N_("let files replace directories and vice-versa"), 1),
954 OPT_SET_INT(0, "remove", &allow_remove
,
955 N_("notice files missing from worktree"), 1),
956 OPT_BIT(0, "unmerged", &refresh_args
.flags
,
957 N_("refresh even if index contains unmerged entries"),
959 OPT_CALLBACK_F(0, "refresh", &refresh_args
, NULL
,
960 N_("refresh stat information"),
961 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
,
963 OPT_CALLBACK_F(0, "really-refresh", &refresh_args
, NULL
,
964 N_("like --refresh, but ignore assume-unchanged setting"),
965 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
,
966 really_refresh_callback
),
968 .type
= OPTION_LOWLEVEL_CALLBACK
,
969 .long_name
= "cacheinfo",
970 .argh
= N_("<mode>,<object>,<path>"),
971 .help
= N_("add the specified entry to the index"),
972 .flags
= PARSE_OPT_NOARG
| /* disallow --cacheinfo=<mode> form */
973 PARSE_OPT_NONEG
| PARSE_OPT_LITERAL_ARGHELP
,
974 .ll_callback
= cacheinfo_callback
,
976 OPT_CALLBACK_F(0, "chmod", &set_executable_bit
, "(+|-)x",
977 N_("override the executable bit of the listed files"),
981 .type
= OPTION_SET_INT
,
982 .long_name
= "assume-unchanged",
983 .value
= &mark_valid_only
,
984 .help
= N_("mark files as \"not changing\""),
985 .flags
= PARSE_OPT_NOARG
| PARSE_OPT_NONEG
,
989 .type
= OPTION_SET_INT
,
990 .long_name
= "no-assume-unchanged",
991 .value
= &mark_valid_only
,
992 .help
= N_("clear assumed-unchanged bit"),
993 .flags
= PARSE_OPT_NOARG
| PARSE_OPT_NONEG
,
994 .defval
= UNMARK_FLAG
,
997 .type
= OPTION_SET_INT
,
998 .long_name
= "skip-worktree",
999 .value
= &mark_skip_worktree_only
,
1000 .help
= N_("mark files as \"index-only\""),
1001 .flags
= PARSE_OPT_NOARG
| PARSE_OPT_NONEG
,
1002 .defval
= MARK_FLAG
,
1005 .type
= OPTION_SET_INT
,
1006 .long_name
= "no-skip-worktree",
1007 .value
= &mark_skip_worktree_only
,
1008 .help
= N_("clear skip-worktree bit"),
1009 .flags
= PARSE_OPT_NOARG
| PARSE_OPT_NONEG
,
1010 .defval
= UNMARK_FLAG
,
1012 OPT_BOOL(0, "ignore-skip-worktree-entries", &ignore_skip_worktree_entries
,
1013 N_("do not touch index-only entries")),
1014 OPT_SET_INT(0, "info-only", &info_only
,
1015 N_("add to index only; do not add content to object database"), 1),
1016 OPT_SET_INT(0, "force-remove", &force_remove
,
1017 N_("remove named paths even if present in worktree"), 1),
1018 OPT_BOOL('z', NULL
, &nul_term_line
,
1019 N_("with --stdin: input lines are terminated by null bytes")),
1021 .type
= OPTION_LOWLEVEL_CALLBACK
,
1022 .long_name
= "stdin",
1023 .value
= &read_from_stdin
,
1024 .help
= N_("read list of paths to be updated from standard input"),
1025 .flags
= PARSE_OPT_NONEG
| PARSE_OPT_NOARG
,
1026 .ll_callback
= stdin_callback
,
1029 .type
= OPTION_LOWLEVEL_CALLBACK
,
1030 .long_name
= "index-info",
1031 .value
= &nul_term_line
,
1032 .help
= N_("add entries from standard input to the index"),
1033 .flags
= PARSE_OPT_NONEG
| PARSE_OPT_NOARG
,
1034 .ll_callback
= stdin_cacheinfo_callback
,
1037 .type
= OPTION_LOWLEVEL_CALLBACK
,
1038 .long_name
= "unresolve",
1039 .value
= &has_errors
,
1040 .help
= N_("repopulate stages #2 and #3 for the listed paths"),
1041 .flags
= PARSE_OPT_NONEG
| PARSE_OPT_NOARG
,
1042 .ll_callback
= unresolve_callback
,
1045 .type
= OPTION_LOWLEVEL_CALLBACK
,
1047 .long_name
= "again",
1048 .value
= &has_errors
,
1049 .help
= N_("only update entries that differ from HEAD"),
1050 .flags
= PARSE_OPT_NONEG
| PARSE_OPT_NOARG
,
1051 .ll_callback
= reupdate_callback
,
1053 OPT_BIT(0, "ignore-missing", &refresh_args
.flags
,
1054 N_("ignore files missing from worktree"),
1055 REFRESH_IGNORE_MISSING
),
1056 OPT_SET_INT(0, "verbose", &verbose
,
1057 N_("report actions to standard output"), 1),
1058 OPT_CALLBACK_F(0, "clear-resolve-undo", NULL
, NULL
,
1059 N_("(for porcelains) forget saved unresolved conflicts"),
1060 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
,
1061 resolve_undo_clear_callback
),
1062 OPT_INTEGER(0, "index-version", &preferred_index_format
,
1063 N_("write index in this format")),
1064 OPT_SET_INT(0, "show-index-version", &preferred_index_format
,
1065 N_("report on-disk index format version"), -1),
1066 OPT_BOOL(0, "split-index", &split_index
,
1067 N_("enable or disable split index")),
1068 OPT_BOOL(0, "untracked-cache", &untracked_cache
,
1069 N_("enable/disable untracked cache")),
1070 OPT_SET_INT(0, "test-untracked-cache", &untracked_cache
,
1071 N_("test if the filesystem supports untracked cache"), UC_TEST
),
1072 OPT_SET_INT(0, "force-untracked-cache", &untracked_cache
,
1073 N_("enable untracked cache without testing the filesystem"), UC_FORCE
),
1074 OPT_SET_INT(0, "force-write-index", &force_write
,
1075 N_("write out the index even if is not flagged as changed"), 1),
1076 OPT_BOOL(0, "fsmonitor", &fsmonitor
,
1077 N_("enable or disable file system monitor")),
1079 .type
= OPTION_SET_INT
,
1080 .long_name
= "fsmonitor-valid",
1081 .value
= &mark_fsmonitor_only
,
1082 .help
= N_("mark files as fsmonitor valid"),
1083 .flags
= PARSE_OPT_NOARG
| PARSE_OPT_NONEG
,
1084 .defval
= MARK_FLAG
,
1087 .type
= OPTION_SET_INT
,
1088 .long_name
= "no-fsmonitor-valid",
1089 .value
= &mark_fsmonitor_only
,
1090 .help
= N_("clear fsmonitor valid bit"),
1091 .flags
= PARSE_OPT_NOARG
| PARSE_OPT_NONEG
,
1092 .defval
= UNMARK_FLAG
,
1097 show_usage_with_options_if_asked(argc
, argv
,
1098 update_index_usage
, options
);
1100 git_config(git_default_config
, NULL
);
1102 prepare_repo_settings(r
);
1103 the_repository
->settings
.command_requires_full_index
= 0;
1105 /* we will diagnose later if it turns out that we need to update it */
1106 newfd
= repo_hold_locked_index(the_repository
, &lock_file
, 0);
1110 entries
= repo_read_index(the_repository
);
1112 die("cache corrupted");
1114 the_repository
->index
->updated_skipworktree
= 1;
1117 * Custom copy of parse_options() because we want to handle
1118 * filename arguments as they come.
1120 parse_options_start(&ctx
, argc
, argv
, prefix
,
1121 options
, PARSE_OPT_STOP_AT_NON_OPTION
);
1124 * Allow the object layer to optimize adding multiple objects in
1127 begin_odb_transaction();
1129 if (parseopt_state
!= PARSE_OPT_DONE
)
1130 parseopt_state
= parse_options_step(&ctx
, options
,
1131 update_index_usage
);
1134 switch (parseopt_state
) {
1135 case PARSE_OPT_HELP
:
1136 case PARSE_OPT_ERROR
:
1138 case PARSE_OPT_COMPLETE
:
1140 case PARSE_OPT_NON_OPTION
:
1141 case PARSE_OPT_DONE
:
1143 const char *path
= ctx
.argv
[0];
1147 p
= prefix_path(prefix
, prefix_length
, path
);
1149 if (set_executable_bit
)
1150 chmod_path(set_executable_bit
, p
);
1156 case PARSE_OPT_UNKNOWN
:
1157 if (ctx
.argv
[0][1] == '-')
1158 error("unknown option '%s'", ctx
.argv
[0] + 2);
1160 error("unknown switch '%c'", *ctx
.opt
);
1161 usage_with_options(update_index_usage
, options
);
1164 argc
= parse_options_end(&ctx
);
1166 getline_fn
= nul_term_line
? strbuf_getline_nul
: strbuf_getline_lf
;
1167 if (preferred_index_format
) {
1168 if (preferred_index_format
< 0) {
1169 printf(_("%d\n"), the_repository
->index
->version
);
1170 } else if (preferred_index_format
< INDEX_FORMAT_LB
||
1171 INDEX_FORMAT_UB
< preferred_index_format
) {
1172 die("index-version %d not in range: %d..%d",
1173 preferred_index_format
,
1174 INDEX_FORMAT_LB
, INDEX_FORMAT_UB
);
1176 if (the_repository
->index
->version
!= preferred_index_format
)
1177 the_repository
->index
->cache_changed
|= SOMETHING_CHANGED
;
1178 report(_("index-version: was %d, set to %d"),
1179 the_repository
->index
->version
, preferred_index_format
);
1180 the_repository
->index
->version
= preferred_index_format
;
1184 if (read_from_stdin
) {
1185 struct strbuf buf
= STRBUF_INIT
;
1186 struct strbuf unquoted
= STRBUF_INIT
;
1189 while (getline_fn(&buf
, stdin
) != EOF
) {
1191 if (!nul_term_line
&& buf
.buf
[0] == '"') {
1192 strbuf_reset(&unquoted
);
1193 if (unquote_c_style(&unquoted
, buf
.buf
, NULL
))
1194 die("line is badly quoted");
1195 strbuf_swap(&buf
, &unquoted
);
1197 p
= prefix_path(prefix
, prefix_length
, buf
.buf
);
1199 if (set_executable_bit
)
1200 chmod_path(set_executable_bit
, p
);
1203 strbuf_release(&unquoted
);
1204 strbuf_release(&buf
);
1208 * By now we have added all of the new objects
1210 end_odb_transaction();
1212 if (split_index
> 0) {
1213 if (repo_config_get_split_index(the_repository
) == 0)
1214 warning(_("core.splitIndex is set to false; "
1215 "remove or change it, if you really want to "
1216 "enable split index"));
1217 if (the_repository
->index
->split_index
)
1218 the_repository
->index
->cache_changed
|= SPLIT_INDEX_ORDERED
;
1220 add_split_index(the_repository
->index
);
1221 } else if (!split_index
) {
1222 if (repo_config_get_split_index(the_repository
) == 1)
1223 warning(_("core.splitIndex is set to true; "
1224 "remove or change it, if you really want to "
1225 "disable split index"));
1226 remove_split_index(the_repository
->index
);
1229 prepare_repo_settings(r
);
1230 switch (untracked_cache
) {
1231 case UC_UNSPECIFIED
:
1234 if (r
->settings
.core_untracked_cache
== UNTRACKED_CACHE_WRITE
)
1235 warning(_("core.untrackedCache is set to true; "
1236 "remove or change it, if you really want to "
1237 "disable the untracked cache"));
1238 remove_untracked_cache(the_repository
->index
);
1239 report(_("Untracked cache disabled"));
1243 return !test_if_untracked_cache_is_supported();
1246 if (r
->settings
.core_untracked_cache
== UNTRACKED_CACHE_REMOVE
)
1247 warning(_("core.untrackedCache is set to false; "
1248 "remove or change it, if you really want to "
1249 "enable the untracked cache"));
1250 add_untracked_cache(the_repository
->index
);
1251 report(_("Untracked cache enabled for '%s'"), repo_get_work_tree(the_repository
));
1254 BUG("bad untracked_cache value: %d", untracked_cache
);
1257 if (fsmonitor
> 0) {
1258 enum fsmonitor_mode fsm_mode
= fsm_settings__get_mode(r
);
1259 enum fsmonitor_reason reason
= fsm_settings__get_reason(r
);
1262 * The user wants to turn on FSMonitor using the command
1263 * line argument. (We don't know (or care) whether that
1264 * is the IPC or HOOK version.)
1266 * Use one of the __get routines to force load the FSMonitor
1267 * config settings into the repo-settings. That will detect
1268 * whether the file system is compatible so that we can stop
1269 * here with a nice error message.
1271 if (reason
> FSMONITOR_REASON_OK
)
1273 fsm_settings__get_incompatible_msg(r
, reason
));
1275 if (fsm_mode
== FSMONITOR_MODE_DISABLED
) {
1276 warning(_("core.fsmonitor is unset; "
1277 "set it if you really want to "
1278 "enable fsmonitor"));
1280 add_fsmonitor(the_repository
->index
);
1281 report(_("fsmonitor enabled"));
1282 } else if (!fsmonitor
) {
1283 enum fsmonitor_mode fsm_mode
= fsm_settings__get_mode(r
);
1284 if (fsm_mode
> FSMONITOR_MODE_DISABLED
)
1285 warning(_("core.fsmonitor is set; "
1286 "remove it if you really want to "
1287 "disable fsmonitor"));
1288 remove_fsmonitor(the_repository
->index
);
1289 report(_("fsmonitor disabled"));
1292 if (the_repository
->index
->cache_changed
|| force_write
) {
1294 if (refresh_args
.flags
& REFRESH_QUIET
)
1296 unable_to_lock_die(repo_get_index_file(the_repository
), lock_error
);
1298 if (write_locked_index(the_repository
->index
, &lock_file
, COMMIT_LOCK
))
1299 die("Unable to write new index file");
1302 rollback_lock_file(&lock_file
);
1304 return has_errors
? 1 : 0;