From 422e17bc2719c24b2f1d640bf9e70341c0e1986d Mon Sep 17 00:00:00 2001 From: achyrva Date: Mon, 9 Jun 2025 10:16:45 +0000 Subject: [PATCH] [RDNF #10] Remove local action cache if remote has changed (#2284) (#142) * fix: remove local cache if remote is changed * test: TestCloneIfRequired https://github.com/nektos/act/pull/2284 Co-authored-by: Jason Song Reviewed-on: https://code.forgejo.org/forgejo/act/pulls/142 Reviewed-by: earl-warren Co-authored-by: achyrva Co-committed-by: achyrva --- act/common/git/git.go | 9 +++++++++ act/common/git/git_test.go | 30 ++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/act/common/git/git.go b/act/common/git/git.go index 3d29b038..87939be3 100644 --- a/act/common/git/git.go +++ b/act/common/git/git.go @@ -238,6 +238,15 @@ type NewGitCloneExecutorInput struct { // CloneIfRequired ... func CloneIfRequired(ctx context.Context, refName plumbing.ReferenceName, input NewGitCloneExecutorInput, logger log.FieldLogger) (*git.Repository, error) { + // If the remote URL has changed, remove the directory and clone again. + if r, err := git.PlainOpen(input.Dir); err == nil { + if remote, err := r.Remote("origin"); err == nil { + if len(remote.Config().URLs) > 0 && remote.Config().URLs[0] != input.URL { + _ = os.RemoveAll(input.Dir) + } + } + } + r, err := git.PlainOpen(input.Dir) if err != nil { var progressWriter io.Writer diff --git a/act/common/git/git_test.go b/act/common/git/git_test.go index 5d37c32b..7a08a97d 100644 --- a/act/common/git/git_test.go +++ b/act/common/git/git_test.go @@ -12,6 +12,8 @@ import ( log "github.com/sirupsen/logrus" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + + "github.com/nektos/act/pkg/common" ) func TestFindGitSlug(t *testing.T) { @@ -258,3 +260,31 @@ func gitCmd(args ...string) error { } return nil } + +func TestCloneIfRequired(t *testing.T) { + tempDir := t.TempDir() + ctx := context.Background() + + t.Run("clone", func(t *testing.T) { + repo, err := CloneIfRequired(ctx, "refs/heads/main", NewGitCloneExecutorInput{ + URL: "https://github.com/actions/checkout", + Dir: tempDir, + }, common.Logger(ctx)) + assert.NoError(t, err) + assert.NotNil(t, repo) + }) + + t.Run("clone different remote", func(t *testing.T) { + repo, err := CloneIfRequired(ctx, "refs/heads/main", NewGitCloneExecutorInput{ + URL: "https://github.com/actions/setup-go", + Dir: tempDir, + }, common.Logger(ctx)) + require.NoError(t, err) + require.NotNil(t, repo) + + remote, err := repo.Remote("origin") + require.NoError(t, err) + require.Len(t, remote.Config().URLs, 1) + assert.Equal(t, "https://github.com/actions/setup-go", remote.Config().URLs[0]) + }) +}