repo.go

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