1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
14 "git.curoverse.com/arvados.git/sdk/go/arvados"
15 "gopkg.in/src-d/go-billy.v4/osfs"
16 git "gopkg.in/src-d/go-git.v4"
17 git_config "gopkg.in/src-d/go-git.v4/config"
18 git_plumbing "gopkg.in/src-d/go-git.v4/plumbing"
19 git_http "gopkg.in/src-d/go-git.v4/plumbing/transport/http"
20 "gopkg.in/src-d/go-git.v4/storage/memory"
23 type gitMount arvados.Mount
26 sha1re = regexp.MustCompile(`^[0-9a-f]{40}$`)
27 repoUUIDre = regexp.MustCompile(`^[0-9a-z]{5}-s0uqq-[0-9a-z]{15}$`)
30 func (gm gitMount) validate() error {
31 if gm.Path != "" && gm.Path != "/" {
32 return fmt.Errorf("cannot mount git_tree with path %q -- only \"/\" is supported", gm.Path)
34 if !sha1re.MatchString(gm.Commit) {
35 return fmt.Errorf("cannot mount git_tree with commit %q -- must be a 40-char SHA1", gm.Commit)
37 if gm.RepositoryName != "" || gm.GitURL != "" {
38 return fmt.Errorf("cannot mount git_tree -- repository_name and git_url must be empty")
40 if !repoUUIDre.MatchString(gm.UUID) {
41 return fmt.Errorf("cannot mount git_tree with uuid %q -- must be a repository UUID", gm.UUID)
44 return fmt.Errorf("writable git_tree mount is not supported")
49 // ExtractTree extracts the specified tree into dir, which is an
50 // existing empty local directory.
51 func (gm gitMount) extractTree(ac IArvadosClient, dir string, token string) error {
56 baseURL, err := ac.Discovery("gitUrl")
58 return fmt.Errorf("discover gitUrl from API: %s", err)
59 } else if _, ok := baseURL.(string); !ok {
60 return fmt.Errorf("discover gitUrl from API: expected string, found %T", baseURL)
63 u, err := url.Parse(baseURL.(string))
65 return fmt.Errorf("parse gitUrl %q: %s", baseURL, err)
67 u, err = u.Parse("/" + gm.UUID + ".git")
69 return fmt.Errorf("build git url from %q, %q: %s", baseURL, gm.UUID, err)
71 store := memory.NewStorage()
72 repo, err := git.Init(store, osfs.New(dir))
74 return fmt.Errorf("init repo: %s", err)
76 _, err = repo.CreateRemote(&git_config.RemoteConfig{
78 URLs: []string{u.String()},
81 return fmt.Errorf("create remote %q: %s", u.String(), err)
83 err = repo.Fetch(&git.FetchOptions{
85 Auth: &git_http.BasicAuth{
91 return fmt.Errorf("git fetch %q: %s", u.String(), err)
93 wt, err := repo.Worktree()
95 return fmt.Errorf("worktree failed: %s", err)
97 err = wt.Checkout(&git.CheckoutOptions{
98 Hash: git_plumbing.NewHash(gm.Commit),
101 return fmt.Errorf("checkout failed: %s", err)
103 err = filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
107 // copy user rx bits to group and other, in case
108 // prevailing umask is more restrictive than 022
110 mode = mode | ((mode >> 3) & 050) | ((mode >> 6) & 5)
111 return os.Chmod(path, mode)
114 return fmt.Errorf("chmod -R %q: %s", dir, err)