blob_cache.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. package hercules
  2. import (
  3. "fmt"
  4. "os"
  5. "gopkg.in/src-d/go-git.v4"
  6. "gopkg.in/src-d/go-git.v4/config"
  7. "gopkg.in/src-d/go-git.v4/plumbing"
  8. "gopkg.in/src-d/go-git.v4/plumbing/object"
  9. "gopkg.in/src-d/go-git.v4/utils/merkletrie"
  10. )
  11. type BlobCache struct {
  12. repository *git.Repository
  13. }
  14. func (cache *BlobCache) Name() string {
  15. return "BlobCache"
  16. }
  17. func (cache *BlobCache) Provides() []string {
  18. arr := [...]string{"blob_cache"}
  19. return arr[:]
  20. }
  21. func (cache *BlobCache) Requires() []string {
  22. arr := [...]string{"changes"}
  23. return arr[:]
  24. }
  25. func (cache *BlobCache) Initialize(repository *git.Repository) {
  26. cache.repository = repository
  27. }
  28. func (self *BlobCache) Consume(deps map[string]interface{}) (map[string]interface{}, error) {
  29. commit := deps["commit"].(*object.Commit)
  30. changes := deps["changes"].(object.Changes)
  31. cache := make(map[plumbing.Hash]*object.Blob)
  32. for _, change := range changes {
  33. action, err := change.Action()
  34. if err != nil {
  35. fmt.Fprintf(os.Stderr, "no action in %s\n", change.To.TreeEntry.Hash)
  36. return nil, err
  37. }
  38. switch action {
  39. case merkletrie.Insert:
  40. cache[change.To.TreeEntry.Hash], err = self.getBlob(&change.To, commit.File)
  41. if err != nil {
  42. fmt.Fprintf(os.Stderr, "file to %s %s\n", change.To.Name, change.To.TreeEntry.Hash)
  43. }
  44. case merkletrie.Delete:
  45. cache[change.From.TreeEntry.Hash], err = self.getBlob(&change.From, commit.File)
  46. if err != nil {
  47. if err.Error() != plumbing.ErrObjectNotFound.Error() {
  48. fmt.Fprintf(os.Stderr, "file from %s %s\n", change.From.Name, change.From.TreeEntry.Hash)
  49. } else {
  50. cache[change.From.TreeEntry.Hash], err = createDummyBlob(
  51. &change.From.TreeEntry.Hash)
  52. }
  53. }
  54. case merkletrie.Modify:
  55. cache[change.To.TreeEntry.Hash], err = self.getBlob(&change.To, commit.File)
  56. if err != nil {
  57. fmt.Fprintf(os.Stderr, "file to %s\n", change.To.Name)
  58. }
  59. cache[change.From.TreeEntry.Hash], err = self.getBlob(&change.From, commit.File)
  60. if err != nil {
  61. fmt.Fprintf(os.Stderr, "file from %s\n", change.From.Name)
  62. }
  63. default:
  64. panic(fmt.Sprintf("unsupported action: %d", change.Action))
  65. }
  66. if err != nil {
  67. return nil, err
  68. }
  69. }
  70. return map[string]interface{}{"blob_cache": cache}, nil
  71. }
  72. func (cache *BlobCache) Finalize() interface{} {
  73. return nil
  74. }
  75. type FileGetter func(path string) (*object.File, error)
  76. func (cache *BlobCache) getBlob(entry *object.ChangeEntry, fileGetter FileGetter) (
  77. *object.Blob, error) {
  78. blob, err := cache.repository.BlobObject(entry.TreeEntry.Hash)
  79. if err != nil {
  80. if err.Error() != plumbing.ErrObjectNotFound.Error() {
  81. fmt.Fprintf(os.Stderr, "getBlob(%s)\n", entry.TreeEntry.Hash.String())
  82. return nil, err
  83. }
  84. file, err_modules := fileGetter(".gitmodules")
  85. if err_modules != nil {
  86. return nil, err
  87. }
  88. contents, err_modules := file.Contents()
  89. if err_modules != nil {
  90. return nil, err
  91. }
  92. modules := config.NewModules()
  93. err_modules = modules.Unmarshal([]byte(contents))
  94. if err_modules != nil {
  95. return nil, err
  96. }
  97. _, exists := modules.Submodules[entry.Name]
  98. if exists {
  99. // we found that this is a submodule
  100. return createDummyBlob(&entry.TreeEntry.Hash)
  101. }
  102. return nil, err
  103. }
  104. return blob, nil
  105. }