@@ -4,38 +4,39 @@ import (
4
4
"fmt"
5
5
"io"
6
6
"strings"
7
+ "math"
7
8
8
9
"github.com/aryann/difflib"
9
10
"github.com/mgutz/ansi"
10
11
11
12
"github.com/databus23/helm-diff/manifest"
12
13
)
13
14
14
- func DiffManifests (oldIndex , newIndex map [string ]* manifest.MappingResult , suppressedKinds []string , to io.Writer ) {
15
+ func DiffManifests (oldIndex , newIndex map [string ]* manifest.MappingResult , suppressedKinds []string , context int , to io.Writer ) {
15
16
for key , oldContent := range oldIndex {
16
17
if newContent , ok := newIndex [key ]; ok {
17
18
if oldContent .Content != newContent .Content {
18
19
// modified
19
20
fmt .Fprintf (to , ansi .Color ("%s has changed:" , "yellow" )+ "\n " , key )
20
- printDiff (suppressedKinds , oldContent .Kind , oldContent .Content , newContent .Content , to )
21
+ printDiff (suppressedKinds , oldContent .Kind , context , oldContent .Content , newContent .Content , to )
21
22
}
22
23
} else {
23
24
// removed
24
25
fmt .Fprintf (to , ansi .Color ("%s has been removed:" , "yellow" )+ "\n " , key )
25
- printDiff (suppressedKinds , oldContent .Kind , oldContent .Content , "" , to )
26
+ printDiff (suppressedKinds , oldContent .Kind , context , oldContent .Content , "" , to )
26
27
}
27
28
}
28
29
29
30
for key , newContent := range newIndex {
30
31
if _ , ok := oldIndex [key ]; ! ok {
31
32
// added
32
33
fmt .Fprintf (to , ansi .Color ("%s has been added:" , "yellow" )+ "\n " , key )
33
- printDiff (suppressedKinds , newContent .Kind , "" , newContent .Content , to )
34
+ printDiff (suppressedKinds , newContent .Kind , context , "" , newContent .Content , to )
34
35
}
35
36
}
36
37
}
37
38
38
- func printDiff (suppressedKinds []string , kind , before , after string , to io.Writer ) {
39
+ func printDiff (suppressedKinds []string , kind string , context int , before , after string , to io.Writer ) {
39
40
diffs := difflib .Diff (strings .Split (before , "\n " ), strings .Split (after , "\n " ))
40
41
41
42
for _ , ckind := range suppressedKinds {
@@ -46,16 +47,71 @@ func printDiff(suppressedKinds []string, kind, before, after string, to io.Write
46
47
}
47
48
}
48
49
49
- for _ , diff := range diffs {
50
- text := diff .Payload
50
+ if context >= 0 {
51
+ distances := calculateDistances (diffs )
52
+ omitting := false
53
+ for i , diff := range diffs {
54
+ if distances [i ] > context {
55
+ if ! omitting {
56
+ fmt .Fprintln (to , "..." )
57
+ omitting = true
58
+ }
59
+ } else {
60
+ omitting = false
61
+ printDiffRecord (diff , to )
62
+ }
63
+ }
64
+ } else {
65
+ for _ , diff := range diffs {
66
+ printDiffRecord (diff , to )
67
+ }
68
+ }
69
+ }
70
+
71
+ func printDiffRecord (diff difflib.DiffRecord , to io.Writer ) {
72
+ text := diff .Payload
73
+
74
+ switch diff .Delta {
75
+ case difflib .RightOnly :
76
+ fmt .Fprintf (to , "%s\n " , ansi .Color ("+ " + text , "green" ))
77
+ case difflib .LeftOnly :
78
+ fmt .Fprintf (to , "%s\n " , ansi .Color ("- " + text , "red" ))
79
+ case difflib .Common :
80
+ fmt .Fprintf (to , "%s\n " , " " + text )
81
+ }
82
+ }
51
83
52
- switch diff .Delta {
53
- case difflib .RightOnly :
54
- fmt .Fprintf (to , "%s\n " , ansi .Color ("+ " + text , "green" ))
55
- case difflib .LeftOnly :
56
- fmt .Fprintf (to , "%s\n " , ansi .Color ("- " + text , "red" ))
57
- case difflib .Common :
58
- fmt .Fprintf (to , "%s\n " , " " + text )
84
+ // Calculate distance of every diff-line to the closest change
85
+ func calculateDistances (diffs []difflib.DiffRecord ) map [int ]int {
86
+ distances := map [int ]int {}
87
+
88
+ // Iterate forwards through diffs, set 'distance' based on closest 'change' before this line
89
+ change := - 1
90
+ for i , diff := range diffs {
91
+ if diff .Delta != difflib .Common {
92
+ change = i
93
+ }
94
+ distance := math .MaxInt32
95
+ if change != - 1 {
96
+ distance = i - change
97
+ }
98
+ distances [i ] = distance
99
+ }
100
+
101
+ // Iterate backwards through diffs, reduce 'distance' based on closest 'change' after this line
102
+ change = - 1
103
+ for i := len (diffs ) - 1 ; i >= 0 ; i -- {
104
+ diff := diffs [i ]
105
+ if diff .Delta != difflib .Common {
106
+ change = i
107
+ }
108
+ if change != - 1 {
109
+ distance := change - i
110
+ if distance < distances [i ] {
111
+ distances [i ] = distance
112
+ }
59
113
}
60
114
}
115
+
116
+ return distances
61
117
}
0 commit comments