repo.go

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