···99)10101111type FormatPatch struct {1212+ Files []*gitdiff.File1213 *gitdiff.PatchHeader1313- Patch string1414}15151616func ExtractPatches(formatPatch string) ([]FormatPatch, error) {···1919 result := []FormatPatch{}20202121 for _, patch := range patches {2222- _, headerStr, err := gitdiff.Parse(strings.NewReader(patch))2222+ files, headerStr, err := gitdiff.Parse(strings.NewReader(patch))2323 if err != nil {2424 return nil, fmt.Errorf("failed to parse patch: %w", err)2525 }···3030 }31313232 result = append(result, FormatPatch{3333+ Files: files,3334 PatchHeader: header,3434- Patch: patch,3535 })3636 }37373838 return result, nil3939}40404141-// Very basic validation to check if it looks like a diff/patch4242-// A valid patch usually starts with diff or --- lines or git format-patch header4141+// IsPatchValid checks if the given patch string is valid.4242+// It performs very basic sniffing for either git-diff or git-format-patch4343+// header lines.4344func IsPatchValid(patch string) bool {4444- // Basic validation to check if it looks like a diff/patch4545- // A valid patch usually starts with diff or --- lines4645 if len(patch) == 0 {4746 return false4847 }···5152 return false5253 }53545454- // Check for common patch format markers5555 firstLine := strings.TrimSpace(lines[0])5656 return strings.HasPrefix(firstLine, "diff ") ||5757 strings.HasPrefix(firstLine, "--- ") ||···9092}91939294func splitFormatPatch(patchText string) []string {9393- // The pattern to match is "From " followed by a commit hash and the rest of that line9495 re := regexp.MustCompile(`(?m)^From [0-9a-f]{40} .*$`)95969696- // Find all starting positions of patches9797 indexes := re.FindAllStringIndex(patchText, -1)98989999 if len(indexes) == 0 {100100- // No patches found101100 return []string{}102101 }103102···104109 startPos := indexes[i][0]105110 endPos := len(patchText)106111107107- // If there's a next patch, set end position to the start of the next patch108112 if i < len(indexes)-1 {109113 endPos = indexes[i+1][0]110114 }111115112112- // Extract the patch and trim any whitespace113116 patches[i] = strings.TrimSpace(patchText[startPos:endPos])114117 }115118 return patches