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 <martinvonz@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Martin von Zweigbergk committed Jan 14, 2013 at 21:47 UTC 7bca0e451b8e502fa882b458d6fc825da80034cb
1 file changed +23 -16
builtin/reset.c
+23 -16
@@ -240,16 +240,35 @@ static const char **parse_args(const char **argv, const char *prefix, const char
240 return argv[0] ? get_pathspec(prefix, argv) : NULL;
241 }
242
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 +
264 int cmd_reset(int argc, const char **argv, const char *prefix)
265 {
266 int reset_type = NONE, update_ref_status = 0, quiet = 0;
267 int patch_mode = 0;
268 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];
270 const char **pathspec = NULL;
271 struct commit *commit;
252 - struct strbuf msg = STRBUF_INIT;
272 const struct option options[] = {
273 OPT__QUIET(&quiet, N_("be quiet, only report errors")),
274 OPT_SET_INT(0, "mixed", &reset_type,
@@ -333,17 +352,7 @@ int cmd_reset(int argc, const char **argv, const char *prefix)
352
353 /* Any resets update HEAD to the head being switched to,
354 * 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);
356
357 switch (reset_type) {
358 case HARD:
@@ -360,7 +369,5 @@ int cmd_reset(int argc, const char **argv, const char *prefix)
369
370 remove_branch_state();
371
363 - strbuf_release(&msg);
364 -
372 return update_ref_status;
373 }