-
Notifications
You must be signed in to change notification settings - Fork 1
/
gitlog.go
71 lines (60 loc) · 1.53 KB
/
gitlog.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
package main
import (
"bytes"
"fmt"
"github.com/spf13/viper"
"os"
"os/exec"
"regexp"
"strconv"
"strings"
"github.com/pkg/errors"
)
func mergedPrs() ([]int, error) {
commitArg := fmt.Sprintf("%s..%s", fromCommit, toCommit)
var cmd *exec.Cmd
if local := viper.GetBool("local-only"); local {
cmd = exec.Command("git", "log", "--oneline", commitArg)
} else {
gitDirArg := fmt.Sprintf("--git-dir=%s/src/github.com/%s/.git", os.Getenv("GOPATH"), repo)
cmd = exec.Command("git", gitDirArg, "log", "--oneline", commitArg)
}
var out bytes.Buffer
cmd.Stdout = &out
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
return nil, errors.Wrap(err, "failed to list local git history")
}
gitLog := string(out.Bytes())
commits := strings.Split(gitLog, "\n")
var re = regexp.MustCompile(`#\d+`)
var prs []int
for _, match := range re.FindAllString(gitLog, -1) {
prNoStr := strings.Trim(match, "#")
prNo, err := strconv.Atoi(prNoStr)
if err != nil {
continue
}
if findRevertCommit(commits, match) {
continue
}
prs = append(prs, prNo)
}
return prs, nil
}
func findRevertCommit(commits []string, prHash string) bool {
for _, c := range commits {
if hasRevertPrefix(c) && strings.Contains(c, prHash) {
return true
}
}
return false
}
// hasRevertPrefix checks if the commit message (which starts at the 8th character)
/// starts with 'Revert' which may be an indication that it is a PR revert commit.
func hasRevertPrefix(msg string) bool {
if len(msg) < 14 {
return false
}
return msg[8:14] == "Revert"
}