aboutsummaryrefslogtreecommitdiff
path: root/tools/git-blame-stats/main.go
diff options
context:
space:
mode:
authorFranck Cuny <franck@fcuny.net>2022-10-16 19:03:35 -0700
committerFranck Cuny <franck@fcuny.net>2022-10-16 19:06:45 -0700
commit9d3fff359b6c2cea4ac69299f3c1621af8bde64c (patch)
tree95c5db67b6b94e0967404cb30d51f24f79331d72 /tools/git-blame-stats/main.go
parentops(github): archive the repository govanity (diff)
downloadinfra-9d3fff359b6c2cea4ac69299f3c1621af8bde64c.tar.gz
ref(tools/git-blame-stats): rewrite the tool in python
The tool can calculate some stats about authors in a git repository. It computes the number of lines that are authored by authors at a given revision (HEAD by default), and the number of commits.
Diffstat (limited to 'tools/git-blame-stats/main.go')
-rw-r--r--tools/git-blame-stats/main.go86
1 files changed, 0 insertions, 86 deletions
diff --git a/tools/git-blame-stats/main.go b/tools/git-blame-stats/main.go
deleted file mode 100644
index 8b1bc9a..0000000
--- a/tools/git-blame-stats/main.go
+++ /dev/null
@@ -1,86 +0,0 @@
-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
-}