@@ -279,19 +279,66 @@ func (d *DotGit) objectPath(h plumbing.Hash) string {
279
279
return d .fs .Join (objectsPath , hash [0 :2 ], hash [2 :40 ])
280
280
}
281
281
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
+
282
305
// Object returns a fs.File pointing the object file, if exists
283
306
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
285
316
}
286
317
287
318
// ObjectStat returns a os.FileInfo pointing the object file, if exists
288
319
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
290
329
}
291
330
292
331
// ObjectDelete removes the object file, if exists
293
332
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
295
342
}
296
343
297
344
func (d * DotGit ) readReferenceFrom (rd io.Reader , name string ) (ref * plumbing.Reference , err error ) {
0 commit comments