2018-07-12 10:55:13 +03:00
|
|
|
// Package repository contains helper methods for working with a Git repo.
|
|
|
|
package repository
|
|
|
|
|
2018-07-14 23:17:37 +03:00
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"strings"
|
2018-08-13 19:32:11 +03:00
|
|
|
|
2018-09-11 23:04:16 +03:00
|
|
|
"github.com/MichaelMure/git-bug/util/git"
|
|
|
|
"github.com/MichaelMure/git-bug/util/lamport"
|
2018-07-14 23:17:37 +03:00
|
|
|
)
|
2018-07-12 16:14:37 +03:00
|
|
|
|
2018-09-24 16:21:34 +03:00
|
|
|
// RepoCommon represent the common function the we want all the repo to implement
|
2018-09-21 19:18:51 +03:00
|
|
|
type RepoCommon interface {
|
2018-07-12 10:55:13 +03:00
|
|
|
// GetPath returns the path to the repo.
|
|
|
|
GetPath() string
|
|
|
|
|
2018-07-12 13:44:46 +03:00
|
|
|
// GetUserName returns the name the the user has used to configure git
|
|
|
|
GetUserName() (string, error)
|
|
|
|
|
2018-07-12 10:55:13 +03:00
|
|
|
// GetUserEmail returns the email address that the user has used to configure git.
|
|
|
|
GetUserEmail() (string, error)
|
|
|
|
|
|
|
|
// GetCoreEditor returns the name of the editor that the user has used to configure git.
|
|
|
|
GetCoreEditor() (string, error)
|
2018-09-24 16:21:34 +03:00
|
|
|
|
|
|
|
// StoreConfig store a single key/value pair in the config of the repo
|
|
|
|
StoreConfig(key string, value string) error
|
|
|
|
|
|
|
|
// ReadConfigs read all key/value pair matching the key prefix
|
|
|
|
ReadConfigs(keyPrefix string) (map[string]string, error)
|
2018-09-24 17:24:38 +03:00
|
|
|
|
|
|
|
// RmConfigs remove all key/value pair matching the key prefix
|
|
|
|
RmConfigs(keyPrefix string) error
|
2018-09-21 19:18:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Repo represents a source code repository.
|
|
|
|
type Repo interface {
|
|
|
|
RepoCommon
|
2018-07-12 10:55:13 +03:00
|
|
|
|
2018-07-17 02:52:56 +03:00
|
|
|
// FetchRefs fetch git refs from a remote
|
2018-08-12 22:09:30 +03:00
|
|
|
FetchRefs(remote string, refSpec string) (string, error)
|
2018-07-12 10:55:13 +03:00
|
|
|
|
|
|
|
// PushRefs push git refs to a remote
|
2018-08-12 22:09:30 +03:00
|
|
|
PushRefs(remote string, refSpec string) (string, error)
|
2018-07-12 16:14:37 +03:00
|
|
|
|
|
|
|
// StoreData will store arbitrary data and return the corresponding hash
|
2018-09-11 23:04:16 +03:00
|
|
|
StoreData(data []byte) (git.Hash, error)
|
2018-07-13 22:21:24 +03:00
|
|
|
|
2018-07-14 23:17:37 +03:00
|
|
|
// ReadData will attempt to read arbitrary data from the given hash
|
2018-09-11 23:04:16 +03:00
|
|
|
ReadData(hash git.Hash) ([]byte, error)
|
2018-07-14 23:17:37 +03:00
|
|
|
|
2018-07-13 22:21:24 +03:00
|
|
|
// StoreTree will store a mapping key-->Hash as a Git tree
|
2018-09-11 23:04:16 +03:00
|
|
|
StoreTree(mapping []TreeEntry) (git.Hash, error)
|
2018-07-13 22:21:24 +03:00
|
|
|
|
|
|
|
// StoreCommit will store a Git commit with the given Git tree
|
2018-09-11 23:04:16 +03:00
|
|
|
StoreCommit(treeHash git.Hash) (git.Hash, error)
|
2018-07-13 22:21:24 +03:00
|
|
|
|
|
|
|
// StoreCommit will store a Git commit with the given Git tree
|
2018-09-11 23:04:16 +03:00
|
|
|
StoreCommitWithParent(treeHash git.Hash, parent git.Hash) (git.Hash, error)
|
2018-07-13 22:21:24 +03:00
|
|
|
|
|
|
|
// UpdateRef will create or update a Git reference
|
2018-09-11 23:04:16 +03:00
|
|
|
UpdateRef(ref string, hash git.Hash) error
|
2018-07-14 23:17:37 +03:00
|
|
|
|
|
|
|
// ListRefs will return a list of Git ref matching the given refspec
|
|
|
|
ListRefs(refspec string) ([]string, error)
|
|
|
|
|
2018-07-17 02:52:56 +03:00
|
|
|
// RefExist will check if a reference exist in Git
|
|
|
|
RefExist(ref string) (bool, error)
|
|
|
|
|
|
|
|
// CopyRef will create a new reference with the same value as another one
|
|
|
|
CopyRef(source string, dest string) error
|
|
|
|
|
2018-07-14 23:17:37 +03:00
|
|
|
// ListCommits will return the list of tree hashes of a ref, in chronological order
|
2018-09-11 23:04:16 +03:00
|
|
|
ListCommits(ref string) ([]git.Hash, error)
|
2018-07-14 23:17:37 +03:00
|
|
|
|
|
|
|
// ListEntries will return the list of entries in a Git tree
|
2018-09-11 23:04:16 +03:00
|
|
|
ListEntries(hash git.Hash) ([]TreeEntry, error)
|
2018-07-17 02:52:56 +03:00
|
|
|
|
|
|
|
// FindCommonAncestor will return the last common ancestor of two chain of commit
|
2018-09-11 23:04:16 +03:00
|
|
|
FindCommonAncestor(hash1 git.Hash, hash2 git.Hash) (git.Hash, error)
|
2018-07-17 02:52:56 +03:00
|
|
|
|
2018-08-13 16:28:16 +03:00
|
|
|
// GetTreeHash return the git tree hash referenced in a commit
|
2018-09-11 23:04:16 +03:00
|
|
|
GetTreeHash(commit git.Hash) (git.Hash, error)
|
2018-09-21 19:18:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
type ClockedRepo interface {
|
|
|
|
Repo
|
2018-08-06 21:31:20 +03:00
|
|
|
|
2018-09-24 13:46:39 +03:00
|
|
|
// LoadClocks read the clocks values from the on-disk repo
|
2018-08-06 21:31:20 +03:00
|
|
|
LoadClocks() error
|
|
|
|
|
2018-09-24 13:46:39 +03:00
|
|
|
// WriteClocks write the clocks values into the repo
|
2018-08-06 21:31:20 +03:00
|
|
|
WriteClocks() error
|
|
|
|
|
2018-09-24 13:46:39 +03:00
|
|
|
// CreateTimeIncrement increment the creation clock and return the new value.
|
2018-09-11 23:04:16 +03:00
|
|
|
CreateTimeIncrement() (lamport.Time, error)
|
2018-08-06 21:31:20 +03:00
|
|
|
|
2018-09-24 13:46:39 +03:00
|
|
|
// EditTimeIncrement increment the edit clock and return the new value.
|
2018-09-11 23:04:16 +03:00
|
|
|
EditTimeIncrement() (lamport.Time, error)
|
2018-08-06 21:31:20 +03:00
|
|
|
|
2018-09-24 13:46:39 +03:00
|
|
|
// CreateWitness witness another create time and increment the corresponding
|
|
|
|
// clock if needed.
|
2018-09-11 23:04:16 +03:00
|
|
|
CreateWitness(time lamport.Time) error
|
2018-08-06 21:31:20 +03:00
|
|
|
|
2018-09-24 13:46:39 +03:00
|
|
|
// EditWitness witness another edition time and increment the corresponding
|
|
|
|
// clock if needed.
|
2018-09-11 23:04:16 +03:00
|
|
|
EditWitness(time lamport.Time) error
|
2018-07-14 23:17:37 +03:00
|
|
|
}
|
|
|
|
|
2018-12-25 18:35:37 +03:00
|
|
|
// Witnesser is a function that will initialize the clocks of a repo
|
|
|
|
// from scratch
|
|
|
|
type Witnesser func(repo ClockedRepo) error
|
|
|
|
|
2018-07-14 23:17:37 +03:00
|
|
|
func prepareTreeEntries(entries []TreeEntry) bytes.Buffer {
|
|
|
|
var buffer bytes.Buffer
|
|
|
|
|
|
|
|
for _, entry := range entries {
|
|
|
|
buffer.WriteString(entry.Format())
|
|
|
|
}
|
|
|
|
|
|
|
|
return buffer
|
|
|
|
}
|
|
|
|
|
|
|
|
func readTreeEntries(s string) ([]TreeEntry, error) {
|
2019-01-17 05:09:08 +03:00
|
|
|
split := strings.Split(strings.TrimSpace(s), "\n")
|
2018-07-14 23:17:37 +03:00
|
|
|
|
2018-09-09 21:17:12 +03:00
|
|
|
casted := make([]TreeEntry, len(split))
|
|
|
|
for i, line := range split {
|
2018-07-14 23:17:37 +03:00
|
|
|
if line == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
entry, err := ParseTreeEntry(line)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
casted[i] = entry
|
|
|
|
}
|
|
|
|
|
|
|
|
return casted, nil
|
2018-07-12 10:55:13 +03:00
|
|
|
}
|