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 <martinvonz@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Martin von Zweigbergk committed
Jan 14, 2013 at 21:47 UTC
01a19dfc1ac53011deef492b21e52bf7840cef49
1 file changed
+18
-16
builtin/reset.c
+18
-16
@@ -109,19 +109,10 @@ static void print_new_head_line(struct commit *commit)
109
printf("\n");
110
}
111
112
-static int update_index_refresh(int fd, struct lock_file *index_lock, int flags)
112
+static void update_index_refresh(int flags)
113
{
114
- if (!index_lock) {
115
- index_lock = xcalloc(1, sizeof(struct lock_file));
116
- fd = hold_locked_index(index_lock, 1);
117
- }
118
-
114
refresh_index(&the_index, (flags), NULL, NULL,
115
_("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;
116
}
117
118
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)
312
if (pathspec) {
313
struct lock_file *lock = xcalloc(1, sizeof(struct lock_file));
314
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;
323
}
324
325
/* 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)
347
348
if (reset_type == HARD && !update_ref_status && !quiet)
349
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
+ }
359
360
remove_branch_state();
361