Skip to content

Commit 3bbf2f2

Browse files
martinvonzgitster
authored andcommitted
reset.c: finish entire cmd_reset() whether or not pathspec is given
By not returning from inside the "if (pathspec)" block, we can let the pathspec-aware and pathspec-less code share a bit more, making it easier to make future changes that should affect both cases. This also highlights the similarity between read_from_tree() and reset_index(). Signed-off-by: Martin von Zweigbergk <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent bc41bf4 commit 3bbf2f2

File tree

1 file changed

+18
-24
lines changed

1 file changed

+18
-24
lines changed

builtin/reset.c

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -309,19 +309,6 @@ int cmd_reset(int argc, const char **argv, const char *prefix)
309309
die(_("%s reset is not allowed in a bare repository"),
310310
_(reset_type_names[reset_type]));
311311

312-
if (pathspec) {
313-
struct lock_file *lock = xcalloc(1, sizeof(struct lock_file));
314-
int index_fd = hold_locked_index(lock, 1);
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-
325312
/* Soft reset does not touch the index file nor the working tree
326313
* at all, but requires them in a good order. Other resets reset
327314
* the index file to the tree object we are switching to. */
@@ -331,11 +318,16 @@ int cmd_reset(int argc, const char **argv, const char *prefix)
331318
if (reset_type != SOFT) {
332319
struct lock_file *lock = xcalloc(1, sizeof(struct lock_file));
333320
int newfd = hold_locked_index(lock, 1);
334-
int err = reset_index(sha1, reset_type, quiet);
335-
if (reset_type == KEEP && !err)
336-
err = reset_index(sha1, MIXED, quiet);
337-
if (err)
338-
die(_("Could not reset index file to revision '%s'."), rev);
321+
if (pathspec) {
322+
if (read_from_tree(pathspec, sha1))
323+
return 1;
324+
} else {
325+
int err = reset_index(sha1, reset_type, quiet);
326+
if (reset_type == KEEP && !err)
327+
err = reset_index(sha1, MIXED, quiet);
328+
if (err)
329+
die(_("Could not reset index file to revision '%s'."), rev);
330+
}
339331

340332
if (reset_type == MIXED) /* Report what has not been updated. */
341333
update_index_refresh(
@@ -346,14 +338,16 @@ int cmd_reset(int argc, const char **argv, const char *prefix)
346338
die(_("Could not write new index file."));
347339
}
348340

349-
/* Any resets update HEAD to the head being switched to,
350-
* saving the previous head in ORIG_HEAD before. */
351-
update_ref_status = update_refs(rev, sha1);
341+
if (!pathspec) {
342+
/* Any resets without paths update HEAD to the head being
343+
* switched to, saving the previous head in ORIG_HEAD before. */
344+
update_ref_status = update_refs(rev, sha1);
352345

353-
if (reset_type == HARD && !update_ref_status && !quiet)
354-
print_new_head_line(commit);
346+
if (reset_type == HARD && !update_ref_status && !quiet)
347+
print_new_head_line(commit);
355348

356-
remove_branch_state();
349+
remove_branch_state();
350+
}
357351

358352
return update_ref_status;
359353
}

0 commit comments

Comments
 (0)