@@ -27,6 +27,7 @@ type GitGetter struct {
2727}
2828
2929var defaultBranchRegexp = regexp .MustCompile (`\s->\sorigin/(.*)` )
30+ var lsRemoteSymRefRegexp = regexp .MustCompile (`ref: refs/heads/([^\s]+).*` )
3031
3132func (g * GitGetter ) ClientMode (_ * url.URL ) (ClientMode , error ) {
3233 return ClientModeDir , nil
@@ -114,7 +115,7 @@ func (g *GitGetter) Get(dst string, u *url.URL) error {
114115 if err == nil {
115116 err = g .update (ctx , dst , sshKeyFile , ref , depth )
116117 } else {
117- err = g .clone (ctx , dst , sshKeyFile , u , depth )
118+ err = g .clone (ctx , dst , sshKeyFile , u , ref , depth )
118119 }
119120 if err != nil {
120121 return err
@@ -166,14 +167,17 @@ func (g *GitGetter) checkout(dst string, ref string) error {
166167 return getRunCommand (cmd )
167168}
168169
169- func (g * GitGetter ) clone (ctx context.Context , dst , sshKeyFile string , u * url.URL , depth int ) error {
170+ func (g * GitGetter ) clone (ctx context.Context , dst , sshKeyFile string , u * url.URL , ref string , depth int ) error {
170171 args := []string {"clone" }
171172
173+ if ref == "" {
174+ ref = findRemoteDefaultBranch (u )
175+ }
172176 if depth > 0 {
173177 args = append (args , "--depth" , strconv .Itoa (depth ))
174178 }
175179
176- args = append (args , u .String (), dst )
180+ args = append (args , "--branch" , ref , u .String (), dst )
177181 cmd := exec .CommandContext (ctx , "git" , args ... )
178182 setupGitEnv (cmd , sshKeyFile )
179183 return getRunCommand (cmd )
@@ -236,6 +240,20 @@ func findDefaultBranch(dst string) string {
236240 return matches [len (matches )- 1 ]
237241}
238242
243+ // findRemoteDefaultBranch checks the remote repo's HEAD symref to return the remote repo's
244+ // default branch. "master" is returned if no HEAD symref exists.
245+ func findRemoteDefaultBranch (u * url.URL ) string {
246+ var stdoutbuf bytes.Buffer
247+ cmd := exec .Command ("git" , "ls-remote" , "--symref" , u .String (), "HEAD" )
248+ cmd .Stdout = & stdoutbuf
249+ err := cmd .Run ()
250+ matches := lsRemoteSymRefRegexp .FindStringSubmatch (stdoutbuf .String ())
251+ if err != nil || matches == nil {
252+ return "master"
253+ }
254+ return matches [len (matches )- 1 ]
255+ }
256+
239257// setupGitEnv sets up the environment for the given command. This is used to
240258// pass configuration data to git and ssh and enables advanced cloning methods.
241259func setupGitEnv (cmd * exec.Cmd , sshKeyFile string ) {
0 commit comments