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.2KB

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