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 checkout #180

Merged
merged 1 commit into from
Jun 25, 2024
Merged
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
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...)
}
Loading