1
0
Fork 0
mirror of https://github.com/Luzifer/git-recurse-status.git synced 2024-12-23 04:21:17 +00:00

Add support for OR combinations

This commit is contained in:
Knut Ahlers 2016-10-28 15:50:56 +02:00
parent 422f13a4be
commit 72e4c38dfc
Signed by: luzifer
GPG key ID: DC2729FDD34BE99E

24
main.go
View file

@ -36,8 +36,9 @@ const (
var ( var (
cfg = struct { cfg = struct {
Filter []string `flag:"filter,f" default:"" description:"Attributes to filter for (AND combined)"` Filter []string `flag:"filter,f" default:"" description:"Attributes to filter for"`
Format string `flag:"format" vardefault:"format" description:"Output format"` Format string `flag:"format" vardefault:"format" description:"Output format"`
Or bool `flag:"or" default:"false" description:"Switch combining of filters from AND to OR"`
Search string `flag:"search,s" default:"" description:"String to search for in output"` Search string `flag:"search,s" default:"" description:"String to search for in output"`
VersionAndExit bool `flag:"version" default:"false" description:"Prints current version and exits"` VersionAndExit bool `flag:"version" default:"false" description:"Prints current version and exits"`
}{} }{}
@ -110,7 +111,7 @@ func getRepoStatus(path string) (*repoStatus, error) {
} }
func (r repoStatus) matches(filters []string) bool { func (r repoStatus) matches(filters []string) bool {
match := true match := !cfg.Or
for _, f := range filters { for _, f := range filters {
if len(strings.TrimSpace(f)) == 0 { if len(strings.TrimSpace(f)) == 0 {
@ -120,25 +121,30 @@ func (r repoStatus) matches(filters []string) bool {
expect := !strings.HasPrefix(f, "no-") expect := !strings.HasPrefix(f, "no-")
f = strings.TrimPrefix(f, "no-") f = strings.TrimPrefix(f, "no-")
if (r.RemoteStatus == f) != expect && str.StringInSlice(f, collectionStatus) { if str.StringInSlice(f, collectionStatus) {
match = false match = andOrAdd(match, cfg.Or, (r.RemoteStatus == f) == expect)
} }
if r.Modifications[f] != expect && str.StringInSlice(f, collectionModifications) { if str.StringInSlice(f, collectionModifications) {
match = false match = andOrAdd(match, cfg.Or, r.Modifications[f] == expect)
} }
switch f { switch f {
case FILTER_REMOTE: case FILTER_REMOTE:
if (r.Remote != "") != expect { match = andOrAdd(match, cfg.Or, (r.Remote != "") == expect)
match = false
}
} }
} }
return match return match
} }
func andOrAdd(in, or, add bool) bool {
if or {
return in || add
}
return in && add
}
func (r repoStatus) String() string { func (r repoStatus) String() string {
tpl, err := template.New("output").Parse(cfg.Format) tpl, err := template.New("output").Parse(cfg.Format)
if err != nil { if err != nil {