gitutils.go 6.2 KB

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