-
Notifications
You must be signed in to change notification settings - Fork 8
/
git.go
154 lines (121 loc) · 3.58 KB
/
git.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package helpers
import (
"io/ioutil"
"path/filepath"
"testing"
"time"
"github.com/stretchr/testify/assert"
"gopkg.in/src-d/go-git.v4"
"gopkg.in/src-d/go-git.v4/plumbing"
"gopkg.in/src-d/go-git.v4/plumbing/object"
"github.com/reviewboard/rb-gateway/repositories"
)
// Create a Git Repository for testing.
//
// The caller is responsible for cleaning up the filesystem afterwards.
//
// Example:
//
// ```go
// func Test(t *testing.T) {
// repo, rawRepo := testing.CreateGitRepo(t)
// defer testing.CleanupRepository(t, repo.Path)
//
// // ...
// }
// ```
func CreateGitRepo(t *testing.T, name string) (*repositories.GitRepository, *git.Repository) {
t.Helper()
assert := assert.New(t)
path, err := ioutil.TempDir("", "rb-gateway-test-")
assert.Nil(err, "Could not create temporary directory.")
path, err = filepath.EvalSymlinks(path)
assert.Nil(err, "Could not get absolute path.")
rawRepo, err := git.PlainInit(path, false)
assert.Nil(err, "Could not initialize repository.")
repo := &repositories.GitRepository{
RepositoryInfo: repositories.RepositoryInfo{
Name: name,
Path: path,
},
}
return repo, rawRepo
}
// Add files to a repository and commit them, returning the commit ID.
//
// Callers can compare committed file contents with the result of `helpers.GetRepoFiles`.
func SeedGitRepo(t *testing.T, repo *repositories.GitRepository, rawRepo *git.Repository) plumbing.Hash {
t.Helper()
assert := assert.New(t)
worktree, err := rawRepo.Worktree()
assert.Nil(err)
createAndAddFilesGit(t, repo.Path, worktree, repoFiles)
commitId, err := worktree.Commit("Initial commit", &git.CommitOptions{
Author: &object.Signature{
Name: "Author",
Email: "author@example.com",
When: time.Now(),
},
})
assert.Nil(err)
return commitId
}
// Create a new branch with some test files, returning the commit ID.
//
// Callers can compare committed file contents with the result of `helpers.GetRepoFiles`.
func CreateGitBranch(t *testing.T, repo *repositories.GitRepository, rawRepo *git.Repository) *plumbing.Reference {
t.Helper()
assert := assert.New(t)
worktree, err := rawRepo.Worktree()
assert.Nil(err)
err = worktree.Checkout(&git.CheckoutOptions{
Branch: "refs/heads/test-branch",
Create: true,
})
assert.Nil(err)
createAndAddFilesGit(t, repo.Path, worktree, branchFiles)
_, err = worktree.Commit("Add branch", &git.CommitOptions{
Author: &object.Signature{
Name: "Author",
Email: "author@example.com",
When: time.Now(),
},
})
assert.Nil(err)
branch, err := rawRepo.Reference("refs/heads/test-branch", false)
assert.Nil(err)
return branch
}
// Return the object ID of the given file.
func GetRepositoryFileId(t *testing.T, rawRepo *git.Repository, path string) plumbing.Hash {
t.Helper()
assert := assert.New(t)
head, err := rawRepo.Head()
assert.Nil(err)
headCommit, err := rawRepo.CommitObject(head.Hash())
assert.Nil(err)
tree, err := headCommit.Tree()
assert.Nil(err)
entry, err := tree.FindEntry(path)
assert.Nil(err)
return entry.Hash
}
// Get the object ID of the repository head.
func GetRepoHead(t *testing.T, rawRepo *git.Repository) plumbing.Hash {
t.Helper()
head, err := rawRepo.Head()
assert.Nil(t, err)
return head.Hash()
}
// Create some files and add them to to an index.
func createAndAddFilesGit(t *testing.T, path string, worktree *git.Worktree, files map[string][]byte) {
t.Helper()
assert := assert.New(t)
for filename, content := range files {
path := filepath.Join(path, filename)
err := ioutil.WriteFile(path, content, 0644)
assert.Nil(err)
_, err = worktree.Add(filename)
assert.Nil(err)
}
}