Skip to content

Commit a56d0de

Browse files
feat: basic reversal for patches
1 parent bcb4bcb commit a56d0de

5 files changed

Lines changed: 75 additions & 4 deletions

File tree

internal/filesystem/initialization.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ func ValidatePatchesSetup() error {
121121

122122
if pderr != nil || !pdinfo.IsDir() {
123123
return fmt.Errorf("%s directory has gone missing from inside your devlocal setup!!\n"+
124-
"\tIf you think something is wrong run `devlocal cleanup` and reinit.\n\t\t[Note] this will remove the configs in %s so best backitup\n", config.PATCHES_DIR, config.CONFIG_FILE_NAME)
124+
"\tIf you think something is wrong run `devlocal cleanup` and reinit.\n\t\t[Note] this will remove the configs in %s so best backitup", config.PATCHES_DIR, config.CONFIG_FILE_NAME)
125125
}
126126

127127
return nil

internal/git/patches.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,19 @@ func checkPatchValidity(filePath string) error {
1414
return executeGitCommand(checkApplyArgs)
1515
}
1616

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+
1730
func patchFile(filePath string) error {
1831
patchFileArgs := append(
1932
[]string{
@@ -25,6 +38,18 @@ func patchFile(filePath string) error {
2538
return executeGitCommand(patchFileArgs)
2639
}
2740

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+
2853
func ApplyPatches(filesToPatch []string) error {
2954
for _, filePath := range filesToPatch {
3055
if err := checkPatchValidity(filePath); err != nil {
@@ -38,3 +63,17 @@ func ApplyPatches(filesToPatch []string) error {
3863

3964
return nil
4065
}
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+
}

internal/service/apply/applicator.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ func applyPatches(patchFiles []string) error {
1616

1717
fmt.Println("[Running] patch for files: ", strings.Join(patchFiles, ", "))
1818
if len(patchFiles) == 0 {
19-
fmt.Println("[Warn] No patche files found in patches section of devloca config, skipping")
19+
fmt.Println("[Warn] No patch files found in patches section of devlocal config, skipping")
2020
return nil
2121
}
2222

internal/service/revert/reverter.go

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,26 @@ import (
88
"github.com/RohitRavindra-dev/devlocal/internal/git"
99
)
1010

11+
func revertPatches(patchedfiles []string) error {
12+
if err := filesystem.ValidatePatchesSetup(); err != nil {
13+
return err
14+
}
15+
16+
fmt.Println("[Running] revert patches for files: ", strings.Join(patchedfiles, ", "))
17+
18+
if len(patchedfiles) == 0 {
19+
fmt.Println("[Warn] No patch files found in patches section of devlocal config, skipping")
20+
return nil
21+
}
22+
23+
if err := git.RevertPatches(patchedfiles); err != nil {
24+
return err
25+
}
26+
27+
fmt.Println("[Completed] reverting patches")
28+
return nil
29+
}
30+
1131
func revertOverlook(overlookedFiles []string) error {
1232
fmt.Println("[Running] revert git skip worktree for files: ", strings.Join(overlookedFiles, ", "))
1333

@@ -34,12 +54,16 @@ func Run() error {
3454
return err
3555
}
3656

57+
//revert patches applied
58+
if patchesRevertErr := revertPatches(config.Patches); patchesRevertErr != nil {
59+
return patchesRevertErr
60+
}
61+
3762
// revert overlooked files
3863
if overlookRevertErr := revertOverlook(config.Overlook); overlookRevertErr != nil {
3964
return overlookRevertErr
4065
}
4166

42-
//TODO: revert patches
43-
67+
fmt.Println("[Completed] reverting devlocal changes")
4468
return nil
4569
}

todos.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# todos
2+
3+
1. Apply:
4+
- revert
5+
- status
6+
- conflicts
7+
- overlook
8+
- config

0 commit comments

Comments
 (0)