github.com/covergates/covergates@v0.2.2-0.20201009050117-42ef8a19fb95/modules/git/commit.go (about)

     1  package git
     2  
     3  import "github.com/go-git/go-git/v5/plumbing"
     4  
     5  type commitObject struct {
     6  	repo *repository
     7  	hash plumbing.Hash
     8  }
     9  
    10  func (commit *commitObject) InDefaultBranch() bool {
    11  	head := plumbing.NewHash(commit.repo.headCommit())
    12  	memo := make(map[plumbing.Hash]bool)
    13  	return commit.reaches(head, commit.hash, memo)
    14  }
    15  
    16  func (commit *commitObject) reaches(start, target plumbing.Hash, memo map[plumbing.Hash]bool) bool {
    17  	if v, ok := memo[start]; ok {
    18  		return v
    19  	}
    20  	if start == target {
    21  		memo[start] = true
    22  		return true
    23  	}
    24  	repo := commit.repo.gitRepository
    25  	current, err := repo.CommitObject(start)
    26  	if err != nil {
    27  		memo[start] = false
    28  		return false
    29  	}
    30  	for _, parent := range current.ParentHashes {
    31  		ok := commit.reaches(parent, target, memo)
    32  		if ok {
    33  			memo[start] = true
    34  			return true
    35  		}
    36  	}
    37  	memo[start] = false
    38  	return false
    39  }