[PATCH v4 04/11] built-in add -i: refresh the index before running `status`
From: Johannes Schindelin via GitGitGadget <hidden>
Date: 2019-08-27 12:58:05
Subsystem:
the rest · Maintainer:
Linus Torvalds
From: Johannes Schindelin <redacted> This is what the Perl version does, and therefore it is what the built-in version should do, too. Signed-off-by: Johannes Schindelin <redacted> --- add-interactive.c | 4 +++- repository.c | 19 +++++++++++++++++++ repository.h | 7 +++++++ 3 files changed, 29 insertions(+), 1 deletion(-)
diff --git a/add-interactive.c b/add-interactive.c
index d64206ba1c..427abe505e 100644
--- a/add-interactive.c
+++ b/add-interactive.c@@ -259,7 +259,9 @@ int run_add_i(struct repository *r, const struct pathspec *ps) _("staged"), _("unstaged"), _("path")); opts.header = header.buf; - res = run_status(r, ps, &files, &opts); + repo_refresh_and_write_index(r, REFRESH_QUIET, 1); + if (run_status(r, ps, &files, &opts) < 0) + res = -1; release_file_list(&files); strbuf_release(&print_file_item_data.buf);
diff --git a/repository.c b/repository.c
index 682c239fe3..def35c40fc 100644
--- a/repository.c
+++ b/repository.c@@ -275,3 +275,22 @@ int repo_hold_locked_index(struct repository *repo, BUG("the repo hasn't been setup"); return hold_lock_file_for_update(lf, repo->index_file, flags); } + +int repo_refresh_and_write_index(struct repository *r, + unsigned int flags, int gentle) +{ + struct lock_file lock_file = LOCK_INIT; + int fd; + + if (repo_read_index_preload(r, NULL, 0) < 0) + return error(_("could not read index")); + fd = repo_hold_locked_index(r, &lock_file, 0); + if (!gentle && fd < 0) + return error(_("could not lock index for writing")); + refresh_index(r->index, flags, NULL, NULL, NULL); + if (0 <= fd) + repo_update_index_if_able(r, &lock_file); + rollback_lock_file(&lock_file); + + return 0; +}
diff --git a/repository.h b/repository.h
index 4fb6a5885f..cf5d5bab48 100644
--- a/repository.h
+++ b/repository.h@@ -157,5 +157,12 @@ int repo_read_index_unmerged(struct repository *); */ void repo_update_index_if_able(struct repository *, struct lock_file *); +/* + * Refresh the index and write it out. If the index file could not be + * locked, error out, except in gentle mode. The flags will be passed + * through to refresh_index(). + */ +int repo_refresh_and_write_index(struct repository *r, + unsigned int flags, int gentle); #endif /* REPOSITORY_H */
--
gitgitgadget