@@ -2,30 +2,46 @@ package git
22
33import (
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
2137func 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
2642func 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
3147func 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
3753func 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
4258func 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
4763func 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}
0 commit comments