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	// The given hash could be from either a commit or a tree
 86	ReadTree(hash Hash) ([]TreeEntry, error)
 87
 88	// StoreCommit will store a Git commit with the given Git tree
 89	StoreCommit(treeHash Hash) (Hash, error)
 90
 91	// StoreCommit will store a Git commit with the given Git tree
 92	StoreCommitWithParent(treeHash Hash, parent Hash) (Hash, error)
 93
 94	// GetTreeHash return the git tree hash referenced in a commit
 95	GetTreeHash(commit Hash) (Hash, error)
 96
 97	// FindCommonAncestor will return the last common ancestor of two chain of commit
 98	FindCommonAncestor(commit1 Hash, commit2 Hash) (Hash, error)
 99
100	// UpdateRef will create or update a Git reference
101	UpdateRef(ref string, hash Hash) error
102
103	// RemoveRef will remove a Git reference
104	RemoveRef(ref string) error
105
106	// ListRefs will return a list of Git ref matching the given refspec
107	ListRefs(refPrefix string) ([]string, error)
108
109	// RefExist will check if a reference exist in Git
110	RefExist(ref string) (bool, error)
111
112	// CopyRef will create a new reference with the same value as another one
113	CopyRef(source string, dest string) error
114
115	// ListCommits will return the list of tree hashes of a ref, in chronological order
116	ListCommits(ref string) ([]Hash, error)
117}
118
119// RepoClock give access to Lamport clocks
120type RepoClock interface {
121	// GetOrCreateClock return a Lamport clock stored in the Repo.
122	// If the clock doesn't exist, it's created.
123	GetOrCreateClock(name string) (lamport.Clock, error)
124}
125
126// ClockLoader hold which logical clock need to exist for an entity and
127// how to create them if they don't.
128type ClockLoader struct {
129	// Clocks hold the name of all the clocks this loader deal with.
130	// Those clocks will be checked when the repo load. If not present or broken,
131	// Witnesser will be used to create them.
132	Clocks []string
133	// Witnesser is a function that will initialize the clocks of a repo
134	// from scratch
135	Witnesser func(repo ClockedRepo) error
136}
137
138// TestedRepo is an extended ClockedRepo with function for testing only
139type TestedRepo interface {
140	ClockedRepo
141	repoTest
142}
143
144// repoTest give access to test only functions
145type repoTest interface {
146	// AddRemote add a new remote to the repository
147	AddRemote(name string, url string) error
148}