Skip to content

Commit

Permalink
track branches before fetch (#180)
Browse files Browse the repository at this point in the history
  • Loading branch information
kzadorozhny authored Jun 25, 2024
1 parent f3ae719 commit 6c99281
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions gitops/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ 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"}
remoteName := "origin"
args := []string{"clone", "--no-checkout", "--single-branch", "--branch", primaryBranch, "--filter=blob:none", "--no-tags", "--origin", remoteName}
if mirrorDir != "" {
args = append(args, "--reference", mirrorDir)
}
Expand All @@ -48,9 +49,9 @@ func Clone(repo, dir, mirrorDir, primaryBranch, gitopsPath string) (*Repo, error
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 @@ -59,6 +60,8 @@ 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
Expand All @@ -67,9 +70,10 @@ func (r *Repo) Clean() error {
}

// 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) {
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)
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.
Expand Down Expand Up @@ -124,6 +128,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...)
}

0 comments on commit 6c99281

Please sign in to comment.