···99)
10101111type FormatPatch struct {
1212+ Files []*gitdiff.File
1213 *gitdiff.PatchHeader
1313- Patch string
1414}
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, nil
3939}
40404141-// Very basic validation to check if it looks like a diff/patch
4242-// A valid patch usually starts with diff or --- lines or git format-patch header
4141+// IsPatchValid checks if the given patch string is valid.
4242+// It performs very basic sniffing for either git-diff or git-format-patch
4343+// header lines.
4344func IsPatchValid(patch string) bool {
4444- // Basic validation to check if it looks like a diff/patch
4545- // A valid patch usually starts with diff or --- lines
4645 if len(patch) == 0 {
4746 return false
4847 }
···5251 return false
5352 }
54535555- // Check for common patch format markers
5654 firstLine := strings.TrimSpace(lines[0])
5755 return strings.HasPrefix(firstLine, "diff ") ||
5856 strings.HasPrefix(firstLine, "--- ") ||
···9290}
93919492func splitFormatPatch(patchText string) []string {
9595- // The pattern to match is "From " followed by a commit hash and the rest of that line
9693 re := regexp.MustCompile(`(?m)^From [0-9a-f]{40} .*$`)
97949898- // Find all starting positions of patches
9995 indexes := re.FindAllStringIndex(patchText, -1)
1009610197 if len(indexes) == 0 {
102102- // No patches found
10398 return []string{}
10499 }
105100···109104 startPos := indexes[i][0]
110105 endPos := len(patchText)
111106112112- // If there's a next patch, set end position to the start of the next patch
113107 if i < len(indexes)-1 {
114108 endPos = indexes[i+1][0]
115109 }
116110117117- // Extract the patch and trim any whitespace
118111 patches[i] = strings.TrimSpace(patchText[startPos:endPos])
119112 }
120113 return patches