|
| 1 | +package git |
| 2 | + |
| 3 | +import "fmt" |
| 4 | + |
| 5 | +func checkPatchValidity(filePath string) error { |
| 6 | + checkApplyArgs := append( |
| 7 | + []string{ |
| 8 | + "apply", |
| 9 | + "--check", |
| 10 | + }, |
| 11 | + filePath, |
| 12 | + ) |
| 13 | + |
| 14 | + return executeGitCommand(checkApplyArgs) |
| 15 | +} |
| 16 | + |
| 17 | +func checkRevertPatchValidity(filePath string) error { |
| 18 | + checkApplyArgs := append( |
| 19 | + []string{ |
| 20 | + "apply", |
| 21 | + "--check", |
| 22 | + "--reverse", |
| 23 | + }, |
| 24 | + filePath, |
| 25 | + ) |
| 26 | + |
| 27 | + return executeGitCommand(checkApplyArgs) |
| 28 | +} |
| 29 | + |
| 30 | +func patchFile(filePath string) error { |
| 31 | + patchFileArgs := append( |
| 32 | + []string{ |
| 33 | + "apply", |
| 34 | + }, |
| 35 | + filePath, |
| 36 | + ) |
| 37 | + |
| 38 | + return executeGitCommand(patchFileArgs) |
| 39 | +} |
| 40 | + |
| 41 | +func unpatchFile(filePath string) error { |
| 42 | + patchFileArgs := append( |
| 43 | + []string{ |
| 44 | + "apply", |
| 45 | + "--reverse", |
| 46 | + }, |
| 47 | + filePath, |
| 48 | + ) |
| 49 | + |
| 50 | + return executeGitCommand(patchFileArgs) |
| 51 | +} |
| 52 | + |
| 53 | +func ApplyPatches(filesToPatch []string) error { |
| 54 | + for _, filePath := range filesToPatch { |
| 55 | + if err := checkPatchValidity(filePath); err != nil { |
| 56 | + fmt.Printf("\t[Error] while trying to patch file %s : %s\n", filePath, err.Error()) |
| 57 | + continue |
| 58 | + } |
| 59 | + if err := patchFile(filePath); err != nil { |
| 60 | + fmt.Printf("\t[Error] while trying to patch file %s : %s\n", filePath, err.Error()) |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + return nil |
| 65 | +} |
| 66 | + |
| 67 | +func RevertPatches(filesToUnpatch []string) error { |
| 68 | + for _, filePath := range filesToUnpatch { |
| 69 | + if err := checkRevertPatchValidity(filePath); err != nil { |
| 70 | + fmt.Printf("\t[Error] while trying to unpatch file %s : %s\n", filePath, err.Error()) |
| 71 | + continue |
| 72 | + } |
| 73 | + if err := unpatchFile(filePath); err != nil { |
| 74 | + fmt.Printf("\t[Error] while trying to unpatch file %s : %s\n", filePath, err.Error()) |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + return nil |
| 79 | +} |
0 commit comments