Skip to content

Changes the signature of CloneWithOptions function #10366

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

Merged
merged 1 commit into from
Sep 21, 2016
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
2 changes: 1 addition & 1 deletion pkg/build/builder/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ type KeyValue struct {

// GitClient performs git operations
type GitClient interface {
CloneWithOptions(dir string, url string, opts git.CloneOptions) error
CloneWithOptions(dir string, url string, args ...string) error
Checkout(dir string, ref string) error
SubmoduleUpdate(dir string, init, recursive bool) error
TimedListRemote(timeout time.Duration, url string, args ...string) (string, string, error)
Expand Down
17 changes: 14 additions & 3 deletions pkg/build/builder/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,12 +212,23 @@ func extractGitSource(gitClient GitClient, gitSource *api.GitBuildSource, revisi
return true, err
}

// check if we specify a commit, ref, or branch to check out
cloneOptions := []string{}
usingRef := len(gitSource.Ref) != 0 || (revision != nil && revision.Git != nil && len(revision.Git.Commit) != 0)

// check if we specify a commit, ref, or branch to check out
// Recursive clone if we're not going to checkout a ref and submodule update later
if !usingRef {
cloneOptions = append(cloneOptions, "--recursive")
cloneOptions = append(cloneOptions, git.Shallow)
}

glog.V(3).Infof("Cloning source from %s", gitSource.URI)

// Only use the quiet flag if Verbosity is not 5 or greater
quiet := !glog.Is(5)
if err := gitClient.CloneWithOptions(dir, gitSource.URI, git.CloneOptions{Recursive: !usingRef, Quiet: quiet, Shallow: !usingRef}); err != nil {
if !glog.Is(5) {
cloneOptions = append(cloneOptions, "--quiet")
}
if err := gitClient.CloneWithOptions(dir, gitSource.URI, cloneOptions...); err != nil {
return true, err
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/generate/app/sourcelookup.go
Original file line number Diff line number Diff line change
Expand Up @@ -514,7 +514,7 @@ func StrategyAndSourceForRepository(repo *SourceRepository, image *ImageRef) (*B
func CloneAndCheckoutSources(repo git.Repository, remote, ref, localDir, contextDir string) (string, error) {
if len(ref) == 0 {
glog.V(5).Infof("No source ref specified, using shallow git clone")
if err := repo.CloneWithOptions(localDir, remote, git.CloneOptions{Recursive: true, Shallow: true}); err != nil {
if err := repo.CloneWithOptions(localDir, remote, git.Shallow, "--recursive"); err != nil {
return "", fmt.Errorf("shallow cloning repository %q to %q failed: %v", remote, localDir, err)
}
} else {
Expand Down
2 changes: 1 addition & 1 deletion pkg/generate/app/test/fakegit.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func (g *FakeGit) Clone(dir string, url string) error {
return nil
}

func (g *FakeGit) CloneWithOptions(dir string, url string, opts git.CloneOptions) error {
func (g *FakeGit) CloneWithOptions(dir string, url string, args ...string) error {
g.CloneCalled = true
return nil
}
Expand Down
68 changes: 31 additions & 37 deletions pkg/generate/git/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type Repository interface {
GetOriginURL(dir string) (string, bool, error)
GetRef(dir string) string
Clone(dir string, url string) error
CloneWithOptions(dir string, url string, opts CloneOptions) error
CloneWithOptions(dir string, url string, args ...string) error
CloneBare(dir string, url string) error
CloneMirror(dir string, url string) error
Fetch(dir string) error
Expand All @@ -45,6 +45,10 @@ const (
// defaultCommandTimeout is the default timeout for git commands that we want to enforce timeouts on
defaultCommandTimeout = 30 * time.Second

// Shallow maps to --depth=1, which clones a Git repository without
// downloading history
Shallow = "--depth=1"

// noCommandTimeout signals that there should be no timeout for the command when passed as the timeout
// for the default timedExecGitFunc
noCommandTimeout = 0 * time.Second
Expand All @@ -59,15 +63,6 @@ type SourceInfo struct {
s2iapi.SourceInfo
}

// CloneOptions are options used in cloning a git repository
type CloneOptions struct {
Recursive bool
Quiet bool

// Shallow perform a shallow git clone that only fetches the latest master.
Shallow bool
}

// execGitFunc is a function that executes a Git command
type execGitFunc func(dir string, args ...string) (string, string, error)

Expand Down Expand Up @@ -206,27 +201,38 @@ func (r *repository) AddLocalConfig(location, name, value string) error {
}

// CloneWithOptions clones a remote git repository to a local directory
func (r *repository) CloneWithOptions(location string, url string, opts CloneOptions) error {
args := []string{"clone"}
if opts.Quiet {
args = append(args, "--quiet")
}
if opts.Recursive {
args = append(args, "--recursive")
}
if opts.Shallow {
args = append(args, "--depth=1")
r.shallow = true
func (r *repository) CloneWithOptions(location string, url string, args ...string) error {
gitArgs := []string{"clone"}
gitArgs = append(gitArgs, args...)
gitArgs = append(gitArgs, url)
gitArgs = append(gitArgs, location)

// We need to check to see if we're importing reference information, for
// for error checking later on
for _, opt := range gitArgs {
if opt == Shallow {
r.shallow = true
break
}
}
args = append(args, url)
args = append(args, location)
_, _, err := r.git("", args...)

_, _, err := r.git("", gitArgs...)
return err
}

// Clone clones a remote git repository to a local directory
func (r *repository) Clone(location string, url string) error {
return r.CloneWithOptions(location, url, CloneOptions{Recursive: true})
return r.CloneWithOptions(location, url, "--recursive")
}

// CloneMirror clones a remote git repository to a local directory as a mirror
func (r *repository) CloneMirror(location string, url string) error {
return r.CloneWithOptions(location, url, "--mirror")
}

// CloneBare clones a remote git repository to a local directory
func (r *repository) CloneBare(location string, url string) error {
return r.CloneWithOptions(location, url, "--bare")
}

// ListRemote lists references in a remote repository
Expand All @@ -247,18 +253,6 @@ func (r *repository) TimedListRemote(timeout time.Duration, url string, args ...
return r.timedGit(timeout, "", gitArgs...)
}

// CloneMirror clones a remote git repository to a local directory as a mirror
func (r *repository) CloneMirror(location string, url string) error {
_, _, err := r.git("", "clone", "--mirror", url, location)
return err
}

// CloneBare clones a remote git repository to a local directory
func (r *repository) CloneBare(location string, url string) error {
_, _, err := r.git("", "clone", "--bare", url, location)
return err
}

// Fetch updates the provided git repository
func (r *repository) Fetch(location string) error {
_, _, err := r.git(location, "fetch", "--all")
Expand Down