1// Package repository contains helper methods for working with a Git repo.
  2package repository
  3
  4import (
  5	"bytes"
  6	"errors"
  7	"strings"
  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// RepoConfig access the configuration of a repository
 20type RepoConfig interface {
 21	// LocalConfig give access to the repository scoped configuration
 22	LocalConfig() Config
 23
 24	// GlobalConfig give access to the git global configuration
 25	// Deprecated: to remove in favor of Keyring()
 26	// TODO: remove
 27	GlobalConfig() Config
 28}
 29
 30// RepoKeyring give access to a user-wide storage for secrets
 31type RepoKeyring interface {
 32	// Keyring give access to a user-wide storage for secrets
 33	Keyring() Keyring
 34}
 35
 36// RepoCommon represent the common function the we want all the repo to implement
 37type RepoCommon interface {
 38	// GetPath returns the path to the repo.
 39	GetPath() string
 40
 41	// GetUserName returns the name the the user has used to configure git
 42	GetUserName() (string, error)
 43
 44	// GetUserEmail returns the email address that the user has used to configure git.
 45	GetUserEmail() (string, error)
 46
 47	// GetCoreEditor returns the name of the editor that the user has used to configure git.
 48	GetCoreEditor() (string, error)
 49
 50	// GetRemotes returns the configured remotes repositories.
 51	GetRemotes() (map[string]string, error)
 52}
 53
 54// Repo represents a source code repository.
 55type Repo interface {
 56	RepoConfig
 57	RepoKeyring
 58	RepoCommon
 59
 60	// FetchRefs fetch git refs from a remote
 61	FetchRefs(remote string, refSpec string) (string, error)
 62
 63	// PushRefs push git refs to a remote
 64	PushRefs(remote string, refSpec string) (string, error)
 65
 66	// StoreData will store arbitrary data and return the corresponding hash
 67	StoreData(data []byte) (Hash, error)
 68
 69	// ReadData will attempt to read arbitrary data from the given hash
 70	ReadData(hash Hash) ([]byte, error)
 71
 72	// StoreTree will store a mapping key-->Hash as a Git tree
 73	StoreTree(mapping []TreeEntry) (Hash, error)
 74
 75	// ReadTree will return the list of entries in a Git tree
 76	ReadTree(hash Hash) ([]TreeEntry, error)
 77
 78	// StoreCommit will store a Git commit with the given Git tree
 79	StoreCommit(treeHash Hash) (Hash, error)
 80
 81	// StoreCommit will store a Git commit with the given Git tree
 82	StoreCommitWithParent(treeHash Hash, parent Hash) (Hash, error)
 83
 84	// GetTreeHash return the git tree hash referenced in a commit
 85	GetTreeHash(commit Hash) (Hash, error)
 86
 87	// FindCommonAncestor will return the last common ancestor of two chain of commit
 88	FindCommonAncestor(commit1 Hash, commit2 Hash) (Hash, error)
 89
 90	// UpdateRef will create or update a Git reference
 91	UpdateRef(ref string, hash Hash) error
 92
 93	// RemoveRef will remove a Git reference
 94	RemoveRef(ref string) error
 95
 96	// ListRefs will return a list of Git ref matching the given refspec
 97	ListRefs(refspec string) ([]string, error)
 98
 99	// RefExist will check if a reference exist in Git
100	RefExist(ref string) (bool, error)
101
102	// CopyRef will create a new reference with the same value as another one
103	CopyRef(source string, dest string) error
104
105	// ListCommits will return the list of tree hashes of a ref, in chronological order
106	ListCommits(ref string) ([]Hash, error)
107}
108
109// ClockedRepo is a Repo that also has Lamport clocks
110type ClockedRepo interface {
111	Repo
112
113	// GetOrCreateClock return a Lamport clock stored in the Repo.
114	// If the clock doesn't exist, it's created.
115	GetOrCreateClock(name string) (lamport.Clock, error)
116}
117
118// ClockLoader hold which logical clock need to exist for an entity and
119// how to create them if they don't.
120type ClockLoader struct {
121	// Clocks hold the name of all the clocks this loader deal with.
122	// Those clocks will be checked when the repo load. If not present or broken,
123	// Witnesser will be used to create them.
124	Clocks []string
125	// Witnesser is a function that will initialize the clocks of a repo
126	// from scratch
127	Witnesser func(repo ClockedRepo) error
128}
129
130func prepareTreeEntries(entries []TreeEntry) bytes.Buffer {
131	var buffer bytes.Buffer
132
133	for _, entry := range entries {
134		buffer.WriteString(entry.Format())
135	}
136
137	return buffer
138}
139
140func readTreeEntries(s string) ([]TreeEntry, error) {
141	split := strings.Split(strings.TrimSpace(s), "\n")
142
143	casted := make([]TreeEntry, len(split))
144	for i, line := range split {
145		if line == "" {
146			continue
147		}
148
149		entry, err := ParseTreeEntry(line)
150
151		if err != nil {
152			return nil, err
153		}
154
155		casted[i] = entry
156	}
157
158	return casted, nil
159}
160
161// TestedRepo is an extended ClockedRepo with function for testing only
162type TestedRepo interface {
163	ClockedRepo
164
165	// AddRemote add a new remote to the repository
166	AddRemote(name string, url string) error
167}