Skip to content

Commit

Permalink
reset.c: extract function for updating {ORIG_,}HEAD
Browse files Browse the repository at this point in the history
By extracting the code for updating the HEAD and ORIG_HEAD symbolic
references to a separate function, we declutter cmd_reset() a bit and
we make it clear that e.g. the four variables {,sha1_}{,old_}orig are
only used by this code.

Signed-off-by: Martin von Zweigbergk <martinvonz@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
  • Loading branch information
Martin von Zweigbergk authored and Junio C Hamano committed Jan 15, 2013
1 parent dca48cf commit 7bca0e4
Showing 1 changed file with 23 additions and 16 deletions.
39 changes: 23 additions & 16 deletions builtin/reset.c
Original file line number Diff line number Diff line change
Expand Up @@ -240,16 +240,35 @@ static const char **parse_args(const char **argv, const char *prefix, const char
return argv[0] ? get_pathspec(prefix, argv) : NULL;
}

static int update_refs(const char *rev, const unsigned char *sha1)
{
int update_ref_status;
struct strbuf msg = STRBUF_INIT;
unsigned char *orig = NULL, sha1_orig[20],
*old_orig = NULL, sha1_old_orig[20];

if (!get_sha1("ORIG_HEAD", sha1_old_orig))
old_orig = sha1_old_orig;
if (!get_sha1("HEAD", sha1_orig)) {
orig = sha1_orig;
set_reflog_message(&msg, "updating ORIG_HEAD", NULL);
update_ref(msg.buf, "ORIG_HEAD", orig, old_orig, 0, MSG_ON_ERR);
} else if (old_orig)
delete_ref("ORIG_HEAD", old_orig, 0);
set_reflog_message(&msg, "updating HEAD", rev);
update_ref_status = update_ref(msg.buf, "HEAD", sha1, orig, 0, MSG_ON_ERR);
strbuf_release(&msg);
return update_ref_status;
}

int cmd_reset(int argc, const char **argv, const char *prefix)
{
int reset_type = NONE, update_ref_status = 0, quiet = 0;
int patch_mode = 0;
const char *rev;
unsigned char sha1[20], *orig = NULL, sha1_orig[20],
*old_orig = NULL, sha1_old_orig[20];
unsigned char sha1[20];
const char **pathspec = NULL;
struct commit *commit;
struct strbuf msg = STRBUF_INIT;
const struct option options[] = {
OPT__QUIET(&quiet, N_("be quiet, only report errors")),
OPT_SET_INT(0, "mixed", &reset_type,
Expand Down Expand Up @@ -333,17 +352,7 @@ int cmd_reset(int argc, const char **argv, const char *prefix)

/* Any resets update HEAD to the head being switched to,
* saving the previous head in ORIG_HEAD before. */
if (!get_sha1("ORIG_HEAD", sha1_old_orig))
old_orig = sha1_old_orig;
if (!get_sha1("HEAD", sha1_orig)) {
orig = sha1_orig;
set_reflog_message(&msg, "updating ORIG_HEAD", NULL);
update_ref(msg.buf, "ORIG_HEAD", orig, old_orig, 0, MSG_ON_ERR);
}
else if (old_orig)
delete_ref("ORIG_HEAD", old_orig, 0);
set_reflog_message(&msg, "updating HEAD", rev);
update_ref_status = update_ref(msg.buf, "HEAD", sha1, orig, 0, MSG_ON_ERR);
update_ref_status = update_refs(rev, sha1);

switch (reset_type) {
case HARD:
Expand All @@ -360,7 +369,5 @@ int cmd_reset(int argc, const char **argv, const char *prefix)

remove_branch_state();

strbuf_release(&msg);

return update_ref_status;
}

0 comments on commit 7bca0e4

Please sign in to comment.