Skip to content

Commit

Permalink
use lightweight checkout (#179)
Browse files Browse the repository at this point in the history
  • Loading branch information
kzadorozhny authored Jun 21, 2024
1 parent fa05503 commit f3ae719
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
13 changes: 10 additions & 3 deletions gitops/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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) {
Expand Down
4 changes: 3 additions & 1 deletion gitops/prer/create_gitops_prs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit f3ae719

Please sign in to comment.