-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
139 lines (118 loc) · 2.8 KB
/
main.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
package main
import (
"encoding/xml"
"fmt"
"os"
"path/filepath"
"time"
git "github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing/object"
"github.com/spf13/cobra"
)
// Contributors is a list of contributors
type Contributors map[string]struct{}
// Graph is a map of contributors
type Graph map[string]Contributors
// Repository is a git repository
type Repository struct {
Path string
Since string
Graph Graph
}
// GraphML is a graphml file
type GraphML struct {
XMLName xml.Name `xml:"graphml"`
Nodes []Node `xml:"graph>node"`
Edges []Edge `xml:"graph>edge"`
}
// Node is a node in the graphml file
type Node struct {
ID string `xml:"id,attr"`
Type string `xml:"type,attr"`
}
// Edge is an edge in the graphml file
type Edge struct {
Source string `xml:"source,attr"`
Target string `xml:"target,attr"`
}
func (r *Repository) Scan(path string, info os.FileInfo, err error) error {
if err != nil {
fmt.Println(err)
return err
}
g, err := git.PlainOpen(path)
if err != nil {
return nil
}
since, err := time.Parse("02/01/2006", r.Since)
if err != nil {
fmt.Println(err)
return nil
}
l, err := g.Log(&git.LogOptions{All: true, Since: &since})
if err != nil {
return nil
}
r.Graph[path] = make(Contributors)
err = l.ForEach(func(c *object.Commit) error {
r.Graph[path][c.Author.Email] = struct{}{}
return nil
})
return filepath.SkipDir
}
func main() {
var (
repoPath string
since string
)
cmd := cobra.Command{
Use: "git-digger",
Short: "git-digger is a tool to dig into git repositories",
Long: "git-digger is a tool to dig into git repositories",
Run: func(cmd *cobra.Command, args []string) {
if len(repoPath) == 0 {
cmd.Help()
os.Exit(1)
}
r := Repository{
Path: repoPath,
Since: since,
Graph: make(Graph),
}
filepath.Walk(repoPath, r.Scan)
graph := GraphML{}
for repo, contributors := range r.Graph {
if len(repo) == 0 {
continue
}
graph.Nodes = append(graph.Nodes, Node{ID: repo, Type: "project"})
for contributor := range contributors {
if len(contributor) == 0 {
continue
}
graph.Nodes = append(graph.Nodes, Node{ID: contributor, Type: "contributor"})
graph.Edges = append(graph.Edges, Edge{Source: contributor, Target: repo})
}
}
file, err := os.Create("graph.graphml")
if err != nil {
fmt.Println(err)
os.Exit(1)
}
defer file.Close()
enc := xml.NewEncoder(file)
enc.Indent("", " ")
if err := enc.Encode(graph); err != nil {
fmt.Printf("error: %v\n", err)
os.Exit(1)
}
},
}
cmd.Flags().StringVarP(&repoPath, "repo", "r", "", "Path to the git repository")
cmd.Flags().StringVarP(&since, "since", "s", "02/01/2006", "Since date")
err := cmd.Execute()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}