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

make warehouse do shallow, single-branch clones #1091

Merged
merged 3 commits into from
Nov 16, 2023
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
38 changes: 34 additions & 4 deletions internal/controller/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,19 @@ type repo struct {
currentBranch string
}

// CloneOptions represents options for cloning a git repository.
type CloneOptions struct {
// Branch is the name of the branch to clone. If not specified, the default
// branch will be cloned.
Branch string
// SingleBranch indicates whether the clone should be a single-branch clone.
SingleBranch bool
// Shallow indicates whether the clone should be with a depth of 1. This is
// useful for speeding up the cloning process when all we care about is the
// latest commit from a single branch.
Shallow bool
Comment on lines +105 to +108
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since I only see CloneOptions used one place, is it true that this PR makes it such that all clones performed by kargo are shallow?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No... only the Warehouse reconciler is exercising these options.

The Promotion reconciler still does a full clone (nil options).

}

// Clone produces a local clone of the remote git repository at the specified
// URL and returns an implementation of the Repo interface that is stateful and
// NOT suitable for use across multiple goroutines. This function will also
Expand All @@ -103,6 +116,7 @@ type repo struct {
func Clone(
repoURL string,
repoCreds RepoCredentials,
opts *CloneOptions,
) (Repo, error) {
homeDir, err := os.MkdirTemp("", "")
if err != nil {
Expand All @@ -120,7 +134,7 @@ func Clone(
if err = r.setupAuth(repoCreds); err != nil {
return nil, err
}
return r, r.clone()
return r, r.clone(opts)
}

func (r *repo) AddAll() error {
Expand All @@ -140,9 +154,25 @@ func (r *repo) Clean() error {
return errors.Wrapf(err, "error cleaning branch %q", r.currentBranch)
}

func (r *repo) clone() error {
r.currentBranch = "HEAD"
cmd := r.buildCommand("clone", "--no-tags", r.url, r.dir)
func (r *repo) clone(opts *CloneOptions) error {
if opts == nil {
opts = &CloneOptions{}
}
args := []string{"clone", "--no-tags"}
if opts.Branch != "" {
args = append(args, "--branch", opts.Branch)
r.currentBranch = opts.Branch
} else {
r.currentBranch = "HEAD"
}
if opts.SingleBranch {
args = append(args, "--single-branch")
}
if opts.Shallow {
args = append(args, "--depth=1")
}
args = append(args, r.url, r.dir)
cmd := r.buildCommand(args...)
cmd.Dir = r.homeDir // Override the cmd.Dir that's set by r.buildCommand()
_, err := libExec.Exec(cmd)
return errors.Wrapf(
Expand Down
2 changes: 1 addition & 1 deletion internal/controller/promotion/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ func (g *gitMechanism) gitCommit(
if creds == nil {
creds = &git.RepoCredentials{}
}
repo, err := git.Clone(update.RepoURL, *creds)
repo, err := git.Clone(update.RepoURL, *creds, nil)
if err != nil {
return "", errors.Wrapf(err, "error cloning git repo %q", update.RepoURL)
}
Expand Down
20 changes: 9 additions & 11 deletions internal/controller/warehouses/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,19 +84,17 @@ func getLatestCommitMeta(
if creds == nil {
creds = &git.RepoCredentials{}
}
repo, err := git.Clone(repoURL, *creds)
repo, err := git.Clone(
repoURL,
*creds,
&git.CloneOptions{
Branch: branch,
SingleBranch: true,
Shallow: true,
},
)
if err != nil {
return nil, errors.Wrapf(err, "error cloning git repo %q", repoURL)

}
if branch != "" {
if err = repo.Checkout(branch); err != nil {
return nil, errors.Wrapf(
err,
"error checking out branch %q from git repo",
repoURL,
)
}
}
var gm gitMeta
gm.Commit, err = repo.LastCommitID()
Expand Down
11 changes: 0 additions & 11 deletions internal/controller/warehouses/git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,17 +155,6 @@ func TestGetLatestCommitID(t *testing.T) {
require.Contains(t, err.Error(), "error cloning git repo")
},
},

{
name: "error checking out branch",
repoURL: "https://github.com/akuity/kargo.git",
branch: "bogus", // This should force a failure
assertions: func(_ *gitMeta, err error) {
require.Error(t, err)
require.Contains(t, err.Error(), "error checking out branch")
},
},

{
name: "success",
repoURL: "https://github.com/akuity/kargo.git",
Expand Down