@@ -95,21 +95,27 @@ func Parse(in string) (FileDiff, error) {
9595 }, nil
9696}
9797
98+ // diffPath parses the diff header line and extracts the file path.
99+ // Uses Recognize to capture the raw path text from "a/path" format.
98100func diffPath () chomp.Combinator [string ] {
99101 return func (s string ) (string , string , error ) {
100102 // diff --git a/scan/scanner.go b/scan/scanner.go
101- var rem string
102- var err error
103-
104- if rem , _ , err = chomp .Tag ("diff --git " )(s ); err != nil {
105- return rem , "" , err
103+ rem , _ , err := chomp .Tag ("diff --git " )(s )
104+ if err != nil {
105+ return s , "" , err
106106 }
107107
108- var path string
109- if rem , path , err = chomp .Until (" " )(rem ); err != nil {
108+ // Use Recognize to capture "a/path" as raw text, then extract path after "/"
109+ var rawPath string
110+ rem , rawPath , err = chomp .Recognize (
111+ chomp .Pair (chomp .Tag ("a/" ), chomp .Until (" " )),
112+ )(rem )
113+ if err != nil {
110114 return rem , "" , err
111115 }
112- path = path [strings .Index (path , "/" )+ 1 :]
116+
117+ // Strip the "a/" prefix
118+ path := rawPath [2 :]
113119
114120 rem , _ , err = chomp .Eol ()(rem )
115121 return rem , path , err
@@ -166,11 +172,7 @@ func diffChunk() chomp.Combinator[[]string] {
166172 + "bytes"
167173 +)
168174 */
169- var rem string
170- var err error
171-
172- var changes []string
173- rem , changes , err = chomp .Delimited (
175+ rem , changes , err := chomp .Delimited (
174176 chomp .Tag (hdrDelim + " " ),
175177 chomp .SepPair (diffChunkHeaderChange (remPrefix ), chomp .Tag (" " ), diffChunkHeaderChange (addPrefix )),
176178 chomp .Eol (),
@@ -201,16 +203,24 @@ func diffChunk() chomp.Combinator[[]string] {
201203 }
202204}
203205
206+ // diffChunkHeaderChange parses line number and optional count from chunk header.
207+ // Uses Verify to ensure line numbers are valid positive integers.
204208func diffChunkHeaderChange (prefix string ) chomp.Combinator [[]string ] {
205209 return func (s string ) (string , []string , error ) {
210+ // Matches patterns like "-25" or "+3,3"
206211 rem , _ , err := chomp .Tag (prefix )(s )
207212 if err != nil {
208213 return rem , nil , err
209214 }
210215
211216 return chomp .All (
212- chomp .While (chomp .IsDigit ),
213- chomp .Opt (chomp .Prefixed (chomp .While (chomp .IsDigit ), chomp .Tag ("," ))),
217+ // Line number - verify it's a valid positive integer
218+ chomp .Verify (chomp .Digit (), func (s string ) bool {
219+ n , err := strconv .Atoi (s )
220+ return err == nil && n >= 0
221+ }),
222+ // Optional count (,N) - defaults to empty string if not present
223+ chomp .Opt (chomp .Prefixed (chomp .Digit (), chomp .Tag ("," ))),
214224 )(rem )
215225 }
216226}
0 commit comments