Skip to content

Commit

Permalink
Add count_commits_from_version_file option
Browse files Browse the repository at this point in the history
  • Loading branch information
dolfinus committed Sep 10, 2020
1 parent 5c4dd0f commit 5a47ac4
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 13 deletions.
26 changes: 17 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ Automatically set package version using git tag/hash

## Compairing with other packages

| 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 |
|:----------------------------------------------------------------------------------------------------|----------------:|:---------------:|:---------------:|:------------------:|:--------------------------------------:|:--------------------------------:|:--------------------------------:|:----------------------------------:|:-----------------------------:|:-------------------------------:|
| [setuptools-git-versioning](https://github.com/dolfinus/setuptools-git-versioning) | 2020 | + | + | + | + | + | + | + | + | + |
| [setuptools-git-ver](https://github.com/camas/setuptools-git-ver) (Base package) | 2020 | - | + | + | + | + | - | - | - | - |
| [even-better-setuptools-git-version](https://github.com/ktemkin/even-better-setuptools-git-version) | 2019 | - | + | + | - | - | + | + | - | - |
| [better-setuptools-git-version](https://github.com/vivin/better-setuptools-git-version) | 2018 | - | + | + | - | - | + | + | - | - |
| [very-good-setuptools-git-version](https://github.com/Kautenja/very-good-setuptools-git-version) | 2018 | - | + | - | - | - | + | - | - | - |
| [setuptools-git-version](https://github.com/pyfidelity/setuptools-git-version) | 2018 | + | + | - | - | - | - | - | - | - |
| 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 |
|:----------------------------------------------------------------------------------------------------|----------------:|:---------------:|:---------------:|:------------------:|:--------------------------------------:|:--------------------------------:|:--------------------------------:|:----------------------------------:|:-----------------------------:|:-------------------------------:|:-----------------------------------------------------------:|
| [setuptools-git-versioning](https://github.com/dolfinus/setuptools-git-versioning) | 2020 | + | + | + | + | + | + | + | + | + | + |
| [setuptools-git-ver](https://github.com/camas/setuptools-git-ver) (Base package) | 2020 | - | + | + | + | + | - | - | - | - | - |
| [even-better-setuptools-git-version](https://github.com/ktemkin/even-better-setuptools-git-version) | 2019 | - | + | + | - | - | + | + | - | - | - |
| [better-setuptools-git-version](https://github.com/vivin/better-setuptools-git-version) | 2018 | - | + | + | - | - | + | + | - | - | - |
| [very-good-setuptools-git-version](https://github.com/Kautenja/very-good-setuptools-git-version) | 2018 | - | + | - | - | - | + | - | - | - | - |
| [setuptools-git-version](https://github.com/pyfidelity/setuptools-git-version) | 2018 | + | + | - | - | - | - | - | - | - | - |

## Installation

Expand Down Expand Up @@ -49,6 +49,7 @@ setuptools.setup(
"starting_version": "0.0.1",
"version_callback": None,
"version_file": None,
"count_commits_from_version_file": False
},
...
setup_requires=['setuptools-git-versioning'],
Expand All @@ -70,11 +71,18 @@ setuptools.setup(

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

- `count_commits_from_version_file`: `True` to fetch `version_file` last commit instead of tag commit, `False` overwise. Example:

You have a project there tags are added to `master` branch only (e.g. '1.0.0').
But you also wish to build development version (e.g. '1.0.0.dev0') from each commit to `dev` branch.
But you don't want neither setup tag with CI/CD for every commit to `dev` branch, nor set such tags manually.
So just fill up `version_file`, set `count_commits_from_version_file` to `True` and that's all.

### Format Options

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

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

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

Expand Down
23 changes: 19 additions & 4 deletions setuptools_git_versioning.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,13 @@ def get_sha(name='HEAD'): # type: (str) -> Optional[str]
return None


def get_latest_file_commit(path): # type: (str) -> Optional[str]
sha = _exec("git log -n 1 --pretty=format:%H -- {path}".format(path=path))
if sha:
return sha[0]
return None


def is_dirty(): # type: () -> bool
res = _exec("git status --short")
if res:
Expand Down Expand Up @@ -89,6 +96,7 @@ def parse_config(dist, _, value): # type: (Distribution, Any, Any) -> None
starting_version = value.get('starting_version', DEFAULT_STARTING_VERSION)
version_callback = value.get('version_callback', None)
version_file = value.get('version_file', None)
count_commits_from_version_file = value.get('count_commits_from_version_file', False)

version = version_from_git(
template=template,
Expand All @@ -97,6 +105,7 @@ def parse_config(dist, _, value): # type: (Distribution, Any, Any) -> None
starting_version=starting_version,
version_callback=version_callback,
version_file=version_file,
count_commits_from_version_file=count_commits_from_version_file
)
dist.metadata.version = version

Expand All @@ -112,7 +121,8 @@ def version_from_git(template=DEFAULT_TEMPLATE,
starting_version=DEFAULT_STARTING_VERSION,
version_callback=None,
version_file=None,
): # type: (str, str, str, str, Optional[Any, Callable], Optional[str]) -> str
count_commits_from_version_file=False
): # type: (str, str, str, str, Optional[Any, Callable], Optional[str], bool) -> str

# Check if PKG-INFO exists and return value in that if it does
if os.path.exists('PKG-INFO'):
Expand All @@ -134,12 +144,17 @@ def version_from_git(template=DEFAULT_TEMPLATE,
return starting_version
else:
tag = read_version_from_file(version_file)
return tag

if not count_commits_from_version_file:
return tag

tag_sha = get_latest_file_commit(version_file)
else:
tag_sha = get_sha(tag)

dirty = is_dirty()
tag_sha = get_sha(tag)
head_sha = get_sha()
ccount = count_since(tag)
ccount = count_since(tag_sha)
on_tag = head_sha == tag_sha
branch = get_branch()

Expand Down

0 comments on commit 5a47ac4

Please sign in to comment.