-
Notifications
You must be signed in to change notification settings - Fork 371
Expand file tree
/
Copy pathgit.go
More file actions
285 lines (242 loc) · 7.42 KB
/
Copy pathgit.go
File metadata and controls
285 lines (242 loc) · 7.42 KB
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 integration
import (
"fmt"
"io"
"log"
"os"
"os/exec"
"path/filepath"
"strings"
"sync"
)
type gitRepository struct {
Path string
}
var testGitRepositoryTemplate = sync.OnceValues(func() (string, error) {
repo, err := createSeedTestGitRepository()
if err != nil {
return "", err
}
return repo.Path, nil
})
func createTestGitRespository() (*gitRepository, error) {
templatePath, err := testGitRepositoryTemplate()
if err != nil {
return nil, err
}
repoPath, err := newGitRepositoryPath()
if err != nil {
return nil, err
}
if err := copyDirectory(templatePath, repoPath); err != nil {
_ = os.RemoveAll(repoPath)
return nil, fmt.Errorf("copying git repo template: %w", err)
}
return &gitRepository{Path: repoPath}, nil
}
func createSeedTestGitRepository() (*gitRepository, error) {
repo, err := newGitRepository()
if err != nil {
return nil, err
}
if err = repo.CreateBranch("main"); err != nil {
return nil, fmt.Errorf("creating main branch: %w", err)
}
if err := os.WriteFile(
filepath.Join(repo.Path, "test.txt"),
[]byte("This is a test"),
0o600,
); err != nil {
return nil, fmt.Errorf("writing test.txt: %w", err)
}
if err = repo.Add("test.txt"); err != nil {
return nil, fmt.Errorf("adding test.txt: %w", err)
}
if err = repo.Commit("Initial Commit"); err != nil {
return nil, fmt.Errorf("initial commit: %w", err)
}
if err = repo.CreateBranch("update-test-txt"); err != nil {
return nil, fmt.Errorf("creating branch: %w", err)
}
if err := os.WriteFile(
filepath.Join(repo.Path, "test.txt"),
[]byte("This is a test pull request"),
0o600,
); err != nil {
return nil, fmt.Errorf("writing to test.txt: %w", err)
}
if err = repo.Add("test.txt"); err != nil {
return nil, fmt.Errorf("adding test.txt again: %w", err)
}
if err = repo.Commit("PR Commit"); err != nil {
return nil, fmt.Errorf("commit PR Commit: %w", err)
}
if _, err = repo.Execute("update-ref", "refs/pull/123/head", "HEAD"); err != nil {
return nil, fmt.Errorf("updating ref: %w", err)
}
// Add the merge ref - this simulates what GitHub creates for PR merges
if _, err := repo.Execute("merge-base", "main", "HEAD"); err != nil {
return nil, fmt.Errorf("finding merge base: %w", err)
}
// Create a temporary merge commit for testing merge refspecs
if err := repo.CheckoutBranch("main"); err != nil {
return nil, fmt.Errorf("checkout main for merge: %w", err)
}
if _, err := repo.Execute("merge", "--no-ff", "-m", "Merge pull request #123", "update-test-txt"); err != nil {
return nil, fmt.Errorf("creating merge commit: %w", err)
}
// Create the merge refspec that points to this merge commit
if _, err := repo.Execute("update-ref", "refs/pull/123/merge", "HEAD"); err != nil {
return nil, fmt.Errorf("updating merge ref: %w", err)
}
// Reset main back to its original state so unrelated tests aren't affected
if _, err := repo.Execute("reset", "--hard", "HEAD~1"); err != nil {
return nil, fmt.Errorf("resetting main: %w", err)
}
if err = repo.CheckoutBranch("main"); err != nil {
return nil, fmt.Errorf("checkout main: %w", err)
}
return repo, nil
}
func newGitRepository() (*gitRepository, error) {
tempDir, err := newGitRepositoryPath()
if err != nil {
return nil, err
}
gr := &gitRepository{Path: tempDir}
gitErr := gr.ExecuteAll([][]string{
{"init"},
{"config", "user.email", "you@example.com"},
{"config", "user.name", "Your Name"},
})
return gr, gitErr
}
func newGitRepositoryPath() (string, error) {
tempDirRaw, err := os.MkdirTemp("", "git-repo")
if err != nil {
return "", fmt.Errorf("Error creating temp dir: %w", err)
}
// io.TempDir on Windows tilde-shortens (8.3 style?) long filenames in the path.
// This becomes a problem when that path is used for `git add`;
// git believes the tilde-style path is outside the repo, but accepts the long-filename path.
//
// C:\Users\Administrator\AppData\Local\Temp\2\git-repo162512502>git add C:\Users\ADMINI~1\AppData\Local\Temp\2\git-repo162512502\.buildkite\hooks\pre-exit.bat
// fatal: C:\Users\ADMINI~1\AppData\Local\Temp\2\git-repo162512502\.buildkite\hooks\pre-exit.bat: 'C:\Users\ADMINI~1\AppData\Local\Temp\2\git-repo162512502\.buildkite\hooks\pre-exit.bat' is outside repository
//
// C:\Users\Administrator\AppData\Local\Temp\2\git-repo162512502>git add C:\Users\Administrator\AppData\Local\Temp\2\git-repo162512502\.buildkite\hooks\pre-exit.bat
// (ok)
//
// Some attempts to resolve the TempDir to a full file path:
// filepath.Abs: C:\Users\ADMINI~1\AppData\Local\Temp\2\git-repo275254366
// filepath.Clean: C:\Users\ADMINI~1\AppData\Local\Temp\2\git-repo275254366
// filepath.EvalSymlinks: C:\Users\Administrator\AppData\Local\Temp\2\git-repo275254366
//
// EvalSymlinks seems best? Maybe there's a better way?
tempDir, err := filepath.EvalSymlinks(tempDirRaw)
if err != nil {
return "", fmt.Errorf("EvalSymlinks for temp dir: %w", err)
}
return tempDir, nil
}
func copyDirectory(src, dst string) error {
return filepath.WalkDir(src, func(path string, d os.DirEntry, walkErr error) error {
if walkErr != nil {
return walkErr
}
relPath, err := filepath.Rel(src, path)
if err != nil {
return err
}
if relPath == "." {
return nil
}
targetPath := filepath.Join(dst, relPath)
info, err := os.Lstat(path)
if err != nil {
return err
}
switch mode := info.Mode(); {
case mode.IsDir():
return os.MkdirAll(targetPath, mode.Perm())
case mode.IsRegular():
return copyFile(path, targetPath, mode)
case mode&os.ModeSymlink != 0:
linkTarget, err := os.Readlink(path)
if err != nil {
return err
}
return os.Symlink(linkTarget, targetPath)
default:
return fmt.Errorf("unsupported file mode %s for %s", mode, path)
}
})
}
func copyFile(src, dst string, mode os.FileMode) error {
reader, err := os.Open(src)
if err != nil {
return err
}
defer reader.Close()
writer, err := os.OpenFile(dst, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, mode.Perm())
if err != nil {
return err
}
if _, err := io.Copy(writer, reader); err != nil {
writer.Close()
return err
}
if err := writer.Close(); err != nil {
return err
}
return os.Chmod(dst, mode.Perm())
}
func (gr *gitRepository) Add(path string) error {
if _, err := gr.Execute("add", path); err != nil {
return err
}
return nil
}
func (gr *gitRepository) Commit(message string, params ...any) error {
if _, err := gr.Execute("commit", "-m", fmt.Sprintf(message, params...)); err != nil {
return err
}
return nil
}
func (gr *gitRepository) CheckoutBranch(branch string) error {
if _, err := gr.Execute("checkout", branch); err != nil {
return err
}
return nil
}
func (gr *gitRepository) CreateBranch(branch string) error {
if _, err := gr.Execute("checkout", "-b", branch); err != nil {
return err
}
return nil
}
func (gr *gitRepository) Close() error {
return os.RemoveAll(gr.Path)
}
func (gr *gitRepository) Execute(args ...string) (string, error) {
path, err := exec.LookPath("git")
if err != nil {
return "", err
}
cmd := exec.Command(path, args...)
cmd.Dir = gr.Path
log.Printf("$ git %s", strings.Join(args, " "))
out, err := cmd.CombinedOutput()
log.Printf("Result: %v %s", err, out)
return string(out), err
}
func (gr *gitRepository) ExecuteAll(argsSlice [][]string) error {
for _, args := range argsSlice {
if _, err := gr.Execute(args...); err != nil {
return err
}
}
return nil
}
func (gr *gitRepository) RevParse(rev string) (string, error) {
return gr.Execute("rev-parse", rev)
}