|
6 | 6 | Evergreen patch builds, Evergreen regular builds).
|
7 | 7 | """
|
8 | 8 |
|
9 |
| -import os |
10 | 9 | import subprocess
|
11 | 10 | from typing import Optional
|
12 | 11 |
|
13 |
| -from scripts.release.constants import get_version_id, is_evg_patch, is_running_in_evg |
14 |
| - |
15 | 12 |
|
16 | 13 | def get_current_branch() -> Optional[str]:
|
17 | 14 | """
|
18 | 15 | Detect the current git branch for cache scoping.
|
19 | 16 |
|
20 |
| - In Evergreen CI: |
21 |
| - - For patch builds: tries to detect the original branch (not evg-pr-test-* branches) |
22 |
| - - For master builds: returns master |
23 |
| -
|
24 | 17 | :return: branch name or 'master' as fallback
|
25 | 18 | """
|
26 |
| - if not is_running_in_evg(): |
27 |
| - # Local development - use git directly |
28 |
| - try: |
29 |
| - result = subprocess.run( |
30 |
| - ["git", "rev-parse", "--abbrev-ref", "HEAD"], capture_output=True, text=True, check=True |
31 |
| - ) |
32 |
| - branch = result.stdout.strip() |
33 |
| - return branch if branch != "HEAD" else "master" |
34 |
| - except (subprocess.CalledProcessError, FileNotFoundError): |
35 |
| - return "master" |
36 |
| - |
37 |
| - # Running in Evergreen |
38 |
| - if is_evg_patch(): |
39 |
| - # For patch builds, try to detect the original branch |
40 |
| - # This logic is based on scripts/evergreen/precommit_bump.sh |
41 |
| - try: |
42 |
| - # Get all remote refs with their commit hashes |
43 |
| - result = subprocess.run( |
44 |
| - ["git", "for-each-ref", "--format=%(refname:short) %(objectname)", "refs/remotes/origin"], |
45 |
| - capture_output=True, |
46 |
| - text=True, |
47 |
| - check=True, |
48 |
| - ) |
49 |
| - |
50 |
| - # Get current commit hash |
51 |
| - current_commit = subprocess.run( |
52 |
| - ["git", "rev-parse", "HEAD"], capture_output=True, text=True, check=True |
53 |
| - ).stdout.strip() |
54 |
| - |
55 |
| - # Find branches that point to the current commit, excluding evg-pr-test-* branches |
56 |
| - for line in result.stdout.strip().split("\n"): |
57 |
| - if not line: |
58 |
| - continue |
59 |
| - parts = line.split() |
60 |
| - if len(parts) >= 2: |
61 |
| - ref_name, commit_hash = parts[0], parts[1] |
62 |
| - if commit_hash == current_commit and not ref_name.startswith("origin/evg-pr-test-"): |
63 |
| - # Remove 'origin/' prefix |
64 |
| - branch = ref_name.replace("origin/", "", 1) |
65 |
| - return branch |
66 |
| - |
67 |
| - except (subprocess.CalledProcessError, FileNotFoundError): |
68 |
| - pass |
69 |
| - else: |
70 |
| - # For non-patch builds, try to get the branch from git |
71 |
| - try: |
72 |
| - result = subprocess.run( |
73 |
| - ["git", "rev-parse", "--abbrev-ref", "HEAD"], capture_output=True, text=True, check=True |
74 |
| - ) |
75 |
| - branch = result.stdout.strip() |
76 |
| - if branch and branch != "HEAD": |
77 |
| - return branch |
78 |
| - except (subprocess.CalledProcessError, FileNotFoundError): |
79 |
| - pass |
80 |
| - |
81 |
| - # Fallback to master |
82 |
| - return "master" |
83 |
| - |
| 19 | + try: |
| 20 | + result = subprocess.run( |
| 21 | + ["git", "rev-parse", "--abbrev-ref", "HEAD"], capture_output=True, text=True, check=True |
| 22 | + ) |
| 23 | + branch = result.stdout.strip() |
| 24 | + if branch and branch != "HEAD": |
| 25 | + return branch |
| 26 | + except (subprocess.CalledProcessError, FileNotFoundError): |
| 27 | + return "master" |
84 | 28 |
|
85 | 29 | def get_cache_scope() -> str:
|
86 | 30 | """
|
|
0 commit comments