Skip to content

Commit

Permalink
Add a 'check' command
Browse files Browse the repository at this point in the history
  • Loading branch information
vHanda committed May 15, 2022
1 parent a56e802 commit 67ba39a
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 18 deletions.
2 changes: 1 addition & 1 deletion common/ignore.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"gopkg.in/src-d/go-git.v4/plumbing/format/gitignore"
)

func shouldIgnoreFile(repoPath string, fullFilePath string) (bool, error) {
func ShouldIgnoreFile(repoPath string, fullFilePath string) (bool, error) {
fileName := filepath.Base(fullFilePath)
var isTempFile = strings.HasSuffix(fileName, ".swp") || // vim
strings.HasPrefix(fileName, "~") || // emacs
Expand Down
2 changes: 1 addition & 1 deletion common/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func WatchForChanges(cfg RepoConfig) error {

for {
ei := <-notifyChannel
ignore, err := shouldIgnoreFile(repoPath, ei.Path())
ignore, err := ShouldIgnoreFile(repoPath, ei.Path())
if err != nil {
return tracerr.Wrap(err)
}
Expand Down
23 changes: 7 additions & 16 deletions daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"
"os"
"path/filepath"
"strings"

"github.com/GitJournal/git-auto-sync/common"
cli "github.com/urfave/cli/v2"
Expand Down Expand Up @@ -40,16 +39,12 @@ func daemonList(ctx *cli.Context) error {

func daemonAdd(ctx *cli.Context) error {
repoPath := ctx.Args().First()
if !strings.HasPrefix(repoPath, string(filepath.Separator)) {
cwd, err := os.Getwd()
if err != nil {
return tracerr.Wrap(err)
}

repoPath = filepath.Join(cwd, repoPath)
repoPath, err := filepath.Abs(repoPath)
if err != nil {
return tracerr.Wrap(err)
}

repoPath, err := isValidGitRepo(repoPath)
repoPath, err = isValidGitRepo(repoPath)
if err != nil {
return tracerr.Wrap(err)
}
Expand Down Expand Up @@ -120,13 +115,9 @@ func isValidGitRepo(repoPath string) (string, error) {

func daemonRm(ctx *cli.Context) error {
repoPath := ctx.Args().First()
if !strings.HasPrefix(repoPath, string(filepath.Separator)) {
cwd, err := os.Getwd()
if err != nil {
return tracerr.Wrap(err)
}

repoPath = filepath.Join(cwd, repoPath)
repoPath, err := filepath.Abs(repoPath)
if err != nil {
return tracerr.Wrap(err)
}

config, err := common.ReadConfig()
Expand Down
31 changes: 31 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package main

import (
"fmt"
"log"
"os"
"path/filepath"

cli "github.com/urfave/cli/v2"
"github.com/ztrue/tracerr"
Expand Down Expand Up @@ -50,6 +52,35 @@ func main() {
return nil
},
},
{
Name: "check",
Usage: "Check if a file will be ignored",
Action: func(ctx *cli.Context) error {
repoPath, err := os.Getwd()
if err != nil {
return tracerr.Wrap(err)
}

repoPath, err = isValidGitRepo(repoPath)
if err != nil {
return tracerr.Wrap(err)
}

path := ctx.Args().First()
path, err = filepath.Abs(path)
if err != nil {
return tracerr.Wrap(err)
}

ignored, err := common.ShouldIgnoreFile(repoPath, path)
if err != nil {
return tracerr.Wrap(err)
}
fmt.Println("Ignored:", ignored)

return nil
},
},
{
Name: "daemon",
Aliases: []string{"d"},
Expand Down

0 comments on commit 67ba39a

Please sign in to comment.