You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

graph.go 2.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // Copyright 2016 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 models
  5. import (
  6. "fmt"
  7. "strings"
  8. "code.gitea.io/git"
  9. "code.gitea.io/gitea/modules/setting"
  10. )
  11. // GraphItem represent one commit, or one relation in timeline
  12. type GraphItem struct {
  13. GraphAcii string
  14. Relation string
  15. Branch string
  16. Rev string
  17. Date string
  18. Author string
  19. AuthorEmail string
  20. ShortRev string
  21. Subject string
  22. OnlyRelation bool
  23. }
  24. // GraphItems is a list of commits from all branches
  25. type GraphItems []GraphItem
  26. // GetCommitGraph return a list of commit (GraphItems) from all branches
  27. func GetCommitGraph(r *git.Repository) (GraphItems, error) {
  28. var CommitGraph []GraphItem
  29. format := "DATA:|%d|%H|%ad|%an|%ae|%h|%s"
  30. graphCmd := git.NewCommand("log")
  31. graphCmd.AddArguments("--graph",
  32. "--date-order",
  33. "--all",
  34. "-C",
  35. "-M",
  36. fmt.Sprintf("-n %d", setting.UI.GraphMaxCommitNum),
  37. "--date=iso",
  38. fmt.Sprintf("--pretty=format:%s", format),
  39. )
  40. graph, err := graphCmd.RunInDir(r.Path)
  41. if err != nil {
  42. return CommitGraph, err
  43. }
  44. CommitGraph = make([]GraphItem, 0, 100)
  45. for _, s := range strings.Split(graph, "\n") {
  46. GraphItem, err := graphItemFromString(s, r)
  47. if err != nil {
  48. return CommitGraph, err
  49. }
  50. CommitGraph = append(CommitGraph, GraphItem)
  51. }
  52. return CommitGraph, nil
  53. }
  54. func graphItemFromString(s string, r *git.Repository) (GraphItem, error) {
  55. var ascii string
  56. var data = "|||||||"
  57. lines := strings.SplitN(s, "DATA:", 2)
  58. switch len(lines) {
  59. case 1:
  60. ascii = lines[0]
  61. case 2:
  62. ascii = lines[0]
  63. data = lines[1]
  64. default:
  65. return GraphItem{}, fmt.Errorf("Failed parsing grap line:%s. Expect 1 or two fields", s)
  66. }
  67. rows := strings.SplitN(data, "|", 8)
  68. if len(rows) < 8 {
  69. return GraphItem{}, fmt.Errorf("Failed parsing grap line:%s - Should containt 8 datafields", s)
  70. }
  71. /* // see format in getCommitGraph()
  72. 0 Relation string
  73. 1 Branch string
  74. 2 Rev string
  75. 3 Date string
  76. 4 Author string
  77. 5 AuthorEmail string
  78. 6 ShortRev string
  79. 7 Subject string
  80. */
  81. gi := GraphItem{ascii,
  82. rows[0],
  83. rows[1],
  84. rows[2],
  85. rows[3],
  86. rows[4],
  87. rows[5],
  88. rows[6],
  89. rows[7],
  90. len(rows[2]) == 0, // no commits referred to, only relation in current line.
  91. }
  92. return gi, nil
  93. }