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