repo.go

  1// Package repository contains helper methods for working with a Git repo.
  2package repository
  3
  4import (
  5	"bytes"
  6	"strings"
  7
  8	"github.com/MichaelMure/git-bug/util"
  9)
 10
 11// Repo represents a source code repository.
 12type Repo interface {
 13	// GetPath returns the path to the repo.
 14	GetPath() string
 15
 16	// GetUserName returns the name the the user has used to configure git
 17	GetUserName() (string, error)
 18
 19	// GetUserEmail returns the email address that the user has used to configure git.
 20	GetUserEmail() (string, error)
 21
 22	// GetCoreEditor returns the name of the editor that the user has used to configure git.
 23	GetCoreEditor() (string, error)
 24
 25	// FetchRefs fetch git refs from a remote
 26	FetchRefs(remote string, refSpec string) (string, error)
 27
 28	// PushRefs push git refs to a remote
 29	PushRefs(remote string, refSpec string) (string, error)
 30
 31	// StoreData will store arbitrary data and return the corresponding hash
 32	StoreData(data []byte) (util.Hash, error)
 33
 34	// ReadData will attempt to read arbitrary data from the given hash
 35	ReadData(hash util.Hash) ([]byte, error)
 36
 37	// StoreTree will store a mapping key-->Hash as a Git tree
 38	StoreTree(mapping []TreeEntry) (util.Hash, error)
 39
 40	// StoreCommit will store a Git commit with the given Git tree
 41	StoreCommit(treeHash util.Hash) (util.Hash, error)
 42
 43	// StoreCommit will store a Git commit with the given Git tree
 44	StoreCommitWithParent(treeHash util.Hash, parent util.Hash) (util.Hash, error)
 45
 46	// UpdateRef will create or update a Git reference
 47	UpdateRef(ref string, hash util.Hash) error
 48
 49	// ListRefs will return a list of Git ref matching the given refspec
 50	ListRefs(refspec string) ([]string, error)
 51
 52	// ListIds will return a list of Git ref matching the given refspec,
 53	// stripped to only the last part of the ref
 54	ListIds(refspec string) ([]string, error)
 55
 56	// RefExist will check if a reference exist in Git
 57	RefExist(ref string) (bool, error)
 58
 59	// CopyRef will create a new reference with the same value as another one
 60	CopyRef(source string, dest string) error
 61
 62	// ListCommits will return the list of tree hashes of a ref, in chronological order
 63	ListCommits(ref string) ([]util.Hash, error)
 64
 65	// ListEntries will return the list of entries in a Git tree
 66	ListEntries(hash util.Hash) ([]TreeEntry, error)
 67
 68	// FindCommonAncestor will return the last common ancestor of two chain of commit
 69	FindCommonAncestor(hash1 util.Hash, hash2 util.Hash) (util.Hash, error)
 70
 71	// GetTreeHash return the git tree hash referenced in a commit
 72	GetTreeHash(commit util.Hash) (util.Hash, error)
 73
 74	LoadClocks() error
 75
 76	WriteClocks() error
 77
 78	CreateTimeIncrement() (util.LamportTime, error)
 79
 80	EditTimeIncrement() (util.LamportTime, error)
 81
 82	CreateWitness(time util.LamportTime) error
 83
 84	EditWitness(time util.LamportTime) error
 85}
 86
 87func prepareTreeEntries(entries []TreeEntry) bytes.Buffer {
 88	var buffer bytes.Buffer
 89
 90	for _, entry := range entries {
 91		buffer.WriteString(entry.Format())
 92	}
 93
 94	return buffer
 95}
 96
 97func readTreeEntries(s string) ([]TreeEntry, error) {
 98	splitted := strings.Split(s, "\n")
 99
100	casted := make([]TreeEntry, len(splitted))
101	for i, line := range splitted {
102		if line == "" {
103			continue
104		}
105
106		entry, err := ParseTreeEntry(line)
107
108		if err != nil {
109			return nil, err
110		}
111
112		casted[i] = entry
113	}
114
115	return casted, nil
116}