Skip to content

Commit 7bca0e4

Browse files
martinvonzgitster
authored andcommitted
reset.c: extract function for updating {ORIG_,}HEAD
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent dca48cf commit 7bca0e4

File tree

1 file changed

+23
-16
lines changed

1 file changed

+23
-16
lines changed

builtin/reset.c

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -240,16 +240,35 @@ static const char **parse_args(const char **argv, const char *prefix, const char
240240
return argv[0] ? get_pathspec(prefix, argv) : NULL;
241241
}
242242

243+
static int update_refs(const char *rev, const unsigned char *sha1)
244+
{
245+
int update_ref_status;
246+
struct strbuf msg = STRBUF_INIT;
247+
unsigned char *orig = NULL, sha1_orig[20],
248+
*old_orig = NULL, sha1_old_orig[20];
249+
250+
if (!get_sha1("ORIG_HEAD", sha1_old_orig))
251+
old_orig = sha1_old_orig;
252+
if (!get_sha1("HEAD", sha1_orig)) {
253+
orig = sha1_orig;
254+
set_reflog_message(&msg, "updating ORIG_HEAD", NULL);
255+
update_ref(msg.buf, "ORIG_HEAD", orig, old_orig, 0, MSG_ON_ERR);
256+
} else if (old_orig)
257+
delete_ref("ORIG_HEAD", old_orig, 0);
258+
set_reflog_message(&msg, "updating HEAD", rev);
259+
update_ref_status = update_ref(msg.buf, "HEAD", sha1, orig, 0, MSG_ON_ERR);
260+
strbuf_release(&msg);
261+
return update_ref_status;
262+
}
263+
243264
int cmd_reset(int argc, const char **argv, const char *prefix)
244265
{
245266
int reset_type = NONE, update_ref_status = 0, quiet = 0;
246267
int patch_mode = 0;
247268
const char *rev;
248-
unsigned char sha1[20], *orig = NULL, sha1_orig[20],
249-
*old_orig = NULL, sha1_old_orig[20];
269+
unsigned char sha1[20];
250270
const char **pathspec = NULL;
251271
struct commit *commit;
252-
struct strbuf msg = STRBUF_INIT;
253272
const struct option options[] = {
254273
OPT__QUIET(&quiet, N_("be quiet, only report errors")),
255274
OPT_SET_INT(0, "mixed", &reset_type,
@@ -333,17 +352,7 @@ int cmd_reset(int argc, const char **argv, const char *prefix)
333352

334353
/* Any resets update HEAD to the head being switched to,
335354
* saving the previous head in ORIG_HEAD before. */
336-
if (!get_sha1("ORIG_HEAD", sha1_old_orig))
337-
old_orig = sha1_old_orig;
338-
if (!get_sha1("HEAD", sha1_orig)) {
339-
orig = sha1_orig;
340-
set_reflog_message(&msg, "updating ORIG_HEAD", NULL);
341-
update_ref(msg.buf, "ORIG_HEAD", orig, old_orig, 0, MSG_ON_ERR);
342-
}
343-
else if (old_orig)
344-
delete_ref("ORIG_HEAD", old_orig, 0);
345-
set_reflog_message(&msg, "updating HEAD", rev);
346-
update_ref_status = update_ref(msg.buf, "HEAD", sha1, orig, 0, MSG_ON_ERR);
355+
update_ref_status = update_refs(rev, sha1);
347356

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

361370
remove_branch_state();
362371

363-
strbuf_release(&msg);
364-
365372
return update_ref_status;
366373
}

0 commit comments

Comments
 (0)