gitutils.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. "os/exec"
  8. "path/filepath"
  9. "strings"
  10. "github.com/docker/docker/pkg/symlink"
  11. "github.com/docker/docker/pkg/urlutil"
  12. "github.com/pkg/errors"
  13. )
  14. type gitRepo struct {
  15. remote string
  16. ref string
  17. subdir string
  18. }
  19. // Clone clones a repository into a newly created directory which
  20. // will be under "docker-build-git"
  21. func Clone(remoteURL string) (string, error) {
  22. repo, err := parseRemoteURL(remoteURL)
  23. if err != nil {
  24. return "", err
  25. }
  26. return cloneGitRepo(repo)
  27. }
  28. func cloneGitRepo(repo gitRepo) (checkoutDir string, err error) {
  29. fetch := fetchArgs(repo.remote, repo.ref)
  30. root, err := ioutil.TempDir("", "docker-build-git")
  31. if err != nil {
  32. return "", err
  33. }
  34. defer func() {
  35. if err != nil {
  36. os.RemoveAll(root)
  37. }
  38. }()
  39. if out, err := gitWithinDir(root, "init"); err != nil {
  40. return "", errors.Wrapf(err, "failed to init repo at %s: %s", root, out)
  41. }
  42. // Add origin remote for compatibility with previous implementation that
  43. // used "git clone" and also to make sure local refs are created for branches
  44. if out, err := gitWithinDir(root, "remote", "add", "origin", repo.remote); err != nil {
  45. return "", errors.Wrapf(err, "failed add origin repo at %s: %s", repo.remote, out)
  46. }
  47. if output, err := gitWithinDir(root, fetch...); err != nil {
  48. return "", errors.Wrapf(err, "error fetching: %s", output)
  49. }
  50. checkoutDir, err = checkoutGit(root, repo.ref, repo.subdir)
  51. if err != nil {
  52. return "", err
  53. }
  54. cmd := exec.Command("git", "submodule", "update", "--init", "--recursive", "--depth=1")
  55. cmd.Dir = root
  56. output, err := cmd.CombinedOutput()
  57. if err != nil {
  58. return "", errors.Wrapf(err, "error initializing submodules: %s", output)
  59. }
  60. return checkoutDir, nil
  61. }
  62. func parseRemoteURL(remoteURL string) (gitRepo, error) {
  63. repo := gitRepo{}
  64. if !isGitTransport(remoteURL) {
  65. remoteURL = "https://" + remoteURL
  66. }
  67. var fragment string
  68. if strings.HasPrefix(remoteURL, "git@") {
  69. // git@.. is not an URL, so cannot be parsed as URL
  70. parts := strings.SplitN(remoteURL, "#", 2)
  71. repo.remote = parts[0]
  72. if len(parts) == 2 {
  73. fragment = parts[1]
  74. }
  75. repo.ref, repo.subdir = getRefAndSubdir(fragment)
  76. } else {
  77. u, err := url.Parse(remoteURL)
  78. if err != nil {
  79. return repo, err
  80. }
  81. repo.ref, repo.subdir = getRefAndSubdir(u.Fragment)
  82. u.Fragment = ""
  83. repo.remote = u.String()
  84. }
  85. if strings.HasPrefix(repo.ref, "-") {
  86. return gitRepo{}, errors.Errorf("invalid refspec: %s", repo.ref)
  87. }
  88. return repo, nil
  89. }
  90. func getRefAndSubdir(fragment string) (ref string, subdir string) {
  91. refAndDir := strings.SplitN(fragment, ":", 2)
  92. ref = "master"
  93. if len(refAndDir[0]) != 0 {
  94. ref = refAndDir[0]
  95. }
  96. if len(refAndDir) > 1 && len(refAndDir[1]) != 0 {
  97. subdir = refAndDir[1]
  98. }
  99. return
  100. }
  101. func fetchArgs(remoteURL string, ref string) []string {
  102. args := []string{"fetch"}
  103. if supportsShallowClone(remoteURL) {
  104. args = append(args, "--depth", "1")
  105. }
  106. return append(args, "origin", "--", ref)
  107. }
  108. // Check if a given git URL supports a shallow git clone,
  109. // i.e. it is a non-HTTP server or a smart HTTP server.
  110. func supportsShallowClone(remoteURL string) bool {
  111. if urlutil.IsURL(remoteURL) {
  112. // Check if the HTTP server is smart
  113. // Smart servers must correctly respond to a query for the git-upload-pack service
  114. serviceURL := remoteURL + "/info/refs?service=git-upload-pack"
  115. // Try a HEAD request and fallback to a Get request on error
  116. res, err := http.Head(serviceURL)
  117. if err != nil || res.StatusCode != http.StatusOK {
  118. res, err = http.Get(serviceURL)
  119. if err == nil {
  120. res.Body.Close()
  121. }
  122. if err != nil || res.StatusCode != http.StatusOK {
  123. // request failed
  124. return false
  125. }
  126. }
  127. if res.Header.Get("Content-Type") != "application/x-git-upload-pack-advertisement" {
  128. // Fallback, not a smart server
  129. return false
  130. }
  131. return true
  132. }
  133. // Non-HTTP protocols always support shallow clones
  134. return true
  135. }
  136. func checkoutGit(root, ref, subdir string) (string, error) {
  137. // Try checking out by ref name first. This will work on branches and sets
  138. // .git/HEAD to the current branch name
  139. if output, err := gitWithinDir(root, "checkout", ref); err != nil {
  140. // If checking out by branch name fails check out the last fetched ref
  141. if _, err2 := gitWithinDir(root, "checkout", "FETCH_HEAD"); err2 != nil {
  142. return "", errors.Wrapf(err, "error checking out %s: %s", ref, output)
  143. }
  144. }
  145. if subdir != "" {
  146. newCtx, err := symlink.FollowSymlinkInScope(filepath.Join(root, subdir), root)
  147. if err != nil {
  148. return "", errors.Wrapf(err, "error setting git context, %q not within git root", subdir)
  149. }
  150. fi, err := os.Stat(newCtx)
  151. if err != nil {
  152. return "", err
  153. }
  154. if !fi.IsDir() {
  155. return "", errors.Errorf("error setting git context, not a directory: %s", newCtx)
  156. }
  157. root = newCtx
  158. }
  159. return root, nil
  160. }
  161. func gitWithinDir(dir string, args ...string) ([]byte, error) {
  162. a := []string{"--work-tree", dir, "--git-dir", filepath.Join(dir, ".git")}
  163. return git(append(a, args...)...)
  164. }
  165. func git(args ...string) ([]byte, error) {
  166. return exec.Command("git", args...).CombinedOutput()
  167. }
  168. // isGitTransport returns true if the provided str is a git transport by inspecting
  169. // the prefix of the string for known protocols used in git.
  170. func isGitTransport(str string) bool {
  171. return urlutil.IsURL(str) || strings.HasPrefix(str, "git://") || strings.HasPrefix(str, "git@")
  172. }