generated from kmdkuk/goct
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add .keep * add test * use coverage * use xerrors * add feat FindDirtyGit * fix
- Loading branch information
Showing
13 changed files
with
223 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -107,3 +107,6 @@ bin/ | |
.history/ | ||
|
||
tmp | ||
|
||
coverage.html | ||
coverage.txt |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] }) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters