@@ -104,3 +104,74 @@ func SetupTestRepo(t *testing.T, remoteURL string, branchName string) (string, f
104104
105105 return tmpDir , cleanup
106106}
107+
108+ // SetupTestRepoWithoutCommit creates a temporary git repository without any commits.
109+ // It returns the temporary directory path and a cleanup function.
110+ func SetupTestRepoWithoutCommit (t * testing.T , remoteURL string , branchName string ) (string , func ()) {
111+ t .Helper ()
112+
113+ // Create temporary directory
114+ tmpDir , err := os .MkdirTemp ("" , "git-test-no-commit" )
115+ if err != nil {
116+ t .Fatal (err )
117+ }
118+
119+ // Initialize git repository
120+ repo , err := git .PlainInit (tmpDir , false )
121+ if err != nil {
122+ t .Fatal (err )
123+ }
124+
125+ // Add remote if URL is provided
126+ if remoteURL != "" {
127+ _ , err = repo .CreateRemote (& config.RemoteConfig {
128+ Name : "origin" ,
129+ URLs : []string {remoteURL },
130+ })
131+ if err != nil {
132+ t .Fatal (err )
133+ }
134+ }
135+
136+ // Create a worktree and add a file (but don't commit)
137+ w , err := repo .Worktree ()
138+ if err != nil {
139+ t .Fatal (err )
140+ }
141+ err = os .WriteFile (filepath .Join (tmpDir , "test.txt" ), []byte ("hello" ), 0644 )
142+ if err != nil {
143+ t .Fatal (err )
144+ }
145+ _ , err = w .Add ("test.txt" )
146+ if err != nil {
147+ t .Fatal (err )
148+ }
149+
150+ // Create a symbolic reference for HEAD to point to the specified branch
151+ if branchName != "" {
152+ headRef := plumbing .NewSymbolicReference (plumbing .HEAD , plumbing .ReferenceName ("refs/heads/" + branchName ))
153+ err = repo .Storer .SetReference (headRef )
154+ if err != nil {
155+ t .Fatal (err )
156+ }
157+ }
158+
159+ // Save current directory
160+ currentDir , err := os .Getwd ()
161+ if err != nil {
162+ t .Fatal (err )
163+ }
164+
165+ // Change to test directory
166+ if err := os .Chdir (tmpDir ); err != nil {
167+ t .Fatal (err )
168+ }
169+
170+ // Return cleanup function
171+ cleanup := func () {
172+ os .Chdir (currentDir )
173+ os .RemoveAll (tmpDir )
174+ }
175+
176+ return tmpDir , cleanup
177+ }
0 commit comments