Skip to content

Commit bc41bf4

Browse files
martinvonzgitster
authored andcommitted
reset [--mixed]: only write index file once
When doing a mixed reset without paths, the index is locked, read, reset, and written back as part of the actual reset operation (in reset_index()). Then, when showing the list of worktree modifications, we lock the index again, refresh it, and write it. Change this so we only write the index once, making "git reset" a little faster. It does mean that the index lock will be held a little longer, but the difference is small compared to the time spent refreshing the index. There is one minor functional difference: We used to say "Could not write new index file." if the first write failed, and "Could not refresh index" if the second write failed. Now, we will only use the first message. This speeds up "git reset" a little on the linux-2.6 repo (best of five, warm cache): Before After real 0m0.239s 0m0.214s user 0m0.160s 0m0.130s sys 0m0.070s 0m0.080s Signed-off-by: Martin von Zweigbergk <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 01a19df commit bc41bf4

File tree

1 file changed

+5
-9
lines changed

1 file changed

+5
-9
lines changed

builtin/reset.c

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,11 @@ int cmd_reset(int argc, const char **argv, const char *prefix)
336336
err = reset_index(sha1, MIXED, quiet);
337337
if (err)
338338
die(_("Could not reset index file to revision '%s'."), rev);
339+
340+
if (reset_type == MIXED) /* Report what has not been updated. */
341+
update_index_refresh(
342+
quiet ? REFRESH_QUIET : REFRESH_IN_PORCELAIN);
343+
339344
if (write_cache(newfd, active_cache, active_nr) ||
340345
commit_locked_index(lock))
341346
die(_("Could not write new index file."));
@@ -347,15 +352,6 @@ int cmd_reset(int argc, const char **argv, const char *prefix)
347352

348353
if (reset_type == HARD && !update_ref_status && !quiet)
349354
print_new_head_line(commit);
350-
else if (reset_type == MIXED) { /* Report what has not been updated. */
351-
struct lock_file *index_lock = xcalloc(1, sizeof(struct lock_file));
352-
int fd = hold_locked_index(index_lock, 1);
353-
update_index_refresh(
354-
quiet ? REFRESH_QUIET : REFRESH_IN_PORCELAIN);
355-
if (write_cache(fd, active_cache, active_nr) ||
356-
commit_locked_index(index_lock))
357-
error("Could not refresh index");
358-
}
359355

360356
remove_branch_state();
361357

0 commit comments

Comments
 (0)