Mirror of @tangled.org/core. Running on a Raspberry Pi Zero 2 (Please be gentle).

patchutil: clean up

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

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