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
|
package main
import (
"fmt"
"log"
"os"
"os/exec"
"regexp"
"sort"
"strings"
)
func main() {
rev := "HEAD"
if len(os.Args) == 2 {
rev = os.Args[1]
}
files := gitListFiles(rev)
authors := gitBlameFiles(rev, files)
sortedAuthors, keys := sortAuthors(authors)
rank := 1
for _, k := range keys {
for i := 0; i < len(sortedAuthors[k]); i++ {
fmt.Printf("%3d - %6d %s\n", rank, k, sortedAuthors[k][i])
rank = rank + 1
}
}
}
func gitListFiles(rev string) []string {
out, err := exec.Command("git", "ls-tree", "--name-only", "-r", rev).Output()
if err != nil {
log.Fatal(err)
}
files := strings.Split(string(out), "\n")
return files
}
func gitBlameFiles(rev string, files []string) map[string]int {
authors := make(map[string]int)
for i := 0; i < len(files)-1; i++ {
gitBlameFile(rev, files[i], authors)
}
return authors
}
func gitBlameFile(rev, file string, authors map[string]int) {
out, err := exec.Command("git", "blame", "-e", "-w", rev, "--", file).Output()
if err != nil {
log.Fatal(err)
}
lines := strings.Split(string(out), "\n")
authorRegex := regexp.MustCompile(`^.*?\((.*?)\s*\d{4}-\d{2}-\d{2}.*`)
for j := 0; j < len(lines)-1; j++ {
if string(lines[j][0]) != "^" {
matched := authorRegex.FindStringSubmatch(string(lines[j]))
if len(matched) > 0 {
authors[matched[1]] = authors[matched[1]] + 1
}
}
}
}
func sortAuthors(authors map[string]int) (map[int][]string, []int) {
var keys []int
sortedAuthors := make(map[int][]string)
for k, v := range authors {
sortedAuthors[v] = append(sortedAuthors[v], k)
if len(sortedAuthors[v]) == 1 {
keys = append(keys, v)
}
}
sort.Sort(sort.Reverse(sort.IntSlice(keys)))
return sortedAuthors, keys
}
|