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