本站源代码
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.

111 lines
2.4KB

  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/gitea/modules/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, page int) (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. fmt.Sprintf("--skip=%d", setting.UI.GraphMaxCommitNum*(page-1)),
  38. "--date=iso",
  39. fmt.Sprintf("--pretty=format:%s", format),
  40. )
  41. graph, err := graphCmd.RunInDir(r.Path)
  42. if err != nil {
  43. return CommitGraph, err
  44. }
  45. CommitGraph = make([]GraphItem, 0, 100)
  46. for _, s := range strings.Split(graph, "\n") {
  47. GraphItem, err := graphItemFromString(s, r)
  48. if err != nil {
  49. return CommitGraph, err
  50. }
  51. CommitGraph = append(CommitGraph, GraphItem)
  52. }
  53. return CommitGraph, nil
  54. }
  55. func graphItemFromString(s string, r *git.Repository) (GraphItem, error) {
  56. var ascii string
  57. var data = "|||||||"
  58. lines := strings.SplitN(s, "DATA:", 2)
  59. switch len(lines) {
  60. case 1:
  61. ascii = lines[0]
  62. case 2:
  63. ascii = lines[0]
  64. data = lines[1]
  65. default:
  66. return GraphItem{}, fmt.Errorf("Failed parsing grap line:%s. Expect 1 or two fields", s)
  67. }
  68. rows := strings.SplitN(data, "|", 8)
  69. if len(rows) < 8 {
  70. return GraphItem{}, fmt.Errorf("Failed parsing grap line:%s - Should containt 8 datafields", s)
  71. }
  72. /* // see format in getCommitGraph()
  73. 0 Relation string
  74. 1 Branch string
  75. 2 Rev string
  76. 3 Date string
  77. 4 Author string
  78. 5 AuthorEmail string
  79. 6 ShortRev string
  80. 7 Subject string
  81. */
  82. gi := GraphItem{ascii,
  83. rows[0],
  84. rows[1],
  85. rows[2],
  86. rows[3],
  87. rows[4],
  88. rows[5],
  89. rows[6],
  90. rows[7],
  91. len(rows[2]) == 0, // no commits referred to, only relation in current line.
  92. }
  93. return gi, nil
  94. }
上海开阖软件有限公司 沪ICP备12045867号-1