common.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. package packfile
  2. import (
  3. "bytes"
  4. "io"
  5. "sync"
  6. "gopkg.in/src-d/go-git.v4/plumbing/storer"
  7. "gopkg.in/src-d/go-git.v4/utils/ioutil"
  8. )
  9. var signature = []byte{'P', 'A', 'C', 'K'}
  10. const (
  11. // VersionSupported is the packfile version supported by this package
  12. VersionSupported uint32 = 2
  13. firstLengthBits = uint8(4) // the first byte into object header has 4 bits to store the length
  14. lengthBits = uint8(7) // subsequent bytes has 7 bits to store the length
  15. maskFirstLength = 15 // 0000 1111
  16. maskContinue = 0x80 // 1000 0000
  17. maskLength = uint8(127) // 0111 1111
  18. maskType = uint8(112) // 0111 0000
  19. )
  20. // UpdateObjectStorage updates the storer with the objects in the given
  21. // packfile.
  22. func UpdateObjectStorage(s storer.Storer, packfile io.Reader) error {
  23. if pw, ok := s.(storer.PackfileWriter); ok {
  24. return WritePackfileToObjectStorage(pw, packfile)
  25. }
  26. p, err := NewParserWithStorage(NewScanner(packfile), s)
  27. if err != nil {
  28. return err
  29. }
  30. _, err = p.Parse()
  31. return err
  32. }
  33. // WritePackfileToObjectStorage writes all the packfile objects into the given
  34. // object storage.
  35. func WritePackfileToObjectStorage(
  36. sw storer.PackfileWriter,
  37. packfile io.Reader,
  38. ) (err error) {
  39. w, err := sw.PackfileWriter()
  40. if err != nil {
  41. return err
  42. }
  43. defer ioutil.CheckClose(w, &err)
  44. var n int64
  45. n, err = io.Copy(w, packfile)
  46. if err == nil && n == 0 {
  47. return ErrEmptyPackfile
  48. }
  49. return err
  50. }
  51. var bufPool = sync.Pool{
  52. New: func() interface{} {
  53. return bytes.NewBuffer(nil)
  54. },
  55. }