Skip to content

Commit

Permalink
--ignore-git (#45)
Browse files Browse the repository at this point in the history
* Return error if multiple worktree git repo is found

* Add ignore-git option

* Bump version
  • Loading branch information
raviqqe authored Oct 17, 2020
1 parent dccf1eb commit 5e99865
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 76 deletions.
19 changes: 9 additions & 10 deletions .snapshots/TestHelp
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@ Usage:
rnm.test [options] <from> <to> [<path>]

Application Options:
-b, --bare Use patterns as they are
-c, --cases= Comma-separated names of enabled case styles
(options: camel, upper-camel, kebab, upper-kebab,
snake, upper-snake, space, upper-space)
-e, --exclude= Exclude paths matched with the given regular
expression
--ignore-untracked Ignore untracked files in Git repositories
-v, --verbose Be verbose
-h, --help Show this help
--version Show version
-b, --bare Use patterns as they are
-c, --cases= Comma-separated names of enabled case styles (options:
camel, upper-camel, kebab, upper-kebab, snake, upper-snake,
space, upper-space)
-e, --exclude= Exclude paths matched with the given regular expression
--ignore-git Ignore Git repository information
-v, --verbose Be verbose
-h, --help Show this help
--version Show version

24 changes: 12 additions & 12 deletions argument_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,18 @@ import (
const usage = "[options] <from> <to> [<path>]"

type arguments struct {
Bare bool `short:"b" long:"bare" description:"Use patterns as they are"`
RawCaseNames string `short:"c" long:"cases" description:"Comma-separated names of enabled case styles (options: camel, upper-camel, kebab, upper-kebab, snake, upper-snake, space, upper-space)"`
RawExclude string `short:"e" long:"exclude" description:"Exclude paths matched with the given regular expression"`
IgnoreUntracked bool `long:"ignore-untracked" description:"Ignore untracked files in Git repositories"`
Verbose bool `short:"v" long:"verbose" description:"Be verbose"`
Help bool `short:"h" long:"help" description:"Show this help"`
Version bool `long:"version" description:"Show version"`
From string
To string
Path string
CaseNames map[caseName]struct{}
Exclude *regexp.Regexp
Bare bool `short:"b" long:"bare" description:"Use patterns as they are"`
RawCaseNames string `short:"c" long:"cases" description:"Comma-separated names of enabled case styles (options: camel, upper-camel, kebab, upper-kebab, snake, upper-snake, space, upper-space)"`
RawExclude string `short:"e" long:"exclude" description:"Exclude paths matched with the given regular expression"`
IgnoreGit bool `long:"ignore-git" description:"Ignore Git repository information"`
Verbose bool `short:"v" long:"verbose" description:"Be verbose"`
Help bool `short:"h" long:"help" description:"Show this help"`
Version bool `long:"version" description:"Show version"`
From string
To string
Path string
CaseNames map[caseName]struct{}
Exclude *regexp.Regexp
}

type argumentParser struct {
Expand Down
2 changes: 1 addition & 1 deletion argument_parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func TestParseArguments(t *testing.T) {
{"-c", "camel,kebab", "foo", "bar"},
{"-e", "foo", "foo", "bar"},
{"--exclude", "foo", "foo", "bar"},
{"--ignore-untracked", "foo", "bar"},
{"--ignore-git", "foo", "bar"},
{"-v", "foo", "bar"},
{"--verbose", "foo", "bar"},
{"-h"},
Expand Down
2 changes: 1 addition & 1 deletion command.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func (c *command) Run(ss []string) error {
)
}

ss, err = c.fileFinder.Find(args.Path, args.Exclude, args.IgnoreUntracked)
ss, err = c.fileFinder.Find(args.Path, args.Exclude, args.IgnoreGit)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion configuration.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package main

const (
version = "0.10.0"
version = "0.11.0"
errorChannelCapacity = 512
maxOpenFiles = 200 // lower than 256 on macOS by default
fileTypeDetectionBufferSize = 512
Expand Down
18 changes: 10 additions & 8 deletions file_finder.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ func newFileFinder(f *repositoryFileFinder, fs billy.Filesystem) *fileFinder {
return &fileFinder{f, fs}
}

func (f *fileFinder) Find(d string, excludedPattern *regexp.Regexp, ignoreUntracked bool) ([]string, error) {
fs, err := f.findFiles(d, ignoreUntracked)
func (f *fileFinder) Find(d string, excludedPattern *regexp.Regexp, ignoreGit bool) ([]string, error) {
fs, err := f.findFiles(d, ignoreGit)
if err != nil {
return nil, err
} else if excludedPattern == nil {
Expand All @@ -36,12 +36,14 @@ func (f *fileFinder) Find(d string, excludedPattern *regexp.Regexp, ignoreUntrac
return ffs, nil
}

func (f *fileFinder) findFiles(d string, ignoreUntracked bool) ([]string, error) {
fs, err := f.repositoryFileFinder.Find(d, ignoreUntracked)
if err != nil {
return nil, err
} else if len(fs) != 0 {
return fs, nil
func (f *fileFinder) findFiles(d string, ignoreGit bool) ([]string, error) {
if !ignoreGit {
fs, err := f.repositoryFileFinder.Find(d)
if err != nil {
return nil, err
} else if len(fs) != 0 {
return fs, nil
}
}

return f.findFilesOutsideRepository(d)
Expand Down
10 changes: 10 additions & 0 deletions file_finder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,16 @@ func TestFileFinderIncludePathsNotIncludedInRepository(t *testing.T) {
assert.Equal(t, []string{"bar", "foo"}, normalizePaths(ss))
}

func TestFileFinderIgnoreGitRepositoryInformation(t *testing.T) {
fs := memfs.New()

commitFiles(t, fs, []string{".foo"})

ss, err := newTestFileFinder(fs).Find(".", nil, true)
assert.Nil(t, err)
assert.Equal(t, []string{}, normalizePaths(ss))
}

func TestFileFinderDoNotFindHiddenFile(t *testing.T) {
fs := memfs.New()
_, err := fs.Create(".foo")
Expand Down
42 changes: 23 additions & 19 deletions repository_file_finder.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"fmt"
"path/filepath"
"regexp"

Expand All @@ -21,9 +22,11 @@ func newRepositoryFileFinder(fs billy.Filesystem) *repositoryFileFinder {
return &repositoryFileFinder{fs}
}

func (f *repositoryFileFinder) Find(d string, ignoreUntracked bool) ([]string, error) {
wd := f.findWorktreeDirectory(d)
if wd == "" {
func (f *repositoryFileFinder) Find(d string) ([]string, error) {
wd, err := f.findWorktreeDirectory(d)
if err != nil {
return nil, err
} else if wd == "" {
return nil, nil
}

Expand Down Expand Up @@ -71,20 +74,18 @@ func (f *repositoryFileFinder) Find(d string, ignoreUntracked bool) ([]string, e
return nil, err
}

if !ignoreUntracked {
w, err := r.Worktree()
if err != nil {
return nil, err
}
w, err := r.Worktree()
if err != nil {
return nil, err
}

st, err := w.Status()
if err != nil {
return nil, err
}
st, err := w.Status()
if err != nil {
return nil, err
}

for p := range st {
ps = append(ps, f.fileSystem.Join(wd, p))
}
for p := range st {
ps = append(ps, f.fileSystem.Join(wd, p))
}

pps := make([]string, 0, len(ps))
Expand All @@ -99,13 +100,16 @@ func (f *repositoryFileFinder) Find(d string, ignoreUntracked bool) ([]string, e
return pps, nil
}

func (f *repositoryFileFinder) findWorktreeDirectory(d string) string {
func (f *repositoryFileFinder) findWorktreeDirectory(d string) (string, error) {
for {
i, err := f.fileSystem.Lstat(f.fileSystem.Join(d, ".git"))
p := f.fileSystem.Join(d, ".git")
i, err := f.fileSystem.Lstat(p)
if err == nil && i.IsDir() {
return d
return d, nil
} else if err == nil && !i.IsDir() {
return "", fmt.Errorf("multiple worktrees not supported: %v", p)
} else if err == billy.ErrCrossedBoundary || d == filepath.Dir(d) {
return ""
return "", nil
}

d = filepath.Dir(d)
Expand Down
35 changes: 11 additions & 24 deletions repository_file_finder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func TestRepositoryFileFinderFindNoPath(t *testing.T) {
fs := memfs.New()
commitFiles(t, fs, nil)

ss, err := newRepositoryFileFinder(fs).Find(".", false)
ss, err := newRepositoryFileFinder(fs).Find(".")
assert.Nil(t, err)
assert.Equal(t, []string{}, ss)
}
Expand All @@ -55,7 +55,7 @@ func TestRepositoryFileFinderFindCommittedPath(t *testing.T) {
fs := memfs.New()
commitFiles(t, fs, []string{"foo"})

ss, err := newRepositoryFileFinder(fs).Find(".", false)
ss, err := newRepositoryFileFinder(fs).Find(".")
assert.Nil(t, err)
assert.Equal(t, []string{"foo"}, ss)
}
Expand All @@ -67,23 +67,11 @@ func TestRepositoryFileFinderFindUncommittedPath(t *testing.T) {
_, err := fs.Create("foo")
assert.Nil(t, err)

ss, err := newRepositoryFileFinder(fs).Find(".", false)
ss, err := newRepositoryFileFinder(fs).Find(".")
assert.Nil(t, err)
assert.Equal(t, []string{"foo"}, ss)
}

func TestRepositoryFileFinderDoNotFindUncommittedPath(t *testing.T) {
fs := memfs.New()
commitFiles(t, fs, nil)

_, err := fs.Create("foo")
assert.Nil(t, err)

ss, err := newRepositoryFileFinder(fs).Find(".", true)
assert.Nil(t, err)
assert.Equal(t, []string{}, ss)
}

func TestRepositoryFileFinderDoNotFindIgnoredUncommittedPath(t *testing.T) {
fs := memfs.New()
commitFiles(t, fs, nil)
Expand All @@ -94,7 +82,7 @@ func TestRepositoryFileFinderDoNotFindIgnoredUncommittedPath(t *testing.T) {
_, err = fs.Create("foo")
assert.Nil(t, err)

ss, err := newRepositoryFileFinder(fs).Find(".", false)
ss, err := newRepositoryFileFinder(fs).Find(".")
assert.Nil(t, err)
assert.Equal(t, []string{".gitignore"}, ss)
}
Expand All @@ -107,7 +95,7 @@ func TestRepositoryFileFinderFindPathInsideDirectory(t *testing.T) {

commitFiles(t, fs, []string{"bar/foo"})

ss, err := newRepositoryFileFinder(fs).Find("bar", false)
ss, err := newRepositoryFileFinder(fs).Find("bar")
assert.Nil(t, err)
assert.Equal(t, []string{"bar/foo"}, normalizePaths(ss))
}
Expand All @@ -120,7 +108,7 @@ func TestRepositoryFileFinderDoNotFindPathOutsideDirectory(t *testing.T) {

commitFiles(t, fs, []string{"foo"})

ss, err := newRepositoryFileFinder(fs).Find("bar", false)
ss, err := newRepositoryFileFinder(fs).Find("bar")
assert.Nil(t, err)
assert.Equal(t, []string{}, normalizePaths(ss))
}
Expand All @@ -136,7 +124,7 @@ func TestRepositoryFileFinderFindUncommittedPathInsideDirectory(t *testing.T) {
_, err = fs.Create("bar/foo")
assert.Nil(t, err)

ss, err := newRepositoryFileFinder(fs).Find("bar", false)
ss, err := newRepositoryFileFinder(fs).Find("bar")
assert.Nil(t, err)
assert.Equal(t, []string{"bar/foo"}, normalizePaths(ss))
}
Expand All @@ -155,13 +143,13 @@ func TestRepositoryFileFinderFindPathInDirectory(t *testing.T) {
_, err = fs.Create("bar")
assert.Nil(t, err)

ss, err := newRepositoryFileFinder(fs).Find("foo", false)
ss, err := newRepositoryFileFinder(fs).Find("foo")
assert.Nil(t, err)
assert.Equal(t, []string{"foo/foo"}, normalizePaths(ss))
}

// TODO Support multiple worktrees of the same repositories.
func TestRepositoryFileFinderDoNotFindPathInDifferentWorktree(t *testing.T) {
func TestRepositoryFileFinderFailToFindPathInDifferentWorktree(t *testing.T) {
fs := memfs.New()

err := fs.MkdirAll("foo", 0o755)
Expand All @@ -185,7 +173,6 @@ func TestRepositoryFileFinderDoNotFindPathInDifferentWorktree(t *testing.T) {
_, err = fs.Create("bar/foo")
assert.Nil(t, err)

ss, err := newRepositoryFileFinder(fs).Find("bar", false)
assert.Nil(t, err)
assert.Equal(t, []string(nil), normalizePaths(ss))
_, err = newRepositoryFileFinder(fs).Find("bar")
assert.NotNil(t, err)
}

0 comments on commit 5e99865

Please sign in to comment.