Skip to content

Commit 01a19df

Browse files
martinvonzgitster
authored andcommitted
reset.c: move lock, write and commit out of update_index_refresh()
In preparation for the/a following patch, move the locking, writing and committing of the index file out of update_index_refresh(). The code duplication caused will soon be taken care of. What remains of update_index_refresh() is just one line, but it is still called from two places, so let's leave it for now. In the process, we expose and fix the minor UI bug that makes us print "Could not refresh index" when we fail to write the index file when invoked with a pathspec. Copy the error message from the pathspec-less codepath ("Could not write new index file."). Signed-off-by: Martin von Zweigbergk <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent bf883f3 commit 01a19df

File tree

1 file changed

+18
-16
lines changed

1 file changed

+18
-16
lines changed

builtin/reset.c

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -109,19 +109,10 @@ static void print_new_head_line(struct commit *commit)
109109
printf("\n");
110110
}
111111

112-
static int update_index_refresh(int fd, struct lock_file *index_lock, int flags)
112+
static void update_index_refresh(int flags)
113113
{
114-
if (!index_lock) {
115-
index_lock = xcalloc(1, sizeof(struct lock_file));
116-
fd = hold_locked_index(index_lock, 1);
117-
}
118-
119114
refresh_index(&the_index, (flags), NULL, NULL,
120115
_("Unstaged changes after reset:"));
121-
if (write_cache(fd, active_cache, active_nr) ||
122-
commit_locked_index(index_lock))
123-
return error ("Could not refresh index");
124-
return 0;
125116
}
126117

127118
static void update_index_from_diff(struct diff_queue_struct *q,
@@ -321,9 +312,14 @@ int cmd_reset(int argc, const char **argv, const char *prefix)
321312
if (pathspec) {
322313
struct lock_file *lock = xcalloc(1, sizeof(struct lock_file));
323314
int index_fd = hold_locked_index(lock, 1);
324-
return read_from_tree(pathspec, sha1) ||
325-
update_index_refresh(index_fd, lock,
326-
quiet ? REFRESH_QUIET : REFRESH_IN_PORCELAIN);
315+
if (read_from_tree(pathspec, sha1))
316+
return 1;
317+
update_index_refresh(
318+
quiet ? REFRESH_QUIET : REFRESH_IN_PORCELAIN);
319+
if (write_cache(index_fd, active_cache, active_nr) ||
320+
commit_locked_index(lock))
321+
return error("Could not write new index file.");
322+
return 0;
327323
}
328324

329325
/* Soft reset does not touch the index file nor the working tree
@@ -351,9 +347,15 @@ int cmd_reset(int argc, const char **argv, const char *prefix)
351347

352348
if (reset_type == HARD && !update_ref_status && !quiet)
353349
print_new_head_line(commit);
354-
else if (reset_type == MIXED) /* Report what has not been updated. */
355-
update_index_refresh(0, NULL,
356-
quiet ? REFRESH_QUIET : REFRESH_IN_PORCELAIN);
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+
}
357359

358360
remove_branch_state();
359361

0 commit comments

Comments
 (0)