1// Package repository contains helper methods for working with a Git repo.
2package repository
3
4import (
5 "errors"
6 "io"
7
8 "github.com/blevesearch/bleve"
9 "github.com/go-git/go-billy/v5"
10 "golang.org/x/crypto/openpgp"
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 from a remote
104 FetchRefs(remote string, refSpec string) (string, error)
105
106 // PushRefs push git refs to a remote
107 PushRefs(remote string, refSpec string) (string, error)
108
109 // StoreData will store arbitrary data and return the corresponding hash
110 StoreData(data []byte) (Hash, error)
111
112 // ReadData will attempt to read arbitrary data from the given hash
113 ReadData(hash Hash) ([]byte, error)
114
115 // StoreTree will store a mapping key-->Hash as a Git tree
116 StoreTree(mapping []TreeEntry) (Hash, error)
117
118 // ReadTree will return the list of entries in a Git tree
119 // The given hash could be from either a commit or a tree
120 ReadTree(hash Hash) ([]TreeEntry, error)
121
122 // StoreCommit will store a Git commit with the given Git tree
123 StoreCommit(treeHash Hash, parents ...Hash) (Hash, error)
124
125 // StoreCommit will store a Git commit with the given Git tree. If signKey is not nil, the commit
126 // will be signed accordingly.
127 StoreSignedCommit(treeHash Hash, signKey *openpgp.Entity, parents ...Hash) (Hash, error)
128
129 // ReadCommit read a Git commit and returns some of its characteristic
130 ReadCommit(hash Hash) (Commit, error)
131
132 // GetTreeHash return the git tree hash referenced in a commit
133 // Deprecated
134 GetTreeHash(commit Hash) (Hash, error)
135
136 // ResolveRef returns the hash of the target commit of the given ref
137 ResolveRef(ref string) (Hash, error)
138
139 // UpdateRef will create or update a Git reference
140 UpdateRef(ref string, hash Hash) error
141
142 // RemoveRef will remove a Git reference
143 RemoveRef(ref string) error
144
145 // ListRefs will return a list of Git ref matching the given refspec
146 ListRefs(refPrefix string) ([]string, error)
147
148 // RefExist will check if a reference exist in Git
149 RefExist(ref string) (bool, error)
150
151 // CopyRef will create a new reference with the same value as another one
152 CopyRef(source string, dest string) error
153
154 // FindCommonAncestor will return the last common ancestor of two chain of commit
155 // Deprecated
156 FindCommonAncestor(commit1 Hash, commit2 Hash) (Hash, error)
157
158 // ListCommits will return the list of tree hashes of a ref, in chronological order
159 ListCommits(ref string) ([]Hash, error)
160}
161
162// RepoClock give access to Lamport clocks
163type RepoClock interface {
164 // AllClocks return all the known clocks
165 AllClocks() (map[string]lamport.Clock, error)
166
167 // GetOrCreateClock return a Lamport clock stored in the Repo.
168 // If the clock doesn't exist, it's created.
169 GetOrCreateClock(name string) (lamport.Clock, error)
170
171 // Increment is equivalent to c = GetOrCreateClock(name) + c.Increment()
172 Increment(name string) (lamport.Time, error)
173
174 // Witness is equivalent to c = GetOrCreateClock(name) + c.Witness(time)
175 Witness(name string, time lamport.Time) error
176}
177
178// ClockLoader hold which logical clock need to exist for an entity and
179// how to create them if they don't.
180type ClockLoader struct {
181 // Clocks hold the name of all the clocks this loader deal with.
182 // Those clocks will be checked when the repo load. If not present or broken,
183 // Witnesser will be used to create them.
184 Clocks []string
185 // Witnesser is a function that will initialize the clocks of a repo
186 // from scratch
187 Witnesser func(repo ClockedRepo) error
188}
189
190// TestedRepo is an extended ClockedRepo with function for testing only
191type TestedRepo interface {
192 ClockedRepo
193 repoTest
194}
195
196// repoTest give access to test only functions
197type repoTest interface {
198 // AddRemote add a new remote to the repository
199 AddRemote(name string, url string) error
200
201 // GetLocalRemote return the URL to use to add this repo as a local remote
202 GetLocalRemote() string
203
204 // EraseFromDisk delete this repository entirely from the disk
205 EraseFromDisk() error
206}