Skip to content

Commit 5118a53

Browse files
Remove complex merge base logic
Signed-off-by: Shreeya Patel <[email protected]>
1 parent d627a70 commit 5118a53

File tree

1 file changed

+39
-52
lines changed

1 file changed

+39
-52
lines changed

.github/workflows/kernel-build-and-test-x86_64.yml

Lines changed: 39 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -233,66 +233,52 @@ jobs:
233233
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
234234
run: |
235235
BASE_BRANCH=""
236+
BRANCH_NAME="${{ github.ref_name }}"
237+
238+
# Define whitelist of valid base branches
239+
VALID_BASES="ciqlts9_2 ciqlts9_4 ciqlts8_6"
240+
241+
echo "Current branch: $BRANCH_NAME"
236242
237243
# First, check if an open PR already exists from this head branch
238-
echo "Checking for existing open PR from branch: ${{ github.ref_name }}"
239-
EXISTING_PR=$(gh pr list --head "${{ github.ref_name }}" --state open --json number,baseRefName --jq '.[0]' || echo "")
244+
echo "Checking for existing open PR from branch: $BRANCH_NAME"
245+
EXISTING_PR=$(gh pr list --head "$BRANCH_NAME" --state open --json number,baseRefName --jq '.[0]' || echo "")
240246
241247
if [ -n "$EXISTING_PR" ] && [ "$EXISTING_PR" != "null" ]; then
242-
# PR exists - use its existing base branch (no merge-base detection on force push)
248+
# PR exists - use its existing base branch
243249
BASE_BRANCH=$(echo "$EXISTING_PR" | jq -r '.baseRefName')
244250
PR_NUMBER=$(echo "$EXISTING_PR" | jq -r '.number')
245251
echo "Found existing PR #$PR_NUMBER, using existing base: $BASE_BRANCH"
246-
echo "Skipping merge-base detection to avoid duplicate PRs on force push"
247252
elif [ -n "${{ github.base_ref }}" ]; then
248253
# For PRs, use the base branch directly
249254
BASE_BRANCH="${{ github.base_ref }}"
250255
echo "Using PR base branch: $BASE_BRANCH"
251256
else
252-
# No existing PR - for direct pushes, find the common ancestor branch
253-
echo "No existing PR found, finding base branch via merge-base"
254-
255-
# Get all remote branches sorted by commit date (most recent first)
256-
# This ensures we check actively developed branches first
257-
CURRENT_COMMIT=$(git rev-parse HEAD)
258-
BEST_BRANCH=""
259-
CLOSEST_DISTANCE=999999
260-
261-
# Only check the 30 most recently updated branches for performance
262-
for remote_branch in $(git for-each-ref --sort=-committerdate --format='%(refname:short)' refs/remotes/origin/ | grep -v 'HEAD\|PR-CHECKER' | head -30); do
263-
branch_name=$(echo "$remote_branch" | sed 's|^origin/||')
264-
265-
# Skip if it's the current branch
266-
if [ "$branch_name" = "${{ github.ref_name }}" ]; then
267-
continue
257+
# Extract base branch from branch name pattern: {name}_base or {name}-base
258+
# Match patterns like {shreeya}_ciqlts9_2 or {shreeya}-ciqlts9_2
259+
if [[ "$BRANCH_NAME" =~ \{[^}]+\}[_-](.+) ]]; then
260+
EXTRACTED_BASE="${BASH_REMATCH[1]}"
261+
echo "Extracted base branch from branch name: $EXTRACTED_BASE"
262+
263+
# Validate against whitelist
264+
if echo "$VALID_BASES" | grep -wq "$EXTRACTED_BASE"; then
265+
BASE_BRANCH="$EXTRACTED_BASE"
266+
echo "Base branch validated: $BASE_BRANCH"
267+
else
268+
echo "::error::Extracted base '$EXTRACTED_BASE' is not in whitelist: $VALID_BASES"
269+
echo "::error::Valid base branches are: $VALID_BASES"
270+
exit 1
268271
fi
269-
270-
MERGE_BASE=$(git merge-base HEAD "$remote_branch" 2>/dev/null || echo "")
271-
272-
# Check if this branch shares history and isn't the current commit
273-
if [ -n "$MERGE_BASE" ] && [ "$MERGE_BASE" != "$CURRENT_COMMIT" ]; then
274-
# Calculate distance (number of commits) from merge-base to current HEAD
275-
DISTANCE=$(git rev-list --count ${MERGE_BASE}..HEAD 2>/dev/null || echo "999999")
276-
277-
# Find the branch with the shortest distance (most recent common ancestor)
278-
if [ "$DISTANCE" -lt "$CLOSEST_DISTANCE" ]; then
279-
CLOSEST_DISTANCE=$DISTANCE
280-
BEST_BRANCH=$branch_name
281-
fi
282-
fi
283-
done
284-
285-
if [ -n "$BEST_BRANCH" ]; then
286-
BASE_BRANCH=$BEST_BRANCH
287-
echo "Found common ancestor with $BASE_BRANCH (distance: $CLOSEST_DISTANCE commits)"
272+
else
273+
echo "::error::Branch name does not match expected pattern {name}_base or {name}-base"
274+
echo "::error::Branch name must be in format {name}_base or {name}-base where base is one of: $VALID_BASES"
275+
exit 1
288276
fi
289277
fi
290278
291279
if [ -z "$BASE_BRANCH" ]; then
292-
echo "::warning::Could not determine base branch for comparison - no common ancestor found"
293-
echo "::warning::Kselftest comparison will be skipped"
294-
echo "base_branch=" >> $GITHUB_OUTPUT
295-
exit 0
280+
echo "::error::Could not determine base branch"
281+
exit 1
296282
fi
297283
298284
echo "base_branch=$BASE_BRANCH" >> $GITHUB_OUTPUT
@@ -456,16 +442,17 @@ jobs:
456442
- name: Get commit information
457443
id: commit_msg
458444
run: |
459-
# Count commits since origin/main (or appropriate base branch)
460-
BASE_BRANCH="main"
445+
# Use the base branch determined by compare-results stage
446+
BASE_BRANCH="${{ needs.compare-results.outputs.base_branch }}"
447+
448+
if [ -z "$BASE_BRANCH" ]; then
449+
echo "::error::Base branch not determined by compare-results stage"
450+
exit 1
451+
fi
452+
461453
if ! git rev-parse origin/$BASE_BRANCH >/dev/null 2>&1; then
462-
# Try other common base branch names
463-
for branch in master lts-9.2 lts-9; do
464-
if git rev-parse origin/$branch >/dev/null 2>&1; then
465-
BASE_BRANCH=$branch
466-
break
467-
fi
468-
done
454+
echo "::error::Base branch origin/$BASE_BRANCH does not exist"
455+
exit 1
469456
fi
470457
471458
COMMIT_COUNT=$(git rev-list --count origin/$BASE_BRANCH..HEAD 2>/dev/null || echo "1")

0 commit comments

Comments
 (0)