fix: unstaged changes handling
All checks were successful
continuous-integration/drone/pr Build is passing
continuous-integration/drone/push Build is passing

See toolshed/organising#651
This commit is contained in:
2024-12-31 11:19:03 +01:00
parent bfed51a69c
commit 5975be6870
26 changed files with 622 additions and 56 deletions

View File

@ -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
View 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))
}