From ca2dbe8dedb8d33596375890e860d37e5c6e2332 Mon Sep 17 00:00:00 2001 From: Arieh Schneier <15041913+AriehSchneier@users.noreply.github.com> Date: Thu, 1 Aug 2024 15:37:41 +1000 Subject: [PATCH] Fix caching a local repo on Windows (#48) (Also fix a couple of other CI issues) --- .github/workflows/test.yaml | 9 +++++++-- .github/workflows/version.yaml | 4 ++-- retriever/git/cacher.go | 11 +++++++++-- retriever/git/git_test.go | 11 ++++++++--- 4 files changed, 26 insertions(+), 9 deletions(-) diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 1467528..2b490b8 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -17,13 +17,14 @@ jobs: runs-on: ${{ matrix.os }} steps: - name: Set up Go - uses: actions/setup-go@v3 + uses: actions/setup-go@v5 with: go-version: 1.21 check-latest: true + cache: false - name: Check out - uses: actions/checkout@v3 + uses: actions/checkout@v4 - name: Make Short Test if: ${{ github.repository_owner != 'anz-bank' }} @@ -34,3 +35,7 @@ jobs: run: make test env: TEST_PRIV_REPO_TOKEN: ${{ secrets.TEST_PRIV_REPO_TOKEN }} + GIT_AUTHOR_NAME: "Tester" + GIT_AUTHOR_EMAIL: "email@address.com" + GIT_COMMITTER_NAME: "Tester" + GIT_COMMITTER_EMAIL: "email@address.com" diff --git a/.github/workflows/version.yaml b/.github/workflows/version.yaml index 138fbf0..977031f 100644 --- a/.github/workflows/version.yaml +++ b/.github/workflows/version.yaml @@ -11,12 +11,12 @@ jobs: runs-on: ${{ vars.RUNNER_UBUNTU && fromJSON(vars.RUNNER_UBUNTU) || 'ubuntu-latest' }} steps: - name: Check out code - uses: actions/checkout@v3 + uses: actions/checkout@v4 with: fetch-depth: '0' - name: Checkout github-tag-action - uses: actions/checkout@v3 + uses: actions/checkout@v4 with: repository: ${{ vars.GENERATE_TAG_REPO || 'anz-bank/github-tag-action' }} ref: ${{ vars.GENERATE_TAG_REF || '1.40.0' }} diff --git a/retriever/git/cacher.go b/retriever/git/cacher.go index 5207627..a205cc4 100644 --- a/retriever/git/cacher.go +++ b/retriever/git/cacher.go @@ -2,6 +2,7 @@ package git import ( "path/filepath" + "strings" "sync" "github.com/go-git/go-billy/v5/osfs" @@ -84,7 +85,7 @@ func (s FsCache) NewStorer(repo string) storage.Storer { } func (s FsCache) repoDir(repo string) string { - return filepath.Join(s.dir, repo) + return filepath.Join(s.dir, cleanForSubPath(repo)) } // PlainFsCache implements the Cacher interface storing repositories in filesystem @@ -118,5 +119,11 @@ func (s PlainFsCache) NewStorer(repo string) storage.Storer { } func (s PlainFsCache) RepoDir(repo string) string { - return filepath.Join(s.dir, repo) + return filepath.Join(s.dir, cleanForSubPath(repo)) +} + +// cleanForSubPath will return a string that is suitable to be used as subpath in the cache directory +// for Windows caching a local repo we need to remove the colon in the drive letter +func cleanForSubPath(repo string) string { + return strings.ReplaceAll(repo, ":", "") } diff --git a/retriever/git/git_test.go b/retriever/git/git_test.go index 6a9eef5..85499d8 100644 --- a/retriever/git/git_test.go +++ b/retriever/git/git_test.go @@ -4,13 +4,14 @@ import ( "bytes" "context" "fmt" - log "github.com/sirupsen/logrus" - "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" "os" "os/exec" "path/filepath" "testing" + + log "github.com/sirupsen/logrus" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) // Verify setting the repository using the various supported reference types (branch, tag, hash) sets the content. @@ -687,6 +688,10 @@ func TestGitSession_Set_Fetch(t *testing.T) { require.NoError(t, err) require.Equal(t, pubRepoMainContent, string(file)) + // Set the config to keep line endings so the test doesn't fail on Windows + err = execute(repoDir, "git", "config", "core.autocrlf", "input") + require.NoError(t, err) + // In an out-of-band process, move the head to a different commit. err = execute(repoDir, "git", "reset", "--hard", pubRepoV1SHA) require.NoError(t, err)