This repository has been archived by the owner on Dec 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgit.go
163 lines (128 loc) · 3.15 KB
/
git.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package main
import (
"github.com/go-martini/martini"
"gopkg.in/libgit2/git2go.v22"
"net/http"
"errors"
"strings"
"time"
)
type Commit struct {
Id string `json:"id"`
AuthorName string `json:"authorname"`
AuthorEmail string `json:"authoremail"`
Date time.Time `json:"date"`
Message string `json:"message"`
}
// Common function to open repository and create walker
// Need repository name
func getWalker(repName string) (walker *git.RevWalk, err error) {
var (
repPath string
)
if repName == "" {
return nil, errors.New("Empty repository")
}
// Remove leading slash
repPath = GitRoot + "/" + strings.TrimPrefix(repName, "/") + ".git"
// Create repository object
repo, err := git.OpenRepository(repPath)
if err != nil {
return nil, err
}
// Get repository walker
return repo.Walk()
}
// Get branch commits
func getBranchCommits(params martini.Params, req *http.Request) (int, string) {
var (
err error
commits []Commit
resp = &Response{
Success: true,
Results: &commits,
}
filter = NewFilter()
)
err = filter.SetProject(params["id"])
if err != nil {
resp.SetError(http.StatusBadRequest, "Project not available")
return resp.Compile()
}
err = filter.SetBranch(params["branch"])
if err != nil {
resp.SetError(http.StatusBadRequest, "Branch "+params["branch"]+" is not available")
return resp.Compile()
}
err = filter.ParseQuery(req.URL.Query())
// Check filter strict params
if err != nil {
resp.SetError(http.StatusBadRequest, err.Error())
return resp.Compile()
}
var (
commitCallback = makeRevCallback(&commits, filter)
)
// Get repository walker
walker, err := getWalker(filter.Project.Path)
if err != nil {
log.Error(err.Error())
resp.SetError(500, "Unable to open repository")
return resp.Compile()
}
// Set sorting
walker.Sorting(git.SortTime | (git.SortTime & git.SortReverse))
// set ref
err = walker.PushRef(filter.BranchRef())
if err != nil {
log.Error(err.Error())
resp.SetError(500, "Unable to set refs/heads/master")
return resp.Compile()
}
err = walker.Iterate(commitCallback)
if err != nil {
log.Error(err.Error())
resp.SetError(500, "Unable to walk through commits")
return resp.Compile()
}
return resp.Compile()
}
// Make revision iterator callback
func makeRevCallback(ret *[]Commit, filter *Filter) func(commit *git.Commit) bool {
return func(commit *git.Commit) bool {
var (
c = Commit{
Id: commit.Id().String(),
Message: commit.Message(),
AuthorName: commit.Author().Name,
AuthorEmail: commit.Author().Email,
Date: commit.Author().When,
}
)
// Count parent
if !filter.Merges && commit.ParentCount() > 1 {
return true
}
// Check Author
if filter.Authors != nil && filter.IsAuthor(commit.Author()) == false {
return true
}
// Grep message
if filter.IsMessage(commit.Message()) == false {
return true
}
// Check since date
if filter.IsSince(c.Date) == false || filter.IsTill(c.Date) == false {
return true
}
switch filter.IsPage() {
case -1:
return true
case 1:
return false
default:
*ret = append(*ret, c)
}
return true
}
}