@@ -50,7 +50,7 @@ func (s *Spec) UpdateExistingTag(packageName string, tag string, value string) (
5050
5151 var updated bool
5252
53- err = s .VisitTags (packageName , func (tagLine * TagLine , ctx * Context ) error {
53+ err = s .VisitTagsPackage (packageName , func (tagLine * TagLine , ctx * Context ) error {
5454 if strings .ToLower (tagLine .Tag ) != tagToCompareAgainst {
5555 return nil
5656 }
@@ -100,20 +100,14 @@ func (s *Spec) RemoveTag(packageName string, tag string, value string) (err erro
100100 return nil
101101}
102102
103- // VisitTags iterates over all tag lines in the given package, calling the visitor function
104- // for each one. The visitor receives the parsed [TagLine] and the mutation [Context]. This
105- // extracts the common target-type / package / tag-type filtering that many tag-oriented
106- // methods need.
107- func (s * Spec ) VisitTags (packageName string , visitor func (tagLine * TagLine , ctx * Context ) error ) error {
103+ // VisitTags iterates over all tag lines across all packages, calling the visitor function
104+ // for each one. The visitor receives the parsed [TagLine] and the mutation [Context].
105+ func (s * Spec ) VisitTags (visitor func (tagLine * TagLine , ctx * Context ) error ) error {
108106 return s .Visit (func (ctx * Context ) error {
109107 if ctx .Target .TargetType != SectionLineTarget {
110108 return nil
111109 }
112110
113- if ctx .CurrentSection .Package != packageName {
114- return nil
115- }
116-
117111 if ctx .Target .Line .Parsed .GetType () != Tag {
118112 return nil
119113 }
@@ -127,13 +121,27 @@ func (s *Spec) VisitTags(packageName string, visitor func(tagLine *TagLine, ctx
127121 })
128122}
129123
124+ // VisitTagsPackage iterates over all tag lines in the given package, calling the visitor
125+ // function for each one. The visitor receives the parsed [TagLine] and the mutation [Context].
126+ // This extracts the common target-type / package / tag-type filtering that many tag-oriented
127+ // methods need.
128+ func (s * Spec ) VisitTagsPackage (packageName string , visitor func (tagLine * TagLine , ctx * Context ) error ) error {
129+ return s .VisitTags (func (tagLine * TagLine , ctx * Context ) error {
130+ if ctx .CurrentSection .Package != packageName {
131+ return nil
132+ }
133+
134+ return visitor (tagLine , ctx )
135+ })
136+ }
137+
130138// RemoveTagsMatching removes all tags in the given package for which the provided matcher
131139// function returns true. The matcher receives the tag name and value as arguments. Returns
132140// the number of tags removed. If no matching tags were found, returns 0 and no error.
133141func (s * Spec ) RemoveTagsMatching (packageName string , matcher func (tag , value string ) bool ) (int , error ) {
134142 removed := 0
135143
136- err := s .VisitTags (packageName , func (tagLine * TagLine , ctx * Context ) error {
144+ err := s .VisitTagsPackage (packageName , func (tagLine * TagLine , ctx * Context ) error {
137145 if ! matcher (tagLine .Tag , tagLine .Value ) {
138146 return nil
139147 }
@@ -600,7 +608,7 @@ func (s *Spec) AddPatchEntry(packageName, filename string) error {
600608 return s .AppendLinesToSection ("%patchlist" , "" , []string {filename })
601609 }
602610
603- highest , err := s .GetHighestPatchTagNumber (packageName )
611+ highest , err := s .GetHighestPatchTagNumber ()
604612 if err != nil {
605613 return fmt .Errorf ("failed to scan for existing patch tags:\n %w" , err )
606614 }
@@ -610,24 +618,13 @@ func (s *Spec) AddPatchEntry(packageName, filename string) error {
610618
611619// RemovePatchEntry removes all references to patches matching the given pattern from the spec.
612620// The pattern is a glob pattern (supporting doublestar syntax) matched against PatchN tag values
613- // and %patchlist entries. Returns an error if no references matched the pattern.
614- func (s * Spec ) RemovePatchEntry (packageName , pattern string ) error {
615- slog .Debug ("Removing patch entry from spec" , "package" , packageName , " pattern" , pattern )
621+ // and %patchlist entries across all packages . Returns an error if no references matched the pattern.
622+ func (s * Spec ) RemovePatchEntry (pattern string ) error {
623+ slog .Debug ("Removing patch entry from spec" , "pattern" , pattern )
616624
617625 totalRemoved := 0
618626
619- tagsRemoved , err := s .RemoveTagsMatching (packageName , func (tag , value string ) bool {
620- if _ , ok := ParsePatchTagNumber (tag ); ! ok {
621- return false
622- }
623-
624- matched , matchErr := doublestar .Match (pattern , value )
625- if matchErr != nil {
626- return false
627- }
628-
629- return matched
630- })
627+ tagsRemoved , err := s .removePatchTagsMatching (pattern )
631628 if err != nil {
632629 return fmt .Errorf ("failed to remove matching patch tags:\n %w" , err )
633630 }
@@ -655,6 +652,33 @@ func (s *Spec) RemovePatchEntry(packageName, pattern string) error {
655652 return nil
656653}
657654
655+ // removePatchTagsMatching removes all PatchN tags across all packages whose values match the
656+ // given glob pattern. Returns the number of tags removed.
657+ func (s * Spec ) removePatchTagsMatching (pattern string ) (int , error ) {
658+ removed := 0
659+
660+ err := s .VisitTags (func (tagLine * TagLine , ctx * Context ) error {
661+ if _ , ok := ParsePatchTagNumber (tagLine .Tag ); ! ok {
662+ return nil
663+ }
664+
665+ matched , matchErr := doublestar .Match (pattern , tagLine .Value )
666+ if matchErr != nil {
667+ return fmt .Errorf ("failed to match glob pattern %#q against %#q:\n %w" , pattern , tagLine .Value , matchErr )
668+ }
669+
670+ if matched {
671+ ctx .RemoveLine ()
672+
673+ removed ++
674+ }
675+
676+ return nil
677+ })
678+
679+ return removed , err
680+ }
681+
658682// removePatchlistEntriesMatching removes lines from the %patchlist section whose trimmed content
659683// matches the given glob pattern. Returns the number of entries removed.
660684func (s * Spec ) removePatchlistEntriesMatching (pattern string ) (int , error ) {
@@ -692,13 +716,13 @@ func (s *Spec) removePatchlistEntriesMatching(pattern string) (int, error) {
692716}
693717
694718// GetHighestPatchTagNumber scans the spec for all PatchN tags (where N is a decimal number)
695- // in the given package and returns the highest N found. Returns -1 if no numeric patch tags
719+ // across all packages and returns the highest N found. Returns -1 if no numeric patch tags
696720// exist. Tags with non-numeric suffixes (e.g., macro-based names like Patch%{n}) are silently
697721// skipped.
698- func (s * Spec ) GetHighestPatchTagNumber (packageName string ) (int , error ) {
722+ func (s * Spec ) GetHighestPatchTagNumber () (int , error ) {
699723 highest := - 1
700724
701- err := s .VisitTags (packageName , func (tagLine * TagLine , _ * Context ) error {
725+ err := s .VisitTags (func (tagLine * TagLine , _ * Context ) error {
702726 num , isPatchTag := ParsePatchTagNumber (tagLine .Tag )
703727 if isPatchTag && num > highest {
704728 highest = num
0 commit comments