Skip to content

Commit

Permalink
Add/rm from git when add/removing files from repositories
Browse files Browse the repository at this point in the history
  • Loading branch information
andornaut committed Nov 14, 2021
1 parent 8ed2de0 commit 47f08ea
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
13 changes: 11 additions & 2 deletions internal/link/link.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"regexp"
"strings"

"github.com/andornaut/gog/internal/git"
"github.com/andornaut/gog/internal/repository"
)

Expand Down Expand Up @@ -109,6 +110,7 @@ func File(repoPath, intPath string) error {
if err == nil {
// Success
printLinked(intPath, extPath)
addToGit(repoPath, intPath, extPath)
return nil
}
if !os.IsExist(err) {
Expand All @@ -122,7 +124,7 @@ func File(repoPath, intPath string) error {
return nil
}
if extFileInfo.IsDir() {
printError(intPath, fmt.Errorf("path expected to be a file, but is a directory: %s", extPath))
printError(intPath, fmt.Errorf("path is expected to be a file, but is a directory: %s", extPath))
return nil
}

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

if actualExtPath == intPath {
// Already linked
addToGit(repoPath, intPath, extPath)
return nil
}

Expand All @@ -154,15 +157,21 @@ func File(repoPath, intPath string) error {
return nil
}
}

if err = os.Symlink(intPath, extPath); err != nil {
printError(intPath, err)
return nil
}
printLinked(intPath, extPath)
addToGit(repoPath, intPath, extPath)
return nil
}

func addToGit(repoPath, intPath, extPath string) {
if err := git.Run(repoPath, "add", intPath); err != nil {
printError(intPath, err)
}
}

func backup(p string) bool {
backupPath := backupPath(p)
if err := os.Rename(p, backupPath); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion internal/link/print.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func printLinked(intPath string, extPath string) {
}

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

func escapeHomeVar(p string) string {
Expand Down
3 changes: 2 additions & 1 deletion internal/link/unlink.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"path/filepath"

"github.com/andornaut/gog/internal/copy"
"github.com/andornaut/gog/internal/git"
"github.com/andornaut/gog/internal/repository"
)

Expand Down Expand Up @@ -47,5 +48,5 @@ func UnlinkFile(repoPath, intPath string) error {
return err
}
printUnLinked(intPath, extPath)
return nil
return git.Run(repoPath, "rm", "-qf", intPath)
}

0 comments on commit 47f08ea

Please sign in to comment.