forked from tangled.org/core
Monorepo for Tangled

patchutil: clean up

authored by anirudh.fi and committed by oppi.li 77809623 e1357392

Changed files
+6 -13
patchutil
+6 -13
patchutil/patchutil.go
··· 9 ) 10 11 type FormatPatch struct { 12 *gitdiff.PatchHeader 13 - Patch string 14 } 15 16 func ExtractPatches(formatPatch string) ([]FormatPatch, error) { ··· 19 result := []FormatPatch{} 20 21 for _, patch := range patches { 22 - _, headerStr, err := gitdiff.Parse(strings.NewReader(patch)) 23 if err != nil { 24 return nil, fmt.Errorf("failed to parse patch: %w", err) 25 } ··· 30 } 31 32 result = append(result, FormatPatch{ 33 PatchHeader: header, 34 - Patch: patch, 35 }) 36 } 37 38 return result, nil 39 } 40 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 43 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 46 if len(patch) == 0 { 47 return false 48 } ··· 52 return false 53 } 54 55 - // Check for common patch format markers 56 firstLine := strings.TrimSpace(lines[0]) 57 return strings.HasPrefix(firstLine, "diff ") || 58 strings.HasPrefix(firstLine, "--- ") || ··· 92 } 93 94 func splitFormatPatch(patchText string) []string { 95 - // The pattern to match is "From " followed by a commit hash and the rest of that line 96 re := regexp.MustCompile(`(?m)^From [0-9a-f]{40} .*$`) 97 98 - // Find all starting positions of patches 99 indexes := re.FindAllStringIndex(patchText, -1) 100 101 if len(indexes) == 0 { 102 - // No patches found 103 return []string{} 104 } 105 ··· 109 startPos := indexes[i][0] 110 endPos := len(patchText) 111 112 - // If there's a next patch, set end position to the start of the next patch 113 if i < len(indexes)-1 { 114 endPos = indexes[i+1][0] 115 } 116 117 - // Extract the patch and trim any whitespace 118 patches[i] = strings.TrimSpace(patchText[startPos:endPos]) 119 } 120 return patches
··· 9 ) 10 11 type FormatPatch struct { 12 + Files []*gitdiff.File 13 *gitdiff.PatchHeader 14 } 15 16 func ExtractPatches(formatPatch string) ([]FormatPatch, error) { ··· 19 result := []FormatPatch{} 20 21 for _, patch := range patches { 22 + files, headerStr, err := gitdiff.Parse(strings.NewReader(patch)) 23 if err != nil { 24 return nil, fmt.Errorf("failed to parse patch: %w", err) 25 } ··· 30 } 31 32 result = append(result, FormatPatch{ 33 + Files: files, 34 PatchHeader: header, 35 }) 36 } 37 38 return result, nil 39 } 40 41 + // IsPatchValid checks if the given patch string is valid. 42 + // It performs very basic sniffing for either git-diff or git-format-patch 43 + // header lines. 44 func IsPatchValid(patch string) bool { 45 if len(patch) == 0 { 46 return false 47 } ··· 51 return false 52 } 53 54 firstLine := strings.TrimSpace(lines[0]) 55 return strings.HasPrefix(firstLine, "diff ") || 56 strings.HasPrefix(firstLine, "--- ") || ··· 90 } 91 92 func splitFormatPatch(patchText string) []string { 93 re := regexp.MustCompile(`(?m)^From [0-9a-f]{40} .*$`) 94 95 indexes := re.FindAllStringIndex(patchText, -1) 96 97 if len(indexes) == 0 { 98 return []string{} 99 } 100 ··· 104 startPos := indexes[i][0] 105 endPos := len(patchText) 106 107 if i < len(indexes)-1 { 108 endPos = indexes[i+1][0] 109 } 110 111 patches[i] = strings.TrimSpace(patchText[startPos:endPos]) 112 } 113 return patches