1// Package repository contains helper methods for working with a Git repo.
2package repository
3
4import (
5 "errors"
6 "io"
7 "time"
8
9 "github.com/ProtonMail/go-crypto/openpgp"
10 "github.com/go-git/go-billy/v5"
11
12 "github.com/git-bug/git-bug/util/lamport"
13)
14
15var (
16 // ErrNotARepo is the error returned when the git repo root can't be found
17 ErrNotARepo = errors.New("not a git repository")
18 // ErrClockNotExist is the error returned when a clock can't be found
19 ErrClockNotExist = errors.New("clock doesn't exist")
20 // ErrNotFound is the error returned when a git object can't be found
21 ErrNotFound = errors.New("ref not found")
22)
23
24// Repo represents a source code repository.
25type Repo interface {
26 RepoConfig
27 RepoKeyring
28 RepoCommon
29 RepoStorage
30 RepoIndex
31 RepoData
32 RepoBrowse
33
34 Close() error
35}
36
37type RepoCommonStorage interface {
38 RepoCommon
39 RepoStorage
40}
41
42// ClockedRepo is a Repo that also has Lamport clocks
43type ClockedRepo interface {
44 Repo
45 RepoClock
46}
47
48// RepoConfig access the configuration of a repository
49type RepoConfig interface {
50 // LocalConfig give access to the repository scoped configuration
51 LocalConfig() Config
52
53 // GlobalConfig give access to the global scoped configuration
54 GlobalConfig() Config
55
56 // AnyConfig give access to a merged local/global configuration
57 AnyConfig() ConfigRead
58}
59
60// RepoKeyring give access to a user-wide storage for secrets
61type RepoKeyring interface {
62 // Keyring give access to a user-wide storage for secrets
63 Keyring() Keyring
64}
65
66// RepoCommon represent the common function we want all repos to implement
67type RepoCommon interface {
68 // GetUserName returns the name the user has used to configure git
69 GetUserName() (string, error)
70
71 // GetUserEmail returns the email address that the user has used to configure git.
72 GetUserEmail() (string, error)
73
74 // GetCoreEditor returns the name of the editor that the user has used to configure git.
75 GetCoreEditor() (string, error)
76
77 // GetRemotes returns the configured remotes repositories.
78 GetRemotes() (map[string]string, error)
79}
80
81type LocalStorage interface {
82 billy.Filesystem
83 RemoveAll(path string) error
84}
85
86// RepoStorage give access to the filesystem
87type RepoStorage interface {
88 // LocalStorage return a billy.Filesystem giving access to $RepoPath/.git/git-bug
89 LocalStorage() LocalStorage
90}
91
92// RepoIndex gives access to full-text search indexes
93type RepoIndex interface {
94 GetIndex(name string) (Index, error)
95}
96
97// Index is a full-text search index
98type Index interface {
99 // IndexOne indexes one document, for the given ID. If the document already exist,
100 // it replaces it.
101 IndexOne(id string, texts []string) error
102
103 // IndexBatch start a batch indexing. The returned indexer function is used the same
104 // way as IndexOne, and the closer function complete the batch insertion.
105 IndexBatch() (indexer func(id string, texts []string) error, closer func() error)
106
107 // Search returns the list of IDs matching the given terms.
108 Search(terms []string) (ids []string, err error)
109
110 // DocCount returns the number of document in the index.
111 DocCount() (uint64, error)
112
113 // Remove delete one document in the index.
114 Remove(id string) error
115
116 // Clear empty the index.
117 Clear() error
118
119 // Close closes the index and make sure everything is safely written. After this call
120 // the index can't be used anymore.
121 Close() error
122}
123
124type Commit struct {
125 Hash Hash
126 Parents []Hash // hashes of the parents, if any
127 TreeHash Hash // hash of the git Tree
128 SignedData io.Reader // if signed, reader for the signed data (likely, the serialized commit)
129 Signature io.Reader // if signed, reader for the (non-armored) signature
130}
131
132// RepoData give access to the git data storage
133type RepoData interface {
134 // FetchRefs fetch git refs matching a directory prefix to a remote
135 // Ex: prefix="foo" will fetch any remote refs matching "refs/foo/*" locally.
136 // The equivalent git refspec would be "refs/foo/*:refs/remotes/<remote>/foo/*"
137 FetchRefs(remote string, prefixes ...string) (string, error)
138
139 // PushRefs push git refs matching a directory prefix to a remote
140 // Ex: prefix="foo" will push any local refs matching "refs/foo/*" to the remote.
141 // The equivalent git refspec would be "refs/foo/*:refs/foo/*"
142 //
143 // Additionally, PushRefs will update the local references in refs/remotes/<remote>/foo to match
144 // the remote state.
145 PushRefs(remote string, prefixes ...string) (string, error)
146
147 // StoreData will store arbitrary data and return the corresponding hash
148 StoreData(data []byte) (Hash, error)
149
150 // ReadData will attempt to read arbitrary data from the given hash
151 // Returns ErrNotFound if not found.
152 ReadData(hash Hash) ([]byte, error)
153
154 // StoreTree will store a mapping key-->Hash as a Git tree
155 StoreTree(mapping []TreeEntry) (Hash, error)
156
157 // ReadTree will return the list of entries in a Git tree
158 // The given hash could be from either a commit or a tree
159 // Returns ErrNotFound if not found.
160 ReadTree(hash Hash) ([]TreeEntry, error)
161
162 // StoreCommit will store a Git commit with the given Git tree
163 StoreCommit(treeHash Hash, parents ...Hash) (Hash, error)
164
165 // StoreSignedCommit will store a Git commit with the given Git tree. If signKey is not nil, the commit
166 // will be signed accordingly.
167 StoreSignedCommit(treeHash Hash, signKey *openpgp.Entity, parents ...Hash) (Hash, error)
168
169 // ReadCommit read a Git commit and returns some of its characteristic
170 // Returns ErrNotFound if not found.
171 ReadCommit(hash Hash) (Commit, error)
172
173 // ResolveRef returns the hash of the target commit of the given ref
174 // Returns ErrNotFound if not found.
175 ResolveRef(ref string) (Hash, error)
176
177 // UpdateRef will create or update a Git reference
178 UpdateRef(ref string, hash Hash) error
179
180 // RemoveRef will remove a Git reference
181 // RemoveRef is idempotent.
182 RemoveRef(ref string) error
183
184 // ListRefs will return a list of Git ref matching the given refspec
185 ListRefs(refPrefix string) ([]string, error)
186
187 // RefExist will check if a reference exists in Git
188 RefExist(ref string) (bool, error)
189
190 // CopyRef will create a new reference with the same value as another one
191 // Returns ErrNotFound if not found.
192 CopyRef(source string, dest string) error
193
194 // ListCommits will return the list of tree hashes of a ref, in chronological order
195 ListCommits(ref string) ([]Hash, error)
196}
197
198// RepoClock give access to Lamport clocks
199type RepoClock interface {
200 // AllClocks return all the known clocks
201 AllClocks() (map[string]lamport.Clock, error)
202
203 // GetOrCreateClock return a Lamport clock stored in the Repo.
204 // If the clock doesn't exist, it's created.
205 GetOrCreateClock(name string) (lamport.Clock, error)
206
207 // Increment is equivalent to c = GetOrCreateClock(name) + c.Increment()
208 Increment(name string) (lamport.Time, error)
209
210 // Witness is equivalent to c = GetOrCreateClock(name) + c.Witness(time)
211 Witness(name string, time lamport.Time) error
212}
213
214// RepoBrowse is implemented by all Repo implementations and provides
215// code-browsing endpoints (file tree, history, diffs).
216//
217// All methods accepting a ref parameter resolve it in order:
218// refs/heads/<ref>, refs/tags/<ref>, full ref name, raw commit hash.
219type RepoBrowse interface {
220 // Branches returns all local branches (refs/heads/*).
221 // IsDefault marks the branch HEAD points to.
222 // All other ref namespaces — including git-bug's internal refs
223 // (refs/bugs/, refs/identities/, …) — are excluded.
224 Branches() ([]BranchInfo, error)
225
226 // Tags returns all tags (refs/tags/*).
227 // All other ref namespaces are excluded.
228 Tags() ([]TagInfo, error)
229
230 // TreeAtPath returns the entries of the directory at path under ref.
231 // An empty path returns the root tree.
232 // Returns ErrNotFound if ref or path does not exist, or if path
233 // resolves to a blob rather than a tree.
234 // Symlinks appear as entries with ObjectType Symlink; they are not followed.
235 TreeAtPath(ref, path string) ([]TreeEntry, error)
236
237 // BlobAtPath returns the raw content, byte size, and git object hash of
238 // the file at path under ref. Returns ErrNotFound if ref or path does
239 // not exist, or if path resolves to a tree. Symlinks are not followed.
240 // The caller must close the reader.
241 BlobAtPath(ref, path string) (io.ReadCloser, int64, Hash, error)
242
243 // CommitLog returns at most limit commits reachable from ref, filtered
244 // to those touching path (empty = unrestricted). after is an exclusive
245 // cursor; pass Hash("") for no cursor. since and until bound the author
246 // date (inclusive); pass nil for no bound. Merge commits appear once,
247 // compared against the first parent only.
248 CommitLog(ref, path string, limit int, after Hash, since, until *time.Time) ([]CommitMeta, error)
249
250 // LastCommitForEntries returns the most recent commit that touched each
251 // name in the directory at path under ref. Entries not resolved within
252 // the implementation's depth limit are silently absent from the result.
253 LastCommitForEntries(ref, path string, names []string) (map[string]CommitMeta, error)
254
255 // CommitDetail returns the full metadata and changed-file list for a
256 // single commit identified by its hash. Diffs against the first parent
257 // only; the initial commit is diffed against the empty tree.
258 CommitDetail(hash Hash) (CommitDetail, error)
259
260 // CommitFileDiff returns the unified diff for a single file in a commit
261 // identified by its hash. Diffs against the first parent only; the
262 // initial commit is diffed against the empty tree.
263 CommitFileDiff(hash Hash, filePath string) (FileDiff, error)
264}
265
266// ClockLoader hold which logical clock need to exist for an entity and
267// how to create them if they don't.
268type ClockLoader struct {
269 // Clocks hold the name of all the clocks this loader deals with.
270 // Those clocks will be checked when the repo loads. If not present or broken,
271 // Witnesser will be used to create them.
272 Clocks []string
273 // Witnesser is a function that will initialize the clocks of a repo
274 // from scratch
275 Witnesser func(repo ClockedRepo) error
276}
277
278// TestedRepo is an extended ClockedRepo with functions for testing only
279type TestedRepo interface {
280 ClockedRepo
281 repoTest
282}
283
284// repoTest give access to test-only functions
285type repoTest interface {
286 // AddRemote add a new remote to the repository
287 AddRemote(name string, url string) error
288
289 // GetLocalRemote return the URL to use to add this repo as a local remote
290 GetLocalRemote() string
291
292 // EraseFromDisk delete this repository entirely from the disk
293 EraseFromDisk() error
294}