2018-02-05 16:05:59 -05:00
|
|
|
package git // import "github.com/docker/docker/builder/remotecontext/git"
|
2015-04-23 13:19:34 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"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"
|
2017-04-10 20:11:28 -04:00
|
|
|
"github.com/pkg/errors"
|
2015-04-23 13:19:34 -04:00
|
|
|
)
|
|
|
|
|
Fix handling of remote "git@" notation
`docker build` accepts remote repositories
using either the `git://` notation, or `git@`.
Docker attempted to parse both as an URL, however,
`git@` is not an URL, but an argument to `git clone`.
Go 1.7 silently ignored this, and managed to
extract the needed information from these
remotes, however, Go 1.8 does a more strict
validation, and invalidated these.
This patch adds a different path for `git@` remotes,
to prevent them from being handled as URL (and
invalidated).
A test is also added, because there were no
tests for handling of `git@` remotes.
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2017-06-15 10:29:18 -04:00
|
|
|
type gitRepo struct {
|
|
|
|
remote string
|
|
|
|
ref string
|
|
|
|
subdir string
|
|
|
|
}
|
|
|
|
|
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) {
|
Fix handling of remote "git@" notation
`docker build` accepts remote repositories
using either the `git://` notation, or `git@`.
Docker attempted to parse both as an URL, however,
`git@` is not an URL, but an argument to `git clone`.
Go 1.7 silently ignored this, and managed to
extract the needed information from these
remotes, however, Go 1.8 does a more strict
validation, and invalidated these.
This patch adds a different path for `git@` remotes,
to prevent them from being handled as URL (and
invalidated).
A test is also added, because there were no
tests for handling of `git@` remotes.
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2017-06-15 10:29:18 -04:00
|
|
|
repo, err := parseRemoteURL(remoteURL)
|
|
|
|
|
2015-04-23 13:19:34 -04:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2017-12-07 16:44:08 -05:00
|
|
|
return cloneGitRepo(repo)
|
|
|
|
}
|
|
|
|
|
2017-12-08 14:51:10 -05:00
|
|
|
func cloneGitRepo(repo gitRepo) (checkoutDir string, err error) {
|
Fix handling of remote "git@" notation
`docker build` accepts remote repositories
using either the `git://` notation, or `git@`.
Docker attempted to parse both as an URL, however,
`git@` is not an URL, but an argument to `git clone`.
Go 1.7 silently ignored this, and managed to
extract the needed information from these
remotes, however, Go 1.8 does a more strict
validation, and invalidated these.
This patch adds a different path for `git@` remotes,
to prevent them from being handled as URL (and
invalidated).
A test is also added, because there were no
tests for handling of `git@` remotes.
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2017-06-15 10:29:18 -04:00
|
|
|
fetch := fetchArgs(repo.remote, repo.ref)
|
|
|
|
|
|
|
|
root, err := ioutil.TempDir("", "docker-build-git")
|
2015-04-24 18:12:45 -04:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2015-04-23 13:19:34 -04:00
|
|
|
|
2017-12-08 14:51:10 -05:00
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
os.RemoveAll(root)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add origin remote for compatibility with previous implementation that
|
|
|
|
// used "git clone" and also to make sure local refs are created for branches
|
Fix handling of remote "git@" notation
`docker build` accepts remote repositories
using either the `git://` notation, or `git@`.
Docker attempted to parse both as an URL, however,
`git@` is not an URL, but an argument to `git clone`.
Go 1.7 silently ignored this, and managed to
extract the needed information from these
remotes, however, Go 1.8 does a more strict
validation, and invalidated these.
This patch adds a different path for `git@` remotes,
to prevent them from being handled as URL (and
invalidated).
A test is also added, because there were no
tests for handling of `git@` remotes.
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2017-06-15 10:29:18 -04:00
|
|
|
if out, err := gitWithinDir(root, "remote", "add", "origin", repo.remote); err != nil {
|
|
|
|
return "", errors.Wrapf(err, "failed add origin repo at %s: %s", repo.remote, out)
|
2017-04-10 20:11:28 -04:00
|
|
|
}
|
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-12-08 14:51:10 -05:00
|
|
|
checkoutDir, err = checkoutGit(root, repo.ref, repo.subdir)
|
2017-12-07 16:44:08 -05:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd := exec.Command("git", "submodule", "update", "--init", "--recursive", "--depth=1")
|
|
|
|
cmd.Dir = root
|
|
|
|
output, err := cmd.CombinedOutput()
|
|
|
|
if err != nil {
|
|
|
|
return "", errors.Wrapf(err, "error initializing submodules: %s", output)
|
|
|
|
}
|
|
|
|
|
2017-12-08 14:51:10 -05:00
|
|
|
return checkoutDir, nil
|
Fix handling of remote "git@" notation
`docker build` accepts remote repositories
using either the `git://` notation, or `git@`.
Docker attempted to parse both as an URL, however,
`git@` is not an URL, but an argument to `git clone`.
Go 1.7 silently ignored this, and managed to
extract the needed information from these
remotes, however, Go 1.8 does a more strict
validation, and invalidated these.
This patch adds a different path for `git@` remotes,
to prevent them from being handled as URL (and
invalidated).
A test is also added, because there were no
tests for handling of `git@` remotes.
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2017-06-15 10:29:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func parseRemoteURL(remoteURL string) (gitRepo, error) {
|
|
|
|
repo := gitRepo{}
|
|
|
|
|
2017-06-26 13:07:04 -04:00
|
|
|
if !isGitTransport(remoteURL) {
|
Fix handling of remote "git@" notation
`docker build` accepts remote repositories
using either the `git://` notation, or `git@`.
Docker attempted to parse both as an URL, however,
`git@` is not an URL, but an argument to `git clone`.
Go 1.7 silently ignored this, and managed to
extract the needed information from these
remotes, however, Go 1.8 does a more strict
validation, and invalidated these.
This patch adds a different path for `git@` remotes,
to prevent them from being handled as URL (and
invalidated).
A test is also added, because there were no
tests for handling of `git@` remotes.
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2017-06-15 10:29:18 -04:00
|
|
|
remoteURL = "https://" + remoteURL
|
|
|
|
}
|
|
|
|
|
|
|
|
var fragment string
|
|
|
|
if strings.HasPrefix(remoteURL, "git@") {
|
|
|
|
// git@.. is not an URL, so cannot be parsed as URL
|
|
|
|
parts := strings.SplitN(remoteURL, "#", 2)
|
|
|
|
|
|
|
|
repo.remote = parts[0]
|
|
|
|
if len(parts) == 2 {
|
|
|
|
fragment = parts[1]
|
|
|
|
}
|
|
|
|
repo.ref, repo.subdir = getRefAndSubdir(fragment)
|
|
|
|
} else {
|
|
|
|
u, err := url.Parse(remoteURL)
|
|
|
|
if err != nil {
|
|
|
|
return repo, err
|
|
|
|
}
|
|
|
|
|
|
|
|
repo.ref, repo.subdir = getRefAndSubdir(u.Fragment)
|
|
|
|
u.Fragment = ""
|
|
|
|
repo.remote = u.String()
|
|
|
|
}
|
2019-02-06 14:58:40 -05:00
|
|
|
|
|
|
|
if strings.HasPrefix(repo.ref, "-") {
|
|
|
|
return gitRepo{}, errors.Errorf("invalid refspec: %s", repo.ref)
|
|
|
|
}
|
|
|
|
|
Fix handling of remote "git@" notation
`docker build` accepts remote repositories
using either the `git://` notation, or `git@`.
Docker attempted to parse both as an URL, however,
`git@` is not an URL, but an argument to `git clone`.
Go 1.7 silently ignored this, and managed to
extract the needed information from these
remotes, however, Go 1.8 does a more strict
validation, and invalidated these.
This patch adds a different path for `git@` remotes,
to prevent them from being handled as URL (and
invalidated).
A test is also added, because there were no
tests for handling of `git@` remotes.
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2017-06-15 10:29:18 -04:00
|
|
|
return repo, nil
|
2017-04-10 20:11:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
Fix handling of remote "git@" notation
`docker build` accepts remote repositories
using either the `git://` notation, or `git@`.
Docker attempted to parse both as an URL, however,
`git@` is not an URL, but an argument to `git clone`.
Go 1.7 silently ignored this, and managed to
extract the needed information from these
remotes, however, Go 1.8 does a more strict
validation, and invalidated these.
This patch adds a different path for `git@` remotes,
to prevent them from being handled as URL (and
invalidated).
A test is also added, because there were no
tests for handling of `git@` remotes.
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2017-06-15 10:29:18 -04:00
|
|
|
func fetchArgs(remoteURL string, ref string) []string {
|
2017-12-07 16:44:08 -05:00
|
|
|
args := []string{"fetch"}
|
2015-04-23 13:19:34 -04:00
|
|
|
|
2017-06-15 15:15:02 -04:00
|
|
|
if supportsShallowClone(remoteURL) {
|
2015-04-23 13:19:34 -04:00
|
|
|
args = append(args, "--depth", "1")
|
|
|
|
}
|
|
|
|
|
2019-02-06 14:58:40 -05:00
|
|
|
return append(args, "origin", "--", ref)
|
2015-04-24 18:12:45 -04:00
|
|
|
}
|
|
|
|
|
2017-06-15 15:15:02 -04:00
|
|
|
// Check if a given git URL supports a shallow git clone,
|
|
|
|
// i.e. it is a non-HTTP server or a smart HTTP server.
|
|
|
|
func supportsShallowClone(remoteURL string) bool {
|
2019-11-05 12:26:30 -05:00
|
|
|
if scheme := getScheme(remoteURL); scheme == "http" || scheme == "https" {
|
2017-06-15 15:15:02 -04:00
|
|
|
// Check if the HTTP server is smart
|
|
|
|
|
|
|
|
// Smart servers must correctly respond to a query for the git-upload-pack service
|
|
|
|
serviceURL := remoteURL + "/info/refs?service=git-upload-pack"
|
|
|
|
|
|
|
|
// Try a HEAD request and fallback to a Get request on error
|
2019-08-28 11:07:29 -04:00
|
|
|
res, err := http.Head(serviceURL) // #nosec G107
|
2017-06-15 15:15:02 -04:00
|
|
|
if err != nil || res.StatusCode != http.StatusOK {
|
2019-08-28 11:07:29 -04:00
|
|
|
res, err = http.Get(serviceURL) // #nosec G107
|
2017-06-15 15:15:02 -04:00
|
|
|
if err == nil {
|
|
|
|
res.Body.Close()
|
|
|
|
}
|
|
|
|
if err != nil || res.StatusCode != http.StatusOK {
|
|
|
|
// request failed
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if res.Header.Get("Content-Type") != "application/x-git-upload-pack-advertisement" {
|
|
|
|
// Fallback, not a smart server
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
// Non-HTTP protocols always support shallow clones
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
2017-06-26 13:07:04 -04:00
|
|
|
|
|
|
|
// isGitTransport returns true if the provided str is a git transport by inspecting
|
|
|
|
// the prefix of the string for known protocols used in git.
|
|
|
|
func isGitTransport(str string) bool {
|
2019-11-05 12:26:30 -05:00
|
|
|
if strings.HasPrefix(str, "git@") {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
switch getScheme(str) {
|
2019-11-05 16:58:58 -05:00
|
|
|
case "git", "http", "https", "ssh":
|
2019-11-05 12:26:30 -05:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// getScheme returns addresses' scheme in lowercase, or an empty
|
|
|
|
// string in case address is an invalid URL.
|
|
|
|
func getScheme(address string) string {
|
|
|
|
u, err := url.Parse(address)
|
|
|
|
if err != nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return u.Scheme
|
2017-06-26 13:07:04 -04:00
|
|
|
}
|