From c3164611a3ac34411af231f3e2815b765edb146b Mon Sep 17 00:00:00 2001 From: Nicolas Simonds Date: Fri, 2 Feb 2024 17:11:09 -0800 Subject: [PATCH] fix: prune detached worktrees on cleanup These are not in any way harmful, but they are also not helpful, and for long-standing cache directories, they can clutter things up for no good reason. --- internal/git/git.go | 1 + internal/git/git_public_test.go | 3 +++ 2 files changed, 4 insertions(+) diff --git a/internal/git/git.go b/internal/git/git.go index 7f3d707..f71d87c 100644 --- a/internal/git/git.go +++ b/internal/git/git.go @@ -86,6 +86,7 @@ func (g *Git) Worktree( // this is just junk data in our use case, so get rid of it if err == nil { _ = os.Remove(filepath.Join(dst, ".git")) + _ = g.execManager.RunCmdInDir("git", []string{"worktree", "prune", "--verbose"}, cloneDir) } return err } diff --git a/internal/git/git_public_test.go b/internal/git/git_public_test.go index dad8272..441ff44 100644 --- a/internal/git/git_public_test.go +++ b/internal/git/git_public_test.go @@ -92,6 +92,9 @@ func (suite *GitManagerPublicTestSuite) TestWorktreeOk() { suite.mockExec.EXPECT(). RunCmdInDir("git", []string{"worktree", "add", "--force", suite.dstDir, suite.gitVersion}, suite.cloneDir). Return(nil) + suite.mockExec.EXPECT(). + RunCmdInDir("git", []string{"worktree", "prune", "--verbose"}, suite.cloneDir). + Return(nil) err := suite.gm.Worktree(suite.cloneDir, suite.gitVersion, suite.dstDir) assert.NoError(suite.T(), err) }