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 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 } ··· 52 51 return false 53 52 } 54 53 55 - // Check for common patch format markers 56 54 firstLine := strings.TrimSpace(lines[0]) 57 55 return strings.HasPrefix(firstLine, "diff ") || 58 56 strings.HasPrefix(firstLine, "--- ") || ··· 92 90 } 93 91 94 92 func splitFormatPatch(patchText string) []string { 95 - // The pattern to match is "From " followed by a commit hash and the rest of that line 96 93 re := regexp.MustCompile(`(?m)^From [0-9a-f]{40} .*$`) 97 94 98 - // Find all starting positions of patches 99 95 indexes := re.FindAllStringIndex(patchText, -1) 100 96 101 97 if len(indexes) == 0 { 102 - // No patches found 103 98 return []string{} 104 99 } 105 100 ··· 109 104 startPos := indexes[i][0] 110 105 endPos := len(patchText) 111 106 112 - // If there's a next patch, set end position to the start of the next patch 113 107 if i < len(indexes)-1 { 114 108 endPos = indexes[i+1][0] 115 109 } 116 110 117 - // Extract the patch and trim any whitespace 118 111 patches[i] = strings.TrimSpace(patchText[startPos:endPos]) 119 112 } 120 113 return patches