2015-04-23 17:19:34 +00:00
|
|
|
package utils
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
2015-04-24 22:12:45 +00:00
|
|
|
"net/url"
|
|
|
|
"os"
|
2015-04-23 17:19:34 +00:00
|
|
|
"os/exec"
|
2015-04-24 22:12:45 +00:00
|
|
|
"path/filepath"
|
2015-04-23 17:19:34 +00:00
|
|
|
"strings"
|
|
|
|
|
2015-06-01 18:41:45 +00:00
|
|
|
"github.com/docker/docker/pkg/symlink"
|
2015-04-23 17:19:34 +00:00
|
|
|
"github.com/docker/docker/pkg/urlutil"
|
|
|
|
)
|
|
|
|
|
|
|
|
func GitClone(remoteURL string) (string, error) {
|
|
|
|
if !urlutil.IsGitTransport(remoteURL) {
|
|
|
|
remoteURL = "https://" + remoteURL
|
|
|
|
}
|
|
|
|
root, err := ioutil.TempDir("", "docker-build-git")
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2015-04-24 22:12:45 +00:00
|
|
|
u, err := url.Parse(remoteURL)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2015-04-23 17:19:34 +00:00
|
|
|
|
2015-04-24 22:12:45 +00:00
|
|
|
fragment := u.Fragment
|
|
|
|
clone := cloneArgs(u, root)
|
|
|
|
|
|
|
|
if output, err := git(clone...); err != nil {
|
2015-04-23 17:19:34 +00:00
|
|
|
return "", fmt.Errorf("Error trying to use git: %s (%s)", err, output)
|
|
|
|
}
|
|
|
|
|
2015-04-24 22:12:45 +00:00
|
|
|
return checkoutGit(fragment, root)
|
2015-04-23 17:19:34 +00:00
|
|
|
}
|
|
|
|
|
2015-04-24 22:12:45 +00:00
|
|
|
func cloneArgs(remoteURL *url.URL, root string) []string {
|
2015-04-23 17:19:34 +00:00
|
|
|
args := []string{"clone", "--recursive"}
|
2015-04-24 22:12:45 +00:00
|
|
|
shallow := len(remoteURL.Fragment) == 0
|
2015-04-23 17:19:34 +00:00
|
|
|
|
2015-04-24 22:12:45 +00:00
|
|
|
if shallow && strings.HasPrefix(remoteURL.Scheme, "http") {
|
2015-04-23 17:19:34 +00:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
2015-04-24 22:12:45 +00:00
|
|
|
if remoteURL.Fragment != "" {
|
|
|
|
remoteURL.Fragment = ""
|
|
|
|
}
|
|
|
|
|
|
|
|
return append(args, remoteURL.String(), root)
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkoutGit(fragment, root string) (string, error) {
|
|
|
|
refAndDir := strings.SplitN(fragment, ":", 2)
|
|
|
|
|
|
|
|
if len(refAndDir[0]) != 0 {
|
|
|
|
if output, err := gitWithinDir(root, "checkout", refAndDir[0]); err != nil {
|
|
|
|
return "", fmt.Errorf("Error trying to use git: %s (%s)", err, output)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(refAndDir) > 1 && len(refAndDir[1]) != 0 {
|
2015-06-01 18:41:45 +00:00
|
|
|
newCtx, err := symlink.FollowSymlinkInScope(filepath.Join(root, refAndDir[1]), root)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("Error setting git context, %q not within git root: %s", refAndDir[1], err)
|
|
|
|
}
|
|
|
|
|
2015-04-24 22:12:45 +00:00
|
|
|
fi, err := os.Stat(newCtx)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if !fi.IsDir() {
|
|
|
|
return "", fmt.Errorf("Error setting git context, not a directory: %s", newCtx)
|
|
|
|
}
|
|
|
|
root = newCtx
|
|
|
|
}
|
|
|
|
|
|
|
|
return root, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func gitWithinDir(dir string, args ...string) ([]byte, error) {
|
|
|
|
a := []string{"--work-tree", dir, "--git-dir", filepath.Join(dir, ".git")}
|
|
|
|
return git(append(a, args...)...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func git(args ...string) ([]byte, error) {
|
|
|
|
return exec.Command("git", args...).CombinedOutput()
|
2015-04-23 17:19:34 +00:00
|
|
|
}
|