Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

repo_attribute.go 2.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. )
  9. // CheckAttributeOpts represents the possible options to CheckAttribute
  10. type CheckAttributeOpts struct {
  11. CachedOnly bool
  12. AllAttributes bool
  13. Attributes []string
  14. Filenames []string
  15. }
  16. // CheckAttribute return the Blame object of file
  17. func (repo *Repository) CheckAttribute(opts CheckAttributeOpts) (map[string]map[string]string, error) {
  18. err := LoadGitVersion()
  19. if err != nil {
  20. return nil, fmt.Errorf("Git version missing: %v", err)
  21. }
  22. stdOut := new(bytes.Buffer)
  23. stdErr := new(bytes.Buffer)
  24. cmdArgs := []string{"check-attr", "-z"}
  25. if opts.AllAttributes {
  26. cmdArgs = append(cmdArgs, "-a")
  27. } else {
  28. for _, attribute := range opts.Attributes {
  29. if attribute != "" {
  30. cmdArgs = append(cmdArgs, attribute)
  31. }
  32. }
  33. }
  34. // git check-attr --cached first appears in git 1.7.8
  35. if opts.CachedOnly && CheckGitVersionAtLeast("1.7.8") == nil {
  36. cmdArgs = append(cmdArgs, "--cached")
  37. }
  38. cmdArgs = append(cmdArgs, "--")
  39. for _, arg := range opts.Filenames {
  40. if arg != "" {
  41. cmdArgs = append(cmdArgs, arg)
  42. }
  43. }
  44. cmd := NewCommand(cmdArgs...)
  45. if err := cmd.RunInDirPipeline(repo.Path, stdOut, stdErr); err != nil {
  46. return nil, fmt.Errorf("Failed to run check-attr: %v\n%s\n%s", err, stdOut.String(), stdErr.String())
  47. }
  48. fields := bytes.Split(stdOut.Bytes(), []byte{'\000'})
  49. if len(fields)%3 != 1 {
  50. return nil, fmt.Errorf("Wrong number of fields in return from check-attr")
  51. }
  52. var name2attribute2info = make(map[string]map[string]string)
  53. for i := 0; i < (len(fields) / 3); i++ {
  54. filename := string(fields[3*i])
  55. attribute := string(fields[3*i+1])
  56. info := string(fields[3*i+2])
  57. attribute2info := name2attribute2info[filename]
  58. if attribute2info == nil {
  59. attribute2info = make(map[string]string)
  60. }
  61. attribute2info[attribute] = info
  62. name2attribute2info[filename] = attribute2info
  63. }
  64. return name2attribute2info, nil
  65. }