This repository has been archived by the owner on Mar 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
versions.go
161 lines (147 loc) · 3.24 KB
/
versions.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
package pica
import (
"fmt"
"time"
"strings"
"errors"
"github.com/sergi/go-diff/diffmatchpatch"
"gopkg.in/src-d/go-git.v4"
"gopkg.in/src-d/go-git.v4/plumbing/object"
"gopkg.in/src-d/go-git.v4/utils/diff"
)
const (
Version = "0.0.1"
)
type VersionChange struct {
Commit *object.Commit
Diffs []diffmatchpatch.Diff
}
type VersionNote struct {
Changes []VersionChange
}
type ApiVersionController struct {
rep *git.Repository
FileName string
}
func NewApiVersionController(filename string) *ApiVersionController {
fmt.Print(filename)
r, err := git.PlainOpen(".")
if err != nil {
panic(errors.New(err.Error() + "please make sure you are under git root dir"))
}
return &ApiVersionController{
rep: r,
FileName: filename,
}
}
func (v *ApiVersionController) Commit(msg string) (string, error) {
w, err := v.rep.Worktree()
if err != nil {
return "", err
}
_, err = w.Add(v.FileName)
if err != nil {
return "", err
}
msg = fmt.Sprintf("[Pica] %s", msg)
hash, err := w.Commit(msg, &git.CommitOptions{
Author: &object.Signature{
Name: "jerloo",
Email: "[email protected]",
When: time.Now(),
},
})
if err != nil {
return "", err
}
return hash.String(), nil
}
func (v *ApiVersionController) Diff(src, dst string) []diffmatchpatch.Diff {
return diff.Do(src, dst)
}
func (v *ApiVersionController) GetCommits() ([]*object.Commit, error) {
ref, err := v.rep.Head()
if err != nil {
return nil, err
}
logs, err := v.rep.Log(&git.LogOptions{
From: ref.Hash(),
})
if err != nil {
return nil, err
}
var commits []*object.Commit
err = logs.ForEach(func(commit *object.Commit) error {
// find [Pica] and filename
tree, err := commit.Tree()
if err != nil {
return err
}
flag := false
// ... get the files iterator and print the file
tree.Files().ForEach(func(f *object.File) error {
if f.Name == v.FileName {
flag = true
return err
}
return nil
})
if flag && strings.Index(commit.Message, "[Pica]") > -1 {
commits = append(commits, commit)
}
return nil
})
if err != nil {
return nil, err
}
return commits, nil
}
func (v *ApiVersionController) Notes() (*VersionNote, error) {
vn := &VersionNote{}
commits, err := v.GetCommits()
if err != nil {
return nil, err
}
if len(commits) < 2 {
for _, commit := range commits {
commit.Message = strings.Replace(commit.Message, "[Pica]", "", -1)
vc := VersionChange{
Commit: commit,
}
vn.Changes = append(vn.Changes, vc)
}
}
for index := 0; index < len(commits)-1; index++ {
commit := commits[index]
getContent := func(commit *object.Commit) (string, error) {
content := ""
tree, err := commit.Tree()
if err != nil {
return "", err
}
// ... get the files iterator and print the file
tree.Files().ForEach(func(f *object.File) error {
if f.Name == v.FileName {
content, err = f.Contents()
return err
}
return nil
})
return content, nil
}
currentContext, err := getContent(commit)
if err != nil {
return nil, err
}
lastContent, err := getContent(commits[index+1])
if err != nil {
return nil, err
}
vc := VersionChange{
Commit: commit,
Diffs: v.Diff(currentContext, lastContent),
}
vn.Changes = append(vn.Changes, vc)
}
return vn, nil
}