languages.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. package plumbing
  2. import (
  3. "path"
  4. "gopkg.in/src-d/enry.v1"
  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. arr := [...]string{DependencyLanguages}
  29. return arr[:]
  30. }
  31. // Requires returns the list of names of entities which are needed by this PipelineItem.
  32. // Each requested entity will be inserted into `deps` of Consume(). In turn, those
  33. // entities are Provides() upstream.
  34. func (langs *LanguagesDetection) Requires() []string {
  35. arr := [...]string{DependencyTreeChanges, DependencyBlobCache}
  36. return arr[:]
  37. }
  38. // ListConfigurationOptions returns the list of changeable public properties of this PipelineItem.
  39. func (langs *LanguagesDetection) ListConfigurationOptions() []core.ConfigurationOption {
  40. return []core.ConfigurationOption{}
  41. }
  42. // Configure sets the properties previously published by ListConfigurationOptions().
  43. func (langs *LanguagesDetection) Configure(facts map[string]interface{}) error {
  44. if l, exists := facts[core.ConfigLogger].(core.Logger); exists {
  45. langs.l = l
  46. } else {
  47. langs.l = core.NewLogger()
  48. }
  49. return nil
  50. }
  51. // Initialize resets the temporary caches and prepares this PipelineItem for a series of Consume()
  52. // calls. The repository which is going to be analysed is supplied as an argument.
  53. func (langs *LanguagesDetection) Initialize(repository *git.Repository) error {
  54. langs.l = core.NewLogger()
  55. return nil
  56. }
  57. // Consume runs this PipelineItem on the next commit data.
  58. // `deps` contain all the results from upstream PipelineItem-s as requested by Requires().
  59. // Additionally, DependencyCommit is always present there and represents the analysed *object.Commit.
  60. // This function returns the mapping with analysis results. The keys must be the same as
  61. // in Provides(). If there was an error, nil is returned.
  62. func (langs *LanguagesDetection) Consume(deps map[string]interface{}) (map[string]interface{}, error) {
  63. changes := deps[DependencyTreeChanges].(object.Changes)
  64. cache := deps[DependencyBlobCache].(map[plumbing.Hash]*CachedBlob)
  65. result := map[plumbing.Hash]string{}
  66. for _, change := range changes {
  67. action, err := change.Action()
  68. if err != nil {
  69. return nil, err
  70. }
  71. switch action {
  72. case merkletrie.Insert:
  73. result[change.To.TreeEntry.Hash] = langs.detectLanguage(
  74. change.To.Name, cache[change.To.TreeEntry.Hash])
  75. case merkletrie.Delete:
  76. result[change.From.TreeEntry.Hash] = langs.detectLanguage(
  77. change.From.Name, cache[change.From.TreeEntry.Hash])
  78. case merkletrie.Modify:
  79. result[change.To.TreeEntry.Hash] = langs.detectLanguage(
  80. change.To.Name, cache[change.To.TreeEntry.Hash])
  81. result[change.From.TreeEntry.Hash] = langs.detectLanguage(
  82. change.From.Name, cache[change.From.TreeEntry.Hash])
  83. }
  84. }
  85. return map[string]interface{}{DependencyLanguages: result}, nil
  86. }
  87. // Fork clones this PipelineItem.
  88. func (langs *LanguagesDetection) Fork(n int) []core.PipelineItem {
  89. return core.ForkSamePipelineItem(langs, n)
  90. }
  91. // detectLanguage returns the programming language of a blob.
  92. func (langs *LanguagesDetection) detectLanguage(name string, blob *CachedBlob) string {
  93. _, err := blob.CountLines()
  94. if err == ErrorBinary {
  95. return ""
  96. }
  97. lang := enry.GetLanguage(path.Base(name), blob.Data)
  98. return lang
  99. }
  100. func init() {
  101. core.Registry.Register(&LanguagesDetection{})
  102. }