Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use lightweight git clone #179

Merged
merged 1 commit into from
Jun 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading