@@ -67,3 +67,59 @@ func TestReadHeadSHA_PackedRefsWithCommentLine(t *testing.T) {
6767 t .Errorf ("ReadHeadSHA packed-refs with comment: got %q want %q" , got , sha )
6868 }
6969}
70+
71+ func TestVerifyCheckout_Match (t * testing.T ) {
72+ sha := "aabbccddaabbccddaabbccddaabbccddaabbccdd"
73+ root := makeGitDir (t , sha )
74+ if ! integrity .VerifyCheckout (root , sha ) {
75+ t .Error ("expected VerifyCheckout true for matching SHA" )
76+ }
77+ }
78+
79+ func TestVerifyCheckout_Mismatch (t * testing.T ) {
80+ sha := "aabbccddaabbccddaabbccddaabbccddaabbccdd"
81+ root := makeGitDir (t , sha )
82+ if integrity .VerifyCheckout (root , "differentsha000000000000000000000000000000" ) {
83+ t .Error ("expected VerifyCheckout false for mismatched SHA" )
84+ }
85+ }
86+
87+ func TestReadHeadSHA_RefPointingToFile (t * testing.T ) {
88+ sha := "1234567890abcdef1234567890abcdef12345678"
89+ root := makeGitDir (t , sha )
90+ got := integrity .ReadHeadSHA (root )
91+ if got != sha {
92+ t .Errorf ("ReadHeadSHA ref to file: got %q want %q" , got , sha )
93+ }
94+ }
95+
96+ func TestReadHeadSHA_WorktreeGitFile (t * testing.T ) {
97+ // .git is a file pointing to a relative gitdir
98+ sha := "cafecafecafecafecafecafecafecafecafecafe"
99+ root := t .TempDir ()
100+ realGitDir := filepath .Join (root , "dotgit" )
101+ _ = os .MkdirAll (filepath .Join (realGitDir , "refs" , "heads" ), 0o700 )
102+ _ = os .WriteFile (filepath .Join (realGitDir , "HEAD" ), []byte ("ref: refs/heads/main\n " ), 0o600 )
103+ _ = os .WriteFile (filepath .Join (realGitDir , "refs" , "heads" , "main" ), []byte (sha + "\n " ), 0o600 )
104+ worktree := filepath .Join (root , "worktree" )
105+ _ = os .MkdirAll (worktree , 0o755 )
106+ _ = os .WriteFile (filepath .Join (worktree , ".git" ), []byte ("gitdir: ../dotgit\n " ), 0o600 )
107+ got := integrity .ReadHeadSHA (worktree )
108+ if got != sha {
109+ t .Errorf ("ReadHeadSHA worktree gitfile: got %q want %q" , got , sha )
110+ }
111+ }
112+
113+ func TestReadHeadSHA_PackedRefsSimple (t * testing.T ) {
114+ sha := "0000000000000000000000000000000000000001"
115+ root := t .TempDir ()
116+ gitDir := filepath .Join (root , ".git" )
117+ _ = os .MkdirAll (gitDir , 0o700 )
118+ _ = os .WriteFile (filepath .Join (gitDir , "HEAD" ), []byte ("ref: refs/heads/feature\n " ), 0o600 )
119+ packed := sha + " refs/heads/feature\n "
120+ _ = os .WriteFile (filepath .Join (gitDir , "packed-refs" ), []byte (packed ), 0o600 )
121+ got := integrity .ReadHeadSHA (root )
122+ if got != sha {
123+ t .Errorf ("ReadHeadSHA packed-refs simple: got %q want %q" , got , sha )
124+ }
125+ }
0 commit comments