@@ -258,7 +258,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);
@@ -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;
+}
@@ -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 */