Skip to contentSkip to conversation

reset: avoid reflog update on no-op reset

PATCH In review Pushkar Singh 3 participants 3 messages Jan 22

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 reset: avoid reflog update on no-op reset

When "git reset" is invoked with a target that already matches HEAD, it currently writes a reflog entry even though no reference is updated.

Detect this no-op case and avoid updating ORIG_HEAD and HEAD, skipping the reflog entry entirely.

Add a regression test to ensure no reflog entry is written for a no-op reset.

---
 builtin/reset.c   |  4 ++++
 t/t1410-reflog.sh | 15 +++++++++++++++
 2 files changed, 19 insertions(+)
builtin/reset.c +4 -0
index ed35802af1..900c2f2fe8 100644
@@ -309,6 +309,10 @@ static int reset_refs(const char *rev, const struct object_id *oid)
 		old_orig = &oid_old_orig;
 	if (!repo_get_oid(the_repository, "HEAD", &oid_orig)) {
 		orig = &oid_orig;
+		if (oideq(orig, oid)) {
+			strbuf_release(&msg);
+			return 0;
+		}
 		set_reflog_message(&msg, "updating ORIG_HEAD", NULL);
 		refs_update_ref(get_main_ref_store(the_repository), msg.buf,
 				"ORIG_HEAD", orig, old_orig, 0,
t/t1410-reflog.sh +15 -0
index ce71f9a30a..54f8692c53 100755
@@ -533,6 +533,21 @@ test_expect_success 'reflog for symref with unborn target can be listed' '
 	)
 '
 
+test_expect_success 'reset does not write reflog entry on no-op' '
+	git init no-op-reset &&
+	(
+		cd no-op-reset &&
+		echo a >file &&
+		git add file &&
+		git commit -m initial &&
+
+		before=$(git reflog | wc -l) &&
+		git reset HEAD &&
+		after=$(git reflog | wc -l) &&
+		test "$before" = "$after"
+	)
+'
+
 test_expect_success 'reflog with invalid object ID can be listed' '
 	test_when_finished "rm -rf repo" &&
 	git init repo &&
Signature · 1 line
2.43.0
#1 lore raw
Quoted 5 lines · Pushkar Singh

When "git reset" is invoked with a target that already matches HEAD, it currently writes a reflog entry even though no reference is updated.

Detect this no-op case and avoid updating ORIG_HEAD and HEAD, skipping the reflog entry entirely.

I am mildly negative on this one. A scripted use that gets which commit to reset to from the caller in the outside world, e.g.,

    #!/bin/sh
    git reset --hard "$1"
    git diff --stat @{1}

would be confused if reflog does not reliably store the state before running "reset --hard" to @{1}. In other words ...

Quoted 2 lines

Add a regression test to ensure no reflog entry is written for a no-op reset.

... this change may already be a regression for existing users.

#2 lore raw
Quoted 15 lines · Junio C Hamano
Quoted 5 lines · Pushkar Singh

When "git reset" is invoked with a target that already matches HEAD, it currently writes a reflog entry even though no reference is updated.

Detect this no-op case and avoid updating ORIG_HEAD and HEAD, skipping the reflog entry entirely.

I am mildly negative on this one. A scripted use that gets which commit to reset to from the caller in the outside world, e.g.,

    #!/bin/sh
    git reset --hard "$1"
    git diff --stat @{1}
Quoted 7 lines

would be confused if reflog does not reliably store the state before running "reset --hard" to @{1}. In other words ...

Quoted 2 lines

Add a regression test to ensure no reflog entry is written for a no-op reset.

... this change may already be a regression for existing users.

I was just writing the same message. ;) In addition to scripted use, that reflog does contain some human-readable information: the message field tells us what name we "git reset --hard" to. That might be helpful when digging through it trying to piece together what happened.

-Peff