gitutils.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. package 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. return repo, nil
  86. }
  87. func getRefAndSubdir(fragment string) (ref string, subdir string) {
  88. refAndDir := strings.SplitN(fragment, ":", 2)
  89. ref = "master"
  90. if len(refAndDir[0]) != 0 {
  91. ref = refAndDir[0]
  92. }
  93. if len(refAndDir) > 1 && len(refAndDir[1]) != 0 {
  94. subdir = refAndDir[1]
  95. }
  96. return
  97. }
  98. func fetchArgs(remoteURL string, ref string) []string {
  99. args := []string{"fetch"}
  100. if supportsShallowClone(remoteURL) {
  101. args = append(args, "--depth", "1")
  102. }
  103. return append(args, "origin", ref)
  104. }
  105. // Check if a given git URL supports a shallow git clone,
  106. // i.e. it is a non-HTTP server or a smart HTTP server.
  107. func supportsShallowClone(remoteURL string) bool {
  108. if urlutil.IsURL(remoteURL) {
  109. // Check if the HTTP server is smart
  110. // Smart servers must correctly respond to a query for the git-upload-pack service
  111. serviceURL := remoteURL + "/info/refs?service=git-upload-pack"
  112. // Try a HEAD request and fallback to a Get request on error
  113. res, err := http.Head(serviceURL)
  114. if err != nil || res.StatusCode != http.StatusOK {
  115. res, err = http.Get(serviceURL)
  116. if err == nil {
  117. res.Body.Close()
  118. }
  119. if err != nil || res.StatusCode != http.StatusOK {
  120. // request failed
  121. return false
  122. }
  123. }
  124. if res.Header.Get("Content-Type") != "application/x-git-upload-pack-advertisement" {
  125. // Fallback, not a smart server
  126. return false
  127. }
  128. return true
  129. }
  130. // Non-HTTP protocols always support shallow clones
  131. return true
  132. }
  133. func checkoutGit(root, ref, subdir string) (string, error) {
  134. // Try checking out by ref name first. This will work on branches and sets
  135. // .git/HEAD to the current branch name
  136. if output, err := gitWithinDir(root, "checkout", ref); err != nil {
  137. // If checking out by branch name fails check out the last fetched ref
  138. if _, err2 := gitWithinDir(root, "checkout", "FETCH_HEAD"); err2 != nil {
  139. return "", errors.Wrapf(err, "error checking out %s: %s", ref, output)
  140. }
  141. }
  142. if subdir != "" {
  143. newCtx, err := symlink.FollowSymlinkInScope(filepath.Join(root, subdir), root)
  144. if err != nil {
  145. return "", errors.Wrapf(err, "error setting git context, %q not within git root", subdir)
  146. }
  147. fi, err := os.Stat(newCtx)
  148. if err != nil {
  149. return "", err
  150. }
  151. if !fi.IsDir() {
  152. return "", errors.Errorf("error setting git context, not a directory: %s", newCtx)
  153. }
  154. root = newCtx
  155. }
  156. return root, nil
  157. }
  158. func gitWithinDir(dir string, args ...string) ([]byte, error) {
  159. a := []string{"--work-tree", dir, "--git-dir", filepath.Join(dir, ".git")}
  160. return git(append(a, args...)...)
  161. }
  162. func git(args ...string) ([]byte, error) {
  163. return exec.Command("git", args...).CombinedOutput()
  164. }
  165. // isGitTransport returns true if the provided str is a git transport by inspecting
  166. // the prefix of the string for known protocols used in git.
  167. func isGitTransport(str string) bool {
  168. return urlutil.IsURL(str) || strings.HasPrefix(str, "git://") || strings.HasPrefix(str, "git@")
  169. }