aboutsummaryrefslogtreecommitdiffstats
path: root/storage/filesystem/internal/dotgit/dotgit.go
diff options
context:
space:
mode:
Diffstat (limited to 'storage/filesystem/internal/dotgit/dotgit.go')
-rw-r--r--storage/filesystem/internal/dotgit/dotgit.go82
1 files changed, 82 insertions, 0 deletions
diff --git a/storage/filesystem/internal/dotgit/dotgit.go b/storage/filesystem/internal/dotgit/dotgit.go
index 448f6a2..75c98ff 100644
--- a/storage/filesystem/internal/dotgit/dotgit.go
+++ b/storage/filesystem/internal/dotgit/dotgit.go
@@ -22,6 +22,8 @@ var (
ErrIdxNotFound = errors.New("idx file not found")
// ErrPackfileNotFound is returned by Packfile when the packfile is not found
ErrPackfileNotFound = errors.New("packfile not found")
+ // ErrObjfileNotFound is returned by Objectfile when the objectffile is not found
+ ErrObjfileNotFound = errors.New("object file not found")
// ErrConfigNotFound is returned by Config when the config is not found
ErrConfigNotFound = errors.New("config file not found")
)
@@ -74,6 +76,10 @@ func (d *DotGit) Packfile() (fs.FS, string, error) {
packDir := d.fs.Join(d.path, "objects", "pack")
files, err := d.fs.ReadDir(packDir)
if err != nil {
+ if os.IsNotExist(err) {
+ return nil, "", ErrPackfileNotFound
+ }
+
return nil, "", err
}
@@ -93,6 +99,10 @@ func (d *DotGit) Idxfile() (fs.FS, string, error) {
packDir := d.fs.Join(d.path, "objects", "pack")
files, err := d.fs.ReadDir(packDir)
if err != nil {
+ if os.IsNotExist(err) {
+ return nil, "", ErrIdxNotFound
+ }
+
return nil, "", err
}
@@ -118,3 +128,75 @@ func (d *DotGit) Config() (fs.FS, string, error) {
return d.fs, configFile, nil
}
+
+// Objectfiles returns a slice with the hashes of objects found under the
+// .git/objects/ directory.
+func (dg *DotGit) Objectfiles() (fs.FS, []core.Hash, error) {
+ objsDir := dg.fs.Join(dg.path, "objects")
+
+ files, err := dg.fs.ReadDir(objsDir)
+ if err != nil {
+ if os.IsNotExist(err) {
+ return nil, nil, ErrObjfileNotFound
+ }
+
+ return nil, nil, err
+ }
+
+ var objects []core.Hash
+ for _, f := range files {
+ if f.IsDir() && len(f.Name()) == 2 && isHex(f.Name()) {
+ objDir := f.Name()
+ d, err := dg.fs.ReadDir(dg.fs.Join(objsDir, objDir))
+ if err != nil {
+ return nil, nil, err
+ }
+
+ for _, o := range d {
+ objects = append(objects, core.NewHash(objDir+o.Name()))
+ }
+ }
+ }
+
+ return dg.fs, objects, nil
+}
+
+func isHex(s string) bool {
+ for _, b := range []byte(s) {
+ if isNum(b) {
+ continue
+ }
+ if isHexAlpha(b) {
+ continue
+ }
+
+ return false
+ }
+
+ return true
+}
+
+func isNum(b byte) bool {
+ return b >= '0' && b <= '9'
+}
+
+func isHexAlpha(b byte) bool {
+ return b >= 'a' && b <= 'f' || b >= 'A' && b <= 'F'
+}
+
+// Objectfile returns the path of the object file for a given hash
+// *if the file exists*, otherwise returns an ErrObjfileNotFound error.
+func (d *DotGit) Objectfile(h core.Hash) (fs.FS, string, error) {
+ hash := h.String()
+ objFile := d.fs.Join(d.path, "objects", hash[0:2], hash[2:40])
+
+ if _, err := d.fs.Stat(objFile); err != nil {
+ if os.IsNotExist(err) {
+ return nil, "", ErrObjfileNotFound
+ }
+
+ return nil, "", err
+ }
+
+ return d.fs, objFile, nil
+}