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

added support for quarantine directory #887

Merged
merged 2 commits into from
Aug 21, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
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
53 changes: 50 additions & 3 deletions storage/filesystem/dotgit/dotgit.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,19 +279,66 @@ func (d *DotGit) objectPath(h plumbing.Hash) string {
return d.fs.Join(objectsPath, hash[0:2], hash[2:40])
}

//incomingObjectPath is intended to add support for a git pre-recieve hook to be written
//it adds support for go-git to find objects in an "incoming" directory, so that the library
//can be used to write a pre-recieve hook that deals with the incoming objects.
//More on git hooks found here : https://git-scm.com/docs/githooks
//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 {
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()
}
}
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])
}

// Object returns a fs.File pointing the object file, if exists
func (d *DotGit) Object(h plumbing.Hash) (billy.File, error) {
return d.fs.Open(d.objectPath(h))
obj1, err1 := d.fs.Open(d.objectPath(h))
if os.IsNotExist(err1) {
obj2, err2 := d.fs.Open(d.incomingObjectPath(h))
if err2 != nil {
return obj1, err1
}
return obj2, err2
}
return obj1, err1
}

// ObjectStat returns a os.FileInfo pointing the object file, if exists
func (d *DotGit) ObjectStat(h plumbing.Hash) (os.FileInfo, error) {
return d.fs.Stat(d.objectPath(h))
obj1, err1 := d.fs.Stat(d.objectPath(h))
if os.IsNotExist(err1) {
obj2, err2 := d.fs.Stat(d.incomingObjectPath(h))
if err2 != nil {
return obj1, err1
}
return obj2, err2
}
return obj1, err1
}

// ObjectDelete removes the object file, if exists
func (d *DotGit) ObjectDelete(h plumbing.Hash) error {
return d.fs.Remove(d.objectPath(h))
err1 := d.fs.Remove(d.objectPath(h))
if os.IsNotExist(err1) {
err2 := d.fs.Remove(d.incomingObjectPath(h))
if err2 != nil {
return err1
}
return err2
}
return err1
}

func (d *DotGit) readReferenceFrom(rd io.Reader, name string) (ref *plumbing.Reference, err error) {
Expand Down
51 changes: 51 additions & 0 deletions storage/filesystem/dotgit/dotgit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,57 @@ func (s *SuiteDotGit) TestObject(c *C) {
file.Name(), fs.Join("objects", "03", "db8e1fbe133a480f2867aac478fd866686d69e")),
Equals, true,
)
incomingHash := "9d25e0f9bde9f82882b49fe29117b9411cb157b7" //made up hash
incomingDirPath := fs.Join("objects", "incoming-123456")
incomingFilePath := fs.Join(incomingDirPath, incomingHash[0:2], incomingHash[2:40])
fs.MkdirAll(incomingDirPath, os.FileMode(0755))
fs.Create(incomingFilePath)

file, err = dir.Object(plumbing.NewHash(incomingHash))
c.Assert(err, IsNil)
}

func (s *SuiteDotGit) TestObjectStat(c *C) {
fs := fixtures.ByTag(".git").ByTag("unpacked").One().DotGit()
dir := New(fs)

hash := plumbing.NewHash("03db8e1fbe133a480f2867aac478fd866686d69e")
_, err := dir.ObjectStat(hash)
c.Assert(err, IsNil)
incomingHash := "9d25e0f9bde9f82882b49fe29117b9411cb157b7" //made up hash
incomingDirPath := fs.Join("objects", "incoming-123456")
incomingFilePath := fs.Join(incomingDirPath, incomingHash[0:2], incomingHash[2:40])
fs.MkdirAll(incomingDirPath, os.FileMode(0755))
fs.Create(incomingFilePath)

_, err = dir.ObjectStat(plumbing.NewHash(incomingHash))
c.Assert(err, IsNil)
}

func (s *SuiteDotGit) TestObjectDelete(c *C) {
fs := fixtures.ByTag(".git").ByTag("unpacked").One().DotGit()
dir := New(fs)

hash := plumbing.NewHash("03db8e1fbe133a480f2867aac478fd866686d69e")
err := dir.ObjectDelete(hash)
c.Assert(err, IsNil)

incomingHash := "9d25e0f9bde9f82882b49fe29117b9411cb157b7" //made up hash
incomingDirPath := fs.Join("objects", "incoming-123456")
incomingSubDirPath := fs.Join(incomingDirPath, incomingHash[0:2])
incomingFilePath := fs.Join(incomingSubDirPath, incomingHash[2:40])

err = fs.MkdirAll(incomingSubDirPath, os.FileMode(0755))
c.Assert(err, IsNil)

f, err := fs.Create(incomingFilePath)
c.Assert(err, IsNil)

err = f.Close()
c.Assert(err, IsNil)

err = dir.ObjectDelete(plumbing.NewHash(incomingHash))
c.Assert(err, IsNil)
}

func (s *SuiteDotGit) TestObjectNotFound(c *C) {
Expand Down