|
1 | 1 | package patch
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "bufio" |
4 | 5 | "fmt"
|
5 | 6 | "os"
|
6 | 7 | "os/exec"
|
| 8 | + "path/filepath" |
| 9 | + "regexp" |
| 10 | + "strings" |
7 | 11 |
|
8 | 12 | "github.com/devstream-io/devstream/internal/log"
|
9 | 13 | )
|
10 | 14 |
|
| 15 | +const ( |
| 16 | + processOptionTabToSpace ProcessOption = "tabToSpace" |
| 17 | + processOptionSpaceToTab ProcessOption = "spaceToTab" |
| 18 | +) |
| 19 | + |
| 20 | +type ProcessOption string |
| 21 | + |
11 | 22 | // Patch calls the patch command to apply a diff file to an original
|
12 | 23 | func Patch(workDir, patchFile string) error {
|
13 | 24 | log.Infof("Patching file: %s", patchFile)
|
14 | 25 |
|
| 26 | + // Fix patch file if it mixed tab and space indentation |
| 27 | + err := fixPatchFile(workDir, patchFile) |
| 28 | + if err != nil { |
| 29 | + return fmt.Errorf("patch file fix failed: %w", err) |
| 30 | + } |
| 31 | + |
15 | 32 | // Check if the patch command exists and is executable
|
16 |
| - err := checkPatchCommand() |
| 33 | + err = checkPatchCommand() |
17 | 34 | if err != nil {
|
18 | 35 | return fmt.Errorf("patch command check failed: %w", err)
|
19 | 36 | }
|
@@ -50,3 +67,135 @@ func checkPatchCommand() error {
|
50 | 67 |
|
51 | 68 | return nil
|
52 | 69 | }
|
| 70 | + |
| 71 | +// fixPatchFile fixes the patch file if it mixed tab and space indentation. |
| 72 | +// The patch file is generated by GPT4, and it may have different indentation with the original file. |
| 73 | +// The original file path is contained in the patch file, so we can use the fix the patch file by using the original file. |
| 74 | +// If the original file uses tab indentation, we replace all spaces with tabs in the patch file. |
| 75 | +// If the original file uses space indentation, we replace all tabs with spaces in the patch file. |
| 76 | +func fixPatchFile(workDir, patchFile string) error { |
| 77 | + // Read the original file path from the patch file |
| 78 | + originalFilePath, err := extractOriginalFilePathFromPatchFile(patchFile) |
| 79 | + originalFilePath = filepath.Join(workDir, originalFilePath) |
| 80 | + |
| 81 | + if err != nil { |
| 82 | + return fmt.Errorf("failed to extract original file path from patch string: %w", err) |
| 83 | + } |
| 84 | + |
| 85 | + // Check if the original file contain tabs in the indentation |
| 86 | + original, err := os.Open(originalFilePath) |
| 87 | + if err != nil { |
| 88 | + return fmt.Errorf("failed to open original file: %w", err) |
| 89 | + } |
| 90 | + defer original.Close() |
| 91 | + |
| 92 | + hasTab := false |
| 93 | + scanner := bufio.NewScanner(original) |
| 94 | + for scanner.Scan() { |
| 95 | + line := scanner.Text() |
| 96 | + if strings.HasPrefix(line, "\t") { |
| 97 | + hasTab = true |
| 98 | + break |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + if err = scanner.Err(); err != nil { |
| 103 | + return fmt.Errorf("failed to read original file: %w", err) |
| 104 | + } |
| 105 | + |
| 106 | + // The original file uses tab indentation |
| 107 | + if hasTab { |
| 108 | + // Replace all space indentation with tabs in the patch file |
| 109 | + if err = processTabSpaceSwitch(patchFile, processOptionSpaceToTab); err != nil { |
| 110 | + return fmt.Errorf("failed to process tab to space: %w", err) |
| 111 | + } |
| 112 | + // The original file uses space indentation |
| 113 | + } else { |
| 114 | + // Replace all tab indentation with spaces in the patch file |
| 115 | + if err = processTabSpaceSwitch(patchFile, processOptionTabToSpace); err != nil { |
| 116 | + return fmt.Errorf("failed to process space to tab: %w", err) |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + return nil |
| 121 | + |
| 122 | +} |
| 123 | + |
| 124 | +// ExtractOriginalFilePathFromPatchString extracts the original file path from a patch string |
| 125 | +// e.g. --- pkg/patch/patch.go 2021-08-15 16:00:00.000000000 +0900 -> pkg/patch/patch.go |
| 126 | +func extractOriginalFilePathFromPatchFile(patchFile string) (string, error) { |
| 127 | + // Read content from the patch file |
| 128 | + fileContent, err := os.ReadFile(patchFile) |
| 129 | + if err != nil { |
| 130 | + return "", fmt.Errorf("failed to read patch file: %w", err) |
| 131 | + } |
| 132 | + |
| 133 | + lines := strings.Split(string(fileContent), "\n") |
| 134 | + |
| 135 | + for _, line := range lines { |
| 136 | + if strings.HasPrefix(line, "--- ") { |
| 137 | + fields := strings.Fields(line) |
| 138 | + if len(fields) > 1 { |
| 139 | + return fields[1], nil |
| 140 | + } |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + return "", fmt.Errorf("original file path not found in patch string") |
| 145 | +} |
| 146 | + |
| 147 | +// processTabSpaceSwitch processes the tab/space indentation switch in a file |
| 148 | +// If the option is processOptionTabToSpace, it replaces all tabs with spaces |
| 149 | +// If the option is processOptionSpaceToTab, it replaces all spaces with tabs |
| 150 | +func processTabSpaceSwitch(filePath string, option ProcessOption) error { |
| 151 | + file, err := os.Open(filePath) |
| 152 | + if err != nil { |
| 153 | + return fmt.Errorf("failed to open file: %w", err) |
| 154 | + } |
| 155 | + defer file.Close() |
| 156 | + |
| 157 | + scanner := bufio.NewScanner(file) |
| 158 | + var processedLines []string |
| 159 | + |
| 160 | + // Matches the start of the string (^) followed by an optional + or - sign, followed by one or more groups of 4 spaces ( {4})+ |
| 161 | + spaceRegex := regexp.MustCompile(`^(\+|\-)?( {4})+`) |
| 162 | + // Matches the start of the string (^) followed by an optional + or - sign, followed by one or more tabs (\t)+ |
| 163 | + tabRegex := regexp.MustCompile(`^(\+|\-)?\t+`) |
| 164 | + |
| 165 | + for scanner.Scan() { |
| 166 | + line := scanner.Text() |
| 167 | + if option == processOptionTabToSpace { |
| 168 | + line = tabRegex.ReplaceAllStringFunc(line, func(s string) string { |
| 169 | + prefix := "" |
| 170 | + if s[0] == '+' || s[0] == '-' { |
| 171 | + prefix = string(s[0]) |
| 172 | + s = s[1:] |
| 173 | + } |
| 174 | + return prefix + strings.Repeat(" ", len(s)) |
| 175 | + }) |
| 176 | + } else if option == processOptionSpaceToTab { |
| 177 | + line = spaceRegex.ReplaceAllStringFunc(line, func(s string) string { |
| 178 | + prefix := "" |
| 179 | + if s[0] == '+' || s[0] == '-' { |
| 180 | + prefix = string(s[0]) |
| 181 | + s = s[1:] |
| 182 | + } |
| 183 | + return prefix + strings.Repeat("\t", len(s)/4) |
| 184 | + }) |
| 185 | + } else { |
| 186 | + return fmt.Errorf("invalid process option: %s", option) |
| 187 | + } |
| 188 | + processedLines = append(processedLines, line) |
| 189 | + } |
| 190 | + |
| 191 | + if err = scanner.Err(); err != nil { |
| 192 | + return fmt.Errorf("failed to read file: %w", err) |
| 193 | + } |
| 194 | + |
| 195 | + err = os.WriteFile(filePath, []byte(strings.Join(processedLines, "\n")+"\n"), 0644) |
| 196 | + if err != nil { |
| 197 | + return fmt.Errorf("failed to write file: %w", err) |
| 198 | + } |
| 199 | + |
| 200 | + return nil |
| 201 | +} |
0 commit comments