isAncestor checks if possibleAncestor is an ancestor of possibleDescendant. Returns true if possibleAncestor is older than (or equal to) possibleDescendant.
(repoPath, possibleAncestor, possibleDescendant string)
| 147 | // isAncestor checks if possibleAncestor is an ancestor of possibleDescendant. |
| 148 | // Returns true if possibleAncestor is older than (or equal to) possibleDescendant. |
| 149 | func isAncestor(repoPath, possibleAncestor, possibleDescendant string) (bool, error) { |
| 150 | if err := validateGitRef(possibleAncestor); err != nil { |
| 151 | return false, err |
| 152 | } |
| 153 | if err := validateGitRef(possibleDescendant); err != nil { |
| 154 | return false, err |
| 155 | } |
| 156 | |
| 157 | _, _, err := runGitCommand(repoPath, "merge-base", "--is-ancestor", possibleAncestor, possibleDescendant) |
| 158 | if err != nil { |
| 159 | // Exit code 1 means not an ancestor, which is not an error for our purposes |
| 160 | if exitErr, ok := err.(*exec.ExitError); ok && exitErr.ExitCode() == 1 { |
| 161 | return false, nil |
| 162 | } |
| 163 | return false, err |
| 164 | } |
| 165 | return true, nil |
| 166 | } |
| 167 | |
| 168 | // NormalizeCommitRange ensures commits are in chronological order (older first). |
| 169 | // If the commits are reversed (newer...older), it swaps them. |