|
@@ -12,6 +12,7 @@ import (
|
|
|
|
|
|
"github.com/docker/docker/pkg/symlink"
|
|
"github.com/docker/docker/pkg/symlink"
|
|
"github.com/docker/docker/pkg/urlutil"
|
|
"github.com/docker/docker/pkg/urlutil"
|
|
|
|
+ "github.com/pkg/errors"
|
|
)
|
|
)
|
|
|
|
|
|
// Clone clones a repository into a newly created directory which
|
|
// Clone clones a repository into a newly created directory which
|
|
@@ -30,21 +31,45 @@ func Clone(remoteURL string) (string, error) {
|
|
return "", err
|
|
return "", err
|
|
}
|
|
}
|
|
|
|
|
|
- fragment := u.Fragment
|
|
|
|
- clone := cloneArgs(u, root)
|
|
|
|
|
|
+ if out, err := gitWithinDir(root, "init"); err != nil {
|
|
|
|
+ return "", errors.Wrapf(err, "failed to init repo at %s: %s", root, out)
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ ref, subdir := getRefAndSubdir(u.Fragment)
|
|
|
|
+ fetch := fetchArgs(u, ref)
|
|
|
|
+
|
|
|
|
+ u.Fragment = ""
|
|
|
|
+
|
|
|
|
+ // Add origin remote for compatibility with previous implementation that
|
|
|
|
+ // used "git clone" and also to make sure local refs are created for branches
|
|
|
|
+ if out, err := gitWithinDir(root, "remote", "add", "origin", u.String()); err != nil {
|
|
|
|
+ return "", errors.Wrapf(err, "failed add origin repo at %s: %s", u.String(), out)
|
|
|
|
+ }
|
|
|
|
|
|
- if output, err := git(clone...); err != nil {
|
|
|
|
- return "", fmt.Errorf("Error trying to use git: %s (%s)", err, output)
|
|
|
|
|
|
+ if output, err := gitWithinDir(root, fetch...); err != nil {
|
|
|
|
+ return "", errors.Wrapf(err, "error fetching: %s", output)
|
|
}
|
|
}
|
|
|
|
|
|
- return checkoutGit(fragment, root)
|
|
|
|
|
|
+ return checkoutGit(root, ref, subdir)
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func getRefAndSubdir(fragment string) (ref string, subdir string) {
|
|
|
|
+ refAndDir := strings.SplitN(fragment, ":", 2)
|
|
|
|
+ ref = "master"
|
|
|
|
+ if len(refAndDir[0]) != 0 {
|
|
|
|
+ ref = refAndDir[0]
|
|
|
|
+ }
|
|
|
|
+ if len(refAndDir) > 1 && len(refAndDir[1]) != 0 {
|
|
|
|
+ subdir = refAndDir[1]
|
|
|
|
+ }
|
|
|
|
+ return
|
|
}
|
|
}
|
|
|
|
|
|
-func cloneArgs(remoteURL *url.URL, root string) []string {
|
|
|
|
- args := []string{"clone", "--recursive"}
|
|
|
|
- shallow := len(remoteURL.Fragment) == 0
|
|
|
|
|
|
+func fetchArgs(remoteURL *url.URL, ref string) []string {
|
|
|
|
+ args := []string{"fetch", "--recurse-submodules=yes"}
|
|
|
|
+ shallow := true
|
|
|
|
|
|
- if shallow && strings.HasPrefix(remoteURL.Scheme, "http") {
|
|
|
|
|
|
+ if strings.HasPrefix(remoteURL.Scheme, "http") {
|
|
res, err := http.Head(fmt.Sprintf("%s/info/refs?service=git-upload-pack", remoteURL))
|
|
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" {
|
|
if err != nil || res.Header.Get("Content-Type") != "application/x-git-upload-pack-advertisement" {
|
|
shallow = false
|
|
shallow = false
|
|
@@ -55,26 +80,23 @@ func cloneArgs(remoteURL *url.URL, root string) []string {
|
|
args = append(args, "--depth", "1")
|
|
args = append(args, "--depth", "1")
|
|
}
|
|
}
|
|
|
|
|
|
- if remoteURL.Fragment != "" {
|
|
|
|
- remoteURL.Fragment = ""
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- return append(args, remoteURL.String(), root)
|
|
|
|
|
|
+ return append(args, "origin", ref)
|
|
}
|
|
}
|
|
|
|
|
|
-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)
|
|
|
|
|
|
+func checkoutGit(root, ref, subdir string) (string, error) {
|
|
|
|
+ // Try checking out by ref name first. This will work on branches and sets
|
|
|
|
+ // .git/HEAD to the current branch name
|
|
|
|
+ if output, err := gitWithinDir(root, "checkout", ref); err != nil {
|
|
|
|
+ // If checking out by branch name fails check out the last fetched ref
|
|
|
|
+ if _, err2 := gitWithinDir(root, "checkout", "FETCH_HEAD"); err2 != nil {
|
|
|
|
+ return "", errors.Wrapf(err, "error checking out %s: %s", ref, output)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- if len(refAndDir) > 1 && len(refAndDir[1]) != 0 {
|
|
|
|
- newCtx, err := symlink.FollowSymlinkInScope(filepath.Join(root, refAndDir[1]), root)
|
|
|
|
|
|
+ if subdir != "" {
|
|
|
|
+ newCtx, err := symlink.FollowSymlinkInScope(filepath.Join(root, subdir), root)
|
|
if err != nil {
|
|
if err != nil {
|
|
- return "", fmt.Errorf("Error setting git context, %q not within git root: %s", refAndDir[1], err)
|
|
|
|
|
|
+ return "", errors.Wrapf(err, "error setting git context, %q not within git root", subdir)
|
|
}
|
|
}
|
|
|
|
|
|
fi, err := os.Stat(newCtx)
|
|
fi, err := os.Stat(newCtx)
|
|
@@ -82,7 +104,7 @@ func checkoutGit(fragment, root string) (string, error) {
|
|
return "", err
|
|
return "", err
|
|
}
|
|
}
|
|
if !fi.IsDir() {
|
|
if !fi.IsDir() {
|
|
- return "", fmt.Errorf("Error setting git context, not a directory: %s", newCtx)
|
|
|
|
|
|
+ return "", errors.Errorf("error setting git context, not a directory: %s", newCtx)
|
|
}
|
|
}
|
|
root = newCtx
|
|
root = newCtx
|
|
}
|
|
}
|