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