github.com/git-chglog/git-chglog@v0.15.5-0.20240126074033-6a6993d52d69/commit_filter.go (about)

     1  package chglog
     2  
     3  import (
     4  	"strings"
     5  )
     6  
     7  func commitFilter(commits []*Commit, filters map[string][]string, noCaseSensitive bool) []*Commit { //nolint:gocyclo
     8  	// NOTE(khos2ow): this function is over our cyclomatic complexity goal.
     9  	// Be wary when adding branches, and look for functionality that could
    10  	// be reasonably moved into an injected dependency.
    11  
    12  	res := []*Commit{}
    13  
    14  	for _, commit := range commits {
    15  		include := false
    16  
    17  		if len(filters) == 0 {
    18  			include = true
    19  		}
    20  
    21  		for key, values := range filters {
    22  			prop, ok := dotGet(commit, key)
    23  			if !ok {
    24  				include = false
    25  				break
    26  			}
    27  
    28  			str, ok := prop.(string)
    29  			if !ok {
    30  				include = false
    31  				break
    32  			}
    33  
    34  			if noCaseSensitive {
    35  				str = strings.ToLower(str)
    36  			}
    37  
    38  			exist := false
    39  
    40  			for _, val := range values {
    41  				if noCaseSensitive {
    42  					val = strings.ToLower(val)
    43  				}
    44  
    45  				if str == val {
    46  					exist = true
    47  				}
    48  			}
    49  
    50  			if !exist {
    51  				include = false
    52  				break
    53  			}
    54  
    55  			include = true
    56  		}
    57  
    58  		if include {
    59  			res = append(res, commit)
    60  		}
    61  	}
    62  
    63  	return res
    64  }