Skip to content

Commit 47f08ea

Browse files
committed
Add/rm from git when add/removing files from repositories
1 parent 8ed2de0 commit 47f08ea

File tree

3 files changed

+14
-4
lines changed

3 files changed

+14
-4
lines changed

internal/link/link.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"regexp"
1111
"strings"
1212

13+
"github.com/andornaut/gog/internal/git"
1314
"github.com/andornaut/gog/internal/repository"
1415
)
1516

@@ -109,6 +110,7 @@ func File(repoPath, intPath string) error {
109110
if err == nil {
110111
// Success
111112
printLinked(intPath, extPath)
113+
addToGit(repoPath, intPath, extPath)
112114
return nil
113115
}
114116
if !os.IsExist(err) {
@@ -122,7 +124,7 @@ func File(repoPath, intPath string) error {
122124
return nil
123125
}
124126
if extFileInfo.IsDir() {
125-
printError(intPath, fmt.Errorf("path expected to be a file, but is a directory: %s", extPath))
127+
printError(intPath, fmt.Errorf("path is expected to be a file, but is a directory: %s", extPath))
126128
return nil
127129
}
128130

@@ -139,6 +141,7 @@ func File(repoPath, intPath string) error {
139141

140142
if actualExtPath == intPath {
141143
// Already linked
144+
addToGit(repoPath, intPath, extPath)
142145
return nil
143146
}
144147

@@ -154,15 +157,21 @@ func File(repoPath, intPath string) error {
154157
return nil
155158
}
156159
}
157-
158160
if err = os.Symlink(intPath, extPath); err != nil {
159161
printError(intPath, err)
160162
return nil
161163
}
162164
printLinked(intPath, extPath)
165+
addToGit(repoPath, intPath, extPath)
163166
return nil
164167
}
165168

169+
func addToGit(repoPath, intPath, extPath string) {
170+
if err := git.Run(repoPath, "add", intPath); err != nil {
171+
printError(intPath, err)
172+
}
173+
}
174+
166175
func backup(p string) bool {
167176
backupPath := backupPath(p)
168177
if err := os.Rename(p, backupPath); err != nil {

internal/link/print.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ func printLinked(intPath string, extPath string) {
1515
}
1616

1717
func printUnLinked(intPath string, extPath string) {
18-
fmt.Printf("%s -> %s\n", escapeHomeVar(intPath), extPath)
18+
fmt.Printf("Removed: %s\n", escapeHomeVar(intPath))
1919
}
2020

2121
func escapeHomeVar(p string) string {

internal/link/unlink.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"path/filepath"
66

77
"github.com/andornaut/gog/internal/copy"
8+
"github.com/andornaut/gog/internal/git"
89
"github.com/andornaut/gog/internal/repository"
910
)
1011

@@ -47,5 +48,5 @@ func UnlinkFile(repoPath, intPath string) error {
4748
return err
4849
}
4950
printUnLinked(intPath, extPath)
50-
return nil
51+
return git.Run(repoPath, "rm", "-qf", intPath)
5152
}

0 commit comments

Comments
 (0)