@@ -6,18 +6,20 @@ import (
6
6
"fmt"
7
7
"os"
8
8
"os/exec"
9
+ "slices"
9
10
"sort"
10
11
"strings"
11
12
12
13
"github.com/sirupsen/logrus"
13
14
)
14
15
15
- type versions struct {
16
- before string
17
- after string
16
+ type entry struct {
17
+ beforeVersion string
18
+ afterVersion string
19
+ linkPrefix string
18
20
}
19
21
20
- type modules = map [string ]versions
22
+ type modules = map [string ]entry
21
23
22
24
// Config is the structure passed to `Run`
23
25
type Config struct {
@@ -100,29 +102,45 @@ func diffModules(mods modules, addLinks bool, headerLevel uint) string {
100
102
var added , removed , changed []string
101
103
for name , mod := range mods {
102
104
txt := fmt .Sprintf ("- %s: " , name )
103
- if mod .before == "" { //nolint: gocritic
104
- if addLinks && isGitHubURL (name ) {
105
- txt += fmt .Sprintf ("[%s](%s/tree/%s)" ,
106
- mod .after , toURL (name ), sanitizeTag (mod .after ))
105
+ splitLinkPrefix := strings .Split (mod .linkPrefix , "/" )
106
+ prefixWithTree := fmt .Sprintf ("%s/%s" , mod .linkPrefix , "tree" )
107
+ if mod .beforeVersion == "" { //nolint: gocritic
108
+ if addLinks && isGitHubURL (mod .linkPrefix ) {
109
+ // Insert the tree part of the URL at index 3 to account for tag names with slashes
110
+ if len (splitLinkPrefix ) >= 3 {
111
+ prefixWithTree = strings .Join (slices .Insert (splitLinkPrefix , 3 , "tree" ), "/" )
112
+ }
113
+ txt += fmt .Sprintf ("[%s](%s/%s)" ,
114
+ mod .afterVersion , toURL (prefixWithTree ), sanitizeTag (mod .afterVersion ))
107
115
} else {
108
- txt += mod .after
116
+ txt += mod .afterVersion
109
117
}
110
118
added = append (added , txt )
111
- } else if mod .after == "" {
112
- if addLinks && isGitHubURL (name ) {
113
- txt += fmt .Sprintf ("[%s](%s/tree/%s)" ,
114
- mod .before , toURL (name ), sanitizeTag (mod .before ))
119
+ } else if mod .afterVersion == "" {
120
+ if addLinks && isGitHubURL (mod .linkPrefix ) {
121
+ if len (splitLinkPrefix ) >= 3 {
122
+ prefixWithTree = strings .Join (slices .Insert (splitLinkPrefix , 3 , "tree" ), "/" )
123
+ }
124
+ txt += fmt .Sprintf ("[%s](%s/%s)" ,
125
+ mod .beforeVersion , toURL (prefixWithTree ), sanitizeTag (mod .beforeVersion ))
115
126
} else {
116
- txt += mod .before
127
+ txt += mod .beforeVersion
117
128
}
118
129
removed = append (removed , txt )
119
- } else if mod .before != mod .after {
120
- if addLinks && isGitHubURL (name ) {
121
- txt += fmt .Sprintf ("[%s → %s](%s/compare/%s...%s)" ,
122
- mod .before , mod .after , toURL (name ),
123
- sanitizeTag (mod .before ), sanitizeTag (mod .after ))
130
+ } else if mod .beforeVersion != mod .afterVersion {
131
+ if addLinks && isGitHubURL (mod .linkPrefix ) {
132
+ prefixWithCompare := fmt .Sprintf ("%s/%s" , mod .linkPrefix , "compare" )
133
+ // Insert tag prefix to the afterVersion to account for tag names with slashes
134
+ afterVersion := sanitizeTag (mod .afterVersion )
135
+ if len (splitLinkPrefix ) > 3 {
136
+ prefixWithCompare = strings .Join (slices .Insert (splitLinkPrefix , 3 , "compare" ), "/" )
137
+ afterVersion = fmt .Sprintf ("%s/%s" , strings .Join (splitLinkPrefix [3 :], "/" ), afterVersion )
138
+ }
139
+ txt += fmt .Sprintf ("[%s → %s](%s/%s...%s)" ,
140
+ mod .beforeVersion , mod .afterVersion , toURL (prefixWithCompare ),
141
+ sanitizeTag (mod .beforeVersion ), afterVersion )
124
142
} else {
125
- txt += fmt .Sprintf ("%s → %s" , mod .before , mod .after )
143
+ txt += fmt .Sprintf ("%s → %s" , mod .beforeVersion , mod .afterVersion )
126
144
}
127
145
changed = append (changed , txt )
128
146
}
@@ -172,7 +190,7 @@ func getModules(workDir, from, to string) (modules, error) {
172
190
173
191
// Parse the modules
174
192
res := modules {}
175
- forEach := func (input string , do func (res * versions , version string )) {
193
+ forEach := func (input string , do func (res * entry , version string )) {
176
194
scanner := bufio .NewScanner (strings .NewReader (input ))
177
195
for scanner .Scan () {
178
196
// Skip version-less modules, like the local one
@@ -193,7 +211,13 @@ func getModules(workDir, from, to string) (modules, error) {
193
211
split [1 ] = split [4 ]
194
212
}
195
213
}
214
+
196
215
name := strings .TrimSpace (split [0 ])
216
+ linkPrefix := name
217
+ // Remove the module name from the link
218
+ if splitLink := strings .Split (linkPrefix , "/" ); len (splitLink ) == 4 {
219
+ linkPrefix = strings .Join (splitLink [:3 ], "/" )
220
+ }
197
221
version := strings .TrimSpace (split [1 ])
198
222
199
223
// Prettify pseudo versions
@@ -210,16 +234,17 @@ func getModules(workDir, from, to string) (modules, error) {
210
234
}
211
235
212
236
// Process the entry
213
- entry := & versions {}
237
+ entry := & entry {}
214
238
if val , ok := res [name ]; ok {
215
239
entry = & val
216
240
}
217
241
do (entry , version )
242
+ entry .linkPrefix = linkPrefix
218
243
res [name ] = * entry
219
244
}
220
245
}
221
- forEach (before , func (res * versions , v string ) { res .before = v })
222
- forEach (after , func (res * versions , v string ) { res .after = v })
246
+ forEach (before , func (res * entry , v string ) { res .beforeVersion = v })
247
+ forEach (after , func (res * entry , v string ) { res .afterVersion = v })
223
248
224
249
logrus .Infof ("%d modules found" , len (res ))
225
250
0 commit comments