Skip to content

Commit 8cafc6a

Browse files
committed
built-in add -i: refresh the index before running status
This is what the Perl version does, and therefore it is what the built-in version should do, too. Signed-off-by: Johannes Schindelin <[email protected]>
1 parent 5e23c07 commit 8cafc6a

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

add-interactive.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,9 @@ int run_add_i(struct repository *r, const struct pathspec *ps)
258258
_("staged"), _("unstaged"), _("path"));
259259
opts.header = header.buf;
260260

261-
res = run_status(r, ps, &files, &opts);
261+
repo_refresh_and_write_index(r, REFRESH_QUIET, 1);
262+
if (run_status(r, ps, &files, &opts) < 0)
263+
res = -1;
262264

263265
release_file_list(&files);
264266
strbuf_release(&print_file_item_data.buf);

repository.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,3 +272,22 @@ int repo_hold_locked_index(struct repository *repo,
272272
BUG("the repo hasn't been setup");
273273
return hold_lock_file_for_update(lf, repo->index_file, flags);
274274
}
275+
276+
int repo_refresh_and_write_index(struct repository *r,
277+
unsigned int flags, int gentle)
278+
{
279+
struct lock_file lock_file = LOCK_INIT;
280+
int fd;
281+
282+
if (repo_read_index_preload(r, NULL, 0) < 0)
283+
return error(_("could not read index"));
284+
fd = repo_hold_locked_index(r, &lock_file, 0);
285+
if (!gentle && fd < 0)
286+
return error(_("could not lock index for writing"));
287+
refresh_index(r->index, flags, NULL, NULL, NULL);
288+
if (0 <= fd)
289+
repo_update_index_if_able(r, &lock_file);
290+
rollback_lock_file(&lock_file);
291+
292+
return 0;
293+
}

repository.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,5 +154,12 @@ int repo_read_index_unmerged(struct repository *);
154154
*/
155155
void repo_update_index_if_able(struct repository *, struct lock_file *);
156156

157+
/*
158+
* Refresh the index and write it out. If the index file could not be
159+
* locked, error out, except in gentle mode. The flags will be passed
160+
* through to refresh_index().
161+
*/
162+
int repo_refresh_and_write_index(struct repository *r,
163+
unsigned int flags, int gentle);
157164

158165
#endif /* REPOSITORY_H */

0 commit comments

Comments
 (0)