blob_cache.go 3.1 KB

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