languages.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. package plumbing
  2. import (
  3. "path"
  4. "github.com/src-d/enry/v2"
  5. "gopkg.in/src-d/go-git.v4/utils/merkletrie"
  6. "gopkg.in/src-d/go-git.v4"
  7. "gopkg.in/src-d/go-git.v4/plumbing"
  8. "gopkg.in/src-d/go-git.v4/plumbing/object"
  9. "gopkg.in/src-d/hercules.v10/internal/core"
  10. )
  11. // LanguagesDetection run programming language detection over the changed files.
  12. type LanguagesDetection struct {
  13. core.NoopMerger
  14. l core.Logger
  15. }
  16. const (
  17. // DependencyLanguages is the name of the dependency provided by LanguagesDetection.
  18. DependencyLanguages = "languages"
  19. )
  20. // Name of this PipelineItem. Uniquely identifies the type, used for mapping keys, etc.
  21. func (langs *LanguagesDetection) Name() string {
  22. return "LanguagesDetection"
  23. }
  24. // Provides returns the list of names of entities which are produced by this PipelineItem.
  25. // Each produced entity will be inserted into `deps` of dependent Consume()-s according
  26. // to this list. Also used by core.Registry to build the global map of providers.
  27. func (langs *LanguagesDetection) Provides() []string {
  28. return []string{DependencyLanguages}
  29. }
  30. // Requires returns the list of names of entities which are needed by this PipelineItem.
  31. // Each requested entity will be inserted into `deps` of Consume(). In turn, those
  32. // entities are Provides() upstream.
  33. func (langs *LanguagesDetection) Requires() []string {
  34. return []string{DependencyTreeChanges, DependencyBlobCache}
  35. }
  36. // ListConfigurationOptions returns the list of changeable public properties of this PipelineItem.
  37. func (langs *LanguagesDetection) ListConfigurationOptions() []core.ConfigurationOption {
  38. return []core.ConfigurationOption{}
  39. }
  40. // Configure sets the properties previously published by ListConfigurationOptions().
  41. func (langs *LanguagesDetection) Configure(facts map[string]interface{}) error {
  42. if l, exists := facts[core.ConfigLogger].(core.Logger); exists {
  43. langs.l = l
  44. }
  45. return nil
  46. }
  47. // Initialize resets the temporary caches and prepares this PipelineItem for a series of Consume()
  48. // calls. The repository which is going to be analysed is supplied as an argument.
  49. func (langs *LanguagesDetection) Initialize(repository *git.Repository) error {
  50. langs.l = core.NewLogger()
  51. return nil
  52. }
  53. // Consume runs this PipelineItem on the next commit data.
  54. // `deps` contain all the results from upstream PipelineItem-s as requested by Requires().
  55. // Additionally, DependencyCommit is always present there and represents the analysed *object.Commit.
  56. // This function returns the mapping with analysis results. The keys must be the same as
  57. // in Provides(). If there was an error, nil is returned.
  58. func (langs *LanguagesDetection) Consume(deps map[string]interface{}) (map[string]interface{}, error) {
  59. changes := deps[DependencyTreeChanges].(object.Changes)
  60. cache := deps[DependencyBlobCache].(map[plumbing.Hash]*CachedBlob)
  61. result := map[plumbing.Hash]string{}
  62. for _, change := range changes {
  63. action, err := change.Action()
  64. if err != nil {
  65. return nil, err
  66. }
  67. switch action {
  68. case merkletrie.Insert:
  69. result[change.To.TreeEntry.Hash] = langs.detectLanguage(
  70. change.To.Name, cache[change.To.TreeEntry.Hash])
  71. case merkletrie.Delete:
  72. result[change.From.TreeEntry.Hash] = langs.detectLanguage(
  73. change.From.Name, cache[change.From.TreeEntry.Hash])
  74. case merkletrie.Modify:
  75. result[change.To.TreeEntry.Hash] = langs.detectLanguage(
  76. change.To.Name, cache[change.To.TreeEntry.Hash])
  77. result[change.From.TreeEntry.Hash] = langs.detectLanguage(
  78. change.From.Name, cache[change.From.TreeEntry.Hash])
  79. }
  80. }
  81. return map[string]interface{}{DependencyLanguages: result}, nil
  82. }
  83. // Fork clones this PipelineItem.
  84. func (langs *LanguagesDetection) Fork(n int) []core.PipelineItem {
  85. return core.ForkSamePipelineItem(langs, n)
  86. }
  87. // detectLanguage returns the programming language of a blob.
  88. func (langs *LanguagesDetection) detectLanguage(name string, blob *CachedBlob) string {
  89. _, err := blob.CountLines()
  90. if err == ErrorBinary {
  91. return ""
  92. }
  93. lang := enry.GetLanguage(path.Base(name), blob.Data)
  94. return lang
  95. }
  96. func init() {
  97. core.Registry.Register(&LanguagesDetection{})
  98. }