[fuzz] Return to original branch after running bisect

As-is the bisect mode for the fuzz tool will always return the repo to a
detached HEAD at the originating hash regardless of if the repo was on a
branch or at a detached HEAD to start with.

This CL changes the behaviour to return to the originating branch if the
repo was on one before the tool call, otherwise return to a detached
HEAD.

Bug: 340622694
Change-Id: Ia8110c41d207fc966531b754a1f2a194188d6479
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/313855
Reviewed-by: dan sinclair <dsinclair@chromium.org>
Commit-Queue: James Price <jrprice@google.com>
Auto-Submit: Ryan Harrison <rharrison@chromium.org>
Commit-Queue: dan sinclair <dsinclair@chromium.org>
Commit-Queue: Ryan Harrison <rharrison@chromium.org>
Reviewed-by: James Price <jrprice@google.com>
diff --git a/tools/src/cmd/fuzz/bisect.go b/tools/src/cmd/fuzz/bisect.go
index 5842b43..40ebe0b 100644
--- a/tools/src/cmd/fuzz/bisect.go
+++ b/tools/src/cmd/fuzz/bisect.go
@@ -82,16 +82,23 @@
 	fmt.Printf("Resolved known-failing to commit: %s\n", bc.failingHash)
 	fmt.Printf("Resolved known-passing to commit: %s\n", bc.passingHash)
 
-	origHeadBytes, err := bc.runCmd("git", "rev-parse", "HEAD")
+	origRefBytes, err := bc.runCmd("git", "rev-parse", "--abbrev-ref", "HEAD")
 	if err != nil {
-		return fmt.Errorf("failed to get original HEAD commit: %w", err)
+		return fmt.Errorf("failed to get original HEAD reference: %w", err)
 	}
-	origHead := strings.TrimSpace(string(origHeadBytes))
+	origRef := strings.TrimSpace(string(origRefBytes))
+	if origRef == "HEAD" {
+		origHeadBytes, err := bc.runCmd("git", "rev-parse", "HEAD")
+		if err != nil {
+			return fmt.Errorf("failed to get original HEAD commit: %w", err)
+		}
+		origRef = strings.TrimSpace(string(origHeadBytes))
+	}
 
 	// Ensure repository state restore upon completion
 	defer func() {
-		fmt.Printf("Restoring repository to original HEAD (%s)...\n", origHead)
-		_, _ = bc.runCmd("git", "checkout", origHead)
+		fmt.Printf("Restoring repository to original state (%s)...\n", origRef)
+		_, _ = bc.runCmd("git", "checkout", origRef)
 		_, _ = bc.runCmd("gclient", "sync")
 	}()