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