-
Notifications
You must be signed in to change notification settings - Fork 0
/
git.go
52 lines (43 loc) · 1.1 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
package testgit
import (
"context"
"fmt"
"path/filepath"
"strings"
"github.com/getsyncer/syncer-core/files"
"github.com/getsyncer/syncer-core/git"
)
type TestGit struct {
Location string
TrackedFiles []string
}
func NewFromFilesystem(location string, paths []files.Path) *TestGit {
trackedFiles := make([]string, len(paths))
for i, path := range paths {
trackedFiles[i] = string(path)
}
return New(location, trackedFiles)
}
func New(location string, trackedFiles []string) *TestGit {
return &TestGit{
Location: location,
TrackedFiles: trackedFiles,
}
}
func (t *TestGit) FindGitRoot(_ context.Context, loc string) (string, error) {
loc = filepath.Clean(loc)
if strings.Contains(loc, t.Location) {
return t.Location, nil
}
return "", fmt.Errorf("not found")
}
func (t *TestGit) ListTrackedFiles(ctx context.Context, loc string) ([]string, error) {
_, err := t.FindGitRoot(ctx, loc)
if err != nil {
return nil, fmt.Errorf("failed to find git root: %w", err)
}
ret := make([]string, len(t.TrackedFiles))
copy(ret, t.TrackedFiles)
return ret, nil
}
var _ git.Git = (*TestGit)(nil)