Files
skillshare/internal/git/info_ref_test.go
T
Willie 338bc2c2a4 fix(install): harden ref pinning after code review
- Fallback fetch pulled only the default branch because a depth-1 clone
  is single-branch; fetch all heads and tags so an abbreviated SHA off
  the default branch resolves.
- Move the --track guard from CLI flag parsing into InstallTrackedRepo
  so the Web UI is covered too, and reject tags as well as SHAs: both
  leave HEAD detached with nothing for `git pull` to follow.
- An all-hex branch or tag name (e.g. 20240915) was treated as a SHA in
  check and never queried; look the ref up first and fall back to the
  literal SHA only when the remote has no such ref. Full 40-char SHAs
  still skip the network.
- Rename the git package's 7-char shortHash to abbrevHash to avoid
  clashing with install's 12-char helper.

Refs #281
2026-09-21 02:26:55 +08:00

75 lines
2.3 KiB
Go

package git
import (
"os"
"path/filepath"
"testing"
)
func TestGetRemoteRefHash_TagAndSHA(t *testing.T) {
bareRepo := filepath.Join(t.TempDir(), "test.git")
run(t, "", "git", "init", "--bare", bareRepo)
workDir := filepath.Join(t.TempDir(), "work")
run(t, "", "git", "clone", bareRepo, workDir)
run(t, workDir, "git", "config", "user.email", "test@test.com")
run(t, workDir, "git", "config", "user.name", "Test")
os.WriteFile(filepath.Join(workDir, "a.txt"), []byte("a"), 0644)
run(t, workDir, "git", "add", ".")
run(t, workDir, "git", "commit", "-m", "c1")
run(t, workDir, "git", "tag", "-a", "v1", "-m", "v1")
run(t, workDir, "git", "tag", "light")
run(t, workDir, "git", "push", "origin", "HEAD", "--tags")
run(t, workDir, "git", "checkout", "-q", "-b", "deadbeef")
os.WriteFile(filepath.Join(workDir, "b.txt"), []byte("b"), 0644)
run(t, workDir, "git", "add", ".")
run(t, workDir, "git", "commit", "-m", "c2")
run(t, workDir, "git", "push", "origin", "deadbeef")
head, _ := GetRemoteRefHash(bareRepo, "")
t.Run("annotated tag resolves to peeled commit", func(t *testing.T) {
hash, err := GetRemoteRefHash(bareRepo, "v1")
if err != nil {
t.Fatal(err)
}
if hash != head {
t.Errorf("v1 = %s, want commit %s", hash, head)
}
})
t.Run("lightweight tag", func(t *testing.T) {
hash, err := GetRemoteRefHash(bareRepo, "light")
if err != nil {
t.Fatal(err)
}
if hash != head {
t.Errorf("light = %s, want %s", hash, head)
}
})
t.Run("all-hex branch name resolves via remote, not as a sha", func(t *testing.T) {
hash, err := GetRemoteRefHash(bareRepo, "deadbeef")
if err != nil {
t.Fatal(err)
}
if hash == "deadbee" || hash == head {
t.Errorf("deadbeef = %s, want the branch tip", hash)
}
})
t.Run("short sha unknown to remote refs returns itself", func(t *testing.T) {
hash, err := GetRemoteRefHash(bareRepo, "0123abc")
if err != nil {
t.Fatal(err)
}
if hash != "0123abc" {
t.Errorf("hash = %q, want 0123abc", hash)
}
})
t.Run("full commit sha returns itself without network", func(t *testing.T) {
hash, err := GetRemoteRefHash("/nonexistent/repo.git", "8f14e45fceea167a5a36dedd4bea2543ce848564")
if err != nil {
t.Fatal(err)
}
if hash != "8f14e45" {
t.Errorf("hash = %q, want 8f14e45", hash)
}
})
}