in builtin/update-index.c [922:1305]
int cmd_update_index(int argc,
const char **argv,
const char *prefix,
struct repository *repo UNUSED)
{
int newfd, entries, has_errors = 0, nul_term_line = 0;
enum uc_mode untracked_cache = UC_UNSPECIFIED;
int read_from_stdin = 0;
int prefix_length = prefix ? strlen(prefix) : 0;
int preferred_index_format = 0;
char set_executable_bit = 0;
struct refresh_params refresh_args = {0, &has_errors};
int lock_error = 0;
int split_index = -1;
int force_write = 0;
int fsmonitor = -1;
struct lock_file lock_file = LOCK_INIT;
struct parse_opt_ctx_t ctx;
strbuf_getline_fn getline_fn;
int parseopt_state = PARSE_OPT_UNKNOWN;
struct repository *r = the_repository;
struct option options[] = {
OPT_BIT('q', NULL, &refresh_args.flags,
N_("continue refresh even when index needs update"),
REFRESH_QUIET),
OPT_BIT(0, "ignore-submodules", &refresh_args.flags,
N_("refresh: ignore submodules"),
REFRESH_IGNORE_SUBMODULES),
OPT_SET_INT(0, "add", &allow_add,
N_("do not ignore new files"), 1),
OPT_SET_INT(0, "replace", &allow_replace,
N_("let files replace directories and vice-versa"), 1),
OPT_SET_INT(0, "remove", &allow_remove,
N_("notice files missing from worktree"), 1),
OPT_BIT(0, "unmerged", &refresh_args.flags,
N_("refresh even if index contains unmerged entries"),
REFRESH_UNMERGED),
OPT_CALLBACK_F(0, "refresh", &refresh_args, NULL,
N_("refresh stat information"),
PARSE_OPT_NOARG | PARSE_OPT_NONEG,
refresh_callback),
OPT_CALLBACK_F(0, "really-refresh", &refresh_args, NULL,
N_("like --refresh, but ignore assume-unchanged setting"),
PARSE_OPT_NOARG | PARSE_OPT_NONEG,
really_refresh_callback),
{
.type = OPTION_LOWLEVEL_CALLBACK,
.long_name = "cacheinfo",
.argh = N_("<mode>,<object>,<path>"),
.help = N_("add the specified entry to the index"),
.flags = PARSE_OPT_NOARG | /* disallow --cacheinfo=<mode> form */
PARSE_OPT_NONEG | PARSE_OPT_LITERAL_ARGHELP,
.ll_callback = cacheinfo_callback,
},
OPT_CALLBACK_F(0, "chmod", &set_executable_bit, "(+|-)x",
N_("override the executable bit of the listed files"),
PARSE_OPT_NONEG,
chmod_callback),
{
.type = OPTION_SET_INT,
.long_name = "assume-unchanged",
.value = &mark_valid_only,
.help = N_("mark files as \"not changing\""),
.flags = PARSE_OPT_NOARG | PARSE_OPT_NONEG,
.defval = MARK_FLAG,
},
{
.type = OPTION_SET_INT,
.long_name = "no-assume-unchanged",
.value = &mark_valid_only,
.help = N_("clear assumed-unchanged bit"),
.flags = PARSE_OPT_NOARG | PARSE_OPT_NONEG,
.defval = UNMARK_FLAG,
},
{
.type = OPTION_SET_INT,
.long_name = "skip-worktree",
.value = &mark_skip_worktree_only,
.help = N_("mark files as \"index-only\""),
.flags = PARSE_OPT_NOARG | PARSE_OPT_NONEG,
.defval = MARK_FLAG,
},
{
.type = OPTION_SET_INT,
.long_name = "no-skip-worktree",
.value = &mark_skip_worktree_only,
.help = N_("clear skip-worktree bit"),
.flags = PARSE_OPT_NOARG | PARSE_OPT_NONEG,
.defval = UNMARK_FLAG,
},
OPT_BOOL(0, "ignore-skip-worktree-entries", &ignore_skip_worktree_entries,
N_("do not touch index-only entries")),
OPT_SET_INT(0, "info-only", &info_only,
N_("add to index only; do not add content to object database"), 1),
OPT_SET_INT(0, "force-remove", &force_remove,
N_("remove named paths even if present in worktree"), 1),
OPT_BOOL('z', NULL, &nul_term_line,
N_("with --stdin: input lines are terminated by null bytes")),
{
.type = OPTION_LOWLEVEL_CALLBACK,
.long_name = "stdin",
.value = &read_from_stdin,
.help = N_("read list of paths to be updated from standard input"),
.flags = PARSE_OPT_NONEG | PARSE_OPT_NOARG,
.ll_callback = stdin_callback,
},
{
.type = OPTION_LOWLEVEL_CALLBACK,
.long_name = "index-info",
.value = &nul_term_line,
.help = N_("add entries from standard input to the index"),
.flags = PARSE_OPT_NONEG | PARSE_OPT_NOARG,
.ll_callback = stdin_cacheinfo_callback,
},
{
.type = OPTION_LOWLEVEL_CALLBACK,
.long_name = "unresolve",
.value = &has_errors,
.help = N_("repopulate stages #2 and #3 for the listed paths"),
.flags = PARSE_OPT_NONEG | PARSE_OPT_NOARG,
.ll_callback = unresolve_callback,
},
{
.type = OPTION_LOWLEVEL_CALLBACK,
.short_name = 'g',
.long_name = "again",
.value = &has_errors,
.help = N_("only update entries that differ from HEAD"),
.flags = PARSE_OPT_NONEG | PARSE_OPT_NOARG,
.ll_callback = reupdate_callback,
},
OPT_BIT(0, "ignore-missing", &refresh_args.flags,
N_("ignore files missing from worktree"),
REFRESH_IGNORE_MISSING),
OPT_SET_INT(0, "verbose", &verbose,
N_("report actions to standard output"), 1),
OPT_CALLBACK_F(0, "clear-resolve-undo", NULL, NULL,
N_("(for porcelains) forget saved unresolved conflicts"),
PARSE_OPT_NOARG | PARSE_OPT_NONEG,
resolve_undo_clear_callback),
OPT_INTEGER(0, "index-version", &preferred_index_format,
N_("write index in this format")),
OPT_SET_INT(0, "show-index-version", &preferred_index_format,
N_("report on-disk index format version"), -1),
OPT_BOOL(0, "split-index", &split_index,
N_("enable or disable split index")),
OPT_BOOL(0, "untracked-cache", &untracked_cache,
N_("enable/disable untracked cache")),
OPT_SET_INT(0, "test-untracked-cache", &untracked_cache,
N_("test if the filesystem supports untracked cache"), UC_TEST),
OPT_SET_INT(0, "force-untracked-cache", &untracked_cache,
N_("enable untracked cache without testing the filesystem"), UC_FORCE),
OPT_SET_INT(0, "force-write-index", &force_write,
N_("write out the index even if is not flagged as changed"), 1),
OPT_BOOL(0, "fsmonitor", &fsmonitor,
N_("enable or disable file system monitor")),
{
.type = OPTION_SET_INT,
.long_name = "fsmonitor-valid",
.value = &mark_fsmonitor_only,
.help = N_("mark files as fsmonitor valid"),
.flags = PARSE_OPT_NOARG | PARSE_OPT_NONEG,
.defval = MARK_FLAG,
},
{
.type = OPTION_SET_INT,
.long_name = "no-fsmonitor-valid",
.value = &mark_fsmonitor_only,
.help = N_("clear fsmonitor valid bit"),
.flags = PARSE_OPT_NOARG | PARSE_OPT_NONEG,
.defval = UNMARK_FLAG,
},
OPT_END()
};
show_usage_with_options_if_asked(argc, argv,
update_index_usage, options);
git_config(git_default_config, NULL);
prepare_repo_settings(r);
the_repository->settings.command_requires_full_index = 0;
/* we will diagnose later if it turns out that we need to update it */
newfd = repo_hold_locked_index(the_repository, &lock_file, 0);
if (newfd < 0)
lock_error = errno;
entries = repo_read_index(the_repository);
if (entries < 0)
die("cache corrupted");
the_repository->index->updated_skipworktree = 1;
/*
* Custom copy of parse_options() because we want to handle
* filename arguments as they come.
*/
parse_options_start(&ctx, argc, argv, prefix,
options, PARSE_OPT_STOP_AT_NON_OPTION);
/*
* Allow the object layer to optimize adding multiple objects in
* a batch.
*/
begin_odb_transaction();
while (ctx.argc) {
if (parseopt_state != PARSE_OPT_DONE)
parseopt_state = parse_options_step(&ctx, options,
update_index_usage);
if (!ctx.argc)
break;
switch (parseopt_state) {
case PARSE_OPT_HELP:
case PARSE_OPT_ERROR:
exit(129);
case PARSE_OPT_COMPLETE:
exit(0);
case PARSE_OPT_NON_OPTION:
case PARSE_OPT_DONE:
{
const char *path = ctx.argv[0];
char *p;
setup_work_tree();
p = prefix_path(prefix, prefix_length, path);
update_one(p);
if (set_executable_bit)
chmod_path(set_executable_bit, p);
free(p);
ctx.argc--;
ctx.argv++;
break;
}
case PARSE_OPT_UNKNOWN:
if (ctx.argv[0][1] == '-')
error("unknown option '%s'", ctx.argv[0] + 2);
else
error("unknown switch '%c'", *ctx.opt);
usage_with_options(update_index_usage, options);
}
}
argc = parse_options_end(&ctx);
getline_fn = nul_term_line ? strbuf_getline_nul : strbuf_getline_lf;
if (preferred_index_format) {
if (preferred_index_format < 0) {
printf(_("%d\n"), the_repository->index->version);
} else if (preferred_index_format < INDEX_FORMAT_LB ||
INDEX_FORMAT_UB < preferred_index_format) {
die("index-version %d not in range: %d..%d",
preferred_index_format,
INDEX_FORMAT_LB, INDEX_FORMAT_UB);
} else {
if (the_repository->index->version != preferred_index_format)
the_repository->index->cache_changed |= SOMETHING_CHANGED;
report(_("index-version: was %d, set to %d"),
the_repository->index->version, preferred_index_format);
the_repository->index->version = preferred_index_format;
}
}
if (read_from_stdin) {
struct strbuf buf = STRBUF_INIT;
struct strbuf unquoted = STRBUF_INIT;
setup_work_tree();
while (getline_fn(&buf, stdin) != EOF) {
char *p;
if (!nul_term_line && buf.buf[0] == '"') {
strbuf_reset(&unquoted);
if (unquote_c_style(&unquoted, buf.buf, NULL))
die("line is badly quoted");
strbuf_swap(&buf, &unquoted);
}
p = prefix_path(prefix, prefix_length, buf.buf);
update_one(p);
if (set_executable_bit)
chmod_path(set_executable_bit, p);
free(p);
}
strbuf_release(&unquoted);
strbuf_release(&buf);
}
/*
* By now we have added all of the new objects
*/
end_odb_transaction();
if (split_index > 0) {
if (repo_config_get_split_index(the_repository) == 0)
warning(_("core.splitIndex is set to false; "
"remove or change it, if you really want to "
"enable split index"));
if (the_repository->index->split_index)
the_repository->index->cache_changed |= SPLIT_INDEX_ORDERED;
else
add_split_index(the_repository->index);
} else if (!split_index) {
if (repo_config_get_split_index(the_repository) == 1)
warning(_("core.splitIndex is set to true; "
"remove or change it, if you really want to "
"disable split index"));
remove_split_index(the_repository->index);
}
prepare_repo_settings(r);
switch (untracked_cache) {
case UC_UNSPECIFIED:
break;
case UC_DISABLE:
if (r->settings.core_untracked_cache == UNTRACKED_CACHE_WRITE)
warning(_("core.untrackedCache is set to true; "
"remove or change it, if you really want to "
"disable the untracked cache"));
remove_untracked_cache(the_repository->index);
report(_("Untracked cache disabled"));
break;
case UC_TEST:
setup_work_tree();
return !test_if_untracked_cache_is_supported();
case UC_ENABLE:
case UC_FORCE:
if (r->settings.core_untracked_cache == UNTRACKED_CACHE_REMOVE)
warning(_("core.untrackedCache is set to false; "
"remove or change it, if you really want to "
"enable the untracked cache"));
add_untracked_cache(the_repository->index);
report(_("Untracked cache enabled for '%s'"), repo_get_work_tree(the_repository));
break;
default:
BUG("bad untracked_cache value: %d", untracked_cache);
}
if (fsmonitor > 0) {
enum fsmonitor_mode fsm_mode = fsm_settings__get_mode(r);
enum fsmonitor_reason reason = fsm_settings__get_reason(r);
/*
* The user wants to turn on FSMonitor using the command
* line argument. (We don't know (or care) whether that
* is the IPC or HOOK version.)
*
* Use one of the __get routines to force load the FSMonitor
* config settings into the repo-settings. That will detect
* whether the file system is compatible so that we can stop
* here with a nice error message.
*/
if (reason > FSMONITOR_REASON_OK)
die("%s",
fsm_settings__get_incompatible_msg(r, reason));
if (fsm_mode == FSMONITOR_MODE_DISABLED) {
warning(_("core.fsmonitor is unset; "
"set it if you really want to "
"enable fsmonitor"));
}
add_fsmonitor(the_repository->index);
report(_("fsmonitor enabled"));
} else if (!fsmonitor) {
enum fsmonitor_mode fsm_mode = fsm_settings__get_mode(r);
if (fsm_mode > FSMONITOR_MODE_DISABLED)
warning(_("core.fsmonitor is set; "
"remove it if you really want to "
"disable fsmonitor"));
remove_fsmonitor(the_repository->index);
report(_("fsmonitor disabled"));
}
if (the_repository->index->cache_changed || force_write) {
if (newfd < 0) {
if (refresh_args.flags & REFRESH_QUIET)
exit(128);
unable_to_lock_die(repo_get_index_file(the_repository), lock_error);
}
if (write_locked_index(the_repository->index, &lock_file, COMMIT_LOCK))
die("Unable to write new index file");
}
rollback_lock_file(&lock_file);
return has_errors ? 1 : 0;
}