gitutils.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. package git // import "github.com/docker/docker/builder/remotecontext/git"
  2. import (
  3. "io/ioutil"
  4. "net/http"
  5. "net/url"
  6. "os"
  7. "path/filepath"
  8. "strings"
  9. "github.com/moby/sys/symlink"
  10. "github.com/pkg/errors"
  11. exec "golang.org/x/sys/execabs"
  12. )
  13. type gitRepo struct {
  14. remote string
  15. ref string
  16. subdir string
  17. isolateConfig bool
  18. }
  19. // CloneOption changes the behaviour of Clone().
  20. type CloneOption func(*gitRepo)
  21. // WithIsolatedConfig disables reading the user or system gitconfig files when
  22. // performing Git operations.
  23. func WithIsolatedConfig(v bool) CloneOption {
  24. return func(gr *gitRepo) {
  25. gr.isolateConfig = v
  26. }
  27. }
  28. // Clone clones a repository into a newly created directory which
  29. // will be under "docker-build-git"
  30. func Clone(remoteURL string, opts ...CloneOption) (string, error) {
  31. repo, err := parseRemoteURL(remoteURL)
  32. if err != nil {
  33. return "", err
  34. }
  35. for _, opt := range opts {
  36. opt(&repo)
  37. }
  38. return repo.clone()
  39. }
  40. func (repo gitRepo) clone() (checkoutDir string, err error) {
  41. fetch := fetchArgs(repo.remote, repo.ref)
  42. root, err := ioutil.TempDir("", "docker-build-git")
  43. if err != nil {
  44. return "", err
  45. }
  46. defer func() {
  47. if err != nil {
  48. os.RemoveAll(root)
  49. }
  50. }()
  51. if out, err := repo.gitWithinDir(root, "init"); err != nil {
  52. return "", errors.Wrapf(err, "failed to init repo at %s: %s", root, out)
  53. }
  54. // Add origin remote for compatibility with previous implementation that
  55. // used "git clone" and also to make sure local refs are created for branches
  56. if out, err := repo.gitWithinDir(root, "remote", "add", "origin", repo.remote); err != nil {
  57. return "", errors.Wrapf(err, "failed add origin repo at %s: %s", repo.remote, out)
  58. }
  59. if output, err := repo.gitWithinDir(root, fetch...); err != nil {
  60. return "", errors.Wrapf(err, "error fetching: %s", output)
  61. }
  62. checkoutDir, err = repo.checkout(root)
  63. if err != nil {
  64. return "", err
  65. }
  66. cmd := exec.Command("git", "submodule", "update", "--init", "--recursive", "--depth=1")
  67. cmd.Dir = root
  68. output, err := cmd.CombinedOutput()
  69. if err != nil {
  70. return "", errors.Wrapf(err, "error initializing submodules: %s", output)
  71. }
  72. return checkoutDir, nil
  73. }
  74. func parseRemoteURL(remoteURL string) (gitRepo, error) {
  75. repo := gitRepo{}
  76. if !isGitTransport(remoteURL) {
  77. remoteURL = "https://" + remoteURL
  78. }
  79. var fragment string
  80. if strings.HasPrefix(remoteURL, "git@") {
  81. // git@.. is not an URL, so cannot be parsed as URL
  82. parts := strings.SplitN(remoteURL, "#", 2)
  83. repo.remote = parts[0]
  84. if len(parts) == 2 {
  85. fragment = parts[1]
  86. }
  87. repo.ref, repo.subdir = getRefAndSubdir(fragment)
  88. } else {
  89. u, err := url.Parse(remoteURL)
  90. if err != nil {
  91. return repo, err
  92. }
  93. repo.ref, repo.subdir = getRefAndSubdir(u.Fragment)
  94. u.Fragment = ""
  95. repo.remote = u.String()
  96. }
  97. if strings.HasPrefix(repo.ref, "-") {
  98. return gitRepo{}, errors.Errorf("invalid refspec: %s", repo.ref)
  99. }
  100. return repo, nil
  101. }
  102. func getRefAndSubdir(fragment string) (ref string, subdir string) {
  103. refAndDir := strings.SplitN(fragment, ":", 2)
  104. ref = "master"
  105. if len(refAndDir[0]) != 0 {
  106. ref = refAndDir[0]
  107. }
  108. if len(refAndDir) > 1 && len(refAndDir[1]) != 0 {
  109. subdir = refAndDir[1]
  110. }
  111. return
  112. }
  113. func fetchArgs(remoteURL string, ref string) []string {
  114. args := []string{"fetch"}
  115. if supportsShallowClone(remoteURL) {
  116. args = append(args, "--depth", "1")
  117. }
  118. return append(args, "origin", "--", ref)
  119. }
  120. // Check if a given git URL supports a shallow git clone,
  121. // i.e. it is a non-HTTP server or a smart HTTP server.
  122. func supportsShallowClone(remoteURL string) bool {
  123. if scheme := getScheme(remoteURL); scheme == "http" || scheme == "https" {
  124. // Check if the HTTP server is smart
  125. // Smart servers must correctly respond to a query for the git-upload-pack service
  126. serviceURL := remoteURL + "/info/refs?service=git-upload-pack"
  127. // Try a HEAD request and fallback to a Get request on error
  128. res, err := http.Head(serviceURL) // #nosec G107
  129. if err != nil || res.StatusCode != http.StatusOK {
  130. res, err = http.Get(serviceURL) // #nosec G107
  131. if err == nil {
  132. res.Body.Close()
  133. }
  134. if err != nil || res.StatusCode != http.StatusOK {
  135. // request failed
  136. return false
  137. }
  138. }
  139. if res.Header.Get("Content-Type") != "application/x-git-upload-pack-advertisement" {
  140. // Fallback, not a smart server
  141. return false
  142. }
  143. return true
  144. }
  145. // Non-HTTP protocols always support shallow clones
  146. return true
  147. }
  148. func (repo gitRepo) checkout(root string) (string, error) {
  149. // Try checking out by ref name first. This will work on branches and sets
  150. // .git/HEAD to the current branch name
  151. if output, err := repo.gitWithinDir(root, "checkout", repo.ref); err != nil {
  152. // If checking out by branch name fails check out the last fetched ref
  153. if _, err2 := repo.gitWithinDir(root, "checkout", "FETCH_HEAD"); err2 != nil {
  154. return "", errors.Wrapf(err, "error checking out %s: %s", repo.ref, output)
  155. }
  156. }
  157. if repo.subdir != "" {
  158. newCtx, err := symlink.FollowSymlinkInScope(filepath.Join(root, repo.subdir), root)
  159. if err != nil {
  160. return "", errors.Wrapf(err, "error setting git context, %q not within git root", repo.subdir)
  161. }
  162. fi, err := os.Stat(newCtx)
  163. if err != nil {
  164. return "", err
  165. }
  166. if !fi.IsDir() {
  167. return "", errors.Errorf("error setting git context, not a directory: %s", newCtx)
  168. }
  169. root = newCtx
  170. }
  171. return root, nil
  172. }
  173. func (repo gitRepo) gitWithinDir(dir string, args ...string) ([]byte, error) {
  174. args = append([]string{"-c", "protocol.file.allow=never"}, args...) // Block sneaky repositories from using repos from the filesystem as submodules.
  175. cmd := exec.Command("git", args...)
  176. cmd.Dir = dir
  177. // Disable unsafe remote protocols.
  178. cmd.Env = append(os.Environ(), "GIT_PROTOCOL_FROM_USER=0")
  179. if repo.isolateConfig {
  180. cmd.Env = append(cmd.Env,
  181. "GIT_CONFIG_NOSYSTEM=1", // Disable reading from system gitconfig.
  182. "HOME=/dev/null", // Disable reading from user gitconfig.
  183. )
  184. }
  185. return cmd.CombinedOutput()
  186. }
  187. // isGitTransport returns true if the provided str is a git transport by inspecting
  188. // the prefix of the string for known protocols used in git.
  189. func isGitTransport(str string) bool {
  190. if strings.HasPrefix(str, "git@") {
  191. return true
  192. }
  193. switch getScheme(str) {
  194. case "git", "http", "https", "ssh":
  195. return true
  196. }
  197. return false
  198. }
  199. // getScheme returns addresses' scheme in lowercase, or an empty
  200. // string in case address is an invalid URL.
  201. func getScheme(address string) string {
  202. u, err := url.Parse(address)
  203. if err != nil {
  204. return ""
  205. }
  206. return u.Scheme
  207. }