forked from coop-cloud/abra
parent
6c87d501e6
commit
9be78bc5fa
104
pkg/git/read.go
104
pkg/git/read.go
|
@ -1,17 +1,11 @@
|
||||||
package git
|
package git
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"io/ioutil"
|
|
||||||
"os/user"
|
|
||||||
"path"
|
"path"
|
||||||
"path/filepath"
|
|
||||||
"strings"
|
|
||||||
|
|
||||||
"coopcloud.tech/abra/pkg/config"
|
"coopcloud.tech/abra/pkg/config"
|
||||||
"github.com/go-git/go-git/v5"
|
"github.com/go-git/go-git/v5"
|
||||||
gitConfigPkg "github.com/go-git/go-git/v5/config"
|
|
||||||
"github.com/go-git/go-git/v5/plumbing"
|
"github.com/go-git/go-git/v5/plumbing"
|
||||||
"github.com/go-git/go-git/v5/plumbing/format/gitignore"
|
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -46,12 +40,6 @@ func IsClean(recipeName string) (bool, error) {
|
||||||
return false, err
|
return false, err
|
||||||
}
|
}
|
||||||
|
|
||||||
patterns, err := GetExcludesFiles()
|
|
||||||
if err != nil {
|
|
||||||
return false, err
|
|
||||||
}
|
|
||||||
worktree.Excludes = append(patterns, worktree.Excludes...)
|
|
||||||
|
|
||||||
status, err := worktree.Status()
|
status, err := worktree.Status()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false, err
|
return false, err
|
||||||
|
@ -65,95 +53,3 @@ func IsClean(recipeName string) (bool, error) {
|
||||||
|
|
||||||
return status.IsClean(), nil
|
return status.IsClean(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetExcludesFiles reads the exlude files from a global git ignore
|
|
||||||
func GetExcludesFiles() ([]gitignore.Pattern, error) {
|
|
||||||
var err error
|
|
||||||
var patterns []gitignore.Pattern
|
|
||||||
|
|
||||||
cfg, err := parseGitConfig()
|
|
||||||
if err != nil {
|
|
||||||
return patterns, err
|
|
||||||
}
|
|
||||||
|
|
||||||
excludesfile := getExcludesFile(cfg)
|
|
||||||
patterns, err = parseExcludesFile(excludesfile)
|
|
||||||
if err != nil {
|
|
||||||
return patterns, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return patterns, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func parseGitConfig() (*gitConfigPkg.Config, error) {
|
|
||||||
cfg := gitConfigPkg.NewConfig()
|
|
||||||
|
|
||||||
usr, err := user.Current()
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
b, err := ioutil.ReadFile(usr.HomeDir + "/.gitconfig")
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := cfg.Unmarshal(b); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return cfg, err
|
|
||||||
}
|
|
||||||
|
|
||||||
func getExcludesFile(cfg *gitConfigPkg.Config) string {
|
|
||||||
for _, sec := range cfg.Raw.Sections {
|
|
||||||
if sec.Name == "core" {
|
|
||||||
for _, opt := range sec.Options {
|
|
||||||
if opt.Key == "excludesfile" {
|
|
||||||
return opt.Value
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
|
|
||||||
func parseExcludesFile(excludesfile string) ([]gitignore.Pattern, error) {
|
|
||||||
excludesfile, err := expandTilde(excludesfile)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
data, err := ioutil.ReadFile(excludesfile)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
var ps []gitignore.Pattern
|
|
||||||
for _, s := range strings.Split(string(data), "\n") {
|
|
||||||
if !strings.HasPrefix(s, "#") && len(strings.TrimSpace(s)) > 0 {
|
|
||||||
ps = append(ps, gitignore.ParsePattern(s, nil))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return ps, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func expandTilde(path string) (string, error) {
|
|
||||||
if !strings.HasPrefix(path, "~") {
|
|
||||||
return path, nil
|
|
||||||
}
|
|
||||||
var paths []string
|
|
||||||
u, err := user.Current()
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
for _, p := range strings.Split(path, string(filepath.Separator)) {
|
|
||||||
if p == "~" {
|
|
||||||
paths = append(paths, u.HomeDir)
|
|
||||||
} else {
|
|
||||||
paths = append(paths, p)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return "/" + filepath.Join(paths...), nil
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in New Issue