From 8b0588fb88adbf02cdcb8ca6c0405d50cfd3efbd Mon Sep 17 00:00:00 2001 From: Shinnosuke Sawada <6warashi9@gmail.com> Date: Wed, 29 May 2024 13:09:11 +0900 Subject: [PATCH 1/4] test(git): use not captured variable r but function argument repo Signed-off-by: Shinnosuke Sawada <6warashi9@gmail.com> --- pkg/git/repo_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/git/repo_test.go b/pkg/git/repo_test.go index 65fa286e6c..6089bebcfe 100644 --- a/pkg/git/repo_test.go +++ b/pkg/git/repo_test.go @@ -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") cmd.Dir = r.dir out, err := cmd.CombinedOutput() if err != nil { From b8847abb82cbf5790dd3f6bf5ec1454ed32ecbeb Mon Sep 17 00:00:00 2001 From: Shinnosuke Sawada <6warashi9@gmail.com> Date: Wed, 29 May 2024 13:19:57 +0900 Subject: [PATCH 2/4] refactor(git): remove unused method Signed-off-by: Shinnosuke Sawada <6warashi9@gmail.com> --- pkg/git/client.go | 20 -------------------- 1 file changed, 20 deletions(-) diff --git a/pkg/git/client.go b/pkg/git/client.go index 4fe33da619..11c699663c 100644 --- a/pkg/git/client.go +++ b/pkg/git/client.go @@ -20,7 +20,6 @@ import ( "os" "os/exec" "path/filepath" - "strings" "sync" "time" @@ -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 { From dd527958f01a17a79146a9ebb69880c648922268 Mon Sep 17 00:00:00 2001 From: Shinnosuke Sawada <6warashi9@gmail.com> Date: Wed, 29 May 2024 13:33:15 +0900 Subject: [PATCH 3/4] test(git): test not private function but exported function Signed-off-by: Shinnosuke Sawada <6warashi9@gmail.com> --- pkg/git/url_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/git/url_test.go b/pkg/git/url_test.go index e9a1033366..289168e0ed 100644 --- a/pkg/git/url_test.go +++ b/pkg/git/url_test.go @@ -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) }) From b830e1a3dc66082a2e037449c9d80f0753fa40d8 Mon Sep 17 00:00:00 2001 From: Shinnosuke Sawada <6warashi9@gmail.com> Date: Wed, 29 May 2024 13:53:43 +0900 Subject: [PATCH 4/4] test(git): add test for git repo copy Signed-off-by: Shinnosuke Sawada <6warashi9@gmail.com> --- pkg/git/repo_test.go | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/pkg/git/repo_test.go b/pkg/git/repo_test.go index 6089bebcfe..0d32f7b6ef 100644 --- a/pkg/git/repo_test.go +++ b/pkg/git/repo_test.go @@ -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) +}