Skip to content

Commit

Permalink
add IsRegisteredAsDirectory method for index (#134)
Browse files Browse the repository at this point in the history
  • Loading branch information
JunNishimura committed Jun 22, 2023
1 parent a28078b commit 2e8be96
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions internal/store/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"os"
"path/filepath"
"regexp"
"sort"

"github.com/JunNishimura/Goit/internal/object"
Expand Down Expand Up @@ -89,6 +90,34 @@ func (idx *Index) GetEntry(path []byte) (int, *Entry, bool) {
return newEntryFlag, nil, false
}

func (idx *Index) IsRegisteredAsDirectory(dirName string) bool {
if idx.EntryNum == 0 {
return false
}

dirRegexp := regexp.MustCompile(fmt.Sprintf(`.*%s\/.+`, dirName))

left := 0
right := int(idx.EntryNum)
for {
middle := (left + right) / 2
entry := idx.Entries[middle]
if dirRegexp.MatchString(string(entry.Path)) {
return true
} else if string(entry.Path) < dirName {
left = middle + 1
} else {
right = middle
}

if right-left < 1 {
break
}
}

return false
}

func (idx *Index) Update(rootGoitPath string, hash sha.SHA1, path []byte) (bool, error) {
pos, gotEntry, isFound := idx.GetEntry(path)
if isFound && string(gotEntry.Hash) == string(hash) && string(gotEntry.Path) == string(path) {
Expand Down

0 comments on commit 2e8be96

Please sign in to comment.