forked from sirnewton01/godev
-
Notifications
You must be signed in to change notification settings - Fork 1
/
blame.go
467 lines (381 loc) · 10.6 KB
/
blame.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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
// Copyright 2013 Chris McGee <[email protected]>. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"bufio"
"encoding/json"
"net/http"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
"time"
)
const (
unknown = iota
git = iota
hg = iota
lscm = iota
)
type Blame struct {
AuthorName string
AuthorImage string
Message string
Name string
Time string
internalTime int64
Shade float32
CommitLink string
Start string
End string
}
type lscmAnnotateResult struct {
Annotations []LscmAnnotation `json:"annotations"`
}
type LscmAnnotation struct {
Author string `json:"author"`
Comment string `json:"comment"`
LineNo string `json:"line-no"`
Modified string `json:"modified"`
Uuid string `json:"uuid"`
Workitem string `json:"workitem"`
}
func blameHandler(writer http.ResponseWriter, req *http.Request, path string, pathSegs []string) bool {
switch {
case req.Method == "GET" && pathSegs[1] == "file":
localFilePath := ""
for _, srcDir := range srcDirs {
path := filepath.Join(srcDir, strings.Join(pathSegs[2:], "/"))
_, err := os.Stat(path)
if err == nil {
localFilePath = path
break
}
}
// Determine what type of SCM system is in use (git, mercurial, or Jazz SCM)
scmType := unknown
for parentPath := localFilePath; filepath.Base(parentPath) != parentPath; parentPath = filepath.Dir(parentPath) {
// Git
gitMetaDir := filepath.Join(parentPath, ".git")
_, err := os.Stat(gitMetaDir)
if err == nil {
scmType = git
break
}
// Mercurial
hgMetaDir := filepath.Join(parentPath, ".hg")
_, err = os.Stat(hgMetaDir)
if err == nil {
scmType = hg
break
}
// Jazz Source Control
jazzMetaDir := filepath.Join(parentPath, ".jazz5")
_, err = os.Stat(jazzMetaDir)
if err == nil {
scmType = lscm
break
}
}
if scmType == unknown {
ShowError(writer, 400, "No recognized VCS system could be found for the provided file", nil)
return true
}
blame := []Blame{}
var err error = nil
if scmType == git {
blame, err = loadGitBlame(localFilePath)
if err != nil {
ShowError(writer, 400, "Unable to get git blame for file", err)
return true
}
}
if scmType == lscm {
blame, err = loadLscmBlame(localFilePath)
if err != nil {
ShowError(writer, 400, "Unable to get jazz scm blame for file", err)
return true
}
}
if scmType == hg {
blame, err = loadHgBlame(localFilePath)
if err != nil {
ShowError(writer, 400, "Unable to get mercurial blame for file", err)
return true
}
}
ShowJson(writer, 200, blame)
return true
}
return false
}
func loadGitBlame(path string) (blames []Blame, err error) {
cmd := exec.Command("git", "blame", path, "-p")
// Start git in the directory so that it picks up the .git directory
cmd.Dir = filepath.Dir(path)
output, err := cmd.StdoutPipe()
if err != nil {
return blames, err
}
defer output.Close()
err = cmd.Start()
if err != nil {
return blames, err
}
scanner := bufio.NewScanner(output)
blame := Blame{}
oldestTime := time.Now().Unix()
newestTime := int64(0)
commitMap := make(map[string]Blame)
for scanner.Scan() {
l := scanner.Text()
parts := strings.Split(l, " ")
// Blame Header
if len(parts[0]) == 40 && len(parts) == 4 {
newBlame := false
// New blame section
if blame.Name != parts[0] {
if blame.Name != "" {
// Clone any existing commit information from previous blames
// for this commit
existingCommit := commitMap[blame.Name]
if existingCommit.Name != "" {
blame.AuthorName = existingCommit.AuthorName
blame.Time = existingCommit.Time
blame.internalTime = existingCommit.internalTime
blame.Message = existingCommit.Message
}
blames = append(blames, blame)
commitMap[blame.Name] = blame
}
blame = Blame{}
// TODO figure out how to get per-author images from git
blame.AuthorImage = "http://git-scm.com/favicon.ico"
blame.Name = parts[0]
blame.Start = parts[2]
blame.End = parts[2]
newBlame = true
}
// A second pass should resolve the shades
blame.Shade = 0.0
// Figure out the end of this blame
end, err := strconv.ParseInt(blame.End, 10, 64)
if err != nil {
continue
}
additionalLines, err := strconv.ParseInt(parts[3], 10, 64)
if err != nil {
continue
}
end = end + additionalLines
if newBlame {
end = end - 1
}
blame.End = strconv.FormatInt(end, 10)
} else if parts[0] == "author-time" && len(parts) == 2 {
blame.internalTime, _ = strconv.ParseInt(parts[1], 10, 64)
blame.Time = time.Unix(blame.internalTime, 0).Format(time.RFC1123)
if blame.internalTime < oldestTime {
oldestTime = blame.internalTime
}
if blame.internalTime > newestTime {
newestTime = blame.internalTime
}
// Summaries can have spaces in them but the line must begin with "summary"
// and no tab
} else if parts[0] == "summary" {
blame.Message = l[8:]
} else if parts[0] == "author" {
blame.AuthorName = l[7:]
}
}
cmd.Wait()
if blame.Name != "" {
// Clone any existing commit information from previous blames
// for this commit
existingCommit := commitMap[blame.Name]
if existingCommit.Name != "" {
blame.AuthorName = existingCommit.AuthorName
blame.Time = existingCommit.Time
blame.internalTime = existingCommit.internalTime
blame.Message = existingCommit.Message
}
blames = append(blames, blame)
}
for idx, _ := range blames {
if oldestTime != newestTime {
delta := float32(newestTime - oldestTime)
blames[idx].Shade = float32(blames[idx].internalTime-oldestTime) / delta
// Make sure that there is some kind of shading for the blame
if blames[idx].Shade < 0.2 {
blames[idx].Shade = 0.2
}
} else {
// Give everything an average shading
blames[idx].Shade = 0.5
}
}
return blames, nil
}
func loadLscmBlame(path string) (blames []Blame, err error) {
cmd := exec.Command("lscm", "annotate", path, "--json")
// Start the lscm command in the directory so that it picks up the correct
// sandbox
cmd.Dir = filepath.Dir(path)
output, err := cmd.StdoutPipe()
if err != nil {
return blames, err
}
defer output.Close()
err = cmd.Start()
if err != nil {
return blames, err
}
decoder := json.NewDecoder(output)
annotations := &lscmAnnotateResult{}
err = decoder.Decode(annotations)
if err != nil {
return blames, err
}
err = cmd.Wait()
if err != nil {
return blames, err
}
oldestTime := time.Now().Unix()
newestTime := int64(0)
for _, annotation := range annotations.Annotations {
blame := Blame{}
blame.AuthorName = annotation.Author
// TODO perhaps a per-author image would be nice
// This prevents the blame hover from producing a broken image icon
blame.AuthorImage = "https://jazz.net/favicon.ico"
// TODO Find the repo URL from the lscm status so that this works on any server
blame.CommitLink = "https://hub.jazz.net/ccm01/resource/itemOid/com.ibm.team.scm.ChangeSet/" + annotation.Uuid
blame.Start = annotation.LineNo
blame.End = annotation.LineNo
blame.Message = annotation.Comment
if blame.Message == "" {
blame.Message = "<No Comment>"
}
if annotation.Workitem != "" {
blame.Message = "Work Item " + annotation.Workitem + " - " + blame.Message
}
blame.Name = annotation.Uuid
// We will do a second pass for the shading
blame.Shade = 0.0
// Capture the oldest and newest change times
t, err := time.Parse("2006-01-02 03:04 PM", annotation.Modified)
if err == nil {
blame.internalTime = t.Unix()
if blame.internalTime < oldestTime {
oldestTime = blame.internalTime
}
if blame.internalTime > newestTime {
newestTime = blame.internalTime
}
}
blame.Time = annotation.Modified
blames = append(blames, blame)
}
for idx, _ := range blames {
if oldestTime != newestTime {
delta := float32(newestTime - oldestTime)
blames[idx].Shade = float32(blames[idx].internalTime-oldestTime) / delta
// Make sure that there is some kind of shading for the blame
if blames[idx].Shade < 0.2 {
blames[idx].Shade = 0.2
}
} else {
// Give everything an average shading
blames[idx].Shade = 0.5
}
}
return blames, nil
}
func loadHgBlame(path string) (blames []Blame, err error) {
cmd := exec.Command("hg", "blame", "-vduc", path)
// Start git in the directory so that it picks up the .git directory
cmd.Dir = filepath.Dir(path)
output, err := cmd.StdoutPipe()
if err != nil {
return blames, err
}
defer output.Close()
err = cmd.Start()
if err != nil {
return blames, err
}
scanner := bufio.NewScanner(output)
oldestTime := time.Now().Unix()
newestTime := int64(0)
lineCount := 1
csSummary := make(map[string]string)
for scanner.Scan() {
l := scanner.Text()
idx := strings.Index(l, ">")
authorInfo := l[:idx+1]
authorInfo = strings.Trim(authorInfo, " ")
l = l[idx+2:]
changeSet := l[:12]
date := l[13:43]
blame := Blame{}
// TODO tease out an author image somehow?
blame.AuthorImage = "http://mercurial.selenic.com/favicon.ico"
blame.Start = strconv.FormatInt(int64(lineCount), 10)
blame.End = blame.Start
blame.Name = changeSet
authorInfo = authorInfo[:strings.Index(authorInfo, "<")]
blame.AuthorName = authorInfo
blame.Time = date
t, err := time.Parse("Mon Jan 02 15:04:05 2006 -0700", date)
if err == nil {
blame.internalTime = t.Unix()
if oldestTime > t.Unix() {
oldestTime = t.Unix()
}
if newestTime < t.Unix() {
newestTime = t.Unix()
}
}
summary := csSummary[blame.Name]
if summary == "" {
// Retrieve the change set summary
logCmd := exec.Command("hg", "log", "-r"+blame.Name)
logCmd.Dir = filepath.Dir(path)
b, err := logCmd.Output()
if err == nil {
output := string(b)
idx := strings.Index(output, "\nsummary:")
if idx != -1 {
output = output[idx+9:]
if idx != -1 {
idx := strings.Index(output, "\n")
output = output[0:idx]
summary = strings.Trim(output, " ")
csSummary[blame.Name] = summary
}
}
}
}
blame.Message = summary
blames = append(blames, blame)
lineCount++
}
cmd.Wait()
for idx, _ := range blames {
if oldestTime != newestTime {
delta := float32(newestTime - oldestTime)
blames[idx].Shade = float32(blames[idx].internalTime-oldestTime) / delta
// Make sure that there is some kind of shading for the blame
if blames[idx].Shade < 0.2 {
blames[idx].Shade = 0.2
}
} else {
// Give everything an average shading
blames[idx].Shade = 0.5
}
}
return blames, nil
}