feat(pierre): sanity check
This commit is contained in:
@@ -88,6 +88,10 @@ If project guidelines are provided, treat them as hard rules that must be respec
|
||||
// It tries to split on file boundaries ("diff --git") first, then on hunk boundaries (@@),
|
||||
// and finally on a hard byte limit.
|
||||
func splitDiffIntoChunks(diff []byte, maxSize int) []string {
|
||||
// Preserve the file header for each chunk when a single file's diff is split across multiple chunks.
|
||||
// The header is the portion before the first hunk marker "@@" (including the "diff --git" line).
|
||||
// When we need to split by hunks, we prepend this header to every resulting sub‑chunk.
|
||||
|
||||
if len(diff) <= maxSize {
|
||||
return []string{string(diff)}
|
||||
}
|
||||
@@ -107,22 +111,31 @@ func splitDiffIntoChunks(diff []byte, maxSize int) []string {
|
||||
current.Reset()
|
||||
}
|
||||
if len(seg) > maxSize {
|
||||
// Split further by hunks
|
||||
hunks := strings.Split(seg, "\n@@ ")
|
||||
for j, h := range hunks {
|
||||
var hseg string
|
||||
if j == 0 {
|
||||
// First hunk segment already contains the preceding content (including any needed newline)
|
||||
hseg = h
|
||||
} else {
|
||||
// Subsequent hunks need the leading newline and "@@ " marker restored
|
||||
hseg = "\n@@ " + h
|
||||
// Determine if there is a hunk marker. If not, fall back to simple size‑based chunking.
|
||||
headerEnd := strings.Index(seg, "\n@@ ")
|
||||
if headerEnd == -1 {
|
||||
// No hunk marker – split purely by size.
|
||||
remaining := seg
|
||||
for len(remaining) > maxSize {
|
||||
chunks = append(chunks, remaining[:maxSize])
|
||||
remaining = remaining[maxSize:]
|
||||
}
|
||||
current.WriteString(remaining)
|
||||
continue
|
||||
}
|
||||
// Preserve the header up to the first hunk.
|
||||
header := seg[:headerEnd+1] // include newline before "@@"
|
||||
// Split the rest of the segment into hunks (excluding the header part).
|
||||
hunks := strings.Split(strings.TrimPrefix(seg, header), "\n@@ ")
|
||||
for _, h := range hunks {
|
||||
// Reconstruct each hunk with its header and "@@ " prefix.
|
||||
hseg := header + "@@ " + h
|
||||
if current.Len()+len(hseg) > maxSize && current.Len() > 0 {
|
||||
chunks = append(chunks, current.String())
|
||||
current.Reset()
|
||||
}
|
||||
if len(hseg) > maxSize {
|
||||
// If a single hunk exceeds maxSize, split it further.
|
||||
for len(hseg) > maxSize {
|
||||
chunks = append(chunks, hseg[:maxSize])
|
||||
hseg = hseg[maxSize:]
|
||||
|
||||
Reference in New Issue
Block a user