2015-12-14 05:17:51 -05:00
|
|
|
package gitutils
|
2015-04-23 13:19:34 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
2015-04-24 18:12:45 -04:00
|
|
|
"net/url"
|
|
|
|
"os"
|
2015-04-23 13:19:34 -04:00
|
|
|
"os/exec"
|
2015-04-24 18:12:45 -04:00
|
|
|
"path/filepath"
|
2015-04-23 13:19:34 -04:00
|
|
|
"strings"
|
|
|
|
|
2015-06-01 14:41:45 -04:00
|
|
|
"github.com/docker/docker/pkg/symlink"
|
2015-04-23 13:19:34 -04:00
|
|
|
"github.com/docker/docker/pkg/urlutil"
|
2017-04-10 20:11:28 -04:00
|
|
|
"github.com/pkg/errors"
|
2015-04-23 13:19:34 -04:00
|
|
|
)
|
|
|
|
|
2015-12-14 05:17:51 -05:00
|
|
|
// Clone clones a repository into a newly created directory which
|
2015-07-21 17:20:12 -04:00
|
|
|
// will be under "docker-build-git"
|
2015-12-14 05:17:51 -05:00
|
|
|
func Clone(remoteURL string) (string, error) {
|
2015-04-23 13:19:34 -04:00
|
|
|
if !urlutil.IsGitTransport(remoteURL) {
|
|
|
|
remoteURL = "https://" + remoteURL
|
|
|
|
}
|
|
|
|
root, err := ioutil.TempDir("", "docker-build-git")
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2015-04-24 18:12:45 -04:00
|
|
|
u, err := url.Parse(remoteURL)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2015-04-23 13:19:34 -04:00
|
|
|
|
2017-04-10 20:11:28 -04:00
|
|
|
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)
|
|
|
|
}
|
2015-04-24 18:12:45 -04:00
|
|
|
|
2017-04-10 20:11:28 -04:00
|
|
|
if output, err := gitWithinDir(root, fetch...); err != nil {
|
|
|
|
return "", errors.Wrapf(err, "error fetching: %s", output)
|
2015-04-23 13:19:34 -04:00
|
|
|
}
|
|
|
|
|
2017-04-10 20:11:28 -04:00
|
|
|
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
|
2015-04-23 13:19:34 -04:00
|
|
|
}
|
|
|
|
|
2017-04-10 20:11:28 -04:00
|
|
|
func fetchArgs(remoteURL *url.URL, ref string) []string {
|
|
|
|
args := []string{"fetch", "--recurse-submodules=yes"}
|
|
|
|
shallow := true
|
2015-04-23 13:19:34 -04:00
|
|
|
|
2017-04-10 20:11:28 -04:00
|
|
|
if strings.HasPrefix(remoteURL.Scheme, "http") {
|
2015-04-23 13:19:34 -04: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")
|
|
|
|
}
|
|
|
|
|
2017-04-10 20:11:28 -04:00
|
|
|
return append(args, "origin", ref)
|
2015-04-24 18:12:45 -04:00
|
|
|
}
|
|
|
|
|
2017-04-10 20:11:28 -04:00
|
|
|
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)
|
2015-04-24 18:12:45 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-10 20:11:28 -04:00
|
|
|
if subdir != "" {
|
|
|
|
newCtx, err := symlink.FollowSymlinkInScope(filepath.Join(root, subdir), root)
|
2015-06-01 14:41:45 -04:00
|
|
|
if err != nil {
|
2017-04-10 20:11:28 -04:00
|
|
|
return "", errors.Wrapf(err, "error setting git context, %q not within git root", subdir)
|
2015-06-01 14:41:45 -04:00
|
|
|
}
|
|
|
|
|
2015-04-24 18:12:45 -04:00
|
|
|
fi, err := os.Stat(newCtx)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if !fi.IsDir() {
|
2017-04-10 20:11:28 -04:00
|
|
|
return "", errors.Errorf("error setting git context, not a directory: %s", newCtx)
|
2015-04-24 18:12:45 -04:00
|
|
|
}
|
|
|
|
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 13:19:34 -04:00
|
|
|
}
|