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	// RefExist will check if a reference exist in Git
 53	RefExist(ref string) (bool, error)
 54
 55	// CopyRef will create a new reference with the same value as another one
 56	CopyRef(source string, dest string) error
 57
 58	// ListCommits will return the list of tree hashes of a ref, in chronological order
 59	ListCommits(ref string) ([]util.Hash, error)
 60
 61	// ListEntries will return the list of entries in a Git tree
 62	ListEntries(hash util.Hash) ([]TreeEntry, error)
 63
 64	// FindCommonAncestor will return the last common ancestor of two chain of commit
 65	FindCommonAncestor(hash1 util.Hash, hash2 util.Hash) (util.Hash, error)
 66
 67	// GetTreeHash return the git tree hash referenced in a commit
 68	GetTreeHash(commit util.Hash) (util.Hash, error)
 69
 70	LoadClocks() error
 71
 72	WriteClocks() error
 73
 74	CreateTimeIncrement() (util.LamportTime, error)
 75
 76	EditTimeIncrement() (util.LamportTime, error)
 77
 78	CreateWitness(time util.LamportTime) error
 79
 80	EditWitness(time util.LamportTime) error
 81}
 82
 83func prepareTreeEntries(entries []TreeEntry) bytes.Buffer {
 84	var buffer bytes.Buffer
 85
 86	for _, entry := range entries {
 87		buffer.WriteString(entry.Format())
 88	}
 89
 90	return buffer
 91}
 92
 93func readTreeEntries(s string) ([]TreeEntry, error) {
 94	splitted := strings.Split(s, "\n")
 95
 96	casted := make([]TreeEntry, len(splitted))
 97	for i, line := range splitted {
 98		if line == "" {
 99			continue
100		}
101
102		entry, err := ParseTreeEntry(line)
103
104		if err != nil {
105			return nil, err
106		}
107
108		casted[i] = entry
109	}
110
111	return casted, nil
112}