This repository has been archived by the owner on Mar 7, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
git.go
111 lines (85 loc) · 2.47 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
package main
import (
"fmt"
"strings"
"os/exec"
"io/ioutil"
"regexp"
"code.google.com/p/go.text/encoding/japanese"
"code.google.com/p/go.text/transform"
"gopkg.in/fatih/set.v0"
)
func getPackageNamesOfCommitMessage() ([]string, error) {
msg, err := getLastCommitMessage()
if err != nil {
return nil, err
}
if pattern, err := regexp.Compile(`\[([A-Za-z0-9<>_\.-]+)\]`); err != nil {
return nil, err
} else {
matches := pattern.FindAllStringSubmatch(msg, -1)
names := []string{}
for _, pair := range(matches) {
names = append(names, pair[1])
}
return names, nil
}
}
func getLastCommitMessage() (string, error) {
cmd := exec.Command("git", "log", "-1", "--pretty=format:%s")
if msg, err := getCommandStdout(cmd); err != nil {
return "", err
} else {
return msg, nil
}
}
func getChangedRootDirs(depth int) ([]string, error) {
paths, err := getChangedFilePaths(depth)
if err != nil {
return nil, err
}
pattern, err := regexp.Compile("([^/]*)/")
if err != nil {
return nil, err
}
dirs := set.New()
for _, path := range(paths) {
matches := pattern.FindStringSubmatch(path)
if len(matches) >= 2 {
dirs.Add(matches[1])
}
}
return set.StringSlice(dirs), nil
}
func getChangedFilePaths(depth int) ([]string, error) {
if changes, err := getChanges(depth); err != nil {
return nil, err
} else {
lines := strings.Split(changes, "\n")
return lines, nil
}
}
func getChanges(depth int) (string, error) {
rev := fmt.Sprintf("HEAD~%d", depth)
cmd := exec.Command("git", "diff", "--name-only", rev)
return getCommandStdout(cmd)
}
func getCommandStdout(cmd *exec.Cmd) (string, error) {
stdoutpipe, err := cmd.StdoutPipe()
if err != nil {
return "", err
}
defer stdoutpipe.Close()
if err = cmd.Start(); err != nil {
return "", err
}
stdout, err := ioutil.ReadAll(
transform.NewReader(stdoutpipe, japanese.ShiftJIS.NewDecoder()))
if err != nil {
return "", err
}
if err = cmd.Wait(); err != nil {
return "", err
}
return string(stdout), nil
}