Skip to content

Commit c88b521

Browse files
authored
fix use of git path vs os path (#24)
* NS.Path->NS.OSPath; ns.ParseFromOSPath+ns.ParseFromGitPath; pkg file uses git paths to talk to billy filesystem * remove stale id package * all path arguments for git file system manipulation are now ns.NS * add ns.Append * update github.com/go-git/go-git and go.uber.org/zap dependencies * add ns.Dir and ns.Base; use NS paths in ListFilesRecursively * add TreeRemove and TreeReadDir
1 parent 1f0cca9 commit c88b521

File tree

16 files changed

+123
-360
lines changed

16 files changed

+123
-360
lines changed

file/file.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package file
22

33
import (
44
"context"
5-
"io/ioutil"
5+
"io"
66
"os"
77

88
"github.com/go-git/go-billy/v5"
@@ -19,17 +19,17 @@ func FileToString(ctx context.Context, fs billy.Filesystem, path ns.NS) string {
1919
}
2020

2121
func BytesToFile(ctx context.Context, fs billy.Filesystem, path ns.NS, content []byte) {
22-
file, err := fs.OpenFile(path.Path(), os.O_CREATE|os.O_RDWR, 0644)
22+
file, err := fs.OpenFile(path.GitPath(), os.O_CREATE|os.O_RDWR, 0644)
2323
must.NoError(ctx, err)
2424
defer file.Close()
2525
_, err = file.Write(content)
2626
must.NoError(ctx, err)
2727
}
2828

2929
func FileToBytes(ctx context.Context, fs billy.Filesystem, path ns.NS) []byte {
30-
file, err := fs.Open(path.Path())
30+
file, err := fs.Open(path.GitPath())
3131
must.NoError(ctx, err)
32-
content, err := ioutil.ReadAll(file)
32+
content, err := io.ReadAll(file)
3333
must.NoError(ctx, err)
3434
return content
3535
}

form/form.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77

88
"github.com/go-git/go-billy/v5"
99
"github.com/gov4git/lib4git/must"
10+
"github.com/gov4git/lib4git/ns"
1011
)
1112

1213
type Form interface{}
@@ -59,47 +60,47 @@ func DecodeBytesInto(ctx context.Context, data []byte, into Form) error {
5960
return json.Unmarshal(data, into)
6061
}
6162

62-
func EncodeToFile[F Form](ctx context.Context, fs billy.Filesystem, path string, form F) error {
63-
file, err := fs.Create(path)
63+
func EncodeToFile[F Form](ctx context.Context, fs billy.Filesystem, path ns.NS, form F) error {
64+
file, err := fs.Create(path.GitPath())
6465
if err != nil {
6566
return err
6667
}
6768
defer file.Close()
6869
return Encode(ctx, file, form)
6970
}
7071

71-
func DecodeFromFile[F Form](ctx context.Context, fs billy.Filesystem, path string) (form F, err error) {
72-
file, err := fs.Open(path)
72+
func DecodeFromFile[F Form](ctx context.Context, fs billy.Filesystem, path ns.NS) (form F, err error) {
73+
file, err := fs.Open(path.GitPath())
7374
if err != nil {
7475
return form, err
7576
}
7677
defer file.Close()
7778
return Decode[F](ctx, file)
7879
}
7980

80-
func DecodeFromFileInto(ctx context.Context, fs billy.Filesystem, path string, into Form) error {
81-
file, err := fs.Open(path)
81+
func DecodeFromFileInto(ctx context.Context, fs billy.Filesystem, path ns.NS, into Form) error {
82+
file, err := fs.Open(path.GitPath())
8283
if err != nil {
8384
return err
8485
}
8586
defer file.Close()
8687
return DecodeInto(ctx, file, into)
8788
}
8889

89-
func ToFile[F Form](ctx context.Context, fs billy.Filesystem, path string, form F) {
90+
func ToFile[F Form](ctx context.Context, fs billy.Filesystem, path ns.NS, form F) {
9091
if err := EncodeToFile(ctx, fs, path, form); err != nil {
9192
must.Panic(ctx, err)
9293
}
9394
}
9495

95-
func FromFile[F Form](ctx context.Context, fs billy.Filesystem, path string) F {
96+
func FromFile[F Form](ctx context.Context, fs billy.Filesystem, path ns.NS) F {
9697
f, err := DecodeFromFile[F](ctx, fs, path)
9798
if err != nil {
9899
must.Panic(ctx, err)
99100
}
100101
return f
101102
}
102103

103-
func FromFileInto(ctx context.Context, fs billy.Filesystem, path string, into Form) {
104+
func FromFileInto(ctx context.Context, fs billy.Filesystem, path ns.NS, into Form) {
104105
must.NoError(ctx, DecodeFromFileInto(ctx, fs, path, into))
105106
}

git/fs.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,26 @@
11
package git
22

3-
import "path/filepath"
3+
import (
4+
"github.com/gov4git/lib4git/ns"
5+
)
46

5-
func ListFilesRecursively(t *Tree, dir string) ([]string, error) {
7+
func ListFilesRecursively(t *Tree, dir ns.NS) ([]ns.NS, error) {
68
fs := t.Filesystem
7-
infos, err := fs.ReadDir(dir)
9+
infos, err := fs.ReadDir(dir.GitPath())
810
if err != nil {
911
return nil, err
1012
}
11-
list := []string{}
13+
list := []ns.NS{}
1214
for _, info := range infos {
15+
childPath := dir.Append(info.Name())
1316
if info.IsDir() {
14-
sublist, err := ListFilesRecursively(t, filepath.Join(dir, info.Name()))
17+
sublist, err := ListFilesRecursively(t, childPath)
1518
if err != nil {
1619
return nil, err
1720
}
1821
list = append(list, sublist...)
1922
} else {
20-
list = append(list, filepath.Join(dir, info.Name()))
23+
list = append(list, childPath)
2124
}
2225
}
2326
return list, nil

git/git.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,8 @@ func Worktree(ctx context.Context, repo *Repository) *Tree {
110110
return wt
111111
}
112112

113-
func Add(ctx context.Context, wt *Tree, path string) {
114-
if _, err := wt.Add(path); err != nil {
113+
func Add(ctx context.Context, wt *Tree, path ns.NS) {
114+
if _, err := wt.Add(path.GitPath()); err != nil {
115115
must.Panic(ctx, err)
116116
}
117117
}

git/tree.go

Lines changed: 38 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,46 @@ package git
22

33
import (
44
"context"
5-
"path/filepath"
5+
"io/fs"
66

7+
"github.com/go-git/go-git/v5/plumbing"
78
"github.com/go-git/go-git/v5/plumbing/format/index"
89
"github.com/gov4git/lib4git/file"
910
"github.com/gov4git/lib4git/form"
1011
"github.com/gov4git/lib4git/must"
1112
"github.com/gov4git/lib4git/ns"
1213
)
1314

14-
func TreeMkdirAll(ctx context.Context, t *Tree, path string) {
15-
err := t.Filesystem.MkdirAll(path, 0755)
15+
func TreeMkdirAll(ctx context.Context, t *Tree, path ns.NS) {
16+
if path.Len() == 0 {
17+
return
18+
}
19+
err := t.Filesystem.MkdirAll(path.GitPath(), 0755)
1620
must.NoError(ctx, err)
1721
}
1822

23+
func TreeStat(ctx context.Context, t *Tree, path ns.NS) (fs.FileInfo, error) {
24+
return t.Filesystem.Stat(path.GitPath())
25+
}
26+
27+
func TreeRemove(ctx context.Context, t *Tree, path ns.NS) (plumbing.Hash, error) {
28+
return t.Remove(path.GitPath())
29+
}
30+
31+
func TreeReadDir(ctx context.Context, t *Tree, path ns.NS) ([]fs.FileInfo, error) {
32+
return t.Filesystem.ReadDir(path.GitPath())
33+
}
34+
1935
//
2036

2137
func BytesToFile(ctx context.Context, t *Tree, path ns.NS, content []byte) {
22-
TreeMkdirAll(ctx, t, filepath.Dir(path.Path()))
38+
TreeMkdirAll(ctx, t, path.Dir())
2339
file.BytesToFile(ctx, t.Filesystem, path, content)
2440
}
2541

2642
func BytesToFileStage(ctx context.Context, t *Tree, path ns.NS, content []byte) {
2743
BytesToFile(ctx, t, path, content)
28-
Add(ctx, t, path.Path())
44+
Add(ctx, t, path)
2945
}
3046

3147
func FileToBytes(ctx context.Context, t *Tree, path ns.NS) []byte {
@@ -35,13 +51,13 @@ func FileToBytes(ctx context.Context, t *Tree, path ns.NS) []byte {
3551
//
3652

3753
func StringToFile(ctx context.Context, t *Tree, path ns.NS, content string) {
38-
TreeMkdirAll(ctx, t, filepath.Dir(path.Path()))
54+
TreeMkdirAll(ctx, t, path.Dir())
3955
file.StringToFile(ctx, t.Filesystem, path, content)
4056
}
4157

4258
func StringToFileStage(ctx context.Context, t *Tree, path ns.NS, content string) {
4359
StringToFile(ctx, t, path, content)
44-
Add(ctx, t, path.Path())
60+
Add(ctx, t, path)
4561
}
4662

4763
func FileToString(ctx context.Context, t *Tree, path ns.NS) string {
@@ -50,25 +66,25 @@ func FileToString(ctx context.Context, t *Tree, path ns.NS) string {
5066

5167
//
5268

53-
func ToFile[V form.Form](ctx context.Context, t *Tree, filePath string, value V) {
54-
TreeMkdirAll(ctx, t, filepath.Dir(filePath))
69+
func ToFile[V form.Form](ctx context.Context, t *Tree, filePath ns.NS, value V) {
70+
TreeMkdirAll(ctx, t, filePath.Dir())
5571
form.ToFile(ctx, t.Filesystem, filePath, value)
5672
}
5773

58-
func ToFileStage[V form.Form](ctx context.Context, t *Tree, filePath string, value V) {
74+
func ToFileStage[V form.Form](ctx context.Context, t *Tree, filePath ns.NS, value V) {
5975
ToFile(ctx, t, filePath, value)
6076
Add(ctx, t, filePath)
6177
}
6278

63-
func FromFile[V form.Form](ctx context.Context, t *Tree, filePath string) V {
79+
func FromFile[V form.Form](ctx context.Context, t *Tree, filePath ns.NS) V {
6480
return form.FromFile[V](ctx, t.Filesystem, filePath)
6581
}
6682

67-
func FromFileInto(ctx context.Context, t *Tree, filePath string, into form.Form) {
83+
func FromFileInto(ctx context.Context, t *Tree, filePath ns.NS, into form.Form) {
6884
form.FromFileInto(ctx, t.Filesystem, filePath, into)
6985
}
7086

71-
func TryFromFile[V form.Form](ctx context.Context, t *Tree, filePath string) (v V, err error) {
87+
func TryFromFile[V form.Form](ctx context.Context, t *Tree, filePath ns.NS) (v V, err error) {
7288
err = must.Try(
7389
func() {
7490
v = FromFile[V](ctx, t, filePath)
@@ -77,8 +93,8 @@ func TryFromFile[V form.Form](ctx context.Context, t *Tree, filePath string) (v
7793
return
7894
}
7995

80-
func RenameStage(ctx context.Context, t *Tree, oldPath, newPath string) {
81-
stat, err := t.Filesystem.Stat(oldPath)
96+
func RenameStage(ctx context.Context, t *Tree, oldPath, newPath ns.NS) {
97+
stat, err := t.Filesystem.Stat(oldPath.GitPath())
8298
must.NoError(ctx, err)
8399
if stat.IsDir() {
84100
renameStageDir(ctx, t, oldPath, newPath)
@@ -87,20 +103,20 @@ func RenameStage(ctx context.Context, t *Tree, oldPath, newPath string) {
87103
}
88104
}
89105

90-
func renameStageDir(ctx context.Context, t *Tree, oldPath, newPath string) {
91-
infos, err := t.Filesystem.ReadDir(oldPath)
106+
func renameStageDir(ctx context.Context, t *Tree, oldPath, newPath ns.NS) {
107+
infos, err := t.Filesystem.ReadDir(oldPath.GitPath())
92108
must.NoError(ctx, err)
93109
for _, info := range infos {
94-
RenameStage(ctx, t, filepath.Join(oldPath, info.Name()), filepath.Join(newPath, info.Name()))
110+
RenameStage(ctx, t, oldPath.Sub(info.Name()), newPath.Sub(info.Name()))
95111
}
96112
}
97113

98-
func renameStageFile(ctx context.Context, t *Tree, oldPath, newPath string) {
99-
must.NoError(ctx, t.Filesystem.Rename(oldPath, newPath))
100-
_, err := t.Remove(oldPath)
114+
func renameStageFile(ctx context.Context, t *Tree, oldPath, newPath ns.NS) {
115+
must.NoError(ctx, t.Filesystem.Rename(oldPath.GitPath(), newPath.GitPath()))
116+
_, err := t.Remove(oldPath.GitPath())
101117
if err != index.ErrEntryNotFound {
102118
must.NoError(ctx, err)
103119
}
104-
_, err = t.Add(newPath)
120+
_, err = t.Add(newPath.GitPath())
105121
must.NoError(ctx, err)
106122
}

go.mod

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,10 @@ go 1.21
44

55
require (
66
github.com/go-git/go-billy/v5 v5.5.0
7-
github.com/go-git/go-git/v5 v5.9.0
7+
github.com/go-git/go-git/v5 v5.10.0
88
github.com/gofrs/flock v0.8.1
9-
github.com/google/uuid v1.3.0
109
github.com/whilp/git-urls v1.0.0
11-
go.uber.org/zap v1.24.0
10+
go.uber.org/zap v1.26.0
1211
)
1312

1413
require (
@@ -27,12 +26,11 @@ require (
2726
github.com/sergi/go-diff v1.1.0 // indirect
2827
github.com/skeema/knownhosts v1.2.0 // indirect
2928
github.com/xanzy/ssh-agent v0.3.3 // indirect
30-
go.uber.org/atomic v1.7.0 // indirect
31-
go.uber.org/multierr v1.6.0 // indirect
32-
golang.org/x/crypto v0.13.0 // indirect
29+
go.uber.org/multierr v1.10.0 // indirect
30+
golang.org/x/crypto v0.14.0 // indirect
3331
golang.org/x/mod v0.12.0 // indirect
34-
golang.org/x/net v0.15.0 // indirect
35-
golang.org/x/sys v0.12.0 // indirect
32+
golang.org/x/net v0.17.0 // indirect
33+
golang.org/x/sys v0.13.0 // indirect
3634
golang.org/x/tools v0.13.0 // indirect
3735
gopkg.in/warnings.v0 v0.1.2 // indirect
3836
)

0 commit comments

Comments
 (0)