Skip to content

Commit

Permalink
Merge branch 'main' into randrei/incrementalStamping
Browse files Browse the repository at this point in the history
  • Loading branch information
randrei-adobe committed Jun 26, 2024
2 parents 7379e81 + 6c99281 commit 2e73b01
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
23 changes: 17 additions & 6 deletions gitops/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,22 @@ 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)
}
remoteName := "origin"
args := []string{"clone", "--no-checkout", "--single-branch", "--branch", primaryBranch, "--filter=blob:none", "--no-tags", "--origin", remoteName}
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 {
return nil, fmt.Errorf("Unable to create .git/info/sparse-checkout: %w", err)
}
exec.Mustex(dir, "git", "checkout", primaryBranch)

return &Repo{
Dir: dir,
Dir: dir,
RemoteName: remoteName,
}, nil
}

Expand All @@ -60,13 +62,22 @@ func Clone(repo, dir, mirrorDir, primaryBranch, gitopsPath string) (*Repo, error
type Repo struct {
// Dir is the location of the git repo.
Dir string
// RemoteName is the name of the remote that tracks upstream repository.
RemoteName string
}

// Clean cleans up the repo
func (r *Repo) Clean() error {
return os.RemoveAll(r.Dir)
}

// Fetch branches from the remote repository based on a specified pattern.
// The branches will be be added to the list tracked remote branches ready to be pushed.
func (r *Repo) Fetch(pattern string) {
exec.Mustex(r.Dir, "git", "remote", "set-branches", "--add", r.RemoteName, pattern)
exec.Mustex(r.Dir, "git", "fetch", "--force", "--filter=blob:none", "--no-tags", r.RemoteName)
}

// 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 Expand Up @@ -150,6 +161,6 @@ func (r *Repo) IsClean() bool {
// Push pushes all local changes to the remote repository
// all changes should be already commited
func (r *Repo) Push(branches []string) {
args := append([]string{"push", "origin", "-f", "--set-upstream"}, branches...)
args := append([]string{"push", r.RemoteName, "-f", "--set-upstream"}, branches...)
exec.Mustex(r.Dir, "git", args...)
}
4 changes: 3 additions & 1 deletion gitops/prer/create_gitops_prs.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,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 @@ -195,13 +196,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 2e73b01

Please sign in to comment.