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 <martinvonz@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Martin von Zweigbergk committed
Jan 14, 2013 at 21:47 UTC
3bbf2f20f62ec6aa7998d9f7acc942ded6639870
1 file changed
+18
-24
builtin/reset.c
+18
-24
@@ -309,19 +309,6 @@ int cmd_reset(int argc, const char **argv, const char *prefix)
309
die(_("%s reset is not allowed in a bare repository"),
310
_(reset_type_names[reset_type]));
311
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
-
312
/* Soft reset does not touch the index file nor the working tree
313
* at all, but requires them in a good order. Other resets reset
314
* 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)
318
if (reset_type != SOFT) {
319
struct lock_file *lock = xcalloc(1, sizeof(struct lock_file));
320
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
+ }
331
332
if (reset_type == MIXED) /* Report what has not been updated. */
333
update_index_refresh(
@@ -346,14 +338,16 @@ int cmd_reset(int argc, const char **argv, const char *prefix)
338
die(_("Could not write new index file."));
339
}
340
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);
345
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);
348
356
- remove_branch_state();
349
+ remove_branch_state();
350
+ }
351
352
return update_ref_status;
353
}