MCPcopy Create free account
hub / github.com/LegacyCodeHQ/clarity-cli / parseHunkHeader

Function parseHunkHeader

vcs/git/git_diff_lines.go:182–205  ·  view source on GitHub ↗

parseHunkHeader parses a unified-diff hunk header of the form @@ - [, ] + [, ] @@ returning the four line numbers. When count is omitted it defaults to 1.

(header string)

Source from the content-addressed store, hash-verified

180//
181// returning the four line numbers. When count is omitted it defaults to 1.
182func parseHunkHeader(header string) (oldStart, oldCount, newStart, newCount int, ok bool) {
183 if !strings.HasPrefix(header, "@@") {
184 return 0, 0, 0, 0, false
185 }
186 body := strings.TrimPrefix(header, "@@")
187 body = strings.TrimSpace(body)
188 end := strings.Index(body, "@@")
189 if end == -1 {
190 return 0, 0, 0, 0, false
191 }
192 parts := strings.Fields(strings.TrimSpace(body[:end]))
193 if len(parts) < 2 {
194 return 0, 0, 0, 0, false
195 }
196 oldStart, oldCount, ok = parseHunkSide(parts[0], '-')
197 if !ok {
198 return 0, 0, 0, 0, false
199 }
200 newStart, newCount, ok = parseHunkSide(parts[1], '+')
201 if !ok {
202 return 0, 0, 0, 0, false
203 }
204 return oldStart, oldCount, newStart, newCount, true
205}
206
207func parseHunkSide(s string, sign byte) (start, count int, ok bool) {
208 if len(s) == 0 || s[0] != sign {

Callers 2

parseUnifiedDiffFunction · 0.85
TestParseHunkHeaderFunction · 0.85

Calls 1

parseHunkSideFunction · 0.85

Tested by 1

TestParseHunkHeaderFunction · 0.68