blob_cache.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. // This PipelineItem loads the blobs which correspond to the changed files in a commit.
  12. // It must provide the old and the new objects; "cache" rotates and allows to not load
  13. // the same blobs twice. Outdated objects are removed so "cache" never grows big.
  14. type BlobCache struct {
  15. // Specifies how to handle the situation when we encounter a git submodule - an object without
  16. // the blob. If false, we look inside .gitmodules and if don't find, raise an error.
  17. // If true, we do not look inside .gitmodules and always succeed.
  18. IgnoreMissingSubmodules bool
  19. repository *git.Repository
  20. cache map[plumbing.Hash]*object.Blob
  21. }
  22. const (
  23. ConfigBlobCacheIgnoreMissingSubmodules = "BlobCache.IgnoreMissingSubmodules"
  24. DependencyBlobCache = "blob_cache"
  25. )
  26. func (cache *BlobCache) Name() string {
  27. return "BlobCache"
  28. }
  29. func (cache *BlobCache) Provides() []string {
  30. arr := [...]string{DependencyBlobCache}
  31. return arr[:]
  32. }
  33. func (cache *BlobCache) Requires() []string {
  34. arr := [...]string{DependencyTreeChanges}
  35. return arr[:]
  36. }
  37. func (cache *BlobCache) ListConfigurationOptions() []ConfigurationOption {
  38. options := [...]ConfigurationOption{{
  39. Name: ConfigBlobCacheIgnoreMissingSubmodules,
  40. Description: "Specifies whether to panic if some submodules do not exist and thus " +
  41. "the corresponding Git objects cannot be loaded.",
  42. Flag: "ignore-missing-submodules",
  43. Type: BoolConfigurationOption,
  44. Default: false}}
  45. return options[:]
  46. }
  47. func (cache *BlobCache) Configure(facts map[string]interface{}) {
  48. if val, exists := facts[ConfigBlobCacheIgnoreMissingSubmodules].(bool); exists {
  49. cache.IgnoreMissingSubmodules = val
  50. }
  51. }
  52. func (cache *BlobCache) Initialize(repository *git.Repository) {
  53. cache.repository = repository
  54. cache.cache = map[plumbing.Hash]*object.Blob{}
  55. }
  56. func (self *BlobCache) Consume(deps map[string]interface{}) (map[string]interface{}, error) {
  57. commit := deps["commit"].(*object.Commit)
  58. changes := deps[DependencyTreeChanges].(object.Changes)
  59. cache := map[plumbing.Hash]*object.Blob{}
  60. newCache := map[plumbing.Hash]*object.Blob{}
  61. for _, change := range changes {
  62. action, err := change.Action()
  63. if err != nil {
  64. fmt.Fprintf(os.Stderr, "no action in %s\n", change.To.TreeEntry.Hash)
  65. return nil, err
  66. }
  67. var exists bool
  68. var blob *object.Blob
  69. switch action {
  70. case merkletrie.Insert:
  71. blob, err = self.getBlob(&change.To, commit.File)
  72. if err != nil {
  73. fmt.Fprintf(os.Stderr, "file to %s %s\n", change.To.Name, change.To.TreeEntry.Hash)
  74. } else {
  75. cache[change.To.TreeEntry.Hash] = blob
  76. newCache[change.To.TreeEntry.Hash] = blob
  77. }
  78. case merkletrie.Delete:
  79. cache[change.From.TreeEntry.Hash], exists = self.cache[change.From.TreeEntry.Hash]
  80. if !exists {
  81. cache[change.From.TreeEntry.Hash], err = self.getBlob(&change.From, commit.File)
  82. if err != nil {
  83. if err.Error() != plumbing.ErrObjectNotFound.Error() {
  84. fmt.Fprintf(os.Stderr, "file from %s %s\n", change.From.Name,
  85. change.From.TreeEntry.Hash)
  86. } else {
  87. cache[change.From.TreeEntry.Hash], err = createDummyBlob(
  88. change.From.TreeEntry.Hash)
  89. }
  90. }
  91. }
  92. case merkletrie.Modify:
  93. blob, err = self.getBlob(&change.To, commit.File)
  94. if err != nil {
  95. fmt.Fprintf(os.Stderr, "file to %s\n", change.To.Name)
  96. } else {
  97. cache[change.To.TreeEntry.Hash] = blob
  98. newCache[change.To.TreeEntry.Hash] = blob
  99. }
  100. cache[change.From.TreeEntry.Hash], exists = self.cache[change.From.TreeEntry.Hash]
  101. if !exists {
  102. cache[change.From.TreeEntry.Hash], err = self.getBlob(&change.From, commit.File)
  103. if err != nil {
  104. fmt.Fprintf(os.Stderr, "file from %s\n", change.From.Name)
  105. }
  106. }
  107. }
  108. if err != nil {
  109. return nil, err
  110. }
  111. }
  112. self.cache = newCache
  113. return map[string]interface{}{DependencyBlobCache: cache}, nil
  114. }
  115. // The definition of a function which loads a git file by the specified path.
  116. // The state can be arbitrary though here it always corresponds to the currently processed
  117. // commit.
  118. type FileGetter func(path string) (*object.File, error)
  119. // Returns the blob which corresponds to the specified ChangeEntry.
  120. func (cache *BlobCache) getBlob(entry *object.ChangeEntry, fileGetter FileGetter) (
  121. *object.Blob, error) {
  122. blob, err := cache.repository.BlobObject(entry.TreeEntry.Hash)
  123. if err != nil {
  124. if err.Error() != plumbing.ErrObjectNotFound.Error() {
  125. fmt.Fprintf(os.Stderr, "getBlob(%s)\n", entry.TreeEntry.Hash.String())
  126. return nil, err
  127. }
  128. if entry.TreeEntry.Mode != 0160000 {
  129. // this is not a submodule
  130. return nil, err
  131. } else if cache.IgnoreMissingSubmodules {
  132. return createDummyBlob(entry.TreeEntry.Hash)
  133. }
  134. file, err_modules := fileGetter(".gitmodules")
  135. if err_modules != nil {
  136. return nil, err_modules
  137. }
  138. contents, err_modules := file.Contents()
  139. if err_modules != nil {
  140. return nil, err_modules
  141. }
  142. modules := config.NewModules()
  143. err_modules = modules.Unmarshal([]byte(contents))
  144. if err_modules != nil {
  145. return nil, err_modules
  146. }
  147. _, exists := modules.Submodules[entry.Name]
  148. if exists {
  149. // we found that this is a submodule
  150. return createDummyBlob(entry.TreeEntry.Hash)
  151. }
  152. return nil, err
  153. }
  154. return blob, nil
  155. }
  156. func init() {
  157. Registry.Register(&BlobCache{})
  158. }