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