10 "git.curoverse.com/arvados.git/sdk/go/arvados"
11 "gopkg.in/src-d/go-billy.v4/osfs"
12 git "gopkg.in/src-d/go-git.v4"
13 git_config "gopkg.in/src-d/go-git.v4/config"
14 git_plumbing "gopkg.in/src-d/go-git.v4/plumbing"
15 git_http "gopkg.in/src-d/go-git.v4/plumbing/transport/http"
16 "gopkg.in/src-d/go-git.v4/storage/memory"
19 type gitMount arvados.Mount
22 sha1re = regexp.MustCompile(`^[0-9a-f]{40}$`)
23 repoUUIDre = regexp.MustCompile(`^[0-9a-z]{5}-s0uqq-[0-9a-z]{15}$`)
26 func (gm gitMount) validate() error {
27 if gm.Path != "" && gm.Path != "/" {
28 return fmt.Errorf("cannot mount git_tree with path %q -- only \"/\" is supported", gm.Path)
30 if !sha1re.MatchString(gm.Commit) {
31 return fmt.Errorf("cannot mount git_tree with commit %q -- must be a 40-char SHA1", gm.Commit)
33 if gm.RepositoryName != "" || gm.GitURL != "" {
34 return fmt.Errorf("cannot mount git_tree -- repository_name and git_url must be empty")
36 if !repoUUIDre.MatchString(gm.UUID) {
37 return fmt.Errorf("cannot mount git_tree with uuid %q -- must be a repository UUID", gm.UUID)
40 return fmt.Errorf("writable git_tree mount is not supported")
45 // ExtractTree extracts the specified tree into dir, which is an
46 // existing empty local directory.
47 func (gm gitMount) extractTree(ac IArvadosClient, dir string, token string) error {
52 baseURL, err := ac.Discovery("gitUrl")
54 return fmt.Errorf("discover gitUrl from API: %s", err)
55 } else if _, ok := baseURL.(string); !ok {
56 return fmt.Errorf("discover gitUrl from API: expected string, found %T", baseURL)
59 u, err := url.Parse(baseURL.(string))
61 return fmt.Errorf("parse gitUrl %q: %s", baseURL, err)
63 u, err = u.Parse("/" + gm.UUID + ".git")
65 return fmt.Errorf("build git url from %q, %q: %s", baseURL, gm.UUID, err)
67 store := memory.NewStorage()
68 repo, err := git.Init(store, osfs.New(dir))
70 return fmt.Errorf("init repo: %s", err)
72 _, err = repo.CreateRemote(&git_config.RemoteConfig{
74 URLs: []string{u.String()},
77 return fmt.Errorf("create remote %q: %s", u.String(), err)
79 err = repo.Fetch(&git.FetchOptions{
81 Auth: &git_http.BasicAuth{
87 return fmt.Errorf("git fetch %q: %s", u.String(), err)
89 wt, err := repo.Worktree()
91 return fmt.Errorf("worktree failed: %s", err)
93 err = wt.Checkout(&git.CheckoutOptions{
94 Hash: git_plumbing.NewHash(gm.Commit),
97 return fmt.Errorf("checkout failed: %s", err)
99 err = filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
103 // copy user rx bits to group and other, in case
104 // prevailing umask is more restrictive than 022
106 mode = mode | ((mode >> 3) & 050) | ((mode >> 6) & 5)
107 return os.Chmod(path, mode)
110 return fmt.Errorf("chmod -R %q: %s", dir, err)