Skip to content

Commit

Permalink
Fix where we are deleting tags (don't edit cache\!) (#1112)
Browse files Browse the repository at this point in the history
## Description

Fixes a bug introduced from MZAL where cache could be messed with do to
GitPath being changed in the git receiver.

## Related Issue

Fixes #1079 

## Type of change

- [X] Bug fix (non-breaking change which fixes an issue)
  • Loading branch information
Racer159 authored Dec 12, 2022
1 parent 392a8c5 commit ee635c2
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 8 deletions.
10 changes: 5 additions & 5 deletions src/internal/packager/git/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func (g *Git) fetchTag(tag string) {

refspec := goConfig.RefSpec("refs/tags/" + tag + ":refs/tags/" + tag)

err := g.fetch(refspec)
err := g.fetch(g.GitPath, refspec)

if err != nil {
message.Fatal(err, "Not a valid tag or unable to fetch")
Expand All @@ -33,18 +33,18 @@ func (g *Git) fetchHash(hash string) {

refspec := goConfig.RefSpec(hash + ":" + hash)

err := g.fetch(refspec)
err := g.fetch(g.GitPath, refspec)

if err != nil {
message.Fatal(err, "Not a valid hash or unable to fetch")
}
}

// fetch performs a `git fetch` of _only_ the provided git refspec(s).
func (g *Git) fetch(refspecs ...goConfig.RefSpec) error {
func (g *Git) fetch(gitDirectory string, refspecs ...goConfig.RefSpec) error {
message.Debugf("git.fetch(%#v)", refspecs)

repo, err := git.PlainOpen(g.GitPath)
repo, err := git.PlainOpen(gitDirectory)
if err != nil {
message.Fatal(err, "Unable to load the git repo")
}
Expand Down Expand Up @@ -84,7 +84,7 @@ func (g *Git) fetch(refspecs ...goConfig.RefSpec) error {
for _, refspec := range refspecs {
cmdArgs = append(cmdArgs, refspec.String())
}
_, _, err := utils.ExecCommandWithContextAndDir(context.TODO(), g.GitPath, false, "git", cmdArgs...)
_, _, err := utils.ExecCommandWithContextAndDir(context.TODO(), gitDirectory, false, "git", cmdArgs...)

return err
}
Expand Down
3 changes: 1 addition & 2 deletions src/internal/packager/git/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,7 @@ func (g *Git) pull(gitURL, targetFolder string, repoName string) {
if err == git.ErrRepositoryAlreadyExists {
message.Debug("Repo already cloned, fetching upstream changes...")

g.GitPath = gitCachePath
err = g.fetch()
err = g.fetch(gitCachePath)

if errors.Is(err, git.NoErrAlreadyUpToDate) {
message.Debug("Repo already up to date")
Expand Down
2 changes: 1 addition & 1 deletion src/test/e2e/04_create_templating_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
)

func TestCreateTemplating(t *testing.T) {
t.Log("E2E: Temporary directory deploy")
t.Log("E2E: Create Templating")

e2e.setup(t)
defer e2e.teardown(t)
Expand Down
45 changes: 45 additions & 0 deletions src/test/e2e/05_create_cache_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: 2021-Present The Zarf Authors

// Package test provides e2e tests for zarf
package test

import (
"fmt"
"os"
"path/filepath"
"testing"

"github.com/stretchr/testify/require"
)

func TestCreateCache(t *testing.T) {
t.Log("E2E: Create Cache")

e2e.setup(t)
defer e2e.teardown(t)

// run `zarf package create` with a specified image cache location
cachePath := filepath.Join(os.TempDir(), ".cache-location")

e2e.cleanFiles(cachePath)
// defer the cleanFiles action because of how the zarf command is lauched as a separate process
// and may return earlier clearing the cache and not properly checking for a failure
defer e2e.cleanFiles(cachePath)

pkgName := fmt.Sprintf("zarf-package-git-data-%s-v1.0.0.tar.zst", e2e.arch)

// Test that not specifying a package variable results in an error
stdOut, stdErr, err := e2e.execZarfCommand("package", "create", "examples/git-data", "--confirm", "--zarf-cache", cachePath)
require.NoError(t, err, stdOut, stdErr)

// Test that the cache can be used at least once
stdOut, stdErr, err = e2e.execZarfCommand("package", "create", "examples/git-data", "--confirm", "--zarf-cache", cachePath)
require.NoError(t, err, stdOut, stdErr)

// Test that the cache is not corrupted when used
stdOut, stdErr, err = e2e.execZarfCommand("package", "create", "examples/git-data", "--confirm", "--zarf-cache", cachePath)
require.NoError(t, err, stdOut, stdErr)

e2e.cleanFiles(pkgName)
}

0 comments on commit ee635c2

Please sign in to comment.