Skip to content

Commit 5a47ac4

Browse files
committed
Add count_commits_from_version_file option
1 parent 5c4dd0f commit 5a47ac4

File tree

2 files changed

+36
-13
lines changed

2 files changed

+36
-13
lines changed

README.md

+17-9
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@ Automatically set package version using git tag/hash
88

99
## Compairing with other packages
1010

11-
| Package/Function | Lastest release | Python2 support | Python3 support | PEP 440 compatible | Separated template for not tagged HEAD | Separated template for dirty run | Using functions outside setup.py | Returning fixed version if no tags | Returning callback if no tags | Reading VERSION file if no tags |
12-
|:----------------------------------------------------------------------------------------------------|----------------:|:---------------:|:---------------:|:------------------:|:--------------------------------------:|:--------------------------------:|:--------------------------------:|:----------------------------------:|:-----------------------------:|:-------------------------------:|
13-
| [setuptools-git-versioning](https://github.com/dolfinus/setuptools-git-versioning) | 2020 | + | + | + | + | + | + | + | + | + |
14-
| [setuptools-git-ver](https://github.com/camas/setuptools-git-ver) (Base package) | 2020 | - | + | + | + | + | - | - | - | - |
15-
| [even-better-setuptools-git-version](https://github.com/ktemkin/even-better-setuptools-git-version) | 2019 | - | + | + | - | - | + | + | - | - |
16-
| [better-setuptools-git-version](https://github.com/vivin/better-setuptools-git-version) | 2018 | - | + | + | - | - | + | + | - | - |
17-
| [very-good-setuptools-git-version](https://github.com/Kautenja/very-good-setuptools-git-version) | 2018 | - | + | - | - | - | + | - | - | - |
18-
| [setuptools-git-version](https://github.com/pyfidelity/setuptools-git-version) | 2018 | + | + | - | - | - | - | - | - | - |
11+
| Package/Function | Lastest release | Python2 support | Python3 support | PEP 440 compatible | Separated template for not tagged HEAD | Separated template for dirty run | Using functions outside setup.py | Returning fixed version if no tags | Returning callback if no tags | Reading VERSION file if no tags | Counting commits from latest VERSION file change if no tags |
12+
|:----------------------------------------------------------------------------------------------------|----------------:|:---------------:|:---------------:|:------------------:|:--------------------------------------:|:--------------------------------:|:--------------------------------:|:----------------------------------:|:-----------------------------:|:-------------------------------:|:-----------------------------------------------------------:|
13+
| [setuptools-git-versioning](https://github.com/dolfinus/setuptools-git-versioning) | 2020 | + | + | + | + | + | + | + | + | + | + |
14+
| [setuptools-git-ver](https://github.com/camas/setuptools-git-ver) (Base package) | 2020 | - | + | + | + | + | - | - | - | - | - |
15+
| [even-better-setuptools-git-version](https://github.com/ktemkin/even-better-setuptools-git-version) | 2019 | - | + | + | - | - | + | + | - | - | - |
16+
| [better-setuptools-git-version](https://github.com/vivin/better-setuptools-git-version) | 2018 | - | + | + | - | - | + | + | - | - | - |
17+
| [very-good-setuptools-git-version](https://github.com/Kautenja/very-good-setuptools-git-version) | 2018 | - | + | - | - | - | + | - | - | - | - |
18+
| [setuptools-git-version](https://github.com/pyfidelity/setuptools-git-version) | 2018 | + | + | - | - | - | - | - | - | - | - |
1919

2020
## Installation
2121

@@ -49,6 +49,7 @@ setuptools.setup(
4949
"starting_version": "0.0.1",
5050
"version_callback": None,
5151
"version_file": None,
52+
"count_commits_from_version_file": False
5253
},
5354
...
5455
setup_requires=['setuptools-git-versioning'],
@@ -70,11 +71,18 @@ setuptools.setup(
7071

7172
- `version_file`: path to VERSION file, to read version from it instead of using `static_version`
7273

74+
- `count_commits_from_version_file`: `True` to fetch `version_file` last commit instead of tag commit, `False` overwise. Example:
75+
76+
You have a project there tags are added to `master` branch only (e.g. '1.0.0').
77+
But you also wish to build development version (e.g. '1.0.0.dev0') from each commit to `dev` branch.
78+
But you don't want neither setup tag with CI/CD for every commit to `dev` branch, nor set such tags manually.
79+
So just fill up `version_file`, set `count_commits_from_version_file` to `True` and that's all.
80+
7381
### Format Options
7482

7583
- `{tag}`: Latest tag in the repository
7684

77-
- `{ccount}`: Number of commits since last tag
85+
- `{ccount}`: Number of commits since last tag or last `version_file` commit (see `count_commits_from_version_file`)
7886

7987
- `{sha}`: First 8 characters of the sha hash of the latest commit
8088

setuptools_git_versioning.py

+19-4
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,13 @@ def get_sha(name='HEAD'): # type: (str) -> Optional[str]
5757
return None
5858

5959

60+
def get_latest_file_commit(path): # type: (str) -> Optional[str]
61+
sha = _exec("git log -n 1 --pretty=format:%H -- {path}".format(path=path))
62+
if sha:
63+
return sha[0]
64+
return None
65+
66+
6067
def is_dirty(): # type: () -> bool
6168
res = _exec("git status --short")
6269
if res:
@@ -89,6 +96,7 @@ def parse_config(dist, _, value): # type: (Distribution, Any, Any) -> None
8996
starting_version = value.get('starting_version', DEFAULT_STARTING_VERSION)
9097
version_callback = value.get('version_callback', None)
9198
version_file = value.get('version_file', None)
99+
count_commits_from_version_file = value.get('count_commits_from_version_file', False)
92100

93101
version = version_from_git(
94102
template=template,
@@ -97,6 +105,7 @@ def parse_config(dist, _, value): # type: (Distribution, Any, Any) -> None
97105
starting_version=starting_version,
98106
version_callback=version_callback,
99107
version_file=version_file,
108+
count_commits_from_version_file=count_commits_from_version_file
100109
)
101110
dist.metadata.version = version
102111

@@ -112,7 +121,8 @@ def version_from_git(template=DEFAULT_TEMPLATE,
112121
starting_version=DEFAULT_STARTING_VERSION,
113122
version_callback=None,
114123
version_file=None,
115-
): # type: (str, str, str, str, Optional[Any, Callable], Optional[str]) -> str
124+
count_commits_from_version_file=False
125+
): # type: (str, str, str, str, Optional[Any, Callable], Optional[str], bool) -> str
116126

117127
# Check if PKG-INFO exists and return value in that if it does
118128
if os.path.exists('PKG-INFO'):
@@ -134,12 +144,17 @@ def version_from_git(template=DEFAULT_TEMPLATE,
134144
return starting_version
135145
else:
136146
tag = read_version_from_file(version_file)
137-
return tag
147+
148+
if not count_commits_from_version_file:
149+
return tag
150+
151+
tag_sha = get_latest_file_commit(version_file)
152+
else:
153+
tag_sha = get_sha(tag)
138154

139155
dirty = is_dirty()
140-
tag_sha = get_sha(tag)
141156
head_sha = get_sha()
142-
ccount = count_since(tag)
157+
ccount = count_since(tag_sha)
143158
on_tag = head_sha == tag_sha
144159
branch = get_branch()
145160

0 commit comments

Comments
 (0)