languages.go 3.9 KB

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