Skip to content

Commit 3c21350

Browse files
committed
Add branch subsctitution support
1 parent 85439b4 commit 3c21350

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

readme.md

+2
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,5 @@ setuptools.setup(
7070
- `{ccount}`: Number of commits since last tag
7171

7272
- `{sha}`: First 8 characters of the sha hash of the latest commit
73+
74+
- `{branch}`: Current branch name

setuptools_git_versioning.py

+16-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,20 @@ def _exec(cmd): # type: (str) -> List[str]
2626
return [line.rstrip() for line in lines if line.rstrip()]
2727

2828

29+
def get_branches(): # type: () -> List[str]
30+
branches = _exec("git branch -l --format '%(refname:short)'")
31+
if branches:
32+
return branches
33+
return []
34+
35+
36+
def get_branch(): # type: () -> Optional[str]
37+
branches = _exec("git branch --show-current")
38+
if branches:
39+
return branches[0]
40+
return None
41+
42+
2943
def get_tags(): # type: () -> List[str]
3044
tags = _exec("git tag --sort=-version:refname --merged")
3145
if tags:
@@ -110,6 +124,7 @@ def version_from_git(template=DEFAULT_TEMPLATE,
110124
head_sha = get_sha('HEAD')
111125
ccount = count_since(tag)
112126
on_tag = head_sha == tag_sha
127+
branch = get_branch()
113128

114129
if dirty:
115130
t = dirty_template
@@ -118,4 +133,4 @@ def version_from_git(template=DEFAULT_TEMPLATE,
118133
else:
119134
t = template
120135

121-
return t.format(sha=head_sha[:8], tag=tag, ccount=ccount)
136+
return t.format(sha=head_sha[:8], tag=tag, ccount=ccount, branch=branch)

0 commit comments

Comments
 (0)