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

Commit cc27d4a

Browse files
authored
Merge pull request #887 from noxora/hook-support
added support for quarantine directory
2 parents 8120de8 + 8cf7edb commit cc27d4a

File tree

2 files changed

+101
-3
lines changed

2 files changed

+101
-3
lines changed

storage/filesystem/dotgit/dotgit.go

+50-3
Original file line numberDiff line numberDiff line change
@@ -279,19 +279,66 @@ func (d *DotGit) objectPath(h plumbing.Hash) string {
279279
return d.fs.Join(objectsPath, hash[0:2], hash[2:40])
280280
}
281281

282+
//incomingObjectPath is intended to add support for a git pre-recieve hook to be written
283+
//it adds support for go-git to find objects in an "incoming" directory, so that the library
284+
//can be used to write a pre-recieve hook that deals with the incoming objects.
285+
//More on git hooks found here : https://git-scm.com/docs/githooks
286+
//More on 'quarantine'/incoming directory here : https://git-scm.com/docs/git-receive-pack
287+
func (d *DotGit) incomingObjectPath(h plumbing.Hash) string {
288+
hString := h.String()
289+
directoryContents, err := d.fs.ReadDir(objectsPath)
290+
if err != nil {
291+
return d.fs.Join(objectsPath, hString[0:2], hString[2:40])
292+
}
293+
var incomingDirName string
294+
for _, file := range directoryContents {
295+
if strings.Split(file.Name(), "-")[0] == "incoming" && file.IsDir() {
296+
incomingDirName = file.Name()
297+
}
298+
}
299+
if incomingDirName == "" {
300+
return d.fs.Join(objectsPath, hString[0:2], hString[2:40])
301+
}
302+
return d.fs.Join(objectsPath, incomingDirName, hString[0:2], hString[2:40])
303+
}
304+
282305
// Object returns a fs.File pointing the object file, if exists
283306
func (d *DotGit) Object(h plumbing.Hash) (billy.File, error) {
284-
return d.fs.Open(d.objectPath(h))
307+
obj1, err1 := d.fs.Open(d.objectPath(h))
308+
if os.IsNotExist(err1) {
309+
obj2, err2 := d.fs.Open(d.incomingObjectPath(h))
310+
if err2 != nil {
311+
return obj1, err1
312+
}
313+
return obj2, err2
314+
}
315+
return obj1, err1
285316
}
286317

287318
// ObjectStat returns a os.FileInfo pointing the object file, if exists
288319
func (d *DotGit) ObjectStat(h plumbing.Hash) (os.FileInfo, error) {
289-
return d.fs.Stat(d.objectPath(h))
320+
obj1, err1 := d.fs.Stat(d.objectPath(h))
321+
if os.IsNotExist(err1) {
322+
obj2, err2 := d.fs.Stat(d.incomingObjectPath(h))
323+
if err2 != nil {
324+
return obj1, err1
325+
}
326+
return obj2, err2
327+
}
328+
return obj1, err1
290329
}
291330

292331
// ObjectDelete removes the object file, if exists
293332
func (d *DotGit) ObjectDelete(h plumbing.Hash) error {
294-
return d.fs.Remove(d.objectPath(h))
333+
err1 := d.fs.Remove(d.objectPath(h))
334+
if os.IsNotExist(err1) {
335+
err2 := d.fs.Remove(d.incomingObjectPath(h))
336+
if err2 != nil {
337+
return err1
338+
}
339+
return err2
340+
}
341+
return err1
295342
}
296343

297344
func (d *DotGit) readReferenceFrom(rd io.Reader, name string) (ref *plumbing.Reference, err error) {

storage/filesystem/dotgit/dotgit_test.go

+51
Original file line numberDiff line numberDiff line change
@@ -537,6 +537,57 @@ func (s *SuiteDotGit) TestObject(c *C) {
537537
file.Name(), fs.Join("objects", "03", "db8e1fbe133a480f2867aac478fd866686d69e")),
538538
Equals, true,
539539
)
540+
incomingHash := "9d25e0f9bde9f82882b49fe29117b9411cb157b7" //made up hash
541+
incomingDirPath := fs.Join("objects", "incoming-123456")
542+
incomingFilePath := fs.Join(incomingDirPath, incomingHash[0:2], incomingHash[2:40])
543+
fs.MkdirAll(incomingDirPath, os.FileMode(0755))
544+
fs.Create(incomingFilePath)
545+
546+
file, err = dir.Object(plumbing.NewHash(incomingHash))
547+
c.Assert(err, IsNil)
548+
}
549+
550+
func (s *SuiteDotGit) TestObjectStat(c *C) {
551+
fs := fixtures.ByTag(".git").ByTag("unpacked").One().DotGit()
552+
dir := New(fs)
553+
554+
hash := plumbing.NewHash("03db8e1fbe133a480f2867aac478fd866686d69e")
555+
_, err := dir.ObjectStat(hash)
556+
c.Assert(err, IsNil)
557+
incomingHash := "9d25e0f9bde9f82882b49fe29117b9411cb157b7" //made up hash
558+
incomingDirPath := fs.Join("objects", "incoming-123456")
559+
incomingFilePath := fs.Join(incomingDirPath, incomingHash[0:2], incomingHash[2:40])
560+
fs.MkdirAll(incomingDirPath, os.FileMode(0755))
561+
fs.Create(incomingFilePath)
562+
563+
_, err = dir.ObjectStat(plumbing.NewHash(incomingHash))
564+
c.Assert(err, IsNil)
565+
}
566+
567+
func (s *SuiteDotGit) TestObjectDelete(c *C) {
568+
fs := fixtures.ByTag(".git").ByTag("unpacked").One().DotGit()
569+
dir := New(fs)
570+
571+
hash := plumbing.NewHash("03db8e1fbe133a480f2867aac478fd866686d69e")
572+
err := dir.ObjectDelete(hash)
573+
c.Assert(err, IsNil)
574+
575+
incomingHash := "9d25e0f9bde9f82882b49fe29117b9411cb157b7" //made up hash
576+
incomingDirPath := fs.Join("objects", "incoming-123456")
577+
incomingSubDirPath := fs.Join(incomingDirPath, incomingHash[0:2])
578+
incomingFilePath := fs.Join(incomingSubDirPath, incomingHash[2:40])
579+
580+
err = fs.MkdirAll(incomingSubDirPath, os.FileMode(0755))
581+
c.Assert(err, IsNil)
582+
583+
f, err := fs.Create(incomingFilePath)
584+
c.Assert(err, IsNil)
585+
586+
err = f.Close()
587+
c.Assert(err, IsNil)
588+
589+
err = dir.ObjectDelete(plumbing.NewHash(incomingHash))
590+
c.Assert(err, IsNil)
540591
}
541592

542593
func (s *SuiteDotGit) TestObjectNotFound(c *C) {

0 commit comments

Comments
 (0)