mirror of
https://github.com/grdl/git-get.git
synced 2026-02-05 06:48:48 +00:00
55 lines
1.1 KiB
Go
55 lines
1.1 KiB
Go
package main
|
|
|
|
import (
|
|
git "github.com/libgit2/git2go/v30"
|
|
"github.com/pkg/errors"
|
|
)
|
|
|
|
type RepoStatus int
|
|
|
|
const (
|
|
StatusOk RepoStatus = iota
|
|
StatusUntrackedFiles
|
|
StatusUncommittedChanges
|
|
StatusUnknown
|
|
)
|
|
|
|
func GetStatus(path string) (RepoStatus, error) {
|
|
repo, err := git.OpenRepository(path)
|
|
if err != nil {
|
|
return StatusUnknown, errors.Wrap(err, "failed opening repository")
|
|
}
|
|
|
|
_, err = statusEntries(repo)
|
|
return StatusOk, nil
|
|
}
|
|
|
|
func statusEntries(repo *git.Repository) ([]git.StatusEntry, error) {
|
|
opts := &git.StatusOptions{
|
|
Show: git.StatusShowIndexAndWorkdir,
|
|
Flags: git.StatusOptIncludeUntracked,
|
|
}
|
|
|
|
status, err := repo.StatusList(opts)
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "failed getting repository status")
|
|
}
|
|
|
|
entryCount, err := status.EntryCount()
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "failed getting repository status count")
|
|
}
|
|
|
|
var entries []git.StatusEntry
|
|
for i := 0; i < entryCount; i++ {
|
|
entry, err := status.ByIndex(i)
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "failed getting repository status entry")
|
|
}
|
|
|
|
entries = append(entries, entry)
|
|
}
|
|
|
|
return entries, nil
|
|
}
|