blob_cache.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. package hercules
  2. import (
  3. "log"
  4. "gopkg.in/src-d/go-git.v4"
  5. "gopkg.in/src-d/go-git.v4/config"
  6. "gopkg.in/src-d/go-git.v4/plumbing"
  7. "gopkg.in/src-d/go-git.v4/plumbing/object"
  8. "gopkg.in/src-d/go-git.v4/utils/merkletrie"
  9. )
  10. // BlobCache loads the blobs which correspond to the changed files in a commit.
  11. // It is a PipelineItem.
  12. // It must provide the old and the new objects; "blobCache" rotates and allows to not load
  13. // the same blobs twice. Outdated objects are removed so "blobCache" 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 is the name of the configuration option for
  24. // BlobCache.Configure() to not check if the referenced submodules exist.
  25. ConfigBlobCacheIgnoreMissingSubmodules = "BlobCache.IgnoreMissingSubmodules"
  26. // DependencyBlobCache identifies the dependency provided by BlobCache.
  27. DependencyBlobCache = "blob_cache"
  28. )
  29. // Name of this PipelineItem. Uniquely identifies the type, used for mapping keys, etc.
  30. func (blobCache *BlobCache) Name() string {
  31. return "BlobCache"
  32. }
  33. // Provides returns the list of names of entities which are produced by this PipelineItem.
  34. // Each produced entity will be inserted into `deps` of dependent Consume()-s according
  35. // to this list. Also used by hercules.Registry to build the global map of providers.
  36. func (blobCache *BlobCache) Provides() []string {
  37. arr := [...]string{DependencyBlobCache}
  38. return arr[:]
  39. }
  40. // Requires returns the list of names of entities which are needed by this PipelineItem.
  41. // Each requested entity will be inserted into `deps` of Consume(). In turn, those
  42. // entities are Provides() upstream.
  43. func (blobCache *BlobCache) Requires() []string {
  44. arr := [...]string{DependencyTreeChanges}
  45. return arr[:]
  46. }
  47. // ListConfigurationOptions returns the list of changeable public properties of this PipelineItem.
  48. func (blobCache *BlobCache) ListConfigurationOptions() []ConfigurationOption {
  49. options := [...]ConfigurationOption{{
  50. Name: ConfigBlobCacheIgnoreMissingSubmodules,
  51. Description: "Specifies whether to panic if some referenced submodules do not exist and thus" +
  52. " the corresponding Git objects cannot be loaded. Override this if you know that the " +
  53. "history is dirty and you want to get things done.",
  54. Flag: "ignore-missing-submodules",
  55. Type: BoolConfigurationOption,
  56. Default: false}}
  57. return options[:]
  58. }
  59. // Configure sets the properties previously published by ListConfigurationOptions().
  60. func (blobCache *BlobCache) Configure(facts map[string]interface{}) {
  61. if val, exists := facts[ConfigBlobCacheIgnoreMissingSubmodules].(bool); exists {
  62. blobCache.IgnoreMissingSubmodules = val
  63. }
  64. }
  65. // Initialize resets the temporary caches and prepares this PipelineItem for a series of Consume()
  66. // calls. The repository which is going to be analysed is supplied as an argument.
  67. func (blobCache *BlobCache) Initialize(repository *git.Repository) {
  68. blobCache.repository = repository
  69. blobCache.cache = map[plumbing.Hash]*object.Blob{}
  70. }
  71. // Consume runs this PipelineItem on the next commit data.
  72. // `deps` contain all the results from upstream PipelineItem-s as requested by Requires().
  73. // Additionally, "commit" is always present there and represents the analysed *object.Commit.
  74. // This function returns the mapping with analysis results. The keys must be the same as
  75. // in Provides(). If there was an error, nil is returned.
  76. func (blobCache *BlobCache) Consume(deps map[string]interface{}) (map[string]interface{}, error) {
  77. commit := deps["commit"].(*object.Commit)
  78. changes := deps[DependencyTreeChanges].(object.Changes)
  79. cache := map[plumbing.Hash]*object.Blob{}
  80. newCache := map[plumbing.Hash]*object.Blob{}
  81. for _, change := range changes {
  82. action, err := change.Action()
  83. if err != nil {
  84. log.Printf("no action in %s\n", change.To.TreeEntry.Hash)
  85. return nil, err
  86. }
  87. var exists bool
  88. var blob *object.Blob
  89. switch action {
  90. case merkletrie.Insert:
  91. blob, err = blobCache.getBlob(&change.To, commit.File)
  92. if err != nil {
  93. log.Printf("file to %s %s\n", change.To.Name, change.To.TreeEntry.Hash)
  94. } else {
  95. cache[change.To.TreeEntry.Hash] = blob
  96. newCache[change.To.TreeEntry.Hash] = blob
  97. }
  98. case merkletrie.Delete:
  99. cache[change.From.TreeEntry.Hash], exists = blobCache.cache[change.From.TreeEntry.Hash]
  100. if !exists {
  101. cache[change.From.TreeEntry.Hash], err = blobCache.getBlob(&change.From, commit.File)
  102. if err != nil {
  103. if err.Error() != plumbing.ErrObjectNotFound.Error() {
  104. log.Printf("file from %s %s\n", change.From.Name,
  105. change.From.TreeEntry.Hash)
  106. } else {
  107. cache[change.From.TreeEntry.Hash], err = createDummyBlob(
  108. change.From.TreeEntry.Hash)
  109. }
  110. }
  111. }
  112. case merkletrie.Modify:
  113. blob, err = blobCache.getBlob(&change.To, commit.File)
  114. if err != nil {
  115. log.Printf("file to %s\n", change.To.Name)
  116. } else {
  117. cache[change.To.TreeEntry.Hash] = blob
  118. newCache[change.To.TreeEntry.Hash] = blob
  119. }
  120. cache[change.From.TreeEntry.Hash], exists = blobCache.cache[change.From.TreeEntry.Hash]
  121. if !exists {
  122. cache[change.From.TreeEntry.Hash], err = blobCache.getBlob(&change.From, commit.File)
  123. if err != nil {
  124. log.Printf("file from %s\n", change.From.Name)
  125. }
  126. }
  127. }
  128. if err != nil {
  129. return nil, err
  130. }
  131. }
  132. blobCache.cache = newCache
  133. return map[string]interface{}{DependencyBlobCache: cache}, nil
  134. }
  135. // FileGetter defines a function which loads the Git file by the specified path.
  136. // The state can be arbitrary though here it always corresponds to the currently processed
  137. // commit.
  138. type FileGetter func(path string) (*object.File, error)
  139. // Returns the blob which corresponds to the specified ChangeEntry.
  140. func (blobCache *BlobCache) getBlob(entry *object.ChangeEntry, fileGetter FileGetter) (
  141. *object.Blob, error) {
  142. blob, err := blobCache.repository.BlobObject(entry.TreeEntry.Hash)
  143. if err != nil {
  144. if err.Error() != plumbing.ErrObjectNotFound.Error() {
  145. log.Printf("getBlob(%s)\n", entry.TreeEntry.Hash.String())
  146. return nil, err
  147. }
  148. if entry.TreeEntry.Mode != 0160000 {
  149. // this is not a submodule
  150. return nil, err
  151. } else if blobCache.IgnoreMissingSubmodules {
  152. return createDummyBlob(entry.TreeEntry.Hash)
  153. }
  154. file, errModules := fileGetter(".gitmodules")
  155. if errModules != nil {
  156. return nil, errModules
  157. }
  158. contents, errModules := file.Contents()
  159. if errModules != nil {
  160. return nil, errModules
  161. }
  162. modules := config.NewModules()
  163. errModules = modules.Unmarshal([]byte(contents))
  164. if errModules != nil {
  165. return nil, errModules
  166. }
  167. _, exists := modules.Submodules[entry.Name]
  168. if exists {
  169. // we found that this is a submodule
  170. return createDummyBlob(entry.TreeEntry.Hash)
  171. }
  172. return nil, err
  173. }
  174. return blob, nil
  175. }
  176. func init() {
  177. Registry.Register(&BlobCache{})
  178. }