Skip to content

Commit

Permalink
fix: fetch current tags from remote on clone cache hit
Browse files Browse the repository at this point in the history
Special care and handling is needed to keep tags up-to-date with
the remote; all other assets seem to be fetched/updated as needed.

Fixes: Issue #142
  • Loading branch information
nisimond authored and retr0h committed Feb 3, 2024
1 parent c316461 commit 2bb01a1
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 0 deletions.
1 change: 1 addition & 0 deletions internal/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@ package internal
type GitManager interface {
Clone(gitURL string, cloneDir string) error
Worktree(cloneDir string, version string, dstDir string) error
Update(cloneDir string) error
}
6 changes: 6 additions & 0 deletions internal/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ func (g *Git) Clone(
)
}

// Update the repo. Fetch the current HEAD and any new tags that may have
// appeared, and update the cache.
func (g *Git) Update(cloneDir string) error {
return g.execManager.RunCmdInDir("git", []string{"fetch", "--tags"}, cloneDir)
}

// Worktree create a working tree from the repo in `cloneDir` at `version` in `dstDir`.
// Under the covers, this will download any/all required objects from origin
// into the cache
Expand Down
17 changes: 17 additions & 0 deletions internal/git/git_public_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,23 @@ func (suite *GitManagerPublicTestSuite) TestWorktreeError() {
assert.Error(suite.T(), err)
}

func (suite *GitManagerPublicTestSuite) TestUpdateOk() {
suite.mockExec.EXPECT().
RunCmdInDir("git", []string{"fetch", "--tags"}, suite.cloneDir).
Return(nil)
err := suite.gm.Update(suite.cloneDir)
assert.NoError(suite.T(), err)
}

func (suite *GitManagerPublicTestSuite) TestUpdateError() {
errors := errors.New("tests error")
suite.mockExec.EXPECT().
RunCmdInDir("git", []string{"fetch", "--tags"}, suite.cloneDir).
Return(errors)
err := suite.gm.Update(suite.cloneDir)
assert.Error(suite.T(), err)
}

// In order for `go test` to run this suite, we need to create
// a normal test function and pass our suite to suite.Run.
func TestGitManagerPublicTestSuite(t *testing.T) {
Expand Down
14 changes: 14 additions & 0 deletions internal/mocks/git/git_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions internal/repository/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ func (r *Repository) Clone(
}
} else {
r.logger.Info("clone already exists", slog.String("dstDir", targetDir))
if err := r.gitManager.Update(targetDir); err != nil {
return targetDir, err
}
}

return targetDir, nil
Expand Down
18 changes: 18 additions & 0 deletions internal/repository/repository_public_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,29 @@ func (suite *RepositoryPublicTestSuite) TestCloneDoesNotCloneWhenCloneDirExists(
targetDir := filepath.Join(suite.cloneDir, suite.cacheDir)

_ = suite.appFs.MkdirAll(targetDir, 0o755)
suite.mockGit.EXPECT().Update(targetDir).Return(nil)

_, err := repo.Clone(c, suite.cloneDir)
assert.NoError(suite.T(), err)
}

func (suite *RepositoryPublicTestSuite) TestCloneUpdateCloneDirThrowsError() {
repo := suite.NewRepositoryManager()

c := config.Repository{
Git: suite.gitURL,
Version: suite.gitSHA,
}
targetDir := filepath.Join(suite.cloneDir, suite.cacheDir)

_ = suite.appFs.MkdirAll(targetDir, 0o755)
errors := errors.New("tests error")
suite.mockGit.EXPECT().Update(targetDir).Return(errors)

_, err := repo.Clone(c, suite.cloneDir)
assert.Error(suite.T(), err)
}

func (suite *RepositoryPublicTestSuite) TestCopySourcesOkWhenSourceIsDirAndDstDirDoesNotExist() {
repo := suite.NewRepositoryManager()
specs := []FileSpec{
Expand Down

0 comments on commit 2bb01a1

Please sign in to comment.