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) error
26
27 // PushRefs push git refs to a remote
28 PushRefs(remote string, refSpec 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 // Return the git tree hash referenced in a commit
71 GetTreeHash(commit util.Hash) (util.Hash, error)
72}
73
74func prepareTreeEntries(entries []TreeEntry) bytes.Buffer {
75 var buffer bytes.Buffer
76
77 for _, entry := range entries {
78 buffer.WriteString(entry.Format())
79 }
80
81 return buffer
82}
83
84func readTreeEntries(s string) ([]TreeEntry, error) {
85 splitted := strings.Split(s, "\n")
86
87 casted := make([]TreeEntry, len(splitted))
88 for i, line := range splitted {
89 if line == "" {
90 continue
91 }
92
93 entry, err := ParseTreeEntry(line)
94
95 if err != nil {
96 return nil, err
97 }
98
99 casted[i] = entry
100 }
101
102 return casted, nil
103}