···
type FormatPatch struct {
func ExtractPatches(formatPatch string) ([]FormatPatch, error) {
···
result := []FormatPatch{}
for _, patch := range patches {
-
_, headerStr, err := gitdiff.Parse(strings.NewReader(patch))
return nil, fmt.Errorf("failed to parse patch: %w", err)
···
result = append(result, FormatPatch{
-
// Very basic validation to check if it looks like a diff/patch
-
// A valid patch usually starts with diff or --- lines or git format-patch header
func IsPatchValid(patch string) bool {
-
// Basic validation to check if it looks like a diff/patch
-
// A valid patch usually starts with diff or --- lines
···
-
// Check for common patch format markers
firstLine := strings.TrimSpace(lines[0])
return strings.HasPrefix(firstLine, "diff ") ||
strings.HasPrefix(firstLine, "--- ") ||
···
func splitFormatPatch(patchText string) []string {
-
// 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} .*$`)
-
// Find all starting positions of patches
indexes := re.FindAllStringIndex(patchText, -1)
···
startPos := indexes[i][0]
-
// If there's a next patch, set end position to the start of the next patch
-
// Extract the patch and trim any whitespace
patches[i] = strings.TrimSpace(patchText[startPos:endPos])
···
type FormatPatch struct {
func ExtractPatches(formatPatch string) ([]FormatPatch, error) {
···
result := []FormatPatch{}
for _, patch := range patches {
+
files, headerStr, err := gitdiff.Parse(strings.NewReader(patch))
return nil, fmt.Errorf("failed to parse patch: %w", err)
···
result = append(result, FormatPatch{
+
// IsPatchValid checks if the given patch string is valid.
+
// It performs very basic sniffing for either git-diff or git-format-patch
func IsPatchValid(patch string) bool {
···
firstLine := strings.TrimSpace(lines[0])
return strings.HasPrefix(firstLine, "diff ") ||
strings.HasPrefix(firstLine, "--- ") ||
···
func splitFormatPatch(patchText string) []string {
re := regexp.MustCompile(`(?m)^From [0-9a-f]{40} .*$`)
indexes := re.FindAllStringIndex(patchText, -1)
···
startPos := indexes[i][0]
patches[i] = strings.TrimSpace(patchText[startPos:endPos])