blob_cache.go 7.1 KB

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