@ -1,6 +1,8 @@
|
||||
package git
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"os/user"
|
||||
@ -17,12 +19,16 @@ import (
|
||||
func IsClean(repoPath string) (bool, error) {
|
||||
repo, err := git.PlainOpen(repoPath)
|
||||
if err != nil {
|
||||
return false, err
|
||||
if errors.Is(err, git.ErrRepositoryNotExists) {
|
||||
return false, git.ErrRepositoryNotExists
|
||||
}
|
||||
|
||||
return false, fmt.Errorf("unable to open %s: %s", repoPath, err)
|
||||
}
|
||||
|
||||
worktree, err := repo.Worktree()
|
||||
if err != nil {
|
||||
return false, err
|
||||
return false, fmt.Errorf("unable to open worktree of %s: %s", repoPath, err)
|
||||
}
|
||||
|
||||
patterns, err := GetExcludesFiles()
|
||||
@ -36,14 +42,14 @@ func IsClean(repoPath string) (bool, error) {
|
||||
|
||||
status, err := worktree.Status()
|
||||
if err != nil {
|
||||
return false, err
|
||||
return false, fmt.Errorf("unable to query status of %s: %s", repoPath, err)
|
||||
}
|
||||
|
||||
if status.String() != "" {
|
||||
noNewline := strings.TrimSuffix(status.String(), "\n")
|
||||
log.Debugf("discovered git status in %s: %s", repoPath, noNewline)
|
||||
log.Debugf("git status: %s: %s", repoPath, noNewline)
|
||||
} else {
|
||||
log.Debugf("discovered clean git status in %s", repoPath)
|
||||
log.Debugf("git status: %s: clean", repoPath)
|
||||
}
|
||||
|
||||
return status.IsClean(), nil
|
||||
|
15
pkg/git/read_test.go
Normal file
15
pkg/git/read_test.go
Normal file
@ -0,0 +1,15 @@
|
||||
package git
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
"github.com/go-git/go-git/v5"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestIsClean(t *testing.T) {
|
||||
isClean, err := IsClean("/tmp")
|
||||
assert.Equal(t, isClean, false)
|
||||
assert.True(t, errors.Is(err, git.ErrRepositoryNotExists))
|
||||
}
|
Reference in New Issue
Block a user