/
git-whowhat.go
144 lines (119 loc) · 2.83 KB
/
git-whowhat.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
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
package main
import (
"bufio"
"flag"
"fmt"
"github.com/holygeek/piper"
"log"
"os"
"sort"
"strings"
)
const WHO = " WHO:"
func main() {
type Author string
type Filename string
type AuthorSet map[Author]bool
flag.Usage = showHelp
help := flag.Bool("h", false, "Show usage")
debug := flag.Bool("d", false, "Debug")
flag.Parse()
if *help {
showHelp()
return
}
var currAuthor Author
whatwho := make(map[Filename]AuthorSet)
lineScanner := runGitLog(debug)
for lineScanner.Scan() {
line := lineScanner.Text()
if line == "" {
continue
}
if isAuthorLine(&line) {
currAuthor = Author(line[len(WHO):])
continue
}
fname := Filename(line)
if authorSet, ok := whatwho[fname]; ok {
authorSet[currAuthor] = true
} else {
whatwho[fname] = AuthorSet{currAuthor: true}
}
}
authorsAndFiles := make(map[string][]string)
for fname, authorSet := range whatwho {
authors := make([]string, 0)
for author, _ := range authorSet {
authors = append(authors, string(author))
}
sort.Strings(authors)
key := strings.Join(authors, ",")
if filelist, ok := authorsAndFiles[key]; ok {
authorsAndFiles[key] = append(filelist, string(fname))
} else {
authorsAndFiles[key] = []string{string(fname)}
}
}
//l := len(authorsAndFiles)
sortedAuthors := make([]string, len(authorsAndFiles))
i := 0
for authors, _ := range authorsAndFiles {
sortedAuthors[i] = authors
i++
}
sort.Strings(sortedAuthors)
for _, authors := range sortedAuthors {
files := authorsAndFiles[authors]
fmt.Print(strings.Replace(authors, ",", "\n", -1))
fmt.Print("\n\t")
sort.Strings(files)
fmt.Println(strings.Join(files, "\n\t"))
}
}
func runGitLog(debug *bool) *bufio.Scanner {
gitargs := setupGitLogArgs()
if *debug {
fmt.Println("git", strings.Join(gitargs, " "))
}
_, stdout, stderr := piper.MustPipe("git", gitargs...)
go func() {
for stderr.Scan() {
fmt.Fprintln(os.Stderr, stderr.Text())
}
}()
return stdout
}
func setupGitLogArgs() []string {
gitargs := make([]string, 0)
gitargs = append(gitargs, "log", "--format="+WHO+"%an", "--name-only")
if flag.NArg() > 0 {
gitargs = append(gitargs, flag.Args()...)
} else {
gitargs = append(gitargs, "ORIG_HEAD..")
}
return gitargs
}
func isAuthorLine(line *string) bool {
return len(*line) > len(WHO) && (*line)[0:len(WHO)] == WHO
}
func showHelp() {
usage := `NAME
git-whowhat - Show authors and the files that they modified.
SYNOPSIS
git whowhat [<options>] [<since>..<until> [[--] <path>...]
OPTIONS
-d
Print debugging information
-h
Show this help message
[<since>..<until> [[--] <path>...]
These are the same argument understood by git log
If none is specified, "ORIG_HEAD.." is used as the sole argument
`
fmt.Print(usage)
}
func die(reason ...interface{}) {
message := fmt.Sprint("git-whowhat", reason)
log.Fatal(message)
}