|
| 1 | +package git |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "github.com/go-git/go-billy/v5/memfs" |
| 6 | + "github.com/go-git/go-git/v5" |
| 7 | + "github.com/go-git/go-git/v5/plumbing" |
| 8 | + "github.com/go-git/go-git/v5/storage/memory" |
| 9 | + "io/ioutil" |
| 10 | + "regexp" |
| 11 | +) |
| 12 | + |
| 13 | +type Locator struct { |
| 14 | + URL string |
| 15 | + Path string |
| 16 | + Revision string |
| 17 | +} |
| 18 | + |
| 19 | +const ( |
| 20 | + // Captures the path after / in non-greedy manner. |
| 21 | + optionalPath = `(?:/(?P<path>.*?))?` |
| 22 | + |
| 23 | + // Captures the revision after @ in non-greedy manner. |
| 24 | + optionalRevision = `(?:@(?P<revision>.*))?` |
| 25 | +) |
| 26 | + |
| 27 | +var regexVariants = []*regexp.Regexp{ |
| 28 | + // GitHub |
| 29 | + regexp.MustCompile(`^(?P<root>github\.com/.*?/.*?)` + optionalPath + optionalRevision + `$`), |
| 30 | + // Other Git hosting services |
| 31 | + regexp.MustCompile(`^(?P<root>.*?)\.git` + optionalPath + optionalRevision + `$`), |
| 32 | +} |
| 33 | + |
| 34 | +func Parse(module string) *Locator { |
| 35 | + result := &Locator{ |
| 36 | + Path: "lib.star", |
| 37 | + Revision: "master", |
| 38 | + } |
| 39 | + |
| 40 | + for _, regex := range regexVariants { |
| 41 | + matches := regex.FindStringSubmatch(module) |
| 42 | + if matches == nil { |
| 43 | + continue |
| 44 | + } |
| 45 | + |
| 46 | + result.URL = "https://" + matches[regex.SubexpIndex("root")] + ".git" |
| 47 | + |
| 48 | + path := matches[regex.SubexpIndex("path")] |
| 49 | + if path != "" { |
| 50 | + result.Path = path |
| 51 | + } |
| 52 | + |
| 53 | + revision := matches[regex.SubexpIndex("revision")] |
| 54 | + if revision != "" { |
| 55 | + result.Revision = revision |
| 56 | + } |
| 57 | + |
| 58 | + return result |
| 59 | + } |
| 60 | + |
| 61 | + return nil |
| 62 | +} |
| 63 | + |
| 64 | +func Retrieve(ctx context.Context, locator *Locator) ([]byte, error) { |
| 65 | + storer := memory.NewStorage() |
| 66 | + fs := memfs.New() |
| 67 | + |
| 68 | + // Clone the repository |
| 69 | + repo, err := git.CloneContext(ctx, storer, fs, &git.CloneOptions{ |
| 70 | + URL: locator.URL, |
| 71 | + Depth: 1, |
| 72 | + }) |
| 73 | + if err != nil { |
| 74 | + return nil, err |
| 75 | + } |
| 76 | + |
| 77 | + // Checkout the working tree to the specified revision |
| 78 | + worktree, err := repo.Worktree() |
| 79 | + if err != nil { |
| 80 | + return nil, err |
| 81 | + } |
| 82 | + |
| 83 | + hash, err := repo.ResolveRevision(plumbing.Revision(locator.Revision)) |
| 84 | + if err != nil { |
| 85 | + return nil, err |
| 86 | + } |
| 87 | + |
| 88 | + if err := worktree.Checkout(&git.CheckoutOptions{ |
| 89 | + Hash: *hash, |
| 90 | + }); err != nil { |
| 91 | + return nil, err |
| 92 | + } |
| 93 | + |
| 94 | + // Read the file from the working tree |
| 95 | + file, err := worktree.Filesystem.Open(locator.Path) |
| 96 | + if err != nil { |
| 97 | + return nil, err |
| 98 | + } |
| 99 | + |
| 100 | + fileBytes, err := ioutil.ReadAll(file) |
| 101 | + if err != nil { |
| 102 | + return nil, err |
| 103 | + } |
| 104 | + |
| 105 | + return fileBytes, nil |
| 106 | +} |
0 commit comments