@@ -21,6 +21,7 @@ import (
21
21
"net/http"
22
22
"net/url"
23
23
"path"
24
+ "reflect"
24
25
"regexp"
25
26
"strings"
26
27
"time"
@@ -88,50 +89,82 @@ func (vi *VersionInfo) HasLastAffectedVersions() bool {
88
89
}
89
90
90
91
func (vi * VersionInfo ) HasIntroducedCommits (repo string ) bool {
91
- for _ , av := range vi .AffectedCommits {
92
- if av .Repo == repo && av .Introduced != "" {
92
+ for _ , ac := range vi .AffectedCommits {
93
+ if ac .Repo == repo && ac .Introduced != "" {
93
94
return true
94
95
}
95
96
}
96
97
return false
97
98
}
98
99
99
100
func (vi * VersionInfo ) HasFixedCommits (repo string ) bool {
100
- for _ , av := range vi .AffectedCommits {
101
- if av .Repo == repo && av .Fixed != "" {
101
+ for _ , ac := range vi .AffectedCommits {
102
+ if ac .Repo == repo && ac .Fixed != "" {
102
103
return true
103
104
}
104
105
}
105
106
return false
106
107
}
107
108
108
109
func (vi * VersionInfo ) HasLastAffectedCommits (repo string ) bool {
109
- for _ , av := range vi .AffectedCommits {
110
- if av .Repo == repo && av .LastAffected != "" {
110
+ for _ , ac := range vi .AffectedCommits {
111
+ if ac .Repo == repo && ac .LastAffected != "" {
111
112
return true
112
113
}
113
114
}
114
115
return false
115
116
}
116
117
117
118
func (vi * VersionInfo ) FixedCommits (repo string ) (FixedCommits []string ) {
118
- for _ , av := range vi .AffectedCommits {
119
- if av .Repo == repo && av .Fixed != "" {
120
- FixedCommits = append (FixedCommits , av .Fixed )
119
+ for _ , ac := range vi .AffectedCommits {
120
+ if ac .Repo == repo && ac .Fixed != "" {
121
+ FixedCommits = append (FixedCommits , ac .Fixed )
121
122
}
122
123
}
123
124
return FixedCommits
124
125
}
125
126
126
127
func (vi * VersionInfo ) LastAffectedCommits (repo string ) (LastAffectedCommits []string ) {
127
- for _ , av := range vi .AffectedCommits {
128
- if av .Repo == repo && av .LastAffected != "" {
129
- LastAffectedCommits = append (LastAffectedCommits , av .Fixed )
128
+ for _ , ac := range vi .AffectedCommits {
129
+ if ac .Repo == repo && ac .LastAffected != "" {
130
+ LastAffectedCommits = append (LastAffectedCommits , ac .Fixed )
130
131
}
131
132
}
132
133
return LastAffectedCommits
133
134
}
134
135
136
+ // Check if the same commit appears in multiple fields of the AffectedCommits array.
137
+ // See https://github.com/google/osv.dev/issues/1984 for more context.
138
+ func (vi * VersionInfo ) Duplicated (candidate AffectedCommit ) bool {
139
+ fieldsToCheck := []string {"Introduced" , "LastAffected" , "Limit" , "Fixed" }
140
+
141
+ // Get the commit hash to look for.
142
+ v := reflect .ValueOf (& candidate ).Elem ()
143
+
144
+ commit := ""
145
+ for _ , field := range fieldsToCheck {
146
+ commit = v .FieldByName (field ).String ()
147
+ if commit != "" {
148
+ break
149
+ }
150
+ }
151
+ if commit == "" {
152
+ return false
153
+ }
154
+
155
+ // Look through what is already present.
156
+ for _ , ac := range vi .AffectedCommits {
157
+ v = reflect .ValueOf (& ac ).Elem ()
158
+ for _ , field := range fieldsToCheck {
159
+ existingCommit := v .FieldByName (field ).String ()
160
+ if existingCommit == commit {
161
+ return true
162
+ }
163
+ }
164
+ }
165
+ return false
166
+ }
167
+
135
168
// Synthetic enum of supported commit types.
136
169
type CommitType int
137
170
@@ -492,18 +525,18 @@ var (
492
525
)
493
526
494
527
func repoGitWeb (parsedURL * url.URL ) (string , error ) {
495
- params := strings .Split (parsedURL .RawQuery , ";" )
496
- for _ , param := range params {
497
- if ! strings .HasPrefix (param , "p=" ) {
498
- continue
499
- }
500
- repo , err := url .JoinPath (strings .TrimSuffix (strings .TrimSuffix (parsedURL .Path , "/gitweb.cgi" ), "cgi-bin" ), strings .Split (param , "=" )[1 ])
501
- if err != nil {
502
- return "" , err
503
- }
504
- return fmt .Sprintf ("git://%s%s" , parsedURL .Hostname (), repo ), nil
528
+ params := strings .Split (parsedURL .RawQuery , ";" )
529
+ for _ , param := range params {
530
+ if ! strings .HasPrefix (param , "p=" ) {
531
+ continue
505
532
}
506
- return "" , fmt .Errorf ("unsupported GitWeb URL: %s" , parsedURL .String ())
533
+ repo , err := url .JoinPath (strings .TrimSuffix (strings .TrimSuffix (parsedURL .Path , "/gitweb.cgi" ), "cgi-bin" ), strings .Split (param , "=" )[1 ])
534
+ if err != nil {
535
+ return "" , err
536
+ }
537
+ return fmt .Sprintf ("git://%s%s" , parsedURL .Hostname (), repo ), nil
538
+ }
539
+ return "" , fmt .Errorf ("unsupported GitWeb URL: %s" , parsedURL .String ())
507
540
}
508
541
509
542
// Returns the base repository URL for supported repository hosts.
0 commit comments