-
-
Notifications
You must be signed in to change notification settings - Fork 104
/
git_repository.go
285 lines (256 loc) · 10.2 KB
/
git_repository.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
package test
import (
"fmt"
"io/ioutil"
"os"
"path"
"sort"
"strings"
"github.com/Originate/git-town/src/git"
"github.com/Originate/git-town/src/util"
"github.com/pkg/errors"
)
// GitRepository is a Git repository that exists inside a Git environment.
type GitRepository struct {
// Dir contains the path of the directory that this repository is in.
Dir string
// originalCommits contains the commits in this repository before the test ran.
originalCommits []Commit
// ShellRunner enables to run console commands in this repo.
ShellRunner
// configCache contains the Git Town configuration to use.
// This value is lazy loaded. Please use Configuration() to access it.
configCache *git.Configuration
}
// NewGitRepository provides a new GitRepository instance working in the given directory.
// The directory must contain an existing Git repo.
func NewGitRepository(workingDir string, homeDir string) GitRepository {
result := GitRepository{Dir: workingDir}
result.ShellRunner = NewShellRunner(workingDir, homeDir)
return result
}
// InitGitRepository initializes a fully functioning Git repository in the given path,
// including necessary Git configuration.
// Creates missing folders as needed.
func InitGitRepository(workingDir string, homeDir string) (GitRepository, error) {
// create the folder
err := os.MkdirAll(workingDir, 0744)
if err != nil {
return GitRepository{}, errors.Wrapf(err, "cannot create directory %q", workingDir)
}
// initialize the repo in the folder
result := NewGitRepository(workingDir, homeDir)
output, err := result.Run("git", "init")
if err != nil {
return result, errors.Wrapf(err, `error running "git init" in %q: %s`, workingDir, output)
}
err = result.RunMany([][]string{
{"git", "config", "--global", "user.name", "user"},
{"git", "config", "--global", "user.email", "email@example.com"},
{"git", "config", "--global", "core.editor", "vim"},
})
return result, err
}
// CloneGitRepository clones the given parent repo into a new GitRepository.
func CloneGitRepository(originDir, workingDir, homeDir string) (GitRepository, error) {
runner := NewShellRunner(".", homeDir)
_, err := runner.Run("git", "clone", originDir, workingDir)
if err != nil {
return GitRepository{}, errors.Wrapf(err, "cannot clone repo %q", originDir)
}
return NewGitRepository(workingDir, homeDir), nil
}
// Branches provides the names of the local branches in this Git repository,
// sorted alphabetically.
func (repo *GitRepository) Branches() (result []string, err error) {
output, err := repo.Run("git", "branch")
if err != nil {
return result, errors.Wrapf(err, "cannot run 'git branch' in repo %q", repo.workingDir)
}
for _, line := range strings.Split(strings.TrimSpace(output), "\n") {
line = strings.Replace(line, "* ", "", 1)
result = append(result, strings.TrimSpace(line))
}
return sort.StringSlice(result), nil
}
// CheckoutBranch checks out the Git branch with the given name in this repo.
func (repo *GitRepository) CheckoutBranch(name string) error {
output, err := repo.Run("git", "checkout", name)
if err != nil {
return errors.Wrapf(err, "cannot check out branch %q in repo %q: %s", name, repo.workingDir, output)
}
return nil
}
// Commits provides a tabular list of the commits in this Git repository with the given fields.
func (repo *GitRepository) Commits(fields []string) (result []Commit, err error) {
branches, err := repo.Branches()
if err != nil {
return result, errors.Wrap(err, "cannot determine the Git branches")
}
for _, branch := range branches {
commits, err := repo.commitsInBranch(branch, fields)
if err != nil {
return result, err
}
result = append(result, commits...)
}
return result, nil
}
// CommitsInBranch provides all commits in the given Git branch.
func (repo *GitRepository) commitsInBranch(branch string, fields []string) (result []Commit, err error) {
output, err := repo.Run("git", "log", branch, "--format=%h|%s|%an <%ae>", "--topo-order", "--reverse")
if err != nil {
return result, errors.Wrapf(err, "cannot get commits in branch %q", branch)
}
for _, line := range strings.Split(strings.TrimSpace(output), "\n") {
parts := strings.Split(line, "|")
commit := Commit{Branch: branch, SHA: parts[0], Message: parts[1], Author: parts[2]}
if strings.EqualFold(commit.Message, "initial commit") {
continue
}
if util.DoesStringArrayContain(fields, "FILE NAME") {
filenames, err := repo.FilesInCommit(commit.SHA)
if err != nil {
return result, errors.Wrapf(err, "cannot determine file name for commit %q in branch %q", commit.SHA, branch)
}
commit.FileName = strings.Join(filenames, ", ")
}
if util.DoesStringArrayContain(fields, "FILE CONTENT") {
filecontent, err := repo.FileContentInCommit(commit.SHA, commit.FileName)
if err != nil {
return result, errors.Wrapf(err, "cannot determine file content for commit %q in branch %q", commit.SHA, branch)
}
commit.FileContent = filecontent
}
result = append(result, commit)
}
return result, nil
}
// Configuration lazy-loads the Git-Town configuration for this repo.
func (repo *GitRepository) Configuration() *git.Configuration {
if repo.configCache == nil {
repo.configCache = git.NewConfiguration(repo.Dir)
}
return repo.configCache
}
// CreateBranch creates a new branch with the given name.
// The created branch is a normal branch.
// To create feature branches, use CreateFeatureBranch.
func (repo *GitRepository) CreateBranch(name string) error {
output, err := repo.Run("git", "checkout", "-b", name)
if err != nil {
return errors.Wrapf(err, "cannot create branch %q: %s", name, output)
}
return nil
}
// CreateCommit creates a commit with the given properties in this Git repo.
func (repo *GitRepository) CreateCommit(commit Commit) error {
repo.originalCommits = append(repo.originalCommits, commit)
err := repo.CheckoutBranch(commit.Branch)
if err != nil {
return errors.Wrapf(err, "cannot checkout branch %q", commit.Branch)
}
err = repo.CreateFile(commit.FileName, commit.FileContent)
if err != nil {
return errors.Wrapf(err, "cannot create file %q needed for commit", commit.FileName)
}
output, err := repo.Run("git", "add", commit.FileName)
if err != nil {
return errors.Wrapf(err, "cannot add file to commit: %s", output)
}
output, err = repo.Run("git", "commit", "-m", commit.Message)
if err != nil {
return errors.Wrapf(err, "cannot commit: %s", output)
}
return nil
}
// CreateFeatureBranch creates a branch with the given name in this repository.
func (repo *GitRepository) CreateFeatureBranch(name string) error {
output, err := repo.Run("git", "town", "hack", name)
if err != nil {
return errors.Wrapf(err, "cannot create branch %q in repo: %s", name, output)
}
return nil
}
// CreateFile creates a file with the given name and content in this repository.
func (repo *GitRepository) CreateFile(name, content string) error {
err := ioutil.WriteFile(path.Join(repo.Dir, name), []byte(content), 0744)
if err != nil {
return errors.Wrapf(err, "cannot create file %q", name)
}
return nil
}
// CreatePerennialBranches creates perennial branches with the given names in this repository.
func (repo *GitRepository) CreatePerennialBranches(names ...string) error {
for _, name := range names {
err := repo.CreateBranch(name)
if err != nil {
return errors.Wrapf(err, "cannot create perennial branch %q in repo %q", name, repo.Dir)
}
}
repo.Configuration().AddToPerennialBranches(names...)
return nil
}
// CurrentBranch provides the currently checked out branch for this repo.
func (repo *GitRepository) CurrentBranch() (result string, err error) {
output, err := repo.Run("git", "rev-parse", "--abbrev-ref", "HEAD")
if err != nil {
return result, errors.Wrapf(err, "cannot determine the current branch: %s", output)
}
return strings.TrimSpace(output), nil
}
// FileContentInCommit provides the content of the file with the given name in the commit with the given SHA.
func (repo *GitRepository) FileContentInCommit(sha string, filename string) (result string, err error) {
output, err := repo.Run("git", "show", sha+":"+filename)
if err != nil {
return result, errors.Wrapf(err, "cannot determine the content for file %q in commit %q", filename, sha)
}
return output, nil
}
// FilesInCommit provides the names of the files that the commit with the given SHA changes.
func (repo *GitRepository) FilesInCommit(sha string) (result []string, err error) {
output, err := repo.Run("git", "diff-tree", "--no-commit-id", "--name-only", "-r", sha)
if err != nil {
return result, errors.Wrapf(err, "cannot get files for commit %q", sha)
}
return strings.Split(strings.TrimSpace(output), "\n"), nil
}
// HasFile indicates whether this repository contains a file with the given name and content.
func (repo *GitRepository) HasFile(name, content string) (result bool, err error) {
rawContent, err := ioutil.ReadFile(path.Join(repo.Dir, name))
if err != nil {
return result, errors.Wrapf(err, "repo doesn't have file %q", name)
}
actualContent := string(rawContent)
if actualContent != content {
return result, fmt.Errorf("file %q should have content %q but has %q", name, content, actualContent)
}
return true, nil
}
// PushBranch pushes the branch with the given name to the remote.
func (repo *GitRepository) PushBranch(name string) error {
output, err := repo.Run("git", "push", "-u", "origin", name)
if err != nil {
return errors.Wrapf(err, "cannot push branch %q in repo %q to origin: %s", name, repo.Dir, output)
}
return nil
}
// RegisterOriginalCommit tracks the given commit as existing in this repo before the system under test executed.
func (repo *GitRepository) RegisterOriginalCommit(commit Commit) {
repo.originalCommits = append(repo.originalCommits, commit)
}
// SetOffline enables or disables offline mode for this GitRepository.
func (repo *GitRepository) SetOffline(enabled bool) error {
output, err := repo.Run("git", "config", "--global", "git-town.offline", "true")
if err != nil {
return errors.Wrapf(err, "cannot set offline mode in repo %q: %s", repo.Dir, output)
}
return nil
}
// SetRemote sets the remote of this Git repository to the given target.
func (repo *GitRepository) SetRemote(target string) error {
return repo.RunMany([][]string{
{"git", "remote", "remove", "origin"},
{"git", "remote", "add", "origin", target},
})
}