Skip to content

Commit bb811bb

Browse files
committed
Added functionality to remove files when upstream files are deleted
1 parent bf3f903 commit bb811bb

4 files changed

Lines changed: 32 additions & 187 deletions

File tree

cmd/init.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,14 +82,11 @@ var initCmd = &cobra.Command{
8282
os.Exit(1)
8383
}
8484

85-
LsFiles := string(gitCmd)
86-
87-
// convert to Array
88-
LsFilesArray := strings.Fields(LsFiles)
85+
LsFiles := strings.Fields(string(gitCmd))
8986

9087
// backup existing dotfiles
91-
if len(LsFilesArray) >= 0 {
92-
for _, filePath := range LsFilesArray {
88+
if len(LsFiles) >= 0 {
89+
for _, filePath := range LsFiles {
9390
os.MkdirAll(utils.DotfileBackupDir+"/"+filepath.Dir(filePath), os.ModePerm)
9491
os.Rename(utils.HomeDir+"/"+filePath, utils.DotfileBackupDir+"/"+filePath)
9592
}

cmd/updateExternals.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ var updateExternalsCmd = &cobra.Command{
1818
Short: "Downloads and updates git externals like plugins, etc",
1919
Long: `Downloads and updates git externals like plugins, etc`,
2020
Run: func(cmd *cobra.Command, args []string) {
21+
// TODO: if files get deleted in upstream
2122
// issue with unmarshalling the yaml config https://github.com/spf13/viper/issues/338
2223
// type configPathAttributes struct {
2324
// url string `mapstructure:"url"`
@@ -60,10 +61,36 @@ var updateExternalsCmd = &cobra.Command{
6061
if externalsPaths != nil {
6162
// clone externals repo
6263
if utils.IsGitRepoDir(repoPath) {
64+
// Get current commit id
65+
currentCommitID, _ := utils.GitCommand(false,
66+
repoPath,
67+
"rev-parse",
68+
"HEAD",
69+
)
70+
6371
isUpToDate, _ = utils.GitCommand(false,
6472
repoPath,
6573
"pull",
6674
)
75+
76+
// Get files deleted between previous commit to latest commit
77+
deletedFiles, _ := utils.GitCommand(false,
78+
repoPath,
79+
"show",
80+
utils.RemoveRunes(string(currentCommitID))+"..HEAD",
81+
"--diff-filter=D",
82+
"--name-only",
83+
"--no-commit-id",
84+
)
85+
86+
deletedFilesList := strings.Fields(string(deletedFiles))
87+
88+
if len(deletedFilesList) >= 0 {
89+
for _, file := range deletedFilesList {
90+
os.Remove(utils.HomeDir + "/" + externalKey + "/" + file)
91+
}
92+
}
93+
6794
} else {
6895
utils.GitCommand(true,
6996
"",

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,18 @@ require (
66
github.com/manifoldco/promptui v0.9.0
77
github.com/spf13/cobra v1.3.0
88
github.com/spf13/viper v1.10.1
9-
github.com/tcnksm/go-gitconfig v0.1.2
109
)
1110

1211
require (
1312
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e // indirect
13+
github.com/cpuguy83/go-md2man/v2 v2.0.1 // indirect
1414
github.com/fsnotify/fsnotify v1.5.1 // indirect
1515
github.com/hashicorp/hcl v1.0.0 // indirect
1616
github.com/inconshreveable/mousetrap v1.0.0 // indirect
1717
github.com/magiconair/properties v1.8.5 // indirect
1818
github.com/mitchellh/mapstructure v1.4.3 // indirect
19-
github.com/onsi/gomega v1.17.0 // indirect
2019
github.com/pelletier/go-toml v1.9.4 // indirect
20+
github.com/russross/blackfriday/v2 v2.1.0 // indirect
2121
github.com/spf13/afero v1.6.0 // indirect
2222
github.com/spf13/cast v1.4.1 // indirect
2323
github.com/spf13/jwalterweatherman v1.1.0 // indirect

0 commit comments

Comments
 (0)