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