-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathgit.go
More file actions
168 lines (144 loc) · 3.89 KB
/
git.go
File metadata and controls
168 lines (144 loc) · 3.89 KB
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
164
165
166
167
168
package main
import (
"bytes"
"fmt"
"io/fs"
"log"
"os"
"os/exec"
"path/filepath"
"strings"
)
const (
DIRMODE = 0755
)
type cmdInterface interface {
command(dir, name string, args ...string) (string, error)
}
type OsCmd struct {
}
func (c *OsCmd) command(dir, name string, args ...string) (string, error) {
cmd := exec.Command(name, args...)
var stdout, stderr bytes.Buffer
cmd.Dir = dir
cmd.Stdout = &stdout
cmd.Stderr = &stderr
err := cmd.Run()
if err != nil {
fmt.Printf("cmd.Run() failed with %s\n", err)
return stderr.String(), err
}
return strings.TrimSpace(stdout.String()), nil
}
type ioInterface interface {
ReadFile(filename string) ([]byte, error)
WriteFile(filename string, data []byte, perm fs.FileMode) error
}
type RealIO struct {
}
func (io RealIO) ReadFile(filename string) ([]byte, error) {
return os.ReadFile(filename)
}
func (io RealIO) WriteFile(filename string, data []byte, perm fs.FileMode) error {
return os.WriteFile(filename, data, perm)
}
type gitInterface interface {
getCurrentVersion() string
getLastRelease(fileName string) (string, bool)
diff(last, curr string, inc bool) []GitFileInfo
isAncestor(last, curr string) bool
makeRelease(verPath, version, curr string)
getRepoDir() string
}
type Git struct {
io ioInterface
cmd cmdInterface
path string
}
func (git Git) getRepoDir() string {
if git.path != "" {
return git.path
}
return "."
}
func (git Git) doGit(args ...string) {
fmt.Println(" > execute", "git", args)
s, err := git.cmd.command(git.path, "git", args...)
if err != nil {
fmt.Println("FATAL", args, "???", s, "???")
panic(err)
}
}
func (git Git) isAncestor(last, curr string) bool {
return exec.Command("git", "merge-base", "--is-ancestor", last, curr).Run() != nil
}
func (git Git) getFirstCommit() string {
first, _ := git.cmd.command(git.getRepoDir(), "git", "rev-list", "--max-parents=0", "HEAD")
return first
}
func (git Git) getLastRelease(fileName string) (string, bool) {
dat, err := git.io.ReadFile(fileName)
if err == nil {
last := string(dat)
fmt.Printf(" last commit: %s\n", last)
return last, false
}
first := git.getFirstCommit()
fmt.Printf(" first commit: %s\n", first)
return first, true
}
func (git Git) getCurrentVersion() string {
curr, err := git.cmd.command(git.getRepoDir(), "git", "rev-parse", "HEAD")
if err != nil {
log.Fatalf("cmd.Run() failed with %s, error text: %s\n", err, curr)
}
return curr
}
type GitFileInfo struct {
mode string
fileName string
targetFileName string
}
const DIFFLINECOUNT = 2
func makeGitFileInfo(p []string) GitFileInfo {
if len(p) == DIFFLINECOUNT {
return GitFileInfo{mode: p[0], fileName: p[1]}
}
return GitFileInfo{mode: p[0], fileName: p[1], targetFileName: p[2]}
}
func makeGitFileInfos(str string) []GitFileInfo {
files := make([]GitFileInfo, 0)
for _, s := range strings.Split(str, "\n") {
if s == "" {
continue
}
files = append(files, makeGitFileInfo(strings.Split(s, "\t")))
}
return files
}
func (git Git) diff(last, curr string, inc bool) []GitFileInfo {
arr := make([]GitFileInfo, 0)
if last == curr && !inc {
return arr
}
if inc {
str, _ := git.cmd.command(git.getRepoDir(), "git", "show", "--pretty=format:", "--name-status", last)
arr = append(arr, makeGitFileInfos(str)...)
}
str, _ := git.cmd.command(git.getRepoDir(), "git", "diff", "--name-status", last+".."+curr)
return append(arr, makeGitFileInfos(str)...)
}
func (git Git) makeRelease(verPath, version, curr string) {
fmt.Println("=> make release")
fmt.Println(" > saved last_commit")
err := git.io.WriteFile(filepath.Join(git.getRepoDir(), "last_commit"), []byte(curr), DIRMODE)
if err != nil {
panic(err)
}
git.doGit("add", filepath.Join("src", verPath))
git.doGit("add", "last_commit")
git.doGit("commit", "-m", "version "+version)
git.doGit("tag", "changeset_"+curr)
git.doGit("tag", "v"+version)
git.doGit("push", "--tags", "origin", "master")
}