a5bee27892
message: Change all references to 'master' into 'trunk' change_hash: ABJwxLvHMmj63oJPIv/vNeRCIp1ZDZYuPLQT57x9K1lO credentials: - type: pgp_signature pub_key_id: 95C46FA6A41148AC body: iQIzBAABAgAdFiEEJ6tQKp6olvZKJ0lwlcRvpqQRSKwFAl5Je88ACgkQlcRvpqQRSKyxyw/7B51/vvtlxLan9Z6q6rh7uyGcFf6WpWGiRDIccAJHqzegGP4eAb8V4Jzi9H4JJ82TnAc+EUegs8ewRiOcWj6YkU463b5CgUSwnzYKm86K6SyHGW1WH9OxFIDEMzCaVsktMEc9iLMl0dJNzakhPzu+qy74pU5xlypjCBzRLFeuqmnf4M1fq4FAq6fCs7ZVB3LccyC0mhCWsS2eiuCE/mVQ7WROVpxj5tplp71jlX6ZtWU7qsgvQS2V8ggtTVpCT2WE4u6bnu1oYOpSs9g+sxKKOAHKvZfjAMLG9qM3pOl1J+44W2Ms/mtON0VUX7G1Q5XVcmM0hPopXsiWLiAslSOAOuL+LE5iLq4nz1RyIbVh0QakYr+4NJL6Yt0L2I6lNVnUS4SgmMr86n8ZCcCjDAs6g5d7Zchqp3S3EF3bbJuLi0ICoOCxTD2gNkjo2BGverI8APLTUpujQl+9W/sGmT2aEdTpruGYjIcwsRaGo8VMDFECdZply5Ng1FbBoohv+j3vdO05fRyNkvRu3CBxP2tUe39jLxUmpu62igGF2VjZptsK0bLdzDpQ5Hv6jSZjn+mPyFJ5w+EX2JCRYpjn2eaYYtI/IyULGTaftzcEZeZibEhd/wQJnsEJNqXpCilDKgZajZyYhgy7BSeT9xDf2d8qyFoqWpvFhshHcesbUfG2O+Y= account: mediocregopher
129 lines
3.3 KiB
Go
129 lines
3.3 KiB
Go
// Package dehub TODO needs package docs
|
|
package dehub
|
|
|
|
import (
|
|
"dehub/fs"
|
|
"errors"
|
|
"fmt"
|
|
"path/filepath"
|
|
|
|
"gopkg.in/src-d/go-billy.v4"
|
|
"gopkg.in/src-d/go-billy.v4/memfs"
|
|
"gopkg.in/src-d/go-git.v4"
|
|
"gopkg.in/src-d/go-git.v4/plumbing"
|
|
"gopkg.in/src-d/go-git.v4/plumbing/object"
|
|
"gopkg.in/src-d/go-git.v4/storage/memory"
|
|
)
|
|
|
|
const (
|
|
// DehubDir defines the name of the directory where all dehub-related files are
|
|
// expected to be found.
|
|
DehubDir = ".dehub"
|
|
)
|
|
|
|
var (
|
|
// ConfigPath defines the expected path to the Repo's configuration file.
|
|
ConfigPath = filepath.Join(DehubDir, "config.yml")
|
|
|
|
// Trunk defines the reference name of the trunk branch.
|
|
Trunk = plumbing.ReferenceName("refs/heads/trunk")
|
|
)
|
|
|
|
type repoOpts struct {
|
|
bare bool
|
|
}
|
|
|
|
// OpenOption is an option which can be passed to the OpenRepo function to
|
|
// affect the Repo's behavior.
|
|
type OpenOption func(*repoOpts)
|
|
|
|
// OpenBare returns an OpenOption which, if true is given, causes the OpenRepo
|
|
// function to expect to open a bare repo.
|
|
func OpenBare(bare bool) OpenOption {
|
|
return func(o *repoOpts) {
|
|
o.bare = bare
|
|
}
|
|
}
|
|
|
|
// Repo is an object which allows accessing and modifying the dehub repo.
|
|
type Repo struct {
|
|
GitRepo *git.Repository
|
|
}
|
|
|
|
// OpenRepo opens the dehub repo in the given directory and returns the object
|
|
// for it.
|
|
//
|
|
// The given path is expected to have a git repo and .dehub folder already
|
|
// initialized.
|
|
func OpenRepo(path string, options ...OpenOption) (*Repo, error) {
|
|
var opts repoOpts
|
|
for _, opt := range options {
|
|
opt(&opts)
|
|
}
|
|
|
|
r := Repo{}
|
|
var err error
|
|
openOpts := &git.PlainOpenOptions{
|
|
DetectDotGit: !opts.bare,
|
|
}
|
|
if r.GitRepo, err = git.PlainOpenWithOptions(path, openOpts); err != nil {
|
|
return nil, fmt.Errorf("could not open git repo: %w", err)
|
|
}
|
|
return &r, nil
|
|
}
|
|
|
|
// InitMemRepo initializes an empty repository which only exists in memory.
|
|
func InitMemRepo() *Repo {
|
|
r, err := git.Init(memory.NewStorage(), memfs.New())
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
return &Repo{GitRepo: r}
|
|
}
|
|
|
|
func (r *Repo) billyFilesystem() (billy.Filesystem, error) {
|
|
w, err := r.GitRepo.Worktree()
|
|
if err != nil {
|
|
return nil, fmt.Errorf("could not open git worktree: %w", err)
|
|
}
|
|
return w.Filesystem, nil
|
|
}
|
|
|
|
func (r *Repo) head() (*object.Commit, *object.Tree, error) {
|
|
head, err := r.GitRepo.Head()
|
|
if err != nil {
|
|
return nil, nil, fmt.Errorf("could not get repo HEAD: %w", err)
|
|
}
|
|
|
|
headHash := head.Hash()
|
|
headCommit, err := r.GitRepo.CommitObject(headHash)
|
|
if err != nil {
|
|
return nil, nil, fmt.Errorf("could not get commit at HEAD (%q): %w", headHash, err)
|
|
}
|
|
|
|
headTree, err := r.GitRepo.TreeObject(headCommit.TreeHash)
|
|
if err != nil {
|
|
return nil, nil, fmt.Errorf("could not get tree object at HEAD (commit:%q tree:%q): %w",
|
|
headHash, headCommit.TreeHash, err)
|
|
}
|
|
|
|
return headCommit, headTree, nil
|
|
}
|
|
|
|
// headOrRawFS returns an FS based on the HEAD commit, or if there is no HEAD
|
|
// commit (it's an empty repo) an FS based on the raw filesystem.
|
|
func (r *Repo) headOrRawFS() (fs.FS, error) {
|
|
_, headTree, err := r.head()
|
|
if errors.Is(err, plumbing.ErrReferenceNotFound) {
|
|
bfs, err := r.billyFilesystem()
|
|
if err != nil {
|
|
return nil, fmt.Errorf("could not get underlying filesystem: %w", err)
|
|
}
|
|
return fs.FromBillyFilesystem(bfs), nil
|
|
} else if err != nil {
|
|
return nil, fmt.Errorf("could not get HEAD tree: %w", err)
|
|
}
|
|
return fs.FromTree(headTree), nil
|
|
}
|