2018-07-12 10:55:13 +03:00
|
|
|
// Package repository contains helper methods for working with a Git repo.
|
|
|
|
package repository
|
|
|
|
|
2018-07-14 23:17:37 +03:00
|
|
|
import (
|
2019-05-27 22:18:46 +03:00
|
|
|
"errors"
|
2021-01-04 01:59:25 +03:00
|
|
|
"io"
|
2018-08-13 19:32:11 +03:00
|
|
|
|
2022-02-15 23:35:49 +03:00
|
|
|
"github.com/ProtonMail/go-crypto/openpgp"
|
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"
|
|
|
|
|
2018-09-11 23:04:16 +03:00
|
|
|
"github.com/MichaelMure/git-bug/util/lamport"
|
2018-07-14 23:17:37 +03:00
|
|
|
)
|
2018-07-12 16:14:37 +03:00
|
|
|
|
2019-09-17 14:59:40 +03:00
|
|
|
var (
|
2021-01-04 01:59:25 +03:00
|
|
|
// ErrNotARepo is the error returned when the git repo root can't be found
|
2020-06-23 19:02:54 +03:00
|
|
|
ErrNotARepo = errors.New("not a git repository")
|
|
|
|
// ErrClockNotExist is the error returned when a clock can't be found
|
|
|
|
ErrClockNotExist = errors.New("clock doesn't exist")
|
2019-09-17 14:59:40 +03:00
|
|
|
)
|
2019-05-27 22:18:46 +03:00
|
|
|
|
2020-07-28 14:02:32 +03:00
|
|
|
// Repo represents a source code repository.
|
|
|
|
type Repo interface {
|
|
|
|
RepoConfig
|
|
|
|
RepoKeyring
|
|
|
|
RepoCommon
|
2020-12-01 23:19:23 +03:00
|
|
|
RepoStorage
|
2020-12-08 16:42:13 +03:00
|
|
|
RepoBleve
|
2020-11-08 19:54:28 +03:00
|
|
|
RepoData
|
2020-12-08 16:42:13 +03:00
|
|
|
|
|
|
|
Close() error
|
2020-07-28 14:02:32 +03:00
|
|
|
}
|
|
|
|
|
2020-12-05 05:08:54 +03:00
|
|
|
type RepoCommonStorage interface {
|
|
|
|
RepoCommon
|
|
|
|
RepoStorage
|
|
|
|
}
|
|
|
|
|
2020-09-26 15:06:01 +03:00
|
|
|
// ClockedRepo is a Repo that also has Lamport clocks
|
|
|
|
type ClockedRepo interface {
|
|
|
|
Repo
|
|
|
|
RepoClock
|
|
|
|
}
|
|
|
|
|
2019-12-08 23:15:06 +03:00
|
|
|
// RepoConfig access the configuration of a repository
|
|
|
|
type RepoConfig interface {
|
|
|
|
// LocalConfig give access to the repository scoped configuration
|
|
|
|
LocalConfig() Config
|
2020-09-27 01:54:14 +03:00
|
|
|
|
|
|
|
// GlobalConfig give access to the global scoped configuration
|
|
|
|
GlobalConfig() Config
|
|
|
|
|
|
|
|
// AnyConfig give access to a merged local/global configuration
|
|
|
|
AnyConfig() ConfigRead
|
2019-12-08 23:15:06 +03:00
|
|
|
}
|
|
|
|
|
2020-07-27 01:14:01 +03:00
|
|
|
// RepoKeyring give access to a user-wide storage for secrets
|
|
|
|
type RepoKeyring interface {
|
|
|
|
// Keyring give access to a user-wide storage for secrets
|
|
|
|
Keyring() Keyring
|
|
|
|
}
|
|
|
|
|
2018-09-24 16:21:34 +03:00
|
|
|
// RepoCommon represent the common function the we want all the repo to implement
|
2018-09-21 19:18:51 +03:00
|
|
|
type RepoCommon interface {
|
2018-07-12 13:44:46 +03:00
|
|
|
// GetUserName returns the name the the user has used to configure git
|
|
|
|
GetUserName() (string, error)
|
|
|
|
|
2018-07-12 10:55:13 +03:00
|
|
|
// GetUserEmail returns the email address that the user has used to configure git.
|
|
|
|
GetUserEmail() (string, error)
|
|
|
|
|
|
|
|
// GetCoreEditor returns the name of the editor that the user has used to configure git.
|
|
|
|
GetCoreEditor() (string, error)
|
2018-09-24 16:21:34 +03:00
|
|
|
|
2019-05-25 16:13:40 +03:00
|
|
|
// GetRemotes returns the configured remotes repositories.
|
|
|
|
GetRemotes() (map[string]string, error)
|
2018-09-21 19:18:51 +03:00
|
|
|
}
|
|
|
|
|
2020-12-08 16:42:13 +03:00
|
|
|
// RepoStorage give access to the filesystem
|
2020-12-01 23:19:23 +03:00
|
|
|
type RepoStorage interface {
|
|
|
|
// LocalStorage return a billy.Filesystem giving access to $RepoPath/.git/git-bug
|
|
|
|
LocalStorage() billy.Filesystem
|
|
|
|
}
|
|
|
|
|
2020-12-08 16:42:13 +03:00
|
|
|
// RepoBleve give access to Bleve to implement full-text search indexes.
|
|
|
|
type RepoBleve interface {
|
|
|
|
// GetBleveIndex return a bleve.Index that can be used to index documents
|
|
|
|
GetBleveIndex(name string) (bleve.Index, error)
|
|
|
|
|
|
|
|
// ClearBleveIndex will wipe the given index
|
|
|
|
ClearBleveIndex(name string) error
|
|
|
|
}
|
|
|
|
|
2020-12-21 13:05:47 +03:00
|
|
|
type Commit struct {
|
2021-01-04 01:59:25 +03:00
|
|
|
Hash Hash
|
|
|
|
Parents []Hash // hashes of the parents, if any
|
|
|
|
TreeHash Hash // hash of the git Tree
|
|
|
|
SignedData io.Reader // if signed, reader for the signed data (likely, the serialized commit)
|
|
|
|
Signature io.Reader // if signed, reader for the (non-armored) signature
|
2020-12-21 13:05:47 +03:00
|
|
|
}
|
|
|
|
|
2020-07-28 14:02:32 +03:00
|
|
|
// RepoData give access to the git data storage
|
|
|
|
type RepoData interface {
|
2021-02-05 13:18:38 +03:00
|
|
|
// FetchRefs fetch git refs matching a directory prefix to a remote
|
|
|
|
// Ex: prefix="foo" will fetch any remote refs matching "refs/foo/*" locally.
|
|
|
|
// The equivalent git refspec would be "refs/foo/*:refs/remotes/<remote>/foo/*"
|
|
|
|
FetchRefs(remote string, prefix string) (string, error)
|
|
|
|
|
|
|
|
// PushRefs push git refs matching a directory prefix to a remote
|
|
|
|
// Ex: prefix="foo" will push any local refs matching "refs/foo/*" to the remote.
|
|
|
|
// The equivalent git refspec would be "refs/foo/*:refs/foo/*"
|
|
|
|
//
|
|
|
|
// Additionally, PushRefs will update the local references in refs/remotes/<remote>/foo to match
|
|
|
|
// the remote state.
|
|
|
|
PushRefs(remote string, prefix string) (string, error)
|
2018-07-12 16:14:37 +03:00
|
|
|
|
|
|
|
// StoreData will store arbitrary data and return the corresponding hash
|
2020-07-01 20:39:02 +03:00
|
|
|
StoreData(data []byte) (Hash, error)
|
2018-07-13 22:21:24 +03:00
|
|
|
|
2018-07-14 23:17:37 +03:00
|
|
|
// ReadData will attempt to read arbitrary data from the given hash
|
2020-07-01 20:39:02 +03:00
|
|
|
ReadData(hash Hash) ([]byte, error)
|
2018-07-14 23:17:37 +03:00
|
|
|
|
2018-07-13 22:21:24 +03:00
|
|
|
// StoreTree will store a mapping key-->Hash as a Git tree
|
2020-07-01 20:39:02 +03:00
|
|
|
StoreTree(mapping []TreeEntry) (Hash, error)
|
2018-07-13 22:21:24 +03:00
|
|
|
|
2020-06-27 23:00:15 +03:00
|
|
|
// ReadTree will return the list of entries in a Git tree
|
2020-09-27 22:14:25 +03:00
|
|
|
// The given hash could be from either a commit or a tree
|
2020-07-01 20:39:02 +03:00
|
|
|
ReadTree(hash Hash) ([]TreeEntry, error)
|
2020-06-27 23:00:15 +03:00
|
|
|
|
2018-07-13 22:21:24 +03:00
|
|
|
// StoreCommit will store a Git commit with the given Git tree
|
2021-01-04 01:59:25 +03:00
|
|
|
StoreCommit(treeHash Hash, parents ...Hash) (Hash, error)
|
2018-07-13 22:21:24 +03:00
|
|
|
|
2022-02-15 23:35:49 +03:00
|
|
|
// StoreSignedCommit will store a Git commit with the given Git tree. If signKey is not nil, the commit
|
2021-01-04 01:59:25 +03:00
|
|
|
// will be signed accordingly.
|
|
|
|
StoreSignedCommit(treeHash Hash, signKey *openpgp.Entity, parents ...Hash) (Hash, error)
|
2018-07-13 22:21:24 +03:00
|
|
|
|
2021-01-04 01:59:25 +03:00
|
|
|
// ReadCommit read a Git commit and returns some of its characteristic
|
2020-12-21 13:05:47 +03:00
|
|
|
ReadCommit(hash Hash) (Commit, error)
|
|
|
|
|
2020-06-27 23:00:15 +03:00
|
|
|
// GetTreeHash return the git tree hash referenced in a commit
|
2021-01-25 14:39:34 +03:00
|
|
|
// Deprecated
|
2020-07-01 20:39:02 +03:00
|
|
|
GetTreeHash(commit Hash) (Hash, error)
|
2020-06-27 23:00:15 +03:00
|
|
|
|
2021-01-04 01:59:25 +03:00
|
|
|
// ResolveRef returns the hash of the target commit of the given ref
|
2020-12-21 13:05:47 +03:00
|
|
|
ResolveRef(ref string) (Hash, error)
|
2020-06-27 23:00:15 +03:00
|
|
|
|
2018-07-13 22:21:24 +03:00
|
|
|
// UpdateRef will create or update a Git reference
|
2020-07-01 20:39:02 +03:00
|
|
|
UpdateRef(ref string, hash Hash) error
|
2018-07-14 23:17:37 +03:00
|
|
|
|
2020-07-09 15:40:44 +03:00
|
|
|
// RemoveRef will remove a Git reference
|
2021-02-10 20:22:21 +03:00
|
|
|
// RemoveRef is idempotent.
|
2020-07-09 15:40:44 +03:00
|
|
|
RemoveRef(ref string) error
|
|
|
|
|
2018-07-14 23:17:37 +03:00
|
|
|
// ListRefs will return a list of Git ref matching the given refspec
|
2020-09-08 15:31:40 +03:00
|
|
|
ListRefs(refPrefix string) ([]string, error)
|
2018-07-14 23:17:37 +03:00
|
|
|
|
2018-07-17 02:52:56 +03:00
|
|
|
// RefExist will check if a reference exist in Git
|
|
|
|
RefExist(ref string) (bool, error)
|
|
|
|
|
|
|
|
// CopyRef will create a new reference with the same value as another one
|
|
|
|
CopyRef(source string, dest string) error
|
|
|
|
|
2020-12-21 13:05:47 +03:00
|
|
|
// FindCommonAncestor will return the last common ancestor of two chain of commit
|
|
|
|
// Deprecated
|
|
|
|
FindCommonAncestor(commit1 Hash, commit2 Hash) (Hash, error)
|
|
|
|
|
2018-07-14 23:17:37 +03:00
|
|
|
// ListCommits will return the list of tree hashes of a ref, in chronological order
|
2020-07-01 20:39:02 +03:00
|
|
|
ListCommits(ref string) ([]Hash, error)
|
2018-09-21 19:18:51 +03:00
|
|
|
}
|
|
|
|
|
2020-09-26 15:06:01 +03:00
|
|
|
// RepoClock give access to Lamport clocks
|
|
|
|
type RepoClock interface {
|
2020-11-08 19:54:28 +03:00
|
|
|
// AllClocks return all the known clocks
|
|
|
|
AllClocks() (map[string]lamport.Clock, error)
|
|
|
|
|
2020-06-23 19:02:54 +03:00
|
|
|
// GetOrCreateClock return a Lamport clock stored in the Repo.
|
|
|
|
// If the clock doesn't exist, it's created.
|
|
|
|
GetOrCreateClock(name string) (lamport.Clock, error)
|
2020-11-08 19:54:28 +03:00
|
|
|
|
|
|
|
// Increment is equivalent to c = GetOrCreateClock(name) + c.Increment()
|
|
|
|
Increment(name string) (lamport.Time, error)
|
|
|
|
|
|
|
|
// Witness is equivalent to c = GetOrCreateClock(name) + c.Witness(time)
|
|
|
|
Witness(name string, time lamport.Time) error
|
2018-07-14 23:17:37 +03:00
|
|
|
}
|
|
|
|
|
2020-06-23 19:02:54 +03:00
|
|
|
// ClockLoader hold which logical clock need to exist for an entity and
|
|
|
|
// how to create them if they don't.
|
|
|
|
type ClockLoader struct {
|
|
|
|
// Clocks hold the name of all the clocks this loader deal with.
|
|
|
|
// Those clocks will be checked when the repo load. If not present or broken,
|
|
|
|
// Witnesser will be used to create them.
|
|
|
|
Clocks []string
|
|
|
|
// Witnesser is a function that will initialize the clocks of a repo
|
|
|
|
// from scratch
|
|
|
|
Witnesser func(repo ClockedRepo) error
|
|
|
|
}
|
2018-12-25 18:35:37 +03:00
|
|
|
|
2020-06-23 19:02:54 +03:00
|
|
|
// TestedRepo is an extended ClockedRepo with function for testing only
|
|
|
|
type TestedRepo interface {
|
|
|
|
ClockedRepo
|
2020-09-26 15:06:01 +03:00
|
|
|
repoTest
|
|
|
|
}
|
2020-06-23 19:02:54 +03:00
|
|
|
|
2020-09-26 15:06:01 +03:00
|
|
|
// repoTest give access to test only functions
|
|
|
|
type repoTest interface {
|
2020-06-23 19:02:54 +03:00
|
|
|
// AddRemote add a new remote to the repository
|
|
|
|
AddRemote(name string, url string) error
|
2020-12-01 23:19:23 +03:00
|
|
|
|
|
|
|
// GetLocalRemote return the URL to use to add this repo as a local remote
|
|
|
|
GetLocalRemote() string
|
|
|
|
|
|
|
|
// EraseFromDisk delete this repository entirely from the disk
|
|
|
|
EraseFromDisk() error
|
2020-06-23 19:02:54 +03:00
|
|
|
}
|