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