-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathGitFormatter.go
93 lines (83 loc) · 2.9 KB
/
GitFormatter.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
package git
import (
"encoding/json"
"strconv"
"strings"
"time"
)
const _dl_ = "devtron_delimiter"
// GITFORMAT Refer git official doc for supported placeholders to add new fields
// Need to make sure the output does not break the json structure which is ensured
// by having the _dl_ delimiter which is later replaced by quotes
var GITFORMAT = "--pretty=format:{" +
_dl_ + "commit" + _dl_ + ":" + _dl_ + "%H" + _dl_ + "," +
_dl_ + "parent" + _dl_ + ":" + _dl_ + "%P" + _dl_ + "," +
_dl_ + "refs" + _dl_ + ":" + _dl_ + "%D" + _dl_ + "," +
_dl_ + "subject" + _dl_ + ":" + _dl_ + "%<(1024,trunc)%s" + _dl_ + "," +
_dl_ + "body" + _dl_ + ":" + _dl_ + "%<(1024,trunc)%b" + _dl_ + "," +
_dl_ + "author" + _dl_ +
":{" +
_dl_ + "name" + _dl_ + ":" + _dl_ + "%aN" + _dl_ + "," +
_dl_ + "email" + _dl_ + ":" + _dl_ + "%aE" + _dl_ + "," +
_dl_ + "date" + _dl_ + ":" + _dl_ + "%ad" + _dl_ +
"}," +
_dl_ + "commiter" + _dl_ +
":{" +
_dl_ + "name" + _dl_ + ":" + _dl_ + "%cN" + _dl_ + "," +
_dl_ + "email" + _dl_ + ":" + _dl_ + "%cE" + _dl_ + "," +
_dl_ + "date" + _dl_ + ":" + _dl_ + "%cd" + _dl_ +
"}},"
type GitPerson struct {
Name string `json:"name"`
Email string `json:"email"`
Date time.Time `json:"date"`
}
type GitCommitFormat struct {
Commit string `json:"commit"`
Parent string `json:"parent"`
Refs string `json:"refs"`
Subject string `json:"subject"`
Commiter GitPerson `json:"commiter"`
Author GitPerson `json:"author"`
Body string `json:"body"`
}
func parseFormattedLogOutput(out string) ([]GitCommitFormat, error) {
//remove the new line character which is after each terminal comma
out = strings.ReplaceAll(out, "},\n", "},")
// to escape the special characters like quotes and newline characters in the commit data
var sb strings.Builder
buffer := strconv.AppendQuote(make([]byte, 0, len(out)), out)
sb.Write(buffer)
logOut := sb.String()
//replace the delimiter with quotes to make it parsable json
logOut = strings.ReplaceAll(logOut, _dl_, `"`)
logOut = logOut[1 : len(logOut)-2] // trim surround characters (surrounding quotes and trailing comma)
logOut = strings.Join([]string{"[", "]"}, logOut)
var gitCommitFormattedList []GitCommitFormat
err := json.Unmarshal([]byte(logOut), &gitCommitFormattedList)
if err != nil {
return nil, err
}
return gitCommitFormattedList, nil
}
func (formattedCommit GitCommitFormat) transformToCommit() *Commit {
return &Commit{
Hash: &Hash{
Long: formattedCommit.Commit,
},
Author: &Author{
Name: formattedCommit.Author.Name,
Email: formattedCommit.Author.Email,
Date: formattedCommit.Author.Date,
},
Committer: &Committer{
Name: formattedCommit.Commiter.Name,
Email: formattedCommit.Commiter.Email,
Date: formattedCommit.Commiter.Date,
},
Tag: &Tag{},
Tree: &Tree{},
Subject: strings.TrimSpace(formattedCommit.Subject),
Body: strings.TrimSpace(formattedCommit.Body),
}
}