-
Notifications
You must be signed in to change notification settings - Fork 4
/
git.go
58 lines (53 loc) · 1.3 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
package main
import (
"bytes"
"fmt"
"os/exec"
)
func git(dir string, args ...string) (string, error) {
cmd := exec.Command("git", args...)
var stdout bytes.Buffer
var stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
cmd.Dir = dir
if *verbose {
fmt.Println(cmd.String())
}
if err := cmd.Run(); err != nil {
return "", fmt.Errorf(stderr.String())
}
return stdout.String(), nil
}
// fetchGitRepository will fetch latest commits and tags
func fetchGitRepository() error {
_, err := git(*source, "fetch", "--all")
if err != nil {
return err
}
return nil
}
// getGitTags will query all git tags with created time and subject
func getGitTags() (string, error) {
tags, err := git(*source, "tag", "-n", "-l", "--sort=creatordate", "--format", "%(refname:short);%(creatordate:short);%(subject)")
if err != nil {
return "", err
}
return tags, nil
}
// getGitLogs will query commit records between two tags
func getGitLogs(tag1, tag2 string) (string, error) {
var notation string
if len(tag1) > 0 && len(tag2) > 0 {
notation = fmt.Sprintf("%s..%s", tag1, tag2)
} else if len(tag1) > 0 {
notation = tag1
} else if len(tag2) > 0 {
notation = tag2
}
commits, err := git(*source, "log", "--no-merges", "--format=oneline", notation)
if err != nil {
return "", err
}
return commits, nil
}