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