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