From b17fe1e4d59a9d1d95a7aead5e6fcd13e50939a5 Mon Sep 17 00:00:00 2001
From: Orhan Toy <orhantoy@github.com>
Date: Wed, 12 Jun 2024 17:01:40 +0200
Subject: [PATCH] Handle hidden refs (#1774)

Co-authored-by: Chris Gavin <chris@chrisgavin.me>
---
 __test__/ref-helper.test.ts | 10 ++++++++++
 dist/index.js               |  6 +++++-
 src/ref-helper.ts           |  6 +++++-
 3 files changed, 20 insertions(+), 2 deletions(-)

diff --git a/__test__/ref-helper.test.ts b/__test__/ref-helper.test.ts
index 8a8302a..d3b00b7 100644
--- a/__test__/ref-helper.test.ts
+++ b/__test__/ref-helper.test.ts
@@ -67,6 +67,16 @@ describe('ref-helper tests', () => {
     expect(checkoutInfo.startPoint).toBeFalsy()
   })
 
+  it('getCheckoutInfo refs/', async () => {
+    const checkoutInfo = await refHelper.getCheckoutInfo(
+      git,
+      'refs/gh/queue/main/pr-123',
+      commit
+    )
+    expect(checkoutInfo.ref).toBe(commit)
+    expect(checkoutInfo.startPoint).toBeFalsy()
+  })
+
   it('getCheckoutInfo unqualified branch only', async () => {
     git.branchExists = jest.fn(async (remote: boolean, pattern: string) => {
       return true
diff --git a/dist/index.js b/dist/index.js
index e128adf..9d959a9 100644
--- a/dist/index.js
+++ b/dist/index.js
@@ -2000,9 +2000,13 @@ function getCheckoutInfo(git, ref, commit) {
             result.ref = `refs/remotes/pull/${branch}`;
         }
         // refs/tags/
-        else if (upperRef.startsWith('REFS/')) {
+        else if (upperRef.startsWith('REFS/TAGS/')) {
             result.ref = ref;
         }
+        // refs/
+        else if (upperRef.startsWith('REFS/') && commit) {
+            result.ref = commit;
+        }
         // Unqualified ref, check for a matching branch or tag
         else {
             if (yield git.branchExists(true, `origin/${ref}`)) {
diff --git a/src/ref-helper.ts b/src/ref-helper.ts
index 047e906..00a1d27 100644
--- a/src/ref-helper.ts
+++ b/src/ref-helper.ts
@@ -42,9 +42,13 @@ export async function getCheckoutInfo(
     result.ref = `refs/remotes/pull/${branch}`
   }
   // refs/tags/
-  else if (upperRef.startsWith('REFS/')) {
+  else if (upperRef.startsWith('REFS/TAGS/')) {
     result.ref = ref
   }
+  // refs/
+  else if (upperRef.startsWith('REFS/') && commit) {
+    result.ref = commit
+  }
   // Unqualified ref, check for a matching branch or tag
   else {
     if (await git.branchExists(true, `origin/${ref}`)) {