feat: add git util

This commit is contained in:
Kevin Franklin Kim 2023-02-09 09:09:00 +01:00
parent 7be5b7bd62
commit 0bb527e392
2 changed files with 52 additions and 0 deletions

26
pkg/util/git/config.go Normal file
View File

@ -0,0 +1,26 @@
package git
import (
"context"
"strings"
"github.com/foomo/posh/pkg/log"
"github.com/foomo/posh/pkg/shell"
"github.com/pkg/errors"
)
func ConfigUserName(ctx context.Context, l log.Logger) (string, error) {
value, err := shell.New(ctx, l, "git config user.name").Output()
if err != nil {
return "", errors.Wrap(err, "failed to retrieve git user name")
}
return strings.TrimSpace(string(value)), nil
}
func ConfigUserEmail(ctx context.Context, l log.Logger) (string, error) {
value, err := shell.New(ctx, l, "git config user.email").Output()
if err != nil {
return "", errors.Wrap(err, "failed to retrieve git user name")
}
return strings.TrimSpace(string(value)), nil
}

26
pkg/util/git/ref.go Normal file
View File

@ -0,0 +1,26 @@
package git
import (
"context"
"strings"
"github.com/foomo/posh/pkg/log"
"github.com/foomo/posh/pkg/shell"
"github.com/pkg/errors"
)
func Ref(ctx context.Context, l log.Logger) (string, error) {
value, err := shell.New(ctx, l, "git rev-parse --abbrev-ref HEAD").Output()
if err != nil {
return "", errors.Wrap(err, "failed to retrieve git ref")
}
if strings.TrimSpace(string(value)) == "HEAD" {
value, err = shell.New(ctx, l, "git describe --tags").Output()
if err != nil {
return "", errors.Wrap(err, "failed to retrieve git tag")
}
}
return strings.TrimSpace(string(value)), nil
}