Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

repo_attribute.go 2.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. // Copyright 2019 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package git
  5. import (
  6. "bytes"
  7. "fmt"
  8. "github.com/mcuadros/go-version"
  9. )
  10. // CheckAttributeOpts represents the possible options to CheckAttribute
  11. type CheckAttributeOpts struct {
  12. CachedOnly bool
  13. AllAttributes bool
  14. Attributes []string
  15. Filenames []string
  16. }
  17. // CheckAttribute return the Blame object of file
  18. func (repo *Repository) CheckAttribute(opts CheckAttributeOpts) (map[string]map[string]string, error) {
  19. binVersion, err := BinVersion()
  20. if err != nil {
  21. return nil, fmt.Errorf("Git version missing: %v", err)
  22. }
  23. stdOut := new(bytes.Buffer)
  24. stdErr := new(bytes.Buffer)
  25. cmdArgs := []string{"check-attr", "-z"}
  26. if opts.AllAttributes {
  27. cmdArgs = append(cmdArgs, "-a")
  28. } else {
  29. for _, attribute := range opts.Attributes {
  30. if attribute != "" {
  31. cmdArgs = append(cmdArgs, attribute)
  32. }
  33. }
  34. }
  35. // git check-attr --cached first appears in git 1.7.8
  36. if opts.CachedOnly && version.Compare(binVersion, "1.7.8", ">=") {
  37. cmdArgs = append(cmdArgs, "--cached")
  38. }
  39. cmdArgs = append(cmdArgs, "--")
  40. for _, arg := range opts.Filenames {
  41. if arg != "" {
  42. cmdArgs = append(cmdArgs, arg)
  43. }
  44. }
  45. cmd := NewCommand(cmdArgs...)
  46. if err := cmd.RunInDirPipeline(repo.Path, stdOut, stdErr); err != nil {
  47. return nil, fmt.Errorf("Failed to run check-attr: %v\n%s\n%s", err, stdOut.String(), stdErr.String())
  48. }
  49. fields := bytes.Split(stdOut.Bytes(), []byte{'\000'})
  50. if len(fields)%3 != 1 {
  51. return nil, fmt.Errorf("Wrong number of fields in return from check-attr")
  52. }
  53. var name2attribute2info = make(map[string]map[string]string)
  54. for i := 0; i < (len(fields) / 3); i++ {
  55. filename := string(fields[3*i])
  56. attribute := string(fields[3*i+1])
  57. info := string(fields[3*i+2])
  58. attribute2info := name2attribute2info[filename]
  59. if attribute2info == nil {
  60. attribute2info = make(map[string]string)
  61. }
  62. attribute2info[attribute] = info
  63. name2attribute2info[filename] = attribute2info
  64. }
  65. return name2attribute2info, nil
  66. }