@@ -2,78 +2,84 @@ package git
22
33import (
44 "context"
5+ "encoding/hex"
56 "fmt"
7+ "log"
8+ "strings"
69
710 "github.com/go-git/go-git/v5"
811 "github.com/go-git/go-git/v5/plumbing"
12+ "github.com/go-git/go-git/v5/plumbing/transport/http"
13+ "helm.sh/helm/v3/pkg/repo"
914)
1015
11- func gitCheckoutImpl (ctx context.Context , repoURL , ref , destDir string ) error {
12- /*err := runCmds(destDir, [][]string{
13- {"git", "init", "--quiet"},
14- {"git", "remote", "add", "origin", repoURL},
15-
16- {"git", "config", "core.sparseCheckout", "true"},
17- {"git", "sparse-checkout", "set", path},
18- {"git", "pull", "--quiet", "--depth", "1", "origin", ref},
19-
20- //{"git", "fetch", "--quiet", "--tags", "origin"},
21- //{"git", "checkout", "--quiet", ref},
22- })*/
23- r , err := git .PlainCloneContext (ctx , destDir , false , & git.CloneOptions {
24- URL : repoURL ,
25- // TODO: Auth: ...
26- RemoteName : "origin" ,
27- SingleBranch : true ,
28- Depth : 1 ,
29- NoCheckout : true ,
30- })
16+ func gitCheckoutImpl (ctx context.Context , repoURL , ref string , repo * repo.Entry , destDir string ) error {
17+ cloneOpts := git.CloneOptions {
18+ URL : repoURL ,
19+ RemoteName : "origin" ,
20+ NoCheckout : true ,
21+ }
22+ isCommitRef := isCommitSHA (ref )
23+ if ! isCommitRef {
24+ cloneOpts .SingleBranch = true
25+ cloneOpts .Depth = 1
26+ }
27+ scheme := strings .SplitN (repoURL , ":" , 2 )[0 ]
28+ switch scheme {
29+ case "https" :
30+ cloneOpts .Auth = & http.BasicAuth {
31+ Username : repo .Username ,
32+ Password : repo .Password ,
33+ }
34+ default :
35+ if repo .Username != "" || repo .Password != "" {
36+ log .Printf ("WARNING: ignoring auth config for %s since authentication is not supported for url scheme %q" , repoURL , scheme )
37+ }
38+ }
39+ r , err := git .PlainCloneContext (ctx , destDir , false , & cloneOpts )
3140 if err != nil {
32- return err
41+ return fmt . Errorf ( "git clone: %w" , err )
3342 }
3443 tree , err := r .Worktree ()
3544 if err != nil {
3645 return fmt .Errorf ("git worktree: %w" , err )
3746 }
3847 // TODO: support sparse checkout, see https://github.com/go-git/go-git/issues/90
48+ refType := "without ref"
3949 opts := git.CheckoutOptions {}
4050 if ref != "" {
41- opts .Branch = plumbing .ReferenceName ("refs/tags/" + ref )
51+ // TODO: consider making this more usable: Currently resolving a commit as shown below doesn't work when the commit is not within the main branch (object not found).
52+ if isCommitRef {
53+ opts .Hash = plumbing .NewHash (ref )
54+ refType = fmt .Sprintf ("commit %s" , ref )
55+ } else {
56+ opts .Branch = plumbing .ReferenceName (fmt .Sprintf ("refs/tags/%s" , ref ))
57+ refType = fmt .Sprintf ("tag %s" , ref )
58+ }
4259 }
4360 err = tree .Checkout (& opts )
4461 if err != nil {
45- return err
62+ return fmt . Errorf ( "git checkout %s: %w" , refType , err )
4663 }
47- return nil
48- }
64+ /*err := runCmds(destDir, [][]string{
65+ {"git", "init", "--quiet"},
66+ {"git", "remote", "add", "origin", repoURL},
4967
50- /*
51- func runCmds(dir string, cmds [][]string) error {
52- for _, c := range cmds {
53- err := runCmd(dir, c[0], c[1:]...)
54- if err != nil {
55- return err
56- }
57- }
68+ {"git", "config", "core.sparseCheckout", "true"},
69+ {"git", "sparse-checkout", "set", path},
70+ {"git", "pull", "--quiet", "--depth", "1", "origin", ref},
71+
72+ //{"git", "fetch", "--quiet", "--tags", "origin"},
73+ //{"git", "checkout", "--quiet", ref},
74+ })*/
5875 return nil
5976}
6077
61- func runCmd(dir, cmd string, args ...string) error {
62- ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
63- defer cancel()
64- c := exec.CommandContext(ctx, cmd, args...)
65- var stderr bytes.Buffer
66- c.Stderr = &stderr
67- c.Dir = dir
68- err := c.Run()
69- if err != nil {
70- msg := strings.TrimSpace(stderr.String())
71- if msg == "" {
72- msg = err.Error()
78+ func isCommitSHA (s string ) bool {
79+ if len (s ) == 40 {
80+ if _ , err := hex .DecodeString (s ); err == nil {
81+ return true
7382 }
74- cmds := append([]string{cmd}, args...)
75- return fmt.Errorf("%s: %s", strings.Join(cmds, " "), msg)
7683 }
77- return err
84+ return false
7885}
79- */
0 commit comments