From f3ae719355057444e1f777a985d71d70f3001874 Mon Sep 17 00:00:00 2001 From: Konstantin Zadorozhny Date: Fri, 21 Jun 2024 16:16:04 -0700 Subject: [PATCH] use lightweight checkout (#179) --- gitops/git/git.go | 13 ++++++++++--- gitops/prer/create_gitops_prs.go | 4 +++- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/gitops/git/git.go b/gitops/git/git.go index 78181649..a89003f9 100644 --- a/gitops/git/git.go +++ b/gitops/git/git.go @@ -36,11 +36,12 @@ func Clone(repo, dir, mirrorDir, primaryBranch, gitopsPath string) (*Repo, error if err := os.RemoveAll(dir); err != nil { return nil, fmt.Errorf("Unable to clone repo: %w", err) } + args := []string{"clone", "--no-checkout", "--single-branch", "--branch", primaryBranch, "--filter=blob:none", "--no-tags"} if mirrorDir != "" { - exec.Mustex("", "git", "clone", "-n", "--reference", mirrorDir, repo, dir) - } else { - exec.Mustex("", "git", "clone", "-n", repo, dir) + args = append(args, "--reference", mirrorDir) } + args = append(args, repo, dir) + exec.Mustex("", "git", args...) exec.Mustex(dir, "git", "config", "--local", "core.sparsecheckout", "true") genPath := fmt.Sprintf("%s/\n", gitopsPath) if err := ioutil.WriteFile(filepath.Join(dir, ".git/info/sparse-checkout"), []byte(genPath), 0644); err != nil { @@ -65,6 +66,12 @@ func (r *Repo) Clean() error { return os.RemoveAll(r.Dir) } +// Fetch branches from the remote repository based on a specified pattern. +func (r *Repo) Fetch(pattern string) { + refSpec := fmt.Sprintf("+refs/heads/%s:refs/remotes/origin/%s", pattern, pattern) + exec.Mustex(r.Dir, "git", "fetch", "--force", "--filter=blob:none", "--no-tags", "origin", refSpec) +} + // SwitchToBranch switch the repo to specified branch and checkout primaryBranch files over it. // if branch does not exist it will be created func (r *Repo) SwitchToBranch(branch, primaryBranch string) (new bool) { diff --git a/gitops/prer/create_gitops_prs.go b/gitops/prer/create_gitops_prs.go index 512d409e..a0d660b0 100644 --- a/gitops/prer/create_gitops_prs.go +++ b/gitops/prer/create_gitops_prs.go @@ -65,6 +65,7 @@ var ( prTitle = flag.String("gitops_pr_title", "", "a title for deployment PR") branchName = flag.String("branch_name", "unknown", "Branch name to use in commit message") gitCommit = flag.String("git_commit", "unknown", "Git commit to use in commit message") + deploymentBranchPrefix = flag.String("deployment_branch_prefix", "deploy/", "the prefix to add to all deployment branch names") deploymentBranchSuffix = flag.String("deployment_branch_suffix", "", "suffix to add to all deployment branch names") gitHost = flag.String("git_server", "bitbucket", "the git server api to use. 'bitbucket', 'github' or 'gitlab'") gitopsKind SliceFlags @@ -156,13 +157,14 @@ func main() { if err != nil { log.Fatalf("Unable to clone repo: %v", err) } + workdir.Fetch(*deploymentBranchPrefix + "*") var updatedGitopsTargets []string var updatedGitopsBranches []string for train, targets := range releaseTrains { log.Println("train", train) - branch := fmt.Sprintf("deploy/%s%s", train, *deploymentBranchSuffix) + branch := *deploymentBranchPrefix + train + *deploymentBranchSuffix newBranch := workdir.SwitchToBranch(branch, *prInto) if !newBranch { // Find if we need to recreate the branch because target was deleted