Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor and add tests for git package #4927

Merged
merged 4 commits into from
May 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 0 additions & 20 deletions pkg/git/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"os"
"os/exec"
"path/filepath"
"strings"
"sync"
"time"

Expand Down Expand Up @@ -231,25 +230,6 @@ func (c *client) Clean() error {
return os.RemoveAll(c.cacheDir)
}

// getLatestRemoteHashForBranch returns the hash of the latest commit of a remote branch.
func (c *client) getLatestRemoteHashForBranch(ctx context.Context, remote, branch string) (string, error) {
ref := "refs/heads/" + branch
out, err := retryCommand(3, time.Second, c.logger, func() ([]byte, error) {
return runGitCommand(ctx, c.gitPath, "", c.envsForRepo(remote), "ls-remote", ref)
})
if err != nil {
c.logger.Error("failed to get latest remote hash for branch",
zap.String("remote", remote),
zap.String("branch", branch),
zap.String("out", string(out)),
zap.Error(err),
)
return "", err
}
parts := strings.Split(string(out), "\t")
return parts[0], nil
}

func (c *client) lockRepo(repoID string) {
c.mu.Lock()
if _, ok := c.repoLocks[repoID]; !ok {
Expand Down
37 changes: 36 additions & 1 deletion pkg/git/repo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ func Test_setGCAutoDetach(t *testing.T) {
}

getGCAutoDetach := func(ctx context.Context, repo *repo) (bool, error) {
cmd := exec.CommandContext(ctx, r.gitPath, "config", "--get", "gc.autoDetach")
cmd := exec.CommandContext(ctx, repo.gitPath, "config", "--get", "gc.autoDetach")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Warashi Sorry for the late comment, look like the r variable in the below line (L210) has the same issue with this fixed one. It works because both refer to the outer r variable 👀 Would be great if you could open another PR to fix this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I opened another PR to fix this with other improvements.
#4932

cmd.Dir = r.dir
out, err := cmd.CombinedOutput()
if err != nil {
Expand Down Expand Up @@ -242,3 +242,38 @@ func Test_setGCAutoDetach(t *testing.T) {

assert.Equal(t, false, got)
}

func TestCopy(t *testing.T) {
faker, err := newFaker()
require.NoError(t, err)
defer faker.clean()

var (
org = "test-repo-org"
repoName = "repo-copy"
ctx = context.Background()
)

err = faker.makeRepo(org, repoName)
require.NoError(t, err)
r := &repo{
dir: faker.repoDir(org, repoName),
gitPath: faker.gitPath,
}

commits, err := r.ListCommits(ctx, "")
require.NoError(t, err)
assert.Equal(t, 1, len(commits))

tmpDir := filepath.Join(faker.dir, "tmp-repo")
newRepo, err := r.Copy(tmpDir)
require.NoError(t, err)

assert.NotEqual(t, r, newRepo)

newRepoCommits, err := newRepo.ListCommits(ctx, "")
require.NoError(t, err)
assert.Equal(t, 1, len(newRepoCommits))

assert.Equal(t, commits, newRepoCommits)
}
2 changes: 1 addition & 1 deletion pkg/git/url_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ func TestParseGitURL(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := parseGitURL(tt.rawURL)
got, err := ParseGitURL(tt.rawURL)
assert.Equal(t, tt.wantErr, err != nil)
assert.Equal(t, got, tt.wantURL)
})
Expand Down
Loading