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	// PullRefs pull git refs from a remote
25	PullRefs(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	// ListCommits will return the list of tree hashes of a ref, in chronological order
52	ListCommits(ref string) ([]util.Hash, error)
53
54	// ListEntries will return the list of entries in a Git tree
55	ListEntries(hash util.Hash) ([]TreeEntry, error)
56}
57
58func prepareTreeEntries(entries []TreeEntry) bytes.Buffer {
59	var buffer bytes.Buffer
60
61	for _, entry := range entries {
62		buffer.WriteString(entry.Format())
63	}
64
65	return buffer
66}
67
68func readTreeEntries(s string) ([]TreeEntry, error) {
69	splitted := strings.Split(s, "\n")
70
71	casted := make([]TreeEntry, len(splitted))
72	for i, line := range splitted {
73		if line == "" {
74			continue
75		}
76
77		entry, err := ParseTreeEntry(line)
78
79		if err != nil {
80			return nil, err
81		}
82
83		casted[i] = entry
84	}
85
86	return casted, nil
87}