This repository has been archived by the owner on Sep 12, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
237 lines (202 loc) · 4.97 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
package main
import (
"code.google.com/p/go.exp/inotify"
"flag"
"fmt"
"github.com/Merovius/git2go"
"io"
"log"
"os"
"os/exec"
"syscall"
"time"
)
var noBrokenHead bool
type Dumper interface {
Dump(shorten int, w io.Writer)
}
type Blob struct {
*git.Blob
id *git.Oid
}
type Tree struct {
*git.Tree
id *git.Oid
entries []*git.TreeEntry
}
type Commit struct {
*git.Commit
id *git.Oid
tree *git.Oid
parents []*git.Oid
}
type Reference struct {
name string
id *git.Oid
}
type SymbolicReference struct {
name string
*git.Reference
}
var shorten int
func (b *Blob) Dump(shorten int, w io.Writer) {
fmt.Fprintf(w, "\"%s\" [shape=box,style=filled,fillcolor=\"#ddddff\",color=\"#bbbbff\"]\n", b.id.String()[:shorten])
}
func (t *Tree) Dump(shorten int, w io.Writer) {
fmt.Fprintf(w, "\"%s\" [shape=oval,style=filled,fillcolor=\"#99ff99\"]\n", t.id.String()[:shorten])
for _, e := range t.entries {
fmt.Fprintf(w, "\"%s\" -> \"%s\" [label=\"%s\",fontcolor=\"#666666\"]\n", t.id.String()[:shorten], e.Id.String()[:shorten], e.Name)
}
}
func (c *Commit) Dump(shorten int, w io.Writer) {
fmt.Fprintf(w, "\"%s\" [shape=hexagon,style=filled,fillcolor=\"#ffff99\"]\n",
c.id.String()[:shorten])
fmt.Fprintf(w, "\"%s\" -> \"%s\"\n", c.id.String()[:shorten], c.tree.String()[:shorten])
for _, p := range c.parents {
fmt.Fprintf(w, "\"%s\" -> \"%s\"\n", c.id.String()[:shorten], p.String()[:shorten])
}
}
func (r *Reference) Dump(shorten int, w io.Writer) {
fmt.Fprintf(w, "\"%s\" [shape=box,style=filled,fillcolor=\"#9999ff\"]\n", r.name)
fmt.Fprintf(w, "\"%s\" -> \"%s\"\n", r.name, r.id.String()[:shorten])
}
func (r *SymbolicReference) Dump(shorten int, w io.Writer) {
if r.Type() == git.SYMBOLIC {
fmt.Fprintf(w, "\"%s\" [shape=box,style=filled,fillcolor=\"#ff9999\"]\n", r.name)
fmt.Fprintf(w, "\"%s\" -> \"%s\"\n", r.name, r.SymbolicTarget())
} else {
fmt.Fprintf(w, "\"%s\" [shape=box,style=filled,fillcolor=\"#ff9999\"]\n", r.name)
fmt.Fprintf(w, "\"%s\" -> \"%s\"\n", r.name, r.Target().String()[:shorten])
}
}
func DumpRepo(repo *git.Repository, w io.Writer) {
stuff := make(map[string]Dumper)
var oids []*git.Oid
odb, err := repo.Odb()
if err != nil {
log.Fatal(err)
}
for oid := range odb.ForEach() {
obj, err := repo.Lookup(oid)
if err != nil {
log.Fatal("Lookup:", err)
}
switch obj := obj.(type) {
default:
case *git.Blob:
bl := &Blob{obj, oid}
stuff[oid.String()] = bl
oids = append(oids, oid)
case *git.Tree:
tr := &Tree{obj, oid, nil}
stuff[oid.String()] = tr
for i := uint64(0); i < tr.EntryCount(); i++ {
tr.entries = append(tr.entries, tr.EntryByIndex(i))
}
oids = append(oids, oid)
case *git.Commit:
co := &Commit{obj, oid, obj.TreeId(), nil}
for i := uint(0); i < obj.ParentCount(); i++ {
co.parents = append(co.parents, obj.ParentId(i))
}
stuff[oid.String()] = co
oids = append(oids, oid)
}
}
iter, err := repo.NewReferenceIterator()
if err != nil {
log.Fatal(err)
}
for refname := range iter.Iter() {
ref, err := repo.LookupReference(refname)
if err != nil {
log.Fatal(err)
}
stuff[refname] = &Reference{refname, ref.Target()}
}
ref, err := repo.LookupReference("HEAD")
if err != nil {
log.Fatal(err)
}
if !noBrokenHead ||
(ref.Type() == git.SYMBOLIC && stuff[ref.SymbolicTarget()] != nil) ||
(ref.Type() == git.OID && stuff[ref.Target().String()] != nil) {
stuff["HEAD"] = &SymbolicReference{"HEAD", ref}
}
shorten, err := git.ShortenOids(oids, 4)
if err != nil {
shorten = 40
}
fmt.Fprintln(w, "digraph G {")
for _, dp := range stuff {
dp.Dump(shorten, w)
}
fmt.Fprintln(w, "}")
}
func WatchRepo(repo *git.Repository, ch chan bool) {
for {
cmd := exec.Command("dot", "-Tx11")
inp, err := cmd.StdinPipe()
if err != nil {
log.Fatal(err)
}
err = cmd.Start()
if err != nil {
log.Fatal(err)
}
DumpRepo(repo, inp)
inp.Close()
/* Wait for updates */
<-ch
cmd.Process.Signal(syscall.SIGTERM)
cmd.Wait()
}
}
func main() {
flag.BoolVar(&noBrokenHead, "no-broken-head", false, "Hide a broken HEAD-ref")
watch := flag.Bool("watch", false, "Watch repository with inotify")
flag.Parse()
var dir string
var err error
if flag.NArg() > 0 {
dir = flag.Arg(0)
} else {
dir, err = git.Discover(".", false, nil)
if err != nil {
log.Fatal(err)
}
}
repo, err := git.OpenRepository(dir)
if err != nil {
log.Fatal(err)
}
if !*watch {
DumpRepo(repo, os.Stdout)
return
}
/* Get the path of the git-repository */
dir = repo.Path()
update := make(chan bool)
go WatchRepo(repo, update)
update <- true
watcher, err := inotify.NewWatcher()
if err != nil {
log.Fatal(err)
}
err = watcher.AddWatch(dir, inotify.IN_CREATE|inotify.IN_DELETE|inotify.IN_MODIFY|inotify.IN_MOVE)
if err != nil {
log.Fatal(err)
}
var ch <-chan time.Time
for {
select {
case ev := <-watcher.Event:
log.Println(ev)
ch = time.After(time.Second)
case err = <-watcher.Error:
log.Fatal(err)
case <-ch:
update <- true
}
}
}