@@ -302,7 +302,7 @@ func (r *Repository) ObjectExists(ctx context.Context, obj string) error {
302302// if ref is commit hash then pathspec will be ignored.
303303// if rmGitDir is true `.git` folder will be deleted after the clone.
304304// if dst not empty all its contents will be removed.
305- func (r * Repository ) Clone (ctx context.Context , dst , ref , pathspec string , rmGitDir bool ) (string , error ) {
305+ func (r * Repository ) Clone (ctx context.Context , dst , ref string , pathspecs [] string , rmGitDir bool ) (string , error ) {
306306 if ref == "" {
307307 ref = "HEAD"
308308 }
@@ -330,26 +330,17 @@ func (r *Repository) Clone(ctx context.Context, dst, ref, pathspec string, rmGit
330330 r .lock .RLock ()
331331 defer r .lock .RUnlock ()
332332
333- if IsCommitHash (ref ) {
334- return r .cloneByRef (ctx , dst , ref , pathspec , rmGitDir )
335- }
336- return r .cloneByBranch (ctx , dst , ref , pathspec , rmGitDir )
337- }
338-
339- func (r * Repository ) cloneByBranch (ctx context.Context , dst , branch , pathspec string , rmGitDir bool ) (string , error ) {
340- args := []string {"clone" , "--no-checkout" , "--single-branch" }
341- if branch != "HEAD" {
342- args = append (args , "-b" , branch )
343- }
344- args = append (args , r .dir , dst )
345- // git clone --no-checkout --single-branch [-b <branch>] <remote> <dst>
333+ // create a thin clone of a repository that only contains the history of the given revision
334+ // git clone --no-checkout --revision <ref> <repo_src> <dst>
335+ args := []string {"clone" , "--no-checkout" , "--revision" , ref , r .dir , dst }
346336 if _ , err := runGitCommand (ctx , r .log , nil , "" , args ... ); err != nil {
347337 return "" , err
348338 }
349339
350- args = []string {"checkout" , branch }
351- if pathspec != "" {
352- args = append (args , "--" , pathspec )
340+ args = []string {"checkout" , "HEAD" }
341+ if len (pathspecs ) > 0 {
342+ args = append (args , "--" )
343+ args = append (args , pathspecs ... )
353344 }
354345 // git checkout <branch> -- <pathspec>
355346 if _ , err := runGitCommand (ctx , r .log , nil , dst , args ... ); err != nil {
@@ -358,43 +349,7 @@ func (r *Repository) cloneByBranch(ctx context.Context, dst, branch, pathspec st
358349
359350 // get the hash of the repos HEAD
360351 args = []string {"log" , "--pretty=format:%H" , "-n" , "1" , "HEAD" }
361- if pathspec != "" {
362- args = append (args , "--" , pathspec )
363- }
364- // git log --pretty=format:%H -n 1 HEAD [-- <path>]
365- hash , err := runGitCommand (ctx , r .log , nil , dst , args ... )
366- if err != nil {
367- return "" , err
368- }
369-
370- if rmGitDir {
371- if err := os .RemoveAll (filepath .Join (dst , ".git" )); err != nil {
372- return "" , fmt .Errorf ("unable to delete git dir err:%w" , err )
373- }
374- }
375-
376- return hash , nil
377- }
378-
379- func (r * Repository ) cloneByRef (ctx context.Context , dst , ref , pathspec string , rmGitDir bool ) (string , error ) {
380- // git clone --no-checkout <remote> <dst>
381- if _ , err := runGitCommand (ctx , r .log , nil , "" , "clone" , "--no-checkout" , r .dir , dst ); err != nil {
382- return "" , err
383- }
384-
385- args := []string {"reset" , "--hard" , ref }
386- // git reset --hard <ref>
387- if _ , err := runGitCommand (ctx , r .log , nil , dst , args ... ); err != nil {
388- return "" , err
389- }
390-
391- // get the hash of the repos HEAD
392- args = []string {"log" , "--pretty=format:%H" , "-n" , "1" , "HEAD" }
393- if pathspec != "" {
394- args = append (args , "--" , pathspec )
395- }
396-
397- // git log --pretty=format:%H -n 1 HEAD [-- <path>]
352+ // git log --pretty=format:%H -n 1 HEAD
398353 hash , err := runGitCommand (ctx , r .log , nil , dst , args ... )
399354 if err != nil {
400355 return "" , err
0 commit comments