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 can'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 RepoData
26 RepoStorage
27 RepoBleve
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
91// RepoData give access to the git data storage
92type RepoData interface {
93 // FetchRefs fetch git refs from a remote
94 FetchRefs(remote string, refSpec string) (string, error)
95
96 // PushRefs push git refs to a remote
97 PushRefs(remote string, refSpec string) (string, error)
98
99 // StoreData will store arbitrary data and return the corresponding hash
100 StoreData(data []byte) (Hash, error)
101
102 // ReadData will attempt to read arbitrary data from the given hash
103 ReadData(hash Hash) ([]byte, error)
104
105 // StoreTree will store a mapping key-->Hash as a Git tree
106 StoreTree(mapping []TreeEntry) (Hash, error)
107
108 // ReadTree will return the list of entries in a Git tree
109 // The given hash could be from either a commit or a tree
110 ReadTree(hash Hash) ([]TreeEntry, error)
111
112 // StoreCommit will store a Git commit with the given Git tree
113 StoreCommit(treeHash Hash) (Hash, error)
114
115 // StoreCommit will store a Git commit with the given Git tree
116 StoreCommitWithParent(treeHash Hash, parent Hash) (Hash, error)
117
118 // GetTreeHash return the git tree hash referenced in a commit
119 GetTreeHash(commit Hash) (Hash, error)
120
121 // FindCommonAncestor will return the last common ancestor of two chain of commit
122 FindCommonAncestor(commit1 Hash, commit2 Hash) (Hash, error)
123
124 // UpdateRef will create or update a Git reference
125 UpdateRef(ref string, hash Hash) error
126
127 // RemoveRef will remove a Git reference
128 RemoveRef(ref string) error
129
130 // ListRefs will return a list of Git ref matching the given refspec
131 ListRefs(refPrefix string) ([]string, error)
132
133 // RefExist will check if a reference exist in Git
134 RefExist(ref string) (bool, error)
135
136 // CopyRef will create a new reference with the same value as another one
137 CopyRef(source string, dest string) error
138
139 // ListCommits will return the list of tree hashes of a ref, in chronological order
140 ListCommits(ref string) ([]Hash, error)
141}
142
143// RepoClock give access to Lamport clocks
144type RepoClock interface {
145 // GetOrCreateClock return a Lamport clock stored in the Repo.
146 // If the clock doesn't exist, it's created.
147 GetOrCreateClock(name string) (lamport.Clock, error)
148}
149
150// ClockLoader hold which logical clock need to exist for an entity and
151// how to create them if they don't.
152type ClockLoader struct {
153 // Clocks hold the name of all the clocks this loader deal with.
154 // Those clocks will be checked when the repo load. If not present or broken,
155 // Witnesser will be used to create them.
156 Clocks []string
157 // Witnesser is a function that will initialize the clocks of a repo
158 // from scratch
159 Witnesser func(repo ClockedRepo) error
160}
161
162// TestedRepo is an extended ClockedRepo with function for testing only
163type TestedRepo interface {
164 ClockedRepo
165 repoTest
166}
167
168// repoTest give access to test only functions
169type repoTest interface {
170 // AddRemote add a new remote to the repository
171 AddRemote(name string, url string) error
172
173 // GetLocalRemote return the URL to use to add this repo as a local remote
174 GetLocalRemote() string
175
176 // EraseFromDisk delete this repository entirely from the disk
177 EraseFromDisk() error
178}