Skip to content

Commit 84addae

Browse files
AlexFabreclaude
andcommitted
option: auto-detect default base branch from remote
Instead of hardcoding 'main' as the default base branch, use git symbolic-ref to detect the remote's default branch. This makes the tool work out of the box for repos using 'master' or other default branch names. Falls back to 'main' if detection fails. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 206f4c3 commit 84addae

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

commit_format/commit_format.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,23 @@ def template_check(self, commit: str, commit_message: str) -> int:
392392
return errors
393393

394394

395+
def detect_base_branch() -> str:
396+
"""Auto-detect the default branch from the remote, fallback to 'main'."""
397+
try:
398+
result = subprocess.run(
399+
["git", "symbolic-ref", "refs/remotes/origin/HEAD"],
400+
capture_output=True,
401+
text=True,
402+
check=False,
403+
)
404+
if result.returncode == 0:
405+
# Output is like "refs/remotes/origin/main"
406+
return result.stdout.strip().split("/")[-1]
407+
except FileNotFoundError:
408+
pass
409+
return "main"
410+
411+
395412
def find_config_file() -> str | None:
396413
"""Auto-discover config file in current directory or git root."""
397414
candidates = [".commit-format", ".commit-format.toml"]
@@ -449,12 +466,13 @@ def main():
449466
help="path to a commit-format template file to validate header/body/footer "
450467
"commit message structure",
451468
)
469+
default_base = detect_base_branch()
452470
parser.add_argument(
453471
"-b",
454472
"--base",
455473
type=str,
456-
default="main",
457-
help="name of the base branch. Default 'main'",
474+
default=default_base,
475+
help=f"name of the base branch. Default '{default_base}'",
458476
)
459477
parser.add_argument(
460478
"-a",

0 commit comments

Comments
 (0)