Skip to content

Commit

Permalink
addされていないgit directoryを探す機能 (#2)
Browse files Browse the repository at this point in the history
* add .keep

* add test

* use coverage

* use xerrors

* add feat FindDirtyGit

* fix
  • Loading branch information
kmdkuk authored Jun 12, 2021
1 parent 67909dc commit 1145587
Show file tree
Hide file tree
Showing 13 changed files with 223 additions and 45 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,6 @@ bin/
.history/

tmp

coverage.html
coverage.txt
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ install-go-tools:

test:
./scripts/test_setup.sh
go test ./...
go test -race -timeout 30m -coverprofile=coverage.txt -covermode=atomic ./...
go tool cover -html=coverage.txt -o coverage.html
.PHONY: test

lint:
Expand Down
Empty file added bin/.keep
Empty file.
27 changes: 16 additions & 11 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@ import (
"fmt"

"github.com/kmdkuk/git-push-notifier/lib/file"
"github.com/kmdkuk/git-push-notifier/lib/git"
"github.com/kmdkuk/git-push-notifier/log"
"github.com/spf13/cobra"
"golang.org/x/xerrors"

homedir "github.com/mitchellh/go-homedir"
"github.com/spf13/viper"
Expand All @@ -47,25 +49,28 @@ This application is a tool to generate the needed files
to quickly create a Cobra application.`,
// Uncomment the following line if your bare application
// has an action associated with it:
Run: func(cmd *cobra.Command, args []string) {
RunE: func(cmd *cobra.Command, args []string) error {
f := file.NewFile(filePath)
gitdir, err := f.FindGitDir()
for _, dir := range gitdir {
fmt.Println(dir)
gitDir, err := f.FindGitDir()
if err != nil {
return xerrors.Errorf("%w", err)
}
g := git.NewGit(gitDir)
dirtyDir, err := g.FindDirtyGit()
if err != nil {
log.Fatal(err)
return xerrors.Errorf("%w", err)
}
log.Debugf("%v\n", gitdir)
for _, dir := range dirtyDir {
fmt.Println(dir)
}
return nil
},
}

// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
if err := rootCmd.Execute(); err != nil {
log.Fatal(err)
}
func Execute() error {
return rootCmd.Execute()
}

func init() {
Expand Down Expand Up @@ -93,7 +98,7 @@ func initConfig() {
// Find home directory.
home, err := homedir.Dir()
if err != nil {
log.Fatal(err)
log.Errorf("%+v", err)
}

// Search config in home directory with name ".git-push-notifier" (without extension).
Expand Down
7 changes: 3 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@ go 1.14

require (
github.com/fsnotify/fsnotify v1.4.9 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/go-git/go-git/v5 v5.4.2
github.com/mitchellh/go-homedir v1.1.0
github.com/pkg/errors v0.9.1
github.com/spf13/cobra v1.1.3
github.com/spf13/viper v1.7.1
github.com/stretchr/testify v1.7.0 // indirect
golang.org/x/sync v0.0.0-20190423024810-112230192c58
golang.org/x/sys v0.0.0-20210510120138-977fb7262007 // indirect
golang.org/x/text v0.3.5 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1
)
68 changes: 68 additions & 0 deletions go.sum

Large diffs are not rendered by default.

39 changes: 39 additions & 0 deletions helper/test/test_helper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package test

import (
"path/filepath"
"runtime"
"sort"
)

func GetPath() string {
_, b, _, _ := runtime.Caller(0)
wd := filepath.Dir(b)
testPath := filepath.Join(wd, "..", "..", "tmp", "test")
return testPath
}

func GetGitDirs() []string {
dirAll := make([]string, 0)
dirAll = append(dirAll, GetCleanGitDirs()...)
dirAll = append(dirAll, GetNonStagingGitDirs()...)
return dirAll
}

func GetCleanGitDirs() []string {
testPath := GetPath()
return []string{
filepath.Join(testPath, "plaingitdir"),
}
}

func GetNonStagingGitDirs() []string {
testPath := GetPath()
return []string{
filepath.Join(testPath, "nocommitpushdir"),
}
}

func SortString(strs []string) {
sort.Slice(strs, func(i, j int) bool { return strs[i] < strs[j] })
}
6 changes: 3 additions & 3 deletions lib/file/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"os"
"path/filepath"

"github.com/pkg/errors"
"golang.org/x/xerrors"
)

type File struct {
Expand All @@ -25,15 +25,15 @@ func (f *File) FindGitDir() ([]string, error) {
if _, err := os.Stat(filepath.Join(path, ".git")); !os.IsNotExist(err) && info.IsDir() {
apath, err := filepath.Abs(path)
if err != nil {
return errors.Wrapf(err, "path: %s", path)
return xerrors.Errorf("path = %s, err: %w", path, err)
}
paths = append(paths, apath)
return fs.SkipDir
}
return nil
})
if err != nil {
return nil, errors.WithStack(err)
return nil, xerrors.Errorf("%w", err)
}
return paths, nil
}
31 changes: 6 additions & 25 deletions lib/file/file_test.go
Original file line number Diff line number Diff line change
@@ -1,41 +1,22 @@
package file

import (
"path/filepath"
"reflect"
"runtime"
"sort"
"testing"
)

func testPath() (string, error) {
_, b, _, _ := runtime.Caller(0)
wd := filepath.Dir(b)
testPath := filepath.Join(wd, "..", "..", "tmp", "test")
return testPath, nil
}

func testGitDirs() []string {
testPath, _ := testPath()
return []string{
filepath.Join(testPath, "plaingitdir"),
filepath.Join(testPath, "nocommitpushdir"),
}
}
. "github.com/kmdkuk/git-push-notifier/helper/test"
)

func TestFindGitDir(t *testing.T) {
testPath, err := testPath()
if err != nil {
t.Errorf("error: %+v", err)
}
testPath := GetPath()
f := NewFile(testPath)
actual, err := f.FindGitDir()
if err != nil {
t.Errorf("error: %+v", err)
}
expects := testGitDirs()
sort.Slice(actual, func(i, j int) bool { return actual[i] < actual[j] })
sort.Slice(expects, func(i, j int) bool { return expects[i] < expects[j] })
expects := GetGitDirs()
SortString(actual)
SortString(expects)
if !reflect.DeepEqual(actual, expects) {
t.Errorf("expects: %+v, actual: %+v", expects, actual)
}
Expand Down
56 changes: 56 additions & 0 deletions lib/git/git.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package git

import (
"sync"

gitv5 "github.com/go-git/go-git/v5"
"golang.org/x/sync/errgroup"
"golang.org/x/xerrors"
)

type Git struct {
sync.Mutex
// 対象のgitrepositoryの絶対パスリスト
paths []string
}

func NewGit(paths []string) *Git {
return &Git{
paths: paths,
}
}

func (g *Git) FindDirtyGit() ([]string, error) {
dirtyDir := make([]string, 0)
eg := errgroup.Group{}
for _, dir := range g.paths {
dir := dir
eg.Go(func() error {
r, err := gitv5.PlainOpen(dir)
if err != nil {
return xerrors.Errorf("%w", err)
}

w, err := r.Worktree()
if err != nil {
return xerrors.Errorf("%w", err)
}

status, err := w.Status()
if err != nil {
return xerrors.Errorf("%w", err)
}

g.Lock()
if !status.IsClean() {
dirtyDir = append(dirtyDir, dir)
}
g.Unlock()
return nil
})
}
if err := eg.Wait(); err != nil {
return nil, xerrors.Errorf("%w", err)
}
return dirtyDir, nil
}
24 changes: 24 additions & 0 deletions lib/git/git_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package git

import (
"reflect"
"testing"

. "github.com/kmdkuk/git-push-notifier/helper/test"
)

func TestFindNonStaginGit(t *testing.T) {
gitDirs := GetGitDirs()
g := NewGit(gitDirs)
actual, err := g.FindDirtyGit()
if err != nil {
t.Errorf("error: %+v\n", err)
}
expects := GetNonStagingGitDirs()

SortString(actual)
SortString(expects)
if !reflect.DeepEqual(actual, expects) {
t.Errorf("expects: %+v, actual: %+v", expects, actual)
}
}
4 changes: 3 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ import (

func main() {
log.Debug("start command")
cmd.Execute()
if err := cmd.Execute(); err != nil {
log.Fatalf("%+v", err)
}
log.Debug("finish command")
}
Empty file added tmp/.keep
Empty file.

0 comments on commit 1145587

Please sign in to comment.