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