Skip to content

Commit bfcea3f

Browse files
Merge pull request #12 from RohitRavindra-dev/feat/v1/apply-patches
Feat/v1/apply patches
2 parents d583e2f + a56d0de commit bfcea3f

8 files changed

Lines changed: 194 additions & 29 deletions

File tree

internal/config/constants.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const (
55
APP_NAME = "devlocal"
66
PROJECT_ROOT = ".devlocal"
77
CONFIG_FILE_NAME = "config.yaml"
8+
PATCHES_DIR = "patches"
89
)
910

1011
var (

internal/filesystem/initialization.go

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,13 @@ func createRootDirectory() error {
2929
return os.Mkdir(config.PROJECT_ROOT, 0755)
3030
}
3131

32+
func createPatchesDirectory() error {
33+
return os.MkdirAll(
34+
filepath.Join(config.PROJECT_ROOT, config.PATCHES_DIR),
35+
0755,
36+
)
37+
}
38+
3239
func createConfigFile() error {
3340
err := os.WriteFile(
3441
filepath.Join(config.PROJECT_ROOT, config.CONFIG_FILE_NAME),
@@ -64,11 +71,16 @@ func InitilizeDevLocalFilesystem() error {
6471
if err := createConfigFile(); err != nil {
6572
return err
6673
}
67-
74+
// populate config file with basics
6875
if err := seedYamlConfigFile(); err != nil {
6976
return err
7077
}
7178

79+
// where my patches will go
80+
if err := createPatchesDirectory(); err != nil {
81+
return err
82+
}
83+
7284
fmt.Println("[Completed] initializing devlocal setup for your repo")
7385
return nil
7486
}
@@ -103,3 +115,14 @@ func ValidateDevLocalFilesystem() error {
103115

104116
return nil
105117
}
118+
119+
func ValidatePatchesSetup() error {
120+
pdinfo, pderr := os.Stat(filepath.Join(config.PROJECT_ROOT, config.PATCHES_DIR))
121+
122+
if pderr != nil || !pdinfo.IsDir() {
123+
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", config.PATCHES_DIR, config.CONFIG_FILE_NAME)
125+
}
126+
127+
return nil
128+
}

internal/git/patches.go

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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+
}

internal/git/utils.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package git
2+
3+
import (
4+
"fmt"
5+
"os/exec"
6+
)
7+
8+
func executeGitCommand(args []string) error {
9+
cmd := exec.Command("git", args...)
10+
11+
out, err := cmd.CombinedOutput()
12+
13+
if err != nil {
14+
return fmt.Errorf("Failed to run git command: %s", out)
15+
}
16+
17+
return nil
18+
}
19+
20+
func isFileTracked(file string) bool {
21+
trackCheckArgs := []string{
22+
"ls-files",
23+
"--error-unmatch",
24+
file,
25+
}
26+
27+
err := executeGitCommand(trackCheckArgs)
28+
29+
return err == nil
30+
}

internal/git/worktrees.go

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,6 @@ import (
55
"os/exec"
66
)
77

8-
func executeGitCommand(args []string) error {
9-
cmd := exec.Command("git", args...)
10-
11-
out, err := cmd.CombinedOutput()
12-
13-
if err != nil {
14-
return fmt.Errorf("[Error] Failed to run Skip Worktree: %s", out)
15-
}
16-
17-
return nil
18-
}
19-
208
func SkipWorkTree(files []string) error {
219

2210
filesToSkip := sanitizeToTrackedFiles(files)
@@ -49,18 +37,6 @@ func NoSkipWorkTree(files []string) error {
4937
return executeGitCommand(skipWorktreeArgs)
5038
}
5139

52-
func isFileTracked(file string) bool {
53-
trackCheckArgs := []string{
54-
"ls-files",
55-
"--error-unmatch",
56-
file,
57-
}
58-
59-
err := executeGitCommand(trackCheckArgs)
60-
61-
return err == nil
62-
}
63-
6440
func sanitizeToTrackedFiles(files []string) []string {
6541

6642
filesTracked := []string{}

internal/service/apply/applicator.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 applyPatches(patchFiles []string) error {
12+
13+
if err := filesystem.ValidatePatchesSetup(); err != nil {
14+
return err
15+
}
16+
17+
fmt.Println("[Running] patch for files: ", strings.Join(patchFiles, ", "))
18+
if len(patchFiles) == 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.ApplyPatches(patchFiles); err != nil {
24+
return err
25+
}
26+
27+
fmt.Println("[Completed] applying patches")
28+
return nil
29+
}
30+
1131
func applyOverlook(overlookFiles []string) error {
1232
fmt.Println("[Running] git skip worktree for files: ", strings.Join(overlookFiles, ", "))
1333
if len(overlookFiles) == 0 {
@@ -32,12 +52,16 @@ func Run() error {
3252
return err
3353
}
3454

55+
// apply patches
56+
if patchingErr := applyPatches(config.Patches); patchingErr != nil {
57+
return patchingErr
58+
}
59+
3560
// overlook files
3661
if overlookErr := applyOverlook(config.Overlook); overlookErr != nil {
3762
return overlookErr
3863
}
3964

40-
// TODO: apply patches
41-
65+
fmt.Println("[Completed] applying devlocal changes")
4266
return nil
4367
}

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)