Skip to content

Commit

Permalink
fix: add test
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Crenshaw <michael@crenshaw.dev>
  • Loading branch information
crenshaw-dev committed Mar 25, 2022
1 parent d90887f commit 6618e7a
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 5 deletions.
14 changes: 9 additions & 5 deletions reposerver/repository/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -1678,15 +1678,19 @@ func directoryPermissionInitializer(rootPath string) goio.Closer {
// nolint:unparam
func (s *Service) checkoutRevision(gitClient git.Client, revision string, submoduleEnabled bool) (goio.Closer, error) {
closer := s.gitRepoInitializer(gitClient.Root())
return closer, checkoutRevision(gitClient, revision, submoduleEnabled)
}

func checkoutRevision(gitClient git.Client, revision string, submoduleEnabled bool) error {
err := gitClient.Init()
if err != nil {
return closer, status.Errorf(codes.Internal, "Failed to initialize git repo: %v", err)
return status.Errorf(codes.Internal, "Failed to initialize git repo: %v", err)
}

// Fetching with no revision first. Fetching with an explicit version can cause repo bloat. https://github.com/argoproj/argo-cd/issues/8845
err = gitClient.Fetch("")
if err != nil {
return closer, status.Errorf(codes.Internal, "Failed to fetch default: %v", err)
return status.Errorf(codes.Internal, "Failed to fetch default: %v", err)
}

err = gitClient.Checkout(revision, submoduleEnabled)
Expand All @@ -1698,16 +1702,16 @@ func (s *Service) checkoutRevision(gitClient git.Client, revision string, submod

err = gitClient.Fetch(revision)
if err != nil {
return closer, status.Errorf(codes.Internal, "Failed to checkout revision %s: %v", revision, err)
return status.Errorf(codes.Internal, "Failed to checkout revision %s: %v", revision, err)
}

err = gitClient.Checkout("FETCH_HEAD", submoduleEnabled)
if err != nil {
return closer, status.Errorf(codes.Internal, "Failed to checkout FETCH_HEAD: %v", err)
return status.Errorf(codes.Internal, "Failed to checkout FETCH_HEAD: %v", err)
}
}

return closer, err
return err
}

func (s *Service) GetHelmCharts(ctx context.Context, q *apiclient.HelmChartsRequest) (*apiclient.HelmChartsResponse, error) {
Expand Down
45 changes: 45 additions & 0 deletions reposerver/repository/repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1779,3 +1779,48 @@ func TestInit(t *testing.T) {
require.Error(t, err)
require.NoError(t, initGitRepo(path.Join(dir, "repo2"), "https://github.com/argo-cd/test-repo2"))
}

// TestCheckoutRevisionCanGetNonstandardRefs shows that we can fetch a revision that points to a non-standard ref. In
// other words, we haven't regressed and caused this issue again: https://github.com/argoproj/argo-cd/issues/4935
func TestCheckoutRevision(t *testing.T) {
rootPath, err := ioutil.TempDir("", "")
require.NoError(t, err)

sourceRepoPath, err := ioutil.TempDir(rootPath, "")
require.NoError(t, err)

// Create a repo such that one commit is on a non-standard ref _and nowhere else_. This is meant to simulate, for
// example, a GitHub ref for a pull into one repo from a fork of that repo.
run(t, sourceRepoPath, "git", "init")
run(t, sourceRepoPath, "git", "checkout", "-b", "main") // make sure there's a main branch to switch back to
run(t, sourceRepoPath, "git", "commit", "-m", "empty", "--allow-empty")
run(t, sourceRepoPath, "git", "checkout", "-b", "branch")
run(t, sourceRepoPath, "git", "commit", "-m", "empty", "--allow-empty")
sha := run(t, sourceRepoPath, "git", "rev-parse", "HEAD")
run(t, sourceRepoPath, "git", "update-ref", "refs/pull/123/head", strings.TrimSuffix(sha, "\n"))
run(t, sourceRepoPath, "git", "checkout", "main")
run(t, sourceRepoPath, "git", "branch", "-D", "branch")

destRepoPath, err := ioutil.TempDir(rootPath, "")
require.NoError(t, err)

gitClient, err := git.NewClientExt("file://"+sourceRepoPath, destRepoPath, &git.NopCreds{}, true, false, "")
require.NoError(t, err)

pullSha, err := gitClient.LsRemote("refs/pull/123/head")
require.NoError(t, err)

err = checkoutRevision(gitClient, pullSha, false)
require.NoError(t, err)
}

// run runs a command in the given working directory. If the command succeeds, it returns the combined standard and
// error output. If it fails, it stops the test with a failure message.
func run(t *testing.T, workDir string, command string, args ...string) string {
cmd := exec.Command(command, args...)
cmd.Dir = workDir
out, err := cmd.CombinedOutput()
stringOut := string(out)
require.NoError(t, err, stringOut)
return stringOut
}

0 comments on commit 6618e7a

Please sign in to comment.