-
Notifications
You must be signed in to change notification settings - Fork 7
/
git-hours.go
102 lines (99 loc) · 2.36 KB
/
git-hours.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
package main
import (
"bytes"
"flag"
"fmt"
"os"
"os/exec"
"strings"
"time"
)
func main() {
since, before := beforeMonth()
sincePtr := flag.String("since", since+" 00:00:00 "+timeZoneOffset(), "since(after) date")
beforePtr := flag.String("before", before+" 23:59:59 "+timeZoneOffset(), "before date")
authorPtr := flag.String("author", "", "author name") // git option : --author="\(Adam\)\|\(Jon\)"
durationPtr := flag.String("duration", "1h", "git log duration") // default "1h"
debugPtr := flag.Bool("debug", false, "debug mode")
helpPtr := flag.Bool("help", false, "print help")
flag.Parse()
if *helpPtr {
flag.PrintDefaults()
os.Exit(0)
}
//checkMultiname
var author string
if strings.Contains(*authorPtr, ",") {
author += `\(`
author += strings.Join(strings.Split(*authorPtr, ","), `\)\|\(`)
author += `\)`
} else {
author = *authorPtr
}
cmd := exec.Command(
"git",
"--no-pager",
"log",
"--reverse",
"--date=iso-local",
`--pretty=format:%ad %an %s`,
fmt.Sprintf(`--author=%s`, author),
fmt.Sprintf(`--since="%s"`, *sincePtr),
fmt.Sprintf(`--before="%s"`, *beforePtr),
)
var stdout bytes.Buffer
var stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
err := cmd.Run()
if stderr.String() != "" {
fmt.Fprintf(os.Stderr, stderr.String())
os.Exit(1)
}
if err != nil {
fmt.Fprintf(os.Stderr, stderr.String())
os.Exit(1)
}
total, err := time.ParseDuration("0h")
if err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
os.Exit(1)
}
if stdout.String() == "" {
fmt.Printf("From %q to %q : %s\n", *sincePtr, *beforePtr, total)
os.Exit(0)
}
var beforeCommitTime time.Time
for n, l := range strings.Split(stdout.String(), "\n") {
getTime := findISO8601.FindString(l)
if getTime == "" {
continue
}
rfctime, err := ISO8601ToRFC3339(getTime)
if err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
}
t, err := time.Parse(time.RFC3339, rfctime)
if err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
}
elapsed := t.Sub(beforeCommitTime)
if *debugPtr {
if n != 0 {
fmt.Println(elapsed, ">")
}
fmt.Println("\t", l)
}
h, err := time.ParseDuration(*durationPtr)
if err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
}
if elapsed < h*2 {
total += elapsed
} else {
total += h
}
beforeCommitTime = t
}
fmt.Printf("From %q to %q : %s\n", *sincePtr, *beforePtr, total)
}