mirror of
https://github.com/clearlinux/docker.git
synced 2026-09-06 05:31:47 +00:00
Use git url fragment to specify reference and dir context.
Signed-off-by: David Calavera <david.calavera@gmail.com>
This commit is contained in:
+53
-7
@@ -4,7 +4,10 @@ import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/docker/docker/pkg/urlutil"
|
||||
@@ -19,20 +22,26 @@ func GitClone(remoteURL string) (string, error) {
|
||||
return "", err
|
||||
}
|
||||
|
||||
clone := cloneArgs(remoteURL, root)
|
||||
u, err := url.Parse(remoteURL)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
if output, err := exec.Command("git", clone...).CombinedOutput(); err != nil {
|
||||
fragment := u.Fragment
|
||||
clone := cloneArgs(u, root)
|
||||
|
||||
if output, err := git(clone...); err != nil {
|
||||
return "", fmt.Errorf("Error trying to use git: %s (%s)", err, output)
|
||||
}
|
||||
|
||||
return root, nil
|
||||
return checkoutGit(fragment, root)
|
||||
}
|
||||
|
||||
func cloneArgs(remoteURL, root string) []string {
|
||||
func cloneArgs(remoteURL *url.URL, root string) []string {
|
||||
args := []string{"clone", "--recursive"}
|
||||
shallow := true
|
||||
shallow := len(remoteURL.Fragment) == 0
|
||||
|
||||
if strings.HasPrefix(remoteURL, "http") {
|
||||
if shallow && strings.HasPrefix(remoteURL.Scheme, "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
|
||||
@@ -43,5 +52,42 @@ func cloneArgs(remoteURL, root string) []string {
|
||||
args = append(args, "--depth", "1")
|
||||
}
|
||||
|
||||
return append(args, remoteURL, root)
|
||||
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 {
|
||||
newCtx := filepath.Join(root, refAndDir[1])
|
||||
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()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user