Skip to content
This repository was archived by the owner on Sep 11, 2020. It is now read-only.

storage/dotgit: search for incoming dir only once #935

Merged
merged 2 commits into from
Aug 29, 2018
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 28 additions & 13 deletions storage/filesystem/dotgit/dotgit.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ var (
// type is not zero-value-safe, use the New function to initialize it.
type DotGit struct {
fs billy.Filesystem

// incoming object directory information
incomingChecked bool
incomingDirName string
}

// New returns a DotGit value ready to be used. The path argument must
Expand Down Expand Up @@ -286,26 +290,37 @@ func (d *DotGit) objectPath(h plumbing.Hash) string {
//More on 'quarantine'/incoming directory here : https://git-scm.com/docs/git-receive-pack
func (d *DotGit) incomingObjectPath(h plumbing.Hash) string {
hString := h.String()
directoryContents, err := d.fs.ReadDir(objectsPath)
if err != nil {

if d.incomingDirName == "" {
return d.fs.Join(objectsPath, hString[0:2], hString[2:40])
}
var incomingDirName string
for _, file := range directoryContents {
if strings.Split(file.Name(), "-")[0] == "incoming" && file.IsDir() {
incomingDirName = file.Name()

return d.fs.Join(objectsPath, d.incomingDirName, hString[0:2], hString[2:40])
}

// hasIncomingObjects searches for an incoming directory and keeps its name
// so it doesn't have to be found each time an object is accessed.
func (d *DotGit) hasIncomingObjects() bool {
if !d.incomingChecked {
directoryContents, err := d.fs.ReadDir(objectsPath)
if err == nil {
for _, file := range directoryContents {
if strings.Split(file.Name(), "-")[0] == "incoming" && file.IsDir() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it the same as strings.HasPrefix(file.Name(), "incoming")

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed

d.incomingDirName = file.Name()
}
}
}

d.incomingChecked = true
}
if incomingDirName == "" {
return d.fs.Join(objectsPath, hString[0:2], hString[2:40])
}
return d.fs.Join(objectsPath, incomingDirName, hString[0:2], hString[2:40])

return d.incomingDirName != ""
}

// Object returns a fs.File pointing the object file, if exists
func (d *DotGit) Object(h plumbing.Hash) (billy.File, error) {
obj1, err1 := d.fs.Open(d.objectPath(h))
if os.IsNotExist(err1) {
if os.IsNotExist(err1) && d.hasIncomingObjects() {
obj2, err2 := d.fs.Open(d.incomingObjectPath(h))
if err2 != nil {
return obj1, err1
Expand All @@ -318,7 +333,7 @@ func (d *DotGit) Object(h plumbing.Hash) (billy.File, error) {
// ObjectStat returns a os.FileInfo pointing the object file, if exists
func (d *DotGit) ObjectStat(h plumbing.Hash) (os.FileInfo, error) {
obj1, err1 := d.fs.Stat(d.objectPath(h))
if os.IsNotExist(err1) {
if os.IsNotExist(err1) && d.hasIncomingObjects() {
obj2, err2 := d.fs.Stat(d.incomingObjectPath(h))
if err2 != nil {
return obj1, err1
Expand All @@ -331,7 +346,7 @@ func (d *DotGit) ObjectStat(h plumbing.Hash) (os.FileInfo, error) {
// ObjectDelete removes the object file, if exists
func (d *DotGit) ObjectDelete(h plumbing.Hash) error {
err1 := d.fs.Remove(d.objectPath(h))
if os.IsNotExist(err1) {
if os.IsNotExist(err1) && d.hasIncomingObjects() {
err2 := d.fs.Remove(d.incomingObjectPath(h))
if err2 != nil {
return err1
Expand Down