@@ -6,18 +6,20 @@ import (
66 "fmt"
77 "os"
88 "os/exec"
9+ "slices"
910 "sort"
1011 "strings"
1112
1213 "github.com/sirupsen/logrus"
1314)
1415
15- type versions struct {
16- before string
17- after string
16+ type entry struct {
17+ beforeVersion string
18+ afterVersion string
19+ linkPrefix string
1820}
1921
20- type modules = map [string ]versions
22+ type modules = map [string ]entry
2123
2224// Config is the structure passed to `Run`
2325type Config struct {
@@ -100,29 +102,45 @@ func diffModules(mods modules, addLinks bool, headerLevel uint) string {
100102 var added , removed , changed []string
101103 for name , mod := range mods {
102104 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 ))
107115 } else {
108- txt += mod .after
116+ txt += mod .afterVersion
109117 }
110118 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 ))
115126 } else {
116- txt += mod .before
127+ txt += mod .beforeVersion
117128 }
118129 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 )
124142 } else {
125- txt += fmt .Sprintf ("%s → %s" , mod .before , mod .after )
143+ txt += fmt .Sprintf ("%s → %s" , mod .beforeVersion , mod .afterVersion )
126144 }
127145 changed = append (changed , txt )
128146 }
@@ -172,7 +190,7 @@ func getModules(workDir, from, to string) (modules, error) {
172190
173191 // Parse the modules
174192 res := modules {}
175- forEach := func (input string , do func (res * versions , version string )) {
193+ forEach := func (input string , do func (res * entry , version string )) {
176194 scanner := bufio .NewScanner (strings .NewReader (input ))
177195 for scanner .Scan () {
178196 // Skip version-less modules, like the local one
@@ -193,7 +211,13 @@ func getModules(workDir, from, to string) (modules, error) {
193211 split [1 ] = split [4 ]
194212 }
195213 }
214+
196215 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+ }
197221 version := strings .TrimSpace (split [1 ])
198222
199223 // Prettify pseudo versions
@@ -210,16 +234,17 @@ func getModules(workDir, from, to string) (modules, error) {
210234 }
211235
212236 // Process the entry
213- entry := & versions {}
237+ entry := & entry {}
214238 if val , ok := res [name ]; ok {
215239 entry = & val
216240 }
217241 do (entry , version )
242+ entry .linkPrefix = linkPrefix
218243 res [name ] = * entry
219244 }
220245 }
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 })
223248
224249 logrus .Infof ("%d modules found" , len (res ))
225250
0 commit comments