···
type FormatPatch struct {
12
+
Files []*gitdiff.File
func ExtractPatches(formatPatch string) ([]FormatPatch, error) {
···
result := []FormatPatch{}
for _, patch := range patches {
22
-
_, headerStr, err := gitdiff.Parse(strings.NewReader(patch))
22
+
files, headerStr, err := gitdiff.Parse(strings.NewReader(patch))
return nil, fmt.Errorf("failed to parse patch: %w", err)
···
result = append(result, FormatPatch{
41
-
// Very basic validation to check if it looks like a diff/patch
42
-
// A valid patch usually starts with diff or --- lines or git format-patch header
41
+
// IsPatchValid checks if the given patch string is valid.
42
+
// It performs very basic sniffing for either git-diff or git-format-patch
func IsPatchValid(patch string) bool {
44
-
// Basic validation to check if it looks like a diff/patch
45
-
// A valid patch usually starts with diff or --- lines
···
55
-
// Check for common patch format markers
firstLine := strings.TrimSpace(lines[0])
return strings.HasPrefix(firstLine, "diff ") ||
strings.HasPrefix(firstLine, "--- ") ||
···
func splitFormatPatch(patchText string) []string {
95
-
// The pattern to match is "From " followed by a commit hash and the rest of that line
re := regexp.MustCompile(`(?m)^From [0-9a-f]{40} .*$`)
98
-
// Find all starting positions of patches
indexes := re.FindAllStringIndex(patchText, -1)
102
-
// No patches found
···
startPos := indexes[i][0]
112
-
// If there's a next patch, set end position to the start of the next patch
117
-
// Extract the patch and trim any whitespace
patches[i] = strings.TrimSpace(patchText[startPos:endPos])