Skip to content
Draft
Changes from 1 commit
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
per branch
  • Loading branch information
nammn committed Aug 22, 2025
commit 3599dfb945efd6bc45bd5e05a8291ab6a4543a8e
74 changes: 9 additions & 65 deletions scripts/release/branch_detection.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,81 +6,25 @@
Evergreen patch builds, Evergreen regular builds).
"""

import os
import subprocess
from typing import Optional

from scripts.release.constants import get_version_id, is_evg_patch, is_running_in_evg


def get_current_branch() -> Optional[str]:
"""
Detect the current git branch for cache scoping.

In Evergreen CI:
- For patch builds: tries to detect the original branch (not evg-pr-test-* branches)
- For master builds: returns master

:return: branch name or 'master' as fallback
"""
if not is_running_in_evg():
# Local development - use git directly
try:
result = subprocess.run(
["git", "rev-parse", "--abbrev-ref", "HEAD"], capture_output=True, text=True, check=True
)
branch = result.stdout.strip()
return branch if branch != "HEAD" else "master"
except (subprocess.CalledProcessError, FileNotFoundError):
return "master"

# Running in Evergreen
if is_evg_patch():
# For patch builds, try to detect the original branch
# This logic is based on scripts/evergreen/precommit_bump.sh
try:
# Get all remote refs with their commit hashes
result = subprocess.run(
["git", "for-each-ref", "--format=%(refname:short) %(objectname)", "refs/remotes/origin"],
capture_output=True,
text=True,
check=True,
)

# Get current commit hash
current_commit = subprocess.run(
["git", "rev-parse", "HEAD"], capture_output=True, text=True, check=True
).stdout.strip()

# Find branches that point to the current commit, excluding evg-pr-test-* branches
for line in result.stdout.strip().split("\n"):
if not line:
continue
parts = line.split()
if len(parts) >= 2:
ref_name, commit_hash = parts[0], parts[1]
if commit_hash == current_commit and not ref_name.startswith("origin/evg-pr-test-"):
# Remove 'origin/' prefix
branch = ref_name.replace("origin/", "", 1)
return branch

except (subprocess.CalledProcessError, FileNotFoundError):
pass
else:
# For non-patch builds, try to get the branch from git
try:
result = subprocess.run(
["git", "rev-parse", "--abbrev-ref", "HEAD"], capture_output=True, text=True, check=True
)
branch = result.stdout.strip()
if branch and branch != "HEAD":
return branch
except (subprocess.CalledProcessError, FileNotFoundError):
pass

# Fallback to master
return "master"

try:
result = subprocess.run(
["git", "rev-parse", "--abbrev-ref", "HEAD"], capture_output=True, text=True, check=True
)
branch = result.stdout.strip()
if branch and branch != "HEAD":
return branch
except (subprocess.CalledProcessError, FileNotFoundError):
return "master"

def get_cache_scope() -> str:
"""
Expand Down