Do not try to shallow git history when the protocol doesn't allow it.

This only happens with the old git http dumb protocol, but that's what we use in our integration tests.
We check the Content-Type header advertised in http requests to make sure the http transport is the git smart transport:

See this commit as a reference:
4656bf47fc

Signed-off-by: David Calavera <david.calavera@gmail.com>
This commit is contained in:
David Calavera 2015-04-21 10:58:38 -07:00
parent 36fbf4b864
commit 9fb7204a41
1 changed files with 22 additions and 1 deletions

View File

@ -5,6 +5,7 @@ import (
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"os/exec"
"strings"
@ -114,7 +115,9 @@ func Build(d *daemon.Daemon, buildConfig *Config) error {
}
defer os.RemoveAll(root)
if output, err := exec.Command("git", "clone", "--depth", "1", "--recursive", buildConfig.RemoteURL, root).CombinedOutput(); err != nil {
clone := cloneArgs(buildConfig.RemoteURL, root)
if output, err := exec.Command("git", clone...).CombinedOutput(); err != nil {
return fmt.Errorf("Error trying to use git: %s (%s)", err, output)
}
@ -239,3 +242,21 @@ func Commit(d *daemon.Daemon, name string, c *daemon.ContainerCommitConfig) (str
return img.ID, nil
}
func cloneArgs(remoteURL, root string) []string {
args := []string{"clone", "--recursive"}
shallow := true
if strings.HasPrefix(remoteURL, "http") {
res, err := http.Head(fmt.Sprintf("%s/info/refs?service=git-upload-pack", remoteURL))
if err != nil || res.Header.Get("Content-Type") != "application/x-git-upload-pack-advertisement" {
shallow = false
}
}
if shallow {
args = append(args, "--depth", "1")
}
return append(args, remoteURL, root)
}