blob_cache.go 3.3 KB

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