|
4 | 4 | */ |
5 | 5 |
|
6 | 6 | /** |
7 | | - * Bash script to get git status for a workspace. |
8 | | - * Returns structured output with primary branch, show-branch, and dirty status. |
| 7 | + * Generate bash script to get git status for a workspace. |
| 8 | + * Returns structured output with base ref, show-branch, and dirty status. |
| 9 | + * |
| 10 | + * @param baseRef - The ref to compare against (e.g., "origin/main", "origin/develop"). |
| 11 | + * If not provided, auto-detects origin/main or origin/master. |
9 | 12 | */ |
10 | | -export const GIT_STATUS_SCRIPT = ` |
| 13 | +export function generateGitStatusScript(baseRef?: string): string { |
| 14 | + // If baseRef is provided and looks valid, use it with fallback to auto-detection |
| 15 | + // Otherwise fall back to auto-detection directly |
| 16 | + if (baseRef?.startsWith("origin/")) { |
| 17 | + // Script that tries provided base first, falls back to auto-detection |
| 18 | + return ` |
| 19 | +# Try provided base ref first, fall back to auto-detection if it doesn't exist |
| 20 | +PREFERRED_BASE="${baseRef}" |
| 21 | +BASE_REF="" |
| 22 | +
|
| 23 | +if git rev-parse --verify "$PREFERRED_BASE" >/dev/null 2>&1; then |
| 24 | + BASE_REF="$PREFERRED_BASE" |
| 25 | +else |
| 26 | + # Provided base doesn't exist, auto-detect primary branch |
| 27 | + if git rev-parse --verify "refs/remotes/origin/main" >/dev/null 2>&1; then |
| 28 | + BASE_REF="origin/main" |
| 29 | + elif git rev-parse --verify "refs/remotes/origin/master" >/dev/null 2>&1; then |
| 30 | + BASE_REF="origin/master" |
| 31 | + fi |
| 32 | +fi |
| 33 | +
|
| 34 | +# Final fallback |
| 35 | +if [ -z "$BASE_REF" ]; then |
| 36 | + echo "ERROR: Could not find base ref" |
| 37 | + exit 1 |
| 38 | +fi |
| 39 | +
|
| 40 | +# Get show-branch output for ahead/behind counts |
| 41 | +SHOW_BRANCH=$(git show-branch --sha1-name HEAD "$BASE_REF" 2>/dev/null) |
| 42 | +
|
| 43 | +if [ $? -ne 0 ]; then |
| 44 | + echo "ERROR: git show-branch failed" |
| 45 | + exit 1 |
| 46 | +fi |
| 47 | +
|
| 48 | +# Check for dirty (uncommitted changes) |
| 49 | +DIRTY_COUNT=$(git status --porcelain 2>/dev/null | wc -l | tr -d ' ') |
| 50 | +
|
| 51 | +# Compute line deltas vs merge-base |
| 52 | +MERGE_BASE=$(git merge-base HEAD "$BASE_REF" 2>/dev/null || echo "") |
| 53 | +
|
| 54 | +OUTGOING_STATS="0 0" |
| 55 | +if [ -n "$MERGE_BASE" ]; then |
| 56 | + OUTGOING_STATS=$(git diff --numstat "$MERGE_BASE" 2>/dev/null | awk '{ if ($1 == "-" || $2 == "-") next; add += $1; del += $2 } END { printf "%d %d", add+0, del+0 }') |
| 57 | + if [ -z "$OUTGOING_STATS" ]; then |
| 58 | + OUTGOING_STATS="0 0" |
| 59 | + fi |
| 60 | +fi |
| 61 | +
|
| 62 | +INCOMING_STATS="0 0" |
| 63 | +if [ -n "$MERGE_BASE" ]; then |
| 64 | + INCOMING_STATS=$(git diff --numstat "$MERGE_BASE" "$BASE_REF" 2>/dev/null | awk '{ if ($1 == "-" || $2 == "-") next; add += $1; del += $2 } END { printf "%d %d", add+0, del+0 }') |
| 65 | + if [ -z "$INCOMING_STATS" ]; then |
| 66 | + INCOMING_STATS="0 0" |
| 67 | + fi |
| 68 | +fi |
| 69 | +
|
| 70 | +# Output sections (BASE_REF without origin/ prefix for consistency) |
| 71 | +echo "---PRIMARY---" |
| 72 | +echo "$BASE_REF" | sed 's@^origin/@@' |
| 73 | +echo "---SHOW_BRANCH---" |
| 74 | +echo "$SHOW_BRANCH" |
| 75 | +echo "---DIRTY---" |
| 76 | +echo "$DIRTY_COUNT" |
| 77 | +echo "---LINE_DELTA---" |
| 78 | +echo "$OUTGOING_STATS $INCOMING_STATS" |
| 79 | +`; |
| 80 | + } |
| 81 | + |
| 82 | + // Auto-detect primary branch |
| 83 | + return ` |
11 | 84 | # Get primary branch - try multiple methods |
| 85 | +# Note: symbolic-ref can return stale values when cloned from bundles, |
| 86 | +# but we should trust it if it's a sensible default branch name. |
12 | 87 | PRIMARY_BRANCH="" |
13 | 88 |
|
14 | 89 | # Method 1: symbolic-ref (fastest) |
15 | | -PRIMARY_BRANCH=$(git symbolic-ref refs/remotes/origin/HEAD 2>/dev/null | sed 's@^refs/remotes/origin/@@') |
| 90 | +SYMREF_BRANCH=$(git symbolic-ref refs/remotes/origin/HEAD 2>/dev/null | sed 's@^refs/remotes/origin/@@') |
| 91 | +
|
| 92 | +# Accept symbolic-ref if it's a known default branch name (not a feature branch) |
| 93 | +# Common default branches: main, master, develop, trunk, default, release |
| 94 | +case "$SYMREF_BRANCH" in |
| 95 | + main|master|develop|trunk|default|release) |
| 96 | + PRIMARY_BRANCH="$SYMREF_BRANCH" |
| 97 | + ;; |
| 98 | +esac |
16 | 99 |
|
17 | | -# Method 2: remote show origin (fallback) |
| 100 | +# Method 2: check for main or master directly (fast, reliable fallback) |
| 101 | +# This handles cases where symbolic-ref points to a stale feature branch |
18 | 102 | if [ -z "$PRIMARY_BRANCH" ]; then |
19 | | - PRIMARY_BRANCH=$(git remote show origin 2>/dev/null | grep 'HEAD branch' | cut -d' ' -f5) |
| 103 | + if git rev-parse --verify "refs/remotes/origin/main" >/dev/null 2>&1; then |
| 104 | + PRIMARY_BRANCH="main" |
| 105 | + elif git rev-parse --verify "refs/remotes/origin/master" >/dev/null 2>&1; then |
| 106 | + PRIMARY_BRANCH="master" |
| 107 | + fi |
| 108 | +fi |
| 109 | +
|
| 110 | +# Method 3: If symbolic-ref gave us something and main/master don't exist, |
| 111 | +# trust the symbolic-ref value (handles non-standard default branches) |
| 112 | +if [ -z "$PRIMARY_BRANCH" ] && [ -n "$SYMREF_BRANCH" ]; then |
| 113 | + if git rev-parse --verify "refs/remotes/origin/$SYMREF_BRANCH" >/dev/null 2>&1; then |
| 114 | + PRIMARY_BRANCH="$SYMREF_BRANCH" |
| 115 | + fi |
20 | 116 | fi |
21 | 117 |
|
22 | | -# Method 3: check for main or master |
| 118 | +# Method 4: remote show origin (slow, network call, last resort) |
23 | 119 | if [ -z "$PRIMARY_BRANCH" ]; then |
24 | | - PRIMARY_BRANCH=$(git branch -r 2>/dev/null | grep -E 'origin/(main|master)$' | head -1 | sed 's@^.*origin/@@') |
| 120 | + PRIMARY_BRANCH=$(git remote show origin 2>/dev/null | grep 'HEAD branch' | cut -d' ' -f5) |
25 | 121 | fi |
26 | 122 |
|
27 | | -# Exit if we can't determine primary branch |
| 123 | +# Final fallback: assume main |
28 | 124 | if [ -z "$PRIMARY_BRANCH" ]; then |
29 | | - echo "ERROR: Could not determine primary branch" |
30 | | - exit 1 |
| 125 | + PRIMARY_BRANCH="main" |
31 | 126 | fi |
32 | 127 |
|
33 | 128 | # Get show-branch output for ahead/behind counts |
@@ -74,6 +169,13 @@ echo "$DIRTY_COUNT" |
74 | 169 | echo "---LINE_DELTA---" |
75 | 170 | echo "$OUTGOING_STATS $INCOMING_STATS" |
76 | 171 | `; |
| 172 | +} |
| 173 | + |
| 174 | +/** |
| 175 | + * @deprecated Use generateGitStatusScript() instead |
| 176 | + * Bash script to get git status for a workspace (auto-detects primary branch). |
| 177 | + */ |
| 178 | +export const GIT_STATUS_SCRIPT = generateGitStatusScript(); |
77 | 179 |
|
78 | 180 | /** |
79 | 181 | * Parse the output from GIT_STATUS_SCRIPT. |
@@ -142,10 +244,32 @@ export SSH_ASKPASS=echo |
142 | 244 | export GIT_SSH_COMMAND="\${GIT_SSH_COMMAND:-ssh} -o BatchMode=yes -o StrictHostKeyChecking=accept-new" |
143 | 245 |
|
144 | 246 | # Get primary branch name |
145 | | -PRIMARY_BRANCH=$(git symbolic-ref refs/remotes/origin/HEAD 2>/dev/null | sed 's@^refs/remotes/origin/@@') |
| 247 | +# Note: symbolic-ref can return stale values, but trust it if it's a sensible name |
| 248 | +PRIMARY_BRANCH="" |
| 249 | +SYMREF_BRANCH=$(git symbolic-ref refs/remotes/origin/HEAD 2>/dev/null | sed 's@^refs/remotes/origin/@@') |
| 250 | +
|
| 251 | +# Accept symbolic-ref if it's a known default branch name |
| 252 | +case "$SYMREF_BRANCH" in |
| 253 | + main|master|develop|trunk|default|release) |
| 254 | + PRIMARY_BRANCH="$SYMREF_BRANCH" |
| 255 | + ;; |
| 256 | +esac |
| 257 | +
|
| 258 | +# Fallback: check for main or master directly |
146 | 259 | if [ -z "$PRIMARY_BRANCH" ]; then |
147 | | - PRIMARY_BRANCH=$(git remote show origin 2>/dev/null | grep 'HEAD branch' | cut -d' ' -f5) |
| 260 | + if git rev-parse --verify "refs/remotes/origin/main" >/dev/null 2>&1; then |
| 261 | + PRIMARY_BRANCH="main" |
| 262 | + elif git rev-parse --verify "refs/remotes/origin/master" >/dev/null 2>&1; then |
| 263 | + PRIMARY_BRANCH="master" |
| 264 | + fi |
| 265 | +fi |
| 266 | +
|
| 267 | +# Trust symbolic-ref if main/master don't exist (non-standard default branches) |
| 268 | +if [ -z "$PRIMARY_BRANCH" ] && [ -n "$SYMREF_BRANCH" ]; then |
| 269 | + PRIMARY_BRANCH="$SYMREF_BRANCH" |
148 | 270 | fi |
| 271 | +
|
| 272 | +# Final fallback |
149 | 273 | if [ -z "$PRIMARY_BRANCH" ]; then |
150 | 274 | PRIMARY_BRANCH="main" |
151 | 275 | fi |
|
0 commit comments