|
2 | 2 | # Determines the base branch for the current PR (if we are running in a PR). |
3 | 3 | set -euo pipefail |
4 | 4 |
|
| 5 | +if [[ -n "${CI_COMMIT_BRANCH:-}" ]]; then |
| 6 | + echo "CI_COMMIT_BRANCH is set to $CI_COMMIT_BRANCH" >&2 |
| 7 | +else |
| 8 | + echo "CI_COMMIT_BRANCH is not set, skipping base ref detection" >&2 |
| 9 | + exit 1 |
| 10 | +fi |
| 11 | + |
| 12 | +if [[ $CI_COMMIT_BRANCH =~ ^(master|release/.*)$ ]]; then |
| 13 | + echo "CI_COMMIT_BRANCH is a master or release branch, skipping base ref detection" >&2 |
| 14 | + exit 1 |
| 15 | +fi |
| 16 | + |
5 | 17 | CURRENT_HEAD_SHA="$(git rev-parse HEAD)" |
6 | 18 | if [[ -z "${CURRENT_HEAD_SHA:-}" ]]; then |
7 | 19 | echo "Failed to determine current HEAD SHA" >&2 |
@@ -56,7 +68,15 @@ get_distance_from_merge_base() { |
56 | 68 | # NOTE: GitHub API is more robust for this task, but we hit rate limits. |
57 | 69 | BEST_CANDIDATES=(origin/master) |
58 | 70 | BEST_DISTANCE=$(get_distance_from_merge_base origin/master) |
59 | | -mapfile -t CANDIDATE_BASES < <(git branch -a --sort=committerdate --format='%(refname:short)' --list 'origin/release/v*' | tac) |
| 71 | + |
| 72 | +# If the current branch is not a project/ branch, project/ branches are candidates. |
| 73 | +# This accounts for the case when the project/ branch is being merged to master. |
| 74 | +if [[ ! "$CI_COMMIT_BRANCH" =~ ^project/.*$ ]]; then |
| 75 | + mapfile -t CANDIDATE_BASES < <(git branch -a --sort=committerdate --format='%(refname:short)' --list 'origin/release/v*' --list 'origin/project/*' | tac) |
| 76 | +else |
| 77 | + mapfile -t CANDIDATE_BASES < <(git branch -a --sort=committerdate --format='%(refname:short)' --list 'origin/release/v*' | tac) |
| 78 | +fi |
| 79 | + |
60 | 80 | for candidate_base in "${CANDIDATE_BASES[@]}"; do |
61 | 81 | distance=$(get_distance_from_merge_base "$candidate_base") |
62 | 82 | if [[ $distance -lt $BEST_DISTANCE ]]; then |
|
0 commit comments