-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgitrepo.go
112 lines (84 loc) · 2.08 KB
/
gitrepo.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
package main
import (
"encoding/json"
"log"
"mime"
"net/http"
"strconv"
"time"
git "github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing/object"
)
func GitRepo(w http.ResponseWriter, req *http.Request) {
type GitRepo struct {
Path string `json:"path"`
Repeat string `json:"repeat"`
Sleep string `json:"sleep`
}
type ResponseId struct {
Id int `json:"id"`
}
contentType := req.Header.Get("Content-Type")
mediatype, _, err := mime.ParseMediaType(contentType)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
if mediatype != "application/json" {
http.Error(w, "expect application/json Content-Type", http.StatusUnsupportedMediaType)
return
}
dec := json.NewDecoder(req.Body)
dec.UseNumber()
dec.DisallowUnknownFields()
var gr GitRepo
if err := dec.Decode(&gr); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
var execTime time.Duration
startTime := time.Now()
repeat, err := strconv.Atoi(gr.Repeat)
sleep, err := strconv.Atoi(gr.Sleep)
_, _, _ = getLog(gr.Path, repeat, sleep)
stopTime := time.Now()
executionTime := stopTime.Sub(startTime)
execTime = executionTime
type response struct {
Path string `json:"path"`
Repeat int `json:"repeat"`
Sleep int `json:"sleep"`
ExecutionTime time.Duration `json:"execution_time (nanoseconds)"`
}
res := response{
Path: gr.Path,
Repeat: repeat,
Sleep: sleep,
ExecutionTime: execTime,
}
json.NewEncoder(w).Encode(res)
}
func getLog(path string, repeat int, sleep int) (string, int, int) {
if repeat == 0 {
repeat = 1
}
for i := 1; i <= repeat; i++ {
r, err := git.PlainOpen(path)
if err != nil {
log.Printf("%+v\n", err)
}
ref, err := r.Head()
cIter, err := r.Log(&git.LogOptions{From: ref.Hash()})
var cCount int
err = cIter.ForEach(func(c *object.Commit) error {
cCount++
return nil
})
// r = nil
if repeat == 1 {
} else {
time.Sleep(time.Duration(sleep) * time.Second)
}
}
return path, repeat, sleep
}