git-bug/repository/git.go

501 lines
12 KiB
Go
Raw Normal View History

2018-07-12 10:55:13 +03:00
// Package repository contains helper methods for working with the Git repo.
package repository
import (
"bytes"
"fmt"
2020-12-01 23:19:23 +03:00
"os"
"path/filepath"
2018-07-12 10:55:13 +03:00
"strings"
"sync"
2020-12-08 16:42:13 +03:00
"github.com/blevesearch/bleve"
2020-12-01 23:19:23 +03:00
"github.com/go-git/go-billy/v5"
"github.com/go-git/go-billy/v5/osfs"
"github.com/MichaelMure/git-bug/util/lamport"
2018-07-12 10:55:13 +03:00
)
const (
clockPath = "git-bug"
)
2019-02-19 02:19:27 +03:00
var _ ClockedRepo = &GitRepo{}
var _ TestedRepo = &GitRepo{}
2019-02-19 02:19:27 +03:00
2018-07-12 10:55:13 +03:00
// GitRepo represents an instance of a (local) git repository.
type GitRepo struct {
gitCli
path string
clocksMutex sync.Mutex
clocks map[string]lamport.Clock
2020-12-08 16:42:13 +03:00
indexesMutex sync.Mutex
indexes map[string]bleve.Index
keyring Keyring
2018-07-12 10:55:13 +03:00
}
2020-12-08 16:42:13 +03:00
// OpenGitRepo determines if the given working directory is inside of a git repository,
2018-07-12 10:55:13 +03:00
// and returns the corresponding GitRepo instance if it is.
2020-12-08 16:42:13 +03:00
func OpenGitRepo(path string, clockLoaders []ClockLoader) (*GitRepo, error) {
k, err := defaultKeyring()
if err != nil {
return nil, err
}
repo := &GitRepo{
gitCli: gitCli{path: path},
path: path,
clocks: make(map[string]lamport.Clock),
2020-12-08 16:42:13 +03:00
indexes: make(map[string]bleve.Index),
keyring: k,
}
// Check the repo and retrieve the root path
stdout, err := repo.runGitCommand("rev-parse", "--absolute-git-dir")
// Now dir is fetched with "git rev-parse --git-dir". May be it can
// still return nothing in some cases. Then empty stdout check is
// kept.
if err != nil || stdout == "" {
return nil, ErrNotARepo
2018-07-12 10:55:13 +03:00
}
// Fix the path to be sure we are at the root
repo.path = stdout
repo.gitCli.path = stdout
for _, loader := range clockLoaders {
allExist := true
for _, name := range loader.Clocks {
if _, err := repo.getClock(name); err != nil {
allExist = false
}
}
if !allExist {
err = loader.Witnesser(repo)
if err != nil {
return nil, err
}
}
}
return repo, nil
2018-07-12 10:55:13 +03:00
}
2018-08-13 16:28:16 +03:00
// InitGitRepo create a new empty git repo at the given path
func InitGitRepo(path string) (*GitRepo, error) {
repo := &GitRepo{
2020-12-08 16:42:13 +03:00
gitCli: gitCli{path: path},
path: path + "/.git",
clocks: make(map[string]lamport.Clock),
indexes: make(map[string]bleve.Index),
}
_, err := repo.runGitCommand("init", path)
if err != nil {
return nil, err
}
return repo, nil
}
2018-08-13 16:28:16 +03:00
// InitBareGitRepo create a new --bare empty git repo at the given path
func InitBareGitRepo(path string) (*GitRepo, error) {
repo := &GitRepo{
2020-12-08 16:42:13 +03:00
gitCli: gitCli{path: path},
path: path,
clocks: make(map[string]lamport.Clock),
indexes: make(map[string]bleve.Index),
}
_, err := repo.runGitCommand("init", "--bare", path)
if err != nil {
return nil, err
}
return repo, nil
}
2020-12-08 16:42:13 +03:00
func (repo *GitRepo) Close() error {
var firstErr error
for _, index := range repo.indexes {
err := index.Close()
if err != nil && firstErr == nil {
firstErr = err
}
}
return firstErr
}
// LocalConfig give access to the repository scoped configuration
func (repo *GitRepo) LocalConfig() Config {
return newGitConfig(repo.gitCli, false)
}
// GlobalConfig give access to the global scoped configuration
func (repo *GitRepo) GlobalConfig() Config {
return newGitConfig(repo.gitCli, true)
}
// AnyConfig give access to a merged local/global configuration
func (repo *GitRepo) AnyConfig() ConfigRead {
return mergeConfig(repo.LocalConfig(), repo.GlobalConfig())
}
// Keyring give access to a user-wide storage for secrets
func (repo *GitRepo) Keyring() Keyring {
return repo.keyring
}
2018-07-12 10:55:13 +03:00
// GetPath returns the path to the repo.
func (repo *GitRepo) GetPath() string {
return repo.path
2018-07-12 10:55:13 +03:00
}
// GetUserName returns the name the the user has used to configure git
func (repo *GitRepo) GetUserName() (string, error) {
return repo.runGitCommand("config", "user.name")
}
2018-07-12 10:55:13 +03:00
// GetUserEmail returns the email address that the user has used to configure git.
func (repo *GitRepo) GetUserEmail() (string, error) {
return repo.runGitCommand("config", "user.email")
}
// GetCoreEditor returns the name of the editor that the user has used to configure git.
func (repo *GitRepo) GetCoreEditor() (string, error) {
return repo.runGitCommand("var", "GIT_EDITOR")
}
2019-05-25 16:13:40 +03:00
// GetRemotes returns the configured remotes repositories.
func (repo *GitRepo) GetRemotes() (map[string]string, error) {
stdout, err := repo.runGitCommand("remote", "--verbose")
if err != nil {
return nil, err
}
lines := strings.Split(stdout, "\n")
remotes := make(map[string]string, len(lines))
for _, line := range lines {
if strings.TrimSpace(line) == "" {
continue
}
2019-05-25 16:13:40 +03:00
elements := strings.Fields(line)
if len(elements) != 3 {
return nil, fmt.Errorf("git remote: unexpected output format: %s", line)
2019-05-25 16:13:40 +03:00
}
remotes[elements[0]] = elements[1]
}
return remotes, nil
}
2020-12-01 23:19:23 +03:00
// LocalStorage return a billy.Filesystem giving access to $RepoPath/.git/git-bug
func (repo *GitRepo) LocalStorage() billy.Filesystem {
return osfs.New(repo.path)
}
2020-12-08 16:42:13 +03:00
// GetBleveIndex return a bleve.Index that can be used to index documents
func (repo *GitRepo) GetBleveIndex(name string) (bleve.Index, error) {
repo.indexesMutex.Lock()
defer repo.indexesMutex.Unlock()
if index, ok := repo.indexes[name]; ok {
return index, nil
}
path := filepath.Join(repo.path, "indexes", name)
index, err := bleve.Open(path)
if err == nil {
repo.indexes[name] = index
return index, nil
}
err = os.MkdirAll(path, os.ModeDir)
if err != nil {
return nil, err
}
mapping := bleve.NewIndexMapping()
mapping.DefaultAnalyzer = "en"
index, err = bleve.New(path, mapping)
if err != nil {
return nil, err
}
repo.indexes[name] = index
return index, nil
}
// ClearBleveIndex will wipe the given index
func (repo *GitRepo) ClearBleveIndex(name string) error {
repo.indexesMutex.Lock()
defer repo.indexesMutex.Unlock()
path := filepath.Join(repo.path, "indexes", name)
err := os.RemoveAll(path)
if err != nil {
return err
}
delete(repo.indexes, name)
return nil
}
2018-07-17 02:52:56 +03:00
// FetchRefs fetch git refs from a remote
2018-08-12 22:09:30 +03:00
func (repo *GitRepo) FetchRefs(remote, refSpec string) (string, error) {
stdout, err := repo.runGitCommand("fetch", remote, refSpec)
2018-07-13 22:51:27 +03:00
if err != nil {
2018-08-12 22:09:30 +03:00
return stdout, fmt.Errorf("failed to fetch from the remote '%s': %v", remote, err)
2018-07-13 22:51:27 +03:00
}
2018-07-12 10:55:13 +03:00
2018-08-12 22:09:30 +03:00
return stdout, err
2018-07-12 10:55:13 +03:00
}
// PushRefs push git refs to a remote
2018-08-12 22:09:30 +03:00
func (repo *GitRepo) PushRefs(remote string, refSpec string) (string, error) {
stdout, stderr, err := repo.runGitCommandRaw(nil, "push", remote, refSpec)
2018-07-13 22:51:27 +03:00
2018-07-12 10:55:13 +03:00
if err != nil {
2018-09-06 22:42:05 +03:00
return stdout + stderr, fmt.Errorf("failed to push to the remote '%s': %v", remote, stderr)
2018-07-12 10:55:13 +03:00
}
2018-08-12 22:09:30 +03:00
return stdout + stderr, nil
2018-07-12 10:55:13 +03:00
}
2018-07-12 16:14:37 +03:00
// StoreData will store arbitrary data and return the corresponding hash
func (repo *GitRepo) StoreData(data []byte) (Hash, error) {
2018-07-12 16:14:37 +03:00
var stdin = bytes.NewReader(data)
stdout, err := repo.runGitCommandWithStdin(stdin, "hash-object", "--stdin", "-w")
return Hash(stdout), err
}
// ReadData will attempt to read arbitrary data from the given hash
func (repo *GitRepo) ReadData(hash Hash) ([]byte, error) {
var stdout bytes.Buffer
var stderr bytes.Buffer
err := repo.runGitCommandWithIO(nil, &stdout, &stderr, "cat-file", "-p", string(hash))
if err != nil {
return []byte{}, err
}
return stdout.Bytes(), nil
}
// StoreTree will store a mapping key-->Hash as a Git tree
func (repo *GitRepo) StoreTree(entries []TreeEntry) (Hash, error) {
buffer := prepareTreeEntries(entries)
stdout, err := repo.runGitCommandWithStdin(&buffer, "mktree")
if err != nil {
return "", err
}
2018-07-12 16:14:37 +03:00
return Hash(stdout), nil
2018-07-12 16:14:37 +03:00
}
// StoreCommit will store a Git commit with the given Git tree
func (repo *GitRepo) StoreCommit(treeHash Hash) (Hash, error) {
stdout, err := repo.runGitCommand("commit-tree", string(treeHash))
if err != nil {
return "", err
}
return Hash(stdout), nil
}
// StoreCommitWithParent will store a Git commit with the given Git tree
func (repo *GitRepo) StoreCommitWithParent(treeHash Hash, parent Hash) (Hash, error) {
stdout, err := repo.runGitCommand("commit-tree", string(treeHash),
"-p", string(parent))
if err != nil {
return "", err
}
return Hash(stdout), nil
}
// UpdateRef will create or update a Git reference
func (repo *GitRepo) UpdateRef(ref string, hash Hash) error {
_, err := repo.runGitCommand("update-ref", ref, string(hash))
return err
}
// RemoveRef will remove a Git reference
func (repo *GitRepo) RemoveRef(ref string) error {
_, err := repo.runGitCommand("update-ref", "-d", ref)
return err
}
// ListRefs will return a list of Git ref matching the given refspec
2020-09-08 15:31:40 +03:00
func (repo *GitRepo) ListRefs(refPrefix string) ([]string, error) {
stdout, err := repo.runGitCommand("for-each-ref", "--format=%(refname)", refPrefix)
if err != nil {
return nil, err
}
2018-09-09 21:17:12 +03:00
split := strings.Split(stdout, "\n")
2018-09-09 21:17:12 +03:00
if len(split) == 1 && split[0] == "" {
return []string{}, nil
}
2018-09-09 21:17:12 +03:00
return split, nil
}
2018-07-17 02:52:56 +03:00
// RefExist will check if a reference exist in Git
func (repo *GitRepo) RefExist(ref string) (bool, error) {
stdout, err := repo.runGitCommand("for-each-ref", ref)
if err != nil {
return false, err
}
return stdout != "", nil
}
// CopyRef will create a new reference with the same value as another one
func (repo *GitRepo) CopyRef(source string, dest string) error {
_, err := repo.runGitCommand("update-ref", dest, source)
return err
}
// ListCommits will return the list of commit hashes of a ref, in chronological order
func (repo *GitRepo) ListCommits(ref string) ([]Hash, error) {
stdout, err := repo.runGitCommand("rev-list", "--first-parent", "--reverse", ref)
if err != nil {
return nil, err
}
2018-09-09 21:17:12 +03:00
split := strings.Split(stdout, "\n")
casted := make([]Hash, len(split))
2018-09-09 21:17:12 +03:00
for i, line := range split {
casted[i] = Hash(line)
}
return casted, nil
}
2020-06-27 23:00:15 +03:00
// ReadTree will return the list of entries in a Git tree
func (repo *GitRepo) ReadTree(hash Hash) ([]TreeEntry, error) {
stdout, err := repo.runGitCommand("ls-tree", string(hash))
if err != nil {
return nil, err
}
return readTreeEntries(stdout)
}
2018-07-17 02:52:56 +03:00
// FindCommonAncestor will return the last common ancestor of two chain of commit
func (repo *GitRepo) FindCommonAncestor(hash1 Hash, hash2 Hash) (Hash, error) {
2018-07-17 02:52:56 +03:00
stdout, err := repo.runGitCommand("merge-base", string(hash1), string(hash2))
if err != nil {
return "", err
2018-07-17 02:52:56 +03:00
}
return Hash(stdout), nil
2018-07-17 02:52:56 +03:00
}
2018-08-13 16:28:16 +03:00
// GetTreeHash return the git tree hash referenced in a commit
func (repo *GitRepo) GetTreeHash(commit Hash) (Hash, error) {
2018-07-17 02:52:56 +03:00
stdout, err := repo.runGitCommand("rev-parse", string(commit)+"^{tree}")
if err != nil {
return "", err
2018-07-17 02:52:56 +03:00
}
return Hash(stdout), nil
2018-07-17 02:52:56 +03:00
}
// GetOrCreateClock return a Lamport clock stored in the Repo.
// If the clock doesn't exist, it's created.
func (repo *GitRepo) GetOrCreateClock(name string) (lamport.Clock, error) {
2020-12-08 15:15:21 +03:00
repo.clocksMutex.Lock()
defer repo.clocksMutex.Unlock()
c, err := repo.getClock(name)
if err == nil {
return c, nil
}
if err != ErrClockNotExist {
return nil, err
}
2020-12-05 05:08:54 +03:00
c, err = lamport.NewPersistedClock(repo.LocalStorage(), name+"-clock")
if err != nil {
return nil, err
}
repo.clocks[name] = c
return c, nil
}
func (repo *GitRepo) getClock(name string) (lamport.Clock, error) {
if c, ok := repo.clocks[name]; ok {
return c, nil
}
2020-12-05 05:08:54 +03:00
c, err := lamport.LoadPersistedClock(repo.LocalStorage(), name+"-clock")
if err == nil {
repo.clocks[name] = c
return c, nil
}
if err == lamport.ErrClockNotExist {
return nil, ErrClockNotExist
}
return nil, err
}
// AddRemote add a new remote to the repository
// Not in the interface because it's only used for testing
func (repo *GitRepo) AddRemote(name string, url string) error {
_, err := repo.runGitCommand("remote", "add", name, url)
return err
}
2020-12-01 23:19:23 +03:00
// GetLocalRemote return the URL to use to add this repo as a local remote
func (repo *GitRepo) GetLocalRemote() string {
return repo.path
}
// EraseFromDisk delete this repository entirely from the disk
func (repo *GitRepo) EraseFromDisk() error {
2020-12-08 17:09:58 +03:00
err := repo.Close()
if err != nil {
return err
}
2020-12-01 23:19:23 +03:00
path := filepath.Clean(strings.TrimSuffix(repo.path, string(filepath.Separator)+".git"))
// fmt.Println("Cleaning repo:", path)
return os.RemoveAll(path)
}