repo.go

  1// Package repository contains helper methods for working with a Git repo.
  2package repository
  3
  4import (
  5	"errors"
  6	"io"
  7
  8	"github.com/ProtonMail/go-crypto/openpgp"
  9	"github.com/go-git/go-billy/v5"
 10
 11	"github.com/MichaelMure/git-bug/util/lamport"
 12)
 13
 14var (
 15	// ErrNotARepo is the error returned when the git repo root can't be found
 16	ErrNotARepo = errors.New("not a git repository")
 17	// ErrClockNotExist is the error returned when a clock can't be found
 18	ErrClockNotExist = errors.New("clock doesn't exist")
 19)
 20
 21// Repo represents a source code repository.
 22type Repo interface {
 23	RepoConfig
 24	RepoKeyring
 25	RepoCommon
 26	RepoStorage
 27	RepoIndex
 28	RepoData
 29
 30	Close() error
 31}
 32
 33type RepoCommonStorage interface {
 34	RepoCommon
 35	RepoStorage
 36}
 37
 38// ClockedRepo is a Repo that also has Lamport clocks
 39type ClockedRepo interface {
 40	Repo
 41	RepoClock
 42}
 43
 44// RepoConfig access the configuration of a repository
 45type RepoConfig interface {
 46	// LocalConfig give access to the repository scoped configuration
 47	LocalConfig() Config
 48
 49	// GlobalConfig give access to the global scoped configuration
 50	GlobalConfig() Config
 51
 52	// AnyConfig give access to a merged local/global configuration
 53	AnyConfig() ConfigRead
 54}
 55
 56// RepoKeyring give access to a user-wide storage for secrets
 57type RepoKeyring interface {
 58	// Keyring give access to a user-wide storage for secrets
 59	Keyring() Keyring
 60}
 61
 62// RepoCommon represent the common function we want all repos to implement
 63type RepoCommon interface {
 64	// GetUserName returns the name the user has used to configure git
 65	GetUserName() (string, error)
 66
 67	// GetUserEmail returns the email address that the user has used to configure git.
 68	GetUserEmail() (string, error)
 69
 70	// GetCoreEditor returns the name of the editor that the user has used to configure git.
 71	GetCoreEditor() (string, error)
 72
 73	// GetRemotes returns the configured remotes repositories.
 74	GetRemotes() (map[string]string, error)
 75}
 76
 77// RepoStorage give access to the filesystem
 78type RepoStorage interface {
 79	// LocalStorage return a billy.Filesystem giving access to $RepoPath/.git/git-bug
 80	LocalStorage() billy.Filesystem
 81}
 82
 83// RepoIndex gives access to full-text search indexes
 84type RepoIndex interface {
 85	GetIndex(name string) (Index, error)
 86}
 87
 88// Index is a full-text search index
 89type Index interface {
 90	// IndexOne indexes one document, for the given ID. If the document already exist,
 91	// it replaces it.
 92	IndexOne(id string, texts []string) error
 93
 94	// IndexBatch start a batch indexing. The returned indexer function is used the same
 95	// way as IndexOne, and the closer function complete the batch insertion.
 96	IndexBatch() (indexer func(id string, texts []string) error, closer func() error)
 97
 98	// Search returns the list of IDs matching the given terms.
 99	Search(terms []string) (ids []string, err error)
100
101	// DocCount returns the number of document in the index.
102	DocCount() (uint64, error)
103
104	// Clear empty the index.
105	Clear() error
106
107	// Close closes the index and make sure everything is safely written. After this call
108	// the index can't be used anymore.
109	Close() error
110}
111
112type Commit struct {
113	Hash       Hash
114	Parents    []Hash    // hashes of the parents, if any
115	TreeHash   Hash      // hash of the git Tree
116	SignedData io.Reader // if signed, reader for the signed data (likely, the serialized commit)
117	Signature  io.Reader // if signed, reader for the (non-armored) signature
118}
119
120// RepoData give access to the git data storage
121type RepoData interface {
122	// FetchRefs fetch git refs matching a directory prefix to a remote
123	// Ex: prefix="foo" will fetch any remote refs matching "refs/foo/*" locally.
124	// The equivalent git refspec would be "refs/foo/*:refs/remotes/<remote>/foo/*"
125	FetchRefs(remote string, prefix string) (string, error)
126
127	// PushRefs push git refs matching a directory prefix to a remote
128	// Ex: prefix="foo" will push any local refs matching "refs/foo/*" to the remote.
129	// The equivalent git refspec would be "refs/foo/*:refs/foo/*"
130	//
131	// Additionally, PushRefs will update the local references in refs/remotes/<remote>/foo to match
132	// the remote state.
133	PushRefs(remote string, prefix string) (string, error)
134
135	// StoreData will store arbitrary data and return the corresponding hash
136	StoreData(data []byte) (Hash, error)
137
138	// ReadData will attempt to read arbitrary data from the given hash
139	ReadData(hash Hash) ([]byte, error)
140
141	// StoreTree will store a mapping key-->Hash as a Git tree
142	StoreTree(mapping []TreeEntry) (Hash, error)
143
144	// ReadTree will return the list of entries in a Git tree
145	// The given hash could be from either a commit or a tree
146	ReadTree(hash Hash) ([]TreeEntry, error)
147
148	// StoreCommit will store a Git commit with the given Git tree
149	StoreCommit(treeHash Hash, parents ...Hash) (Hash, error)
150
151	// StoreSignedCommit will store a Git commit with the given Git tree. If signKey is not nil, the commit
152	// will be signed accordingly.
153	StoreSignedCommit(treeHash Hash, signKey *openpgp.Entity, parents ...Hash) (Hash, error)
154
155	// ReadCommit read a Git commit and returns some of its characteristic
156	ReadCommit(hash Hash) (Commit, error)
157
158	// GetTreeHash return the git tree hash referenced in a commit
159	// Deprecated
160	GetTreeHash(commit Hash) (Hash, error)
161
162	// ResolveRef returns the hash of the target commit of the given ref
163	ResolveRef(ref string) (Hash, error)
164
165	// UpdateRef will create or update a Git reference
166	UpdateRef(ref string, hash Hash) error
167
168	// RemoveRef will remove a Git reference
169	// RemoveRef is idempotent.
170	RemoveRef(ref string) error
171
172	// ListRefs will return a list of Git ref matching the given refspec
173	ListRefs(refPrefix string) ([]string, error)
174
175	// RefExist will check if a reference exist in Git
176	RefExist(ref string) (bool, error)
177
178	// CopyRef will create a new reference with the same value as another one
179	CopyRef(source string, dest string) error
180
181	// FindCommonAncestor will return the last common ancestor of two chain of commit
182	// Deprecated
183	FindCommonAncestor(commit1 Hash, commit2 Hash) (Hash, error)
184
185	// ListCommits will return the list of tree hashes of a ref, in chronological order
186	ListCommits(ref string) ([]Hash, error)
187}
188
189// RepoClock give access to Lamport clocks
190type RepoClock interface {
191	// AllClocks return all the known clocks
192	AllClocks() (map[string]lamport.Clock, error)
193
194	// GetOrCreateClock return a Lamport clock stored in the Repo.
195	// If the clock doesn't exist, it's created.
196	GetOrCreateClock(name string) (lamport.Clock, error)
197
198	// Increment is equivalent to c = GetOrCreateClock(name) + c.Increment()
199	Increment(name string) (lamport.Time, error)
200
201	// Witness is equivalent to c = GetOrCreateClock(name) + c.Witness(time)
202	Witness(name string, time lamport.Time) error
203}
204
205// ClockLoader hold which logical clock need to exist for an entity and
206// how to create them if they don't.
207type ClockLoader struct {
208	// Clocks hold the name of all the clocks this loader deal with.
209	// Those clocks will be checked when the repo load. If not present or broken,
210	// Witnesser will be used to create them.
211	Clocks []string
212	// Witnesser is a function that will initialize the clocks of a repo
213	// from scratch
214	Witnesser func(repo ClockedRepo) error
215}
216
217// TestedRepo is an extended ClockedRepo with function for testing only
218type TestedRepo interface {
219	ClockedRepo
220	repoTest
221}
222
223// repoTest give access to test only functions
224type repoTest interface {
225	// AddRemote add a new remote to the repository
226	AddRemote(name string, url string) error
227
228	// GetLocalRemote return the URL to use to add this repo as a local remote
229	GetLocalRemote() string
230
231	// EraseFromDisk delete this repository entirely from the disk
232	EraseFromDisk() error
233}