gitutils.go 6.3 KB

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