Skip to content

Commit

Permalink
feat: allow retrieval commits for tags
Browse files Browse the repository at this point in the history
  • Loading branch information
debanjan97 committed Oct 17, 2024
1 parent f907f10 commit 375ff97
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
13 changes: 11 additions & 2 deletions testhelper/gittest/gittest.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,17 @@ func (s *Server) GetServerCA() []byte {
return getServerCA(s.Server)
}

func (s *Server) GetLatestCommitHash(t testing.TB, branch string) string {
cmd := exec.Command("git", "-c", "http.sslVerify=false", "ls-remote", s.URL, fmt.Sprintf("refs/heads/%s", branch))
func (s *Server) GetLatestCommitHash(t testing.TB, referenceType, ref string) string {
var refType string
switch referenceType {
case "branch":
refType = "heads"
case "tag":
refType = "tags"
default:
require.Fail(t, "invalid refType")
}
cmd := exec.Command("git", "-c", "http.sslVerify=false", "ls-remote", s.URL, fmt.Sprintf("refs/%s/%s", refType, ref))
out, err := cmd.Output()
require.NoError(t, err, "should be able to run the ls-remote command")
commitHash := strings.Split(string(out), "\t")[0]
Expand Down
12 changes: 10 additions & 2 deletions testhelper/gittest/gittest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func TestGitServer(t *testing.T) {
require.NoError(t, err, "should be able to get the HEAD commit")
require.NotEmpty(t, out, "HEAD commit should not be empty")

latestCommit := s.GetLatestCommitHash(t, "develop")
latestCommit := s.GetLatestCommitHash(t, "branch", "develop")
require.Equal(t, strings.TrimSpace(out), latestCommit, "HEAD commit should match the latest commit")
})

Expand All @@ -79,7 +79,15 @@ func TestGitServer(t *testing.T) {
require.NoError(t, err, "should be able to get the HEAD commit")
require.NotEmpty(t, out, "HEAD commit should not be empty")

latestCommit := s.GetLatestCommitHash(t, "main")
out, err = execCmd("git", "-C", tempDir, "tag", "-a", "v1.0.0", "-m", "tag v1.0.0")
require.NoErrorf(t, err, "should be able to create a new tag: %s", out)
out, err = execCmd("git", "-c", "http.sslVerify=false", "-C", tempDir, "push", "origin", "v1.0.0")
require.NoErrorf(t, err, "should be able to push the new tag: %s", out)

out, err = execCmd("git", "-C", tempDir, "rev-parse", "v1.0.0")
require.NoError(t, err, "should be able to get the HEAD commit")
latestCommit := s.GetLatestCommitHash(t, "tag", "v1.0.0")

require.Equal(t, strings.TrimSpace(out), latestCommit, "HEAD commit should match the latest commit")
})
}
Expand Down

0 comments on commit 375ff97

Please sign in to comment.