Browse code

Changes the signature of CloneWithOptions function

We change it to a packed string from a boolean struct. This allows us
to use every option for clone, creating more utility for the package.

oatmealraisin authored on 2016/08/12 07:00:04
Showing 5 changed files
... ...
@@ -30,7 +30,7 @@ type KeyValue struct {
30 30
 
31 31
 // GitClient performs git operations
32 32
 type GitClient interface {
33
-	CloneWithOptions(dir string, url string, opts git.CloneOptions) error
33
+	CloneWithOptions(dir string, url string, args ...string) error
34 34
 	Checkout(dir string, ref string) error
35 35
 	SubmoduleUpdate(dir string, init, recursive bool) error
36 36
 	TimedListRemote(timeout time.Duration, url string, args ...string) (string, string, error)
... ...
@@ -212,12 +212,23 @@ func extractGitSource(gitClient GitClient, gitSource *api.GitBuildSource, revisi
212 212
 		return true, err
213 213
 	}
214 214
 
215
-	// check if we specify a commit, ref, or branch to check out
215
+	cloneOptions := []string{}
216 216
 	usingRef := len(gitSource.Ref) != 0 || (revision != nil && revision.Git != nil && len(revision.Git.Commit) != 0)
217 217
 
218
+	// check if we specify a commit, ref, or branch to check out
219
+	// Recursive clone if we're not going to checkout a ref and submodule update later
220
+	if !usingRef {
221
+		cloneOptions = append(cloneOptions, "--recursive")
222
+		cloneOptions = append(cloneOptions, git.Shallow)
223
+	}
224
+
225
+	glog.V(3).Infof("Cloning source from %s", gitSource.URI)
226
+
218 227
 	// Only use the quiet flag if Verbosity is not 5 or greater
219
-	quiet := !glog.Is(5)
220
-	if err := gitClient.CloneWithOptions(dir, gitSource.URI, git.CloneOptions{Recursive: !usingRef, Quiet: quiet, Shallow: !usingRef}); err != nil {
228
+	if !glog.Is(5) {
229
+		cloneOptions = append(cloneOptions, "--quiet")
230
+	}
231
+	if err := gitClient.CloneWithOptions(dir, gitSource.URI, cloneOptions...); err != nil {
221 232
 		return true, err
222 233
 	}
223 234
 
... ...
@@ -514,7 +514,7 @@ func StrategyAndSourceForRepository(repo *SourceRepository, image *ImageRef) (*B
514 514
 func CloneAndCheckoutSources(repo git.Repository, remote, ref, localDir, contextDir string) (string, error) {
515 515
 	if len(ref) == 0 {
516 516
 		glog.V(5).Infof("No source ref specified, using shallow git clone")
517
-		if err := repo.CloneWithOptions(localDir, remote, git.CloneOptions{Recursive: true, Shallow: true}); err != nil {
517
+		if err := repo.CloneWithOptions(localDir, remote, git.Shallow, "--recursive"); err != nil {
518 518
 			return "", fmt.Errorf("shallow cloning repository %q to %q failed: %v", remote, localDir, err)
519 519
 		}
520 520
 	} else {
... ...
@@ -33,7 +33,7 @@ func (g *FakeGit) Clone(dir string, url string) error {
33 33
 	return nil
34 34
 }
35 35
 
36
-func (g *FakeGit) CloneWithOptions(dir string, url string, opts git.CloneOptions) error {
36
+func (g *FakeGit) CloneWithOptions(dir string, url string, args ...string) error {
37 37
 	g.CloneCalled = true
38 38
 	return nil
39 39
 }
... ...
@@ -25,7 +25,7 @@ type Repository interface {
25 25
 	GetOriginURL(dir string) (string, bool, error)
26 26
 	GetRef(dir string) string
27 27
 	Clone(dir string, url string) error
28
-	CloneWithOptions(dir string, url string, opts CloneOptions) error
28
+	CloneWithOptions(dir string, url string, args ...string) error
29 29
 	CloneBare(dir string, url string) error
30 30
 	CloneMirror(dir string, url string) error
31 31
 	Fetch(dir string) error
... ...
@@ -45,6 +45,10 @@ const (
45 45
 	// defaultCommandTimeout is the default timeout for git commands that we want to enforce timeouts on
46 46
 	defaultCommandTimeout = 30 * time.Second
47 47
 
48
+	// Shallow maps to --depth=1, which clones a Git repository without
49
+	// downloading history
50
+	Shallow = "--depth=1"
51
+
48 52
 	// noCommandTimeout signals that there should be no timeout for the command when passed as the timeout
49 53
 	// for the default timedExecGitFunc
50 54
 	noCommandTimeout = 0 * time.Second
... ...
@@ -59,15 +63,6 @@ type SourceInfo struct {
59 59
 	s2iapi.SourceInfo
60 60
 }
61 61
 
62
-// CloneOptions are options used in cloning a git repository
63
-type CloneOptions struct {
64
-	Recursive bool
65
-	Quiet     bool
66
-
67
-	// Shallow perform a shallow git clone that only fetches the latest master.
68
-	Shallow bool
69
-}
70
-
71 62
 // execGitFunc is a function that executes a Git command
72 63
 type execGitFunc func(dir string, args ...string) (string, string, error)
73 64
 
... ...
@@ -206,27 +201,38 @@ func (r *repository) AddLocalConfig(location, name, value string) error {
206 206
 }
207 207
 
208 208
 // CloneWithOptions clones a remote git repository to a local directory
209
-func (r *repository) CloneWithOptions(location string, url string, opts CloneOptions) error {
210
-	args := []string{"clone"}
211
-	if opts.Quiet {
212
-		args = append(args, "--quiet")
213
-	}
214
-	if opts.Recursive {
215
-		args = append(args, "--recursive")
216
-	}
217
-	if opts.Shallow {
218
-		args = append(args, "--depth=1")
219
-		r.shallow = true
209
+func (r *repository) CloneWithOptions(location string, url string, args ...string) error {
210
+	gitArgs := []string{"clone"}
211
+	gitArgs = append(gitArgs, args...)
212
+	gitArgs = append(gitArgs, url)
213
+	gitArgs = append(gitArgs, location)
214
+
215
+	// We need to check to see if we're importing reference information, for
216
+	// for error checking later on
217
+	for _, opt := range gitArgs {
218
+		if opt == Shallow {
219
+			r.shallow = true
220
+			break
221
+		}
220 222
 	}
221
-	args = append(args, url)
222
-	args = append(args, location)
223
-	_, _, err := r.git("", args...)
223
+
224
+	_, _, err := r.git("", gitArgs...)
224 225
 	return err
225 226
 }
226 227
 
227 228
 // Clone clones a remote git repository to a local directory
228 229
 func (r *repository) Clone(location string, url string) error {
229
-	return r.CloneWithOptions(location, url, CloneOptions{Recursive: true})
230
+	return r.CloneWithOptions(location, url, "--recursive")
231
+}
232
+
233
+// CloneMirror clones a remote git repository to a local directory as a mirror
234
+func (r *repository) CloneMirror(location string, url string) error {
235
+	return r.CloneWithOptions(location, url, "--mirror")
236
+}
237
+
238
+// CloneBare clones a remote git repository to a local directory
239
+func (r *repository) CloneBare(location string, url string) error {
240
+	return r.CloneWithOptions(location, url, "--bare")
230 241
 }
231 242
 
232 243
 // ListRemote lists references in a remote repository
... ...
@@ -247,18 +253,6 @@ func (r *repository) TimedListRemote(timeout time.Duration, url string, args ...
247 247
 	return r.timedGit(timeout, "", gitArgs...)
248 248
 }
249 249
 
250
-// CloneMirror clones a remote git repository to a local directory as a mirror
251
-func (r *repository) CloneMirror(location string, url string) error {
252
-	_, _, err := r.git("", "clone", "--mirror", url, location)
253
-	return err
254
-}
255
-
256
-// CloneBare clones a remote git repository to a local directory
257
-func (r *repository) CloneBare(location string, url string) error {
258
-	_, _, err := r.git("", "clone", "--bare", url, location)
259
-	return err
260
-}
261
-
262 250
 // Fetch updates the provided git repository
263 251
 func (r *repository) Fetch(location string) error {
264 252
 	_, _, err := r.git(location, "fetch", "--all")