Skip to content

Commit aa1ba53

Browse files
authored
fix: strip git name-rev suffix operators from detected branch name (#189)
In detached-HEAD CI checkouts (common in Buildkite/CircleCI/Jenkins/etc. where none of the GitHub/GitLab/Bitbucket CI env vars are recognized), the Python CLI falls back to `git name-rev --name-only HEAD` to detect the current branch. When the checked-out SHA is not exactly at a branch tip (e.g. master moved forward after the pipeline started), name-rev returns strings like `remotes/origin/master~1` or `master^0`. The previous split('/')[-1] cleanup kept the `~N`/`^N` suffix, which the Socket API then rejected with "Invalid branch name". Strip anything from the first `~` or `^` onward before the prefix split. Both characters are forbidden in git ref names per check-ref-format(1), so truncating at them is always safe. Assisted-by: Claude Code:opus-4-7
1 parent 2bbe390 commit aa1ba53

File tree

4 files changed

+12
-2
lines changed

4 files changed

+12
-2
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## 2.2.83
4+
5+
- Fixed branch detection in detached-HEAD CI checkouts. When `git name-rev --name-only HEAD` returned an output with a suffix operator (e.g. `remotes/origin/master~1`, `master^0`), the `~N`/`^N` was previously passed through as the branch name and rejected by the Socket API as an invalid Git ref. The suffix is now stripped before the prefix split, producing the bare branch name.
6+
37
## 2.2.71
48

59
- Added `strace` to the Docker image for debugging purposes.

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ build-backend = "hatchling.build"
66

77
[project]
88
name = "socketsecurity"
9-
version = "2.2.82"
9+
version = "2.2.83"
1010
requires-python = ">= 3.11"
1111
license = {"file" = "LICENSE"}
1212
dependencies = [

socketsecurity/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
__author__ = 'socket.dev'
2-
__version__ = '2.2.82'
2+
__version__ = '2.2.83'
33
USER_AGENT = f'SocketPythonCLI/{__version__}'

socketsecurity/core/git_interface.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import re
12
import urllib.parse
23
import os
34

@@ -108,6 +109,11 @@ def __init__(self, path: str):
108109
# Try git name-rev first (most reliable for detached HEAD)
109110
result = self.repo.git.name_rev('--name-only', 'HEAD')
110111
if result and result != 'undefined':
112+
# Strip name-rev suffix operators (~N, ^N, or combinations
113+
# like master~3^2). These characters are forbidden in git
114+
# ref names, so cutting at the first occurrence can never
115+
# truncate a real branch name.
116+
result = re.split(r'[~^]', result, maxsplit=1)[0]
111117
# Clean up the result (remove any prefixes like 'remotes/origin/')
112118
git_detected_branch = result.split('/')[-1]
113119
log.debug(f"Branch detected from git name-rev: {git_detected_branch}")

0 commit comments

Comments
 (0)