Repository navigation
Expand file tree
/
Copy pathdiff_stat.go
More file actions
143 lines (124 loc) · 3.27 KB
/
Copy pathdiff_stat.go
File metadata and controls
143 lines (124 loc) · 3.27 KB
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
package patchbin
import (
"fmt"
"math"
"strings"
)
const diffStatMaxBarWidth = 30
// DiffStatFile represents a single file's line in a git-diff --stat summary.
type DiffStatFile struct {
DisplayName string
FileAnchor string
Adds int64
Dels int64
Total int64
IsBinary bool
PlusBar string
MinusBar string
}
// DiffStat holds the git-diff --stat style summary for a patch.
type DiffStat struct {
Files []DiffStatFile
FilesChanged int
TotalAdds int64
TotalDels int64
SummaryText string
}
// HasFiles reports whether the diff stat has any files to display.
func (d DiffStat) HasFiles() bool {
return len(d.Files) > 0
}
// ComputeDiffStat builds a git-diff --stat style summary from parsed patch files.
func ComputeDiffStat(patchFiles []*PatchFile) DiffStat {
stat := DiffStat{
Files: make([]DiffStatFile, 0, len(patchFiles)),
FilesChanged: len(patchFiles),
}
if len(patchFiles) == 0 {
return stat
}
var maxTotal int64
for _, pf := range patchFiles {
stat.TotalAdds += pf.Adds
stat.TotalDels += pf.Dels
if !pf.IsBinary {
if total := pf.Adds + pf.Dels; total > maxTotal {
maxTotal = total
}
}
}
for _, pf := range patchFiles {
name := pf.DisplayName
if (pf.IsRename || pf.IsCopy) && pf.OldName != "" && pf.NewName != "" && pf.OldName != pf.NewName {
name = fmt.Sprintf("%s => %s", pf.OldName, pf.NewName)
}
total := pf.Adds + pf.Dels
var plusBar, minusBar string
if !pf.IsBinary && total > 0 {
plusCount, minusCount := scaleDiffStatBar(pf.Adds, pf.Dels, maxTotal, diffStatMaxBarWidth)
plusBar = strings.Repeat("+", plusCount)
minusBar = strings.Repeat("-", minusCount)
}
stat.Files = append(stat.Files, DiffStatFile{
DisplayName: name,
FileAnchor: pf.FileAnchor,
Adds: pf.Adds,
Dels: pf.Dels,
Total: total,
IsBinary: pf.IsBinary,
PlusBar: plusBar,
MinusBar: minusBar,
})
}
stat.SummaryText = formatDiffStatSummary(stat.FilesChanged, stat.TotalAdds, stat.TotalDels)
return stat
}
func scaleDiffStatBar(adds, dels, maxTotal int64, maxWidth int) (int, int) {
total := adds + dels
if total == 0 || maxTotal == 0 {
return 0, 0
}
if maxTotal <= int64(maxWidth) {
return int(adds), int(dels)
}
plusCount := int(math.Round(float64(adds) * float64(maxWidth) / float64(maxTotal)))
minusCount := int(math.Round(float64(dels) * float64(maxWidth) / float64(maxTotal)))
// Ensure non-zero additions or deletions always show at least one symbol.
if adds > 0 && plusCount == 0 {
plusCount = 1
}
if dels > 0 && minusCount == 0 {
minusCount = 1
}
if plusCount+minusCount > maxWidth {
if plusCount >= minusCount && plusCount > 1 {
plusCount--
} else if minusCount > 1 {
minusCount--
}
}
return plusCount, minusCount
}
func formatDiffStatSummary(filesChanged int, totalAdds, totalDels int64) string {
var b strings.Builder
fmt.Fprintf(&b, "%d file", filesChanged)
if filesChanged != 1 {
b.WriteByte('s')
}
b.WriteString(" changed")
if totalAdds > 0 {
fmt.Fprintf(&b, ", %d insertion", totalAdds)
if totalAdds != 1 {
b.WriteByte('s')
}
b.WriteString("(+)")
}
if totalDels > 0 {
fmt.Fprintf(&b, ", %d deletion", totalDels)
if totalDels != 1 {
b.WriteByte('s')
}
b.WriteString("(-)")
}
return b.String()
}