Skip to content

Commit

Permalink
fix use of git path vs os path (#24)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
petar authored Nov 1, 2023
1 parent 1f0cca9 commit c88b521
Show file tree
Hide file tree
Showing 16 changed files with 123 additions and 360 deletions.
8 changes: 4 additions & 4 deletions file/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package file

import (
"context"
"io/ioutil"
"io"
"os"

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

func BytesToFile(ctx context.Context, fs billy.Filesystem, path ns.NS, content []byte) {
file, err := fs.OpenFile(path.Path(), os.O_CREATE|os.O_RDWR, 0644)
file, err := fs.OpenFile(path.GitPath(), os.O_CREATE|os.O_RDWR, 0644)
must.NoError(ctx, err)
defer file.Close()
_, err = file.Write(content)
must.NoError(ctx, err)
}

func FileToBytes(ctx context.Context, fs billy.Filesystem, path ns.NS) []byte {
file, err := fs.Open(path.Path())
file, err := fs.Open(path.GitPath())
must.NoError(ctx, err)
content, err := ioutil.ReadAll(file)
content, err := io.ReadAll(file)
must.NoError(ctx, err)
return content
}
19 changes: 10 additions & 9 deletions form/form.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/go-git/go-billy/v5"
"github.com/gov4git/lib4git/must"
"github.com/gov4git/lib4git/ns"
)

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

func EncodeToFile[F Form](ctx context.Context, fs billy.Filesystem, path string, form F) error {
file, err := fs.Create(path)
func EncodeToFile[F Form](ctx context.Context, fs billy.Filesystem, path ns.NS, form F) error {
file, err := fs.Create(path.GitPath())
if err != nil {
return err
}
defer file.Close()
return Encode(ctx, file, form)
}

func DecodeFromFile[F Form](ctx context.Context, fs billy.Filesystem, path string) (form F, err error) {
file, err := fs.Open(path)
func DecodeFromFile[F Form](ctx context.Context, fs billy.Filesystem, path ns.NS) (form F, err error) {
file, err := fs.Open(path.GitPath())
if err != nil {
return form, err
}
defer file.Close()
return Decode[F](ctx, file)
}

func DecodeFromFileInto(ctx context.Context, fs billy.Filesystem, path string, into Form) error {
file, err := fs.Open(path)
func DecodeFromFileInto(ctx context.Context, fs billy.Filesystem, path ns.NS, into Form) error {
file, err := fs.Open(path.GitPath())
if err != nil {
return err
}
defer file.Close()
return DecodeInto(ctx, file, into)
}

func ToFile[F Form](ctx context.Context, fs billy.Filesystem, path string, form F) {
func ToFile[F Form](ctx context.Context, fs billy.Filesystem, path ns.NS, form F) {
if err := EncodeToFile(ctx, fs, path, form); err != nil {
must.Panic(ctx, err)
}
}

func FromFile[F Form](ctx context.Context, fs billy.Filesystem, path string) F {
func FromFile[F Form](ctx context.Context, fs billy.Filesystem, path ns.NS) F {
f, err := DecodeFromFile[F](ctx, fs, path)
if err != nil {
must.Panic(ctx, err)
}
return f
}

func FromFileInto(ctx context.Context, fs billy.Filesystem, path string, into Form) {
func FromFileInto(ctx context.Context, fs billy.Filesystem, path ns.NS, into Form) {
must.NoError(ctx, DecodeFromFileInto(ctx, fs, path, into))
}
15 changes: 9 additions & 6 deletions git/fs.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
package git

import "path/filepath"
import (
"github.com/gov4git/lib4git/ns"
)

func ListFilesRecursively(t *Tree, dir string) ([]string, error) {
func ListFilesRecursively(t *Tree, dir ns.NS) ([]ns.NS, error) {
fs := t.Filesystem
infos, err := fs.ReadDir(dir)
infos, err := fs.ReadDir(dir.GitPath())
if err != nil {
return nil, err
}
list := []string{}
list := []ns.NS{}
for _, info := range infos {
childPath := dir.Append(info.Name())
if info.IsDir() {
sublist, err := ListFilesRecursively(t, filepath.Join(dir, info.Name()))
sublist, err := ListFilesRecursively(t, childPath)
if err != nil {
return nil, err
}
list = append(list, sublist...)
} else {
list = append(list, filepath.Join(dir, info.Name()))
list = append(list, childPath)
}
}
return list, nil
Expand Down
4 changes: 2 additions & 2 deletions git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,8 @@ func Worktree(ctx context.Context, repo *Repository) *Tree {
return wt
}

func Add(ctx context.Context, wt *Tree, path string) {
if _, err := wt.Add(path); err != nil {
func Add(ctx context.Context, wt *Tree, path ns.NS) {
if _, err := wt.Add(path.GitPath()); err != nil {
must.Panic(ctx, err)
}
}
Expand Down
60 changes: 38 additions & 22 deletions git/tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,46 @@ package git

import (
"context"
"path/filepath"
"io/fs"

"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/format/index"
"github.com/gov4git/lib4git/file"
"github.com/gov4git/lib4git/form"
"github.com/gov4git/lib4git/must"
"github.com/gov4git/lib4git/ns"
)

func TreeMkdirAll(ctx context.Context, t *Tree, path string) {
err := t.Filesystem.MkdirAll(path, 0755)
func TreeMkdirAll(ctx context.Context, t *Tree, path ns.NS) {
if path.Len() == 0 {
return
}
err := t.Filesystem.MkdirAll(path.GitPath(), 0755)
must.NoError(ctx, err)
}

func TreeStat(ctx context.Context, t *Tree, path ns.NS) (fs.FileInfo, error) {
return t.Filesystem.Stat(path.GitPath())
}

func TreeRemove(ctx context.Context, t *Tree, path ns.NS) (plumbing.Hash, error) {
return t.Remove(path.GitPath())
}

func TreeReadDir(ctx context.Context, t *Tree, path ns.NS) ([]fs.FileInfo, error) {
return t.Filesystem.ReadDir(path.GitPath())
}

//

func BytesToFile(ctx context.Context, t *Tree, path ns.NS, content []byte) {
TreeMkdirAll(ctx, t, filepath.Dir(path.Path()))
TreeMkdirAll(ctx, t, path.Dir())
file.BytesToFile(ctx, t.Filesystem, path, content)
}

func BytesToFileStage(ctx context.Context, t *Tree, path ns.NS, content []byte) {
BytesToFile(ctx, t, path, content)
Add(ctx, t, path.Path())
Add(ctx, t, path)
}

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

func StringToFile(ctx context.Context, t *Tree, path ns.NS, content string) {
TreeMkdirAll(ctx, t, filepath.Dir(path.Path()))
TreeMkdirAll(ctx, t, path.Dir())
file.StringToFile(ctx, t.Filesystem, path, content)
}

func StringToFileStage(ctx context.Context, t *Tree, path ns.NS, content string) {
StringToFile(ctx, t, path, content)
Add(ctx, t, path.Path())
Add(ctx, t, path)
}

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

//

func ToFile[V form.Form](ctx context.Context, t *Tree, filePath string, value V) {
TreeMkdirAll(ctx, t, filepath.Dir(filePath))
func ToFile[V form.Form](ctx context.Context, t *Tree, filePath ns.NS, value V) {
TreeMkdirAll(ctx, t, filePath.Dir())
form.ToFile(ctx, t.Filesystem, filePath, value)
}

func ToFileStage[V form.Form](ctx context.Context, t *Tree, filePath string, value V) {
func ToFileStage[V form.Form](ctx context.Context, t *Tree, filePath ns.NS, value V) {
ToFile(ctx, t, filePath, value)
Add(ctx, t, filePath)
}

func FromFile[V form.Form](ctx context.Context, t *Tree, filePath string) V {
func FromFile[V form.Form](ctx context.Context, t *Tree, filePath ns.NS) V {
return form.FromFile[V](ctx, t.Filesystem, filePath)
}

func FromFileInto(ctx context.Context, t *Tree, filePath string, into form.Form) {
func FromFileInto(ctx context.Context, t *Tree, filePath ns.NS, into form.Form) {
form.FromFileInto(ctx, t.Filesystem, filePath, into)
}

func TryFromFile[V form.Form](ctx context.Context, t *Tree, filePath string) (v V, err error) {
func TryFromFile[V form.Form](ctx context.Context, t *Tree, filePath ns.NS) (v V, err error) {
err = must.Try(
func() {
v = FromFile[V](ctx, t, filePath)
Expand All @@ -77,8 +93,8 @@ func TryFromFile[V form.Form](ctx context.Context, t *Tree, filePath string) (v
return
}

func RenameStage(ctx context.Context, t *Tree, oldPath, newPath string) {
stat, err := t.Filesystem.Stat(oldPath)
func RenameStage(ctx context.Context, t *Tree, oldPath, newPath ns.NS) {
stat, err := t.Filesystem.Stat(oldPath.GitPath())
must.NoError(ctx, err)
if stat.IsDir() {
renameStageDir(ctx, t, oldPath, newPath)
Expand All @@ -87,20 +103,20 @@ func RenameStage(ctx context.Context, t *Tree, oldPath, newPath string) {
}
}

func renameStageDir(ctx context.Context, t *Tree, oldPath, newPath string) {
infos, err := t.Filesystem.ReadDir(oldPath)
func renameStageDir(ctx context.Context, t *Tree, oldPath, newPath ns.NS) {
infos, err := t.Filesystem.ReadDir(oldPath.GitPath())
must.NoError(ctx, err)
for _, info := range infos {
RenameStage(ctx, t, filepath.Join(oldPath, info.Name()), filepath.Join(newPath, info.Name()))
RenameStage(ctx, t, oldPath.Sub(info.Name()), newPath.Sub(info.Name()))
}
}

func renameStageFile(ctx context.Context, t *Tree, oldPath, newPath string) {
must.NoError(ctx, t.Filesystem.Rename(oldPath, newPath))
_, err := t.Remove(oldPath)
func renameStageFile(ctx context.Context, t *Tree, oldPath, newPath ns.NS) {
must.NoError(ctx, t.Filesystem.Rename(oldPath.GitPath(), newPath.GitPath()))
_, err := t.Remove(oldPath.GitPath())
if err != index.ErrEntryNotFound {
must.NoError(ctx, err)
}
_, err = t.Add(newPath)
_, err = t.Add(newPath.GitPath())
must.NoError(ctx, err)
}
14 changes: 6 additions & 8 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@ go 1.21

require (
github.com/go-git/go-billy/v5 v5.5.0
github.com/go-git/go-git/v5 v5.9.0
github.com/go-git/go-git/v5 v5.10.0
github.com/gofrs/flock v0.8.1
github.com/google/uuid v1.3.0
github.com/whilp/git-urls v1.0.0
go.uber.org/zap v1.24.0
go.uber.org/zap v1.26.0
)

require (
Expand All @@ -27,12 +26,11 @@ require (
github.com/sergi/go-diff v1.1.0 // indirect
github.com/skeema/knownhosts v1.2.0 // indirect
github.com/xanzy/ssh-agent v0.3.3 // indirect
go.uber.org/atomic v1.7.0 // indirect
go.uber.org/multierr v1.6.0 // indirect
golang.org/x/crypto v0.13.0 // indirect
go.uber.org/multierr v1.10.0 // indirect
golang.org/x/crypto v0.14.0 // indirect
golang.org/x/mod v0.12.0 // indirect
golang.org/x/net v0.15.0 // indirect
golang.org/x/sys v0.12.0 // indirect
golang.org/x/net v0.17.0 // indirect
golang.org/x/sys v0.13.0 // indirect
golang.org/x/tools v0.13.0 // indirect
gopkg.in/warnings.v0 v0.1.2 // indirect
)
Loading

0 comments on commit c88b521

Please sign in to comment.