blob_cache.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. IgnoreMissingSubmodules bool
  13. repository *git.Repository
  14. cache map[plumbing.Hash]*object.Blob
  15. }
  16. func (cache *BlobCache) Name() string {
  17. return "BlobCache"
  18. }
  19. func (cache *BlobCache) Provides() []string {
  20. arr := [...]string{"blob_cache"}
  21. return arr[:]
  22. }
  23. func (cache *BlobCache) Requires() []string {
  24. arr := [...]string{"changes"}
  25. return arr[:]
  26. }
  27. func (cache *BlobCache) Initialize(repository *git.Repository) {
  28. cache.repository = repository
  29. cache.cache = map[plumbing.Hash]*object.Blob{}
  30. }
  31. func (self *BlobCache) Consume(deps map[string]interface{}) (map[string]interface{}, error) {
  32. commit := deps["commit"].(*object.Commit)
  33. changes := deps["changes"].(object.Changes)
  34. cache := map[plumbing.Hash]*object.Blob{}
  35. newCache := map[plumbing.Hash]*object.Blob{}
  36. for _, change := range changes {
  37. action, err := change.Action()
  38. if err != nil {
  39. fmt.Fprintf(os.Stderr, "no action in %s\n", change.To.TreeEntry.Hash)
  40. return nil, err
  41. }
  42. var exists bool
  43. var blob *object.Blob
  44. switch action {
  45. case merkletrie.Insert:
  46. blob, err = self.getBlob(&change.To, commit.File)
  47. if err != nil {
  48. fmt.Fprintf(os.Stderr, "file to %s %s\n", change.To.Name, change.To.TreeEntry.Hash)
  49. } else {
  50. cache[change.To.TreeEntry.Hash] = blob
  51. newCache[change.To.TreeEntry.Hash] = blob
  52. }
  53. case merkletrie.Delete:
  54. cache[change.From.TreeEntry.Hash], exists = self.cache[change.From.TreeEntry.Hash]
  55. if !exists {
  56. cache[change.From.TreeEntry.Hash], err = self.getBlob(&change.From, commit.File)
  57. if err != nil {
  58. if err.Error() != plumbing.ErrObjectNotFound.Error() {
  59. fmt.Fprintf(os.Stderr, "file from %s %s\n", change.From.Name,
  60. change.From.TreeEntry.Hash)
  61. } else {
  62. cache[change.From.TreeEntry.Hash], err = createDummyBlob(
  63. change.From.TreeEntry.Hash)
  64. }
  65. }
  66. }
  67. case merkletrie.Modify:
  68. blob, err = self.getBlob(&change.To, commit.File)
  69. if err != nil {
  70. fmt.Fprintf(os.Stderr, "file to %s\n", change.To.Name)
  71. } else {
  72. cache[change.To.TreeEntry.Hash] = blob
  73. newCache[change.To.TreeEntry.Hash] = blob
  74. }
  75. cache[change.From.TreeEntry.Hash], exists = self.cache[change.From.TreeEntry.Hash]
  76. if !exists {
  77. cache[change.From.TreeEntry.Hash], err = self.getBlob(&change.From, commit.File)
  78. if err != nil {
  79. fmt.Fprintf(os.Stderr, "file from %s\n", change.From.Name)
  80. }
  81. }
  82. }
  83. if err != nil {
  84. return nil, err
  85. }
  86. }
  87. self.cache = newCache
  88. return map[string]interface{}{"blob_cache": cache}, nil
  89. }
  90. func (cache *BlobCache) Finalize() interface{} {
  91. return nil
  92. }
  93. type FileGetter func(path string) (*object.File, error)
  94. func (cache *BlobCache) getBlob(entry *object.ChangeEntry, fileGetter FileGetter) (
  95. *object.Blob, error) {
  96. blob, err := cache.repository.BlobObject(entry.TreeEntry.Hash)
  97. if err != nil {
  98. if err.Error() != plumbing.ErrObjectNotFound.Error() {
  99. fmt.Fprintf(os.Stderr, "getBlob(%s)\n", entry.TreeEntry.Hash.String())
  100. return nil, err
  101. }
  102. if entry.TreeEntry.Mode != 0160000 {
  103. // this is not a submodule
  104. return nil, err
  105. } else if cache.IgnoreMissingSubmodules {
  106. return createDummyBlob(entry.TreeEntry.Hash)
  107. }
  108. file, err_modules := fileGetter(".gitmodules")
  109. if err_modules != nil {
  110. return nil, err
  111. }
  112. contents, err_modules := file.Contents()
  113. if err_modules != nil {
  114. return nil, err
  115. }
  116. modules := config.NewModules()
  117. err_modules = modules.Unmarshal([]byte(contents))
  118. if err_modules != nil {
  119. return nil, err
  120. }
  121. _, exists := modules.Submodules[entry.Name]
  122. if exists {
  123. // we found that this is a submodule
  124. return createDummyBlob(entry.TreeEntry.Hash)
  125. }
  126. return nil, err
  127. }
  128. return blob, nil
  129. }