Skip to content

Commit

Permalink
Merge pull request #295 from cpanato/update-preparefork
Browse files Browse the repository at this point in the history
add option to not update the repo
  • Loading branch information
k8s-ci-robot committed Dec 23, 2023
2 parents f8ebf35 + 10c7701 commit 80e0fd7
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 21 deletions.
32 changes: 17 additions & 15 deletions git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -378,19 +378,19 @@ func CloneOrOpenDefaultGitHubRepoSSH(repoPath string) (*Repo, error) {

// CleanCloneGitHubRepo creates a guaranteed fresh checkout of a given repository. The returned *Repo has a Cleanup()
// method that should be used to delete the repository on-disk afterwards.
func CleanCloneGitHubRepo(owner, repo string, useSSH bool, opts *git.CloneOptions) (*Repo, error) {
func CleanCloneGitHubRepo(owner, repo string, useSSH, updateRepo bool, opts *git.CloneOptions) (*Repo, error) {
repoURL := GetRepoURL(owner, repo, useSSH)
// The use of a blank string for the repo path triggers special behaviour in CloneOrOpenRepo that causes a true
// temporary directory with a random name to be created.
return CloneOrOpenRepo("", repoURL, useSSH, opts)
return CloneOrOpenRepo("", repoURL, useSSH, updateRepo, opts)
}

// CloneOrOpenGitHubRepo works with a repository in the given directory, or creates one if the directory is empty. The
// repo uses the provided GitHub repository via the owner and repo. If useSSH is true, then it will clone the
// repository using the defaultGithubAuthRoot.
func CloneOrOpenGitHubRepo(repoPath, owner, repo string, useSSH bool) (*Repo, error) {
repoURL := GetRepoURL(owner, repo, useSSH)
return CloneOrOpenRepo(repoPath, repoURL, useSSH, nil)
return CloneOrOpenRepo(repoPath, repoURL, useSSH, true, nil)
}

// ShallowCleanCloneGitHubRepo creates a guaranteed fresh checkout of a GitHub
Expand All @@ -415,7 +415,7 @@ func ShallowCloneOrOpenGitHubRepo(owner, repoPath string, useSSH bool) (*Repo, e
// repository already exists in repoPath, then no clone is done and the function
// returns the existing repository.
func ShallowCloneOrOpenRepo(repoPath, repoURL string, useSSH bool) (*Repo, error) { //nolint: revive
return cloneOrOpenRepo(repoPath, repoURL, &git.CloneOptions{Depth: 1})
return cloneOrOpenRepo(repoPath, repoURL, true, &git.CloneOptions{Depth: 1})
}

// CloneOrOpenRepo creates a temp directory containing the provided
Expand All @@ -425,14 +425,14 @@ func ShallowCloneOrOpenRepo(repoPath, repoURL string, useSSH bool) (*Repo, error
//
// The function returns the repository if cloning or updating of the repository
// was successful, otherwise an error.
func CloneOrOpenRepo(repoPath, repoURL string, useSSH bool, opts *git.CloneOptions) (*Repo, error) { //nolint: revive
return cloneOrOpenRepo(repoPath, repoURL, opts)
func CloneOrOpenRepo(repoPath, repoURL string, useSSH, updateRepo bool, opts *git.CloneOptions) (*Repo, error) { //nolint: revive
return cloneOrOpenRepo(repoPath, repoURL, updateRepo, opts)
}

// cloneOrOpenRepo checks that the repoPath exists or creates it before running the
// clone operation and connects the clone progress writer to our logging system
// if needed. This function is the core of the *CloneOrOpenRepo functions.
func cloneOrOpenRepo(repoPath, repoURL string, opts *git.CloneOptions) (*Repo, error) {
func cloneOrOpenRepo(repoPath, repoURL string, updateRepository bool, opts *git.CloneOptions) (*Repo, error) {
// Ensure we have a directory path
targetDir, preexisting, err := ensureRepoPath(repoPath)
if err != nil {
Expand All @@ -441,7 +441,7 @@ func cloneOrOpenRepo(repoPath, repoURL string, opts *git.CloneOptions) (*Repo, e

// If the repo already exists, just update it
if preexisting {
return updateRepo(targetDir)
return updateRepo(targetDir, true)
}

if opts == nil {
Expand Down Expand Up @@ -475,7 +475,7 @@ func cloneOrOpenRepo(repoPath, repoURL string, opts *git.CloneOptions) (*Repo, e
return nil, err
}

return updateRepo(targetDir)
return updateRepo(targetDir, updateRepository)
}

// cloneRepository is a utility function that exposes the bare git clone
Expand Down Expand Up @@ -526,17 +526,19 @@ func ensureRepoPath(repoPath string) (targetDir string, exisitingDir bool, err e

// updateRepo tries to open the provided repoPath and fetches the latest
// changes from the configured remote location
func updateRepo(repoPath string) (*Repo, error) {
func updateRepo(repoPath string, updateRepository bool) (*Repo, error) {
r, err := OpenRepo(repoPath)
if err != nil {
return nil, err
}

// Update the repo
if err := filterCommand(
r.Dir(), "pull", "--rebase",
).RunSilentSuccess(); err != nil {
return nil, fmt.Errorf("unable to pull from remote: %w", err)
if updateRepository {
// Update the repo
if err := filterCommand(
r.Dir(), "pull", "--rebase",
).RunSilentSuccess(); err != nil {
return nil, fmt.Errorf("unable to pull from remote: %w", err)
}
}

return r, nil
Expand Down
4 changes: 2 additions & 2 deletions git/git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@ func TestFetchRemote(t *testing.T) {
defer originRepo.Cleanup() //nolint: errcheck

// Create a new clone of the original repo
testRepo, err := git.CloneOrOpenRepo("", rawRepoDir, false, nil)
testRepo, err := git.CloneOrOpenRepo("", rawRepoDir, false, true, nil)
require.Nil(t, err)
defer testRepo.Cleanup() //nolint: errcheck

Expand Down Expand Up @@ -530,7 +530,7 @@ func TestRebase(t *testing.T) {
defer originRepo.Cleanup() //nolint: errcheck

// Create a new clone of the original repo
testRepo, err := git.CloneOrOpenRepo("", rawRepoDir, false, nil)
testRepo, err := git.CloneOrOpenRepo("", rawRepoDir, false, true, nil)
require.Nil(t, err)
defer testRepo.Cleanup() //nolint: errcheck

Expand Down
4 changes: 2 additions & 2 deletions github/fork.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ const (
)

// PrepareFork prepares a branch from a repo fork
func PrepareFork(branchName, upstreamOrg, upstreamRepo, myOrg, myRepo string, useSSH bool, opts *gogit.CloneOptions) (repo *git.Repo, err error) {
func PrepareFork(branchName, upstreamOrg, upstreamRepo, myOrg, myRepo string, useSSH, updateRepo bool, opts *gogit.CloneOptions) (repo *git.Repo, err error) {
// checkout the upstream repository
logrus.Infof("Cloning/updating repository %s/%s", upstreamOrg, upstreamRepo)

repo, err = git.CleanCloneGitHubRepo(
upstreamOrg, upstreamRepo, false, opts,
upstreamOrg, upstreamRepo, false, updateRepo, opts,
)
if err != nil {
return nil, fmt.Errorf("cloning %s/%s: %w", upstreamOrg, upstreamRepo, err)
Expand Down
4 changes: 2 additions & 2 deletions test/integration/git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ func newTestRepo(t *testing.T) *testRepo {
require.Nil(t, os.RemoveAll(cloneTempDir))

// Provide a system under test inside the test repo
sut, err := git.CloneOrOpenRepo("", bareTempDir, false, nil)
sut, err := git.CloneOrOpenRepo("", bareTempDir, false, true, nil)
require.Nil(t, err)
require.Nil(t, command.NewWithWorkDir(
sut.Dir(), "git", "checkout", branchName,
Expand Down Expand Up @@ -239,7 +239,7 @@ func TestSuccessCloneOrOpen(t *testing.T) {
testRepo := newTestRepo(t)
defer testRepo.cleanup(t)

secondRepo, err := git.CloneOrOpenRepo(testRepo.sut.Dir(), testRepo.dir, false, nil)
secondRepo, err := git.CloneOrOpenRepo(testRepo.sut.Dir(), testRepo.dir, false, true, nil)
require.Nil(t, err)

require.Equal(t, secondRepo.Dir(), testRepo.sut.Dir())
Expand Down

0 comments on commit 80e0fd7

Please sign in to comment.