Compare commits
76 Commits
0.0.1.post
...
1.0.4
| Author | SHA1 | Date | |
|---|---|---|---|
| 382e39e684 | |||
|
|
d4545f999b | ||
|
|
21fc9e7c52 | ||
|
|
13a584c7d9 | ||
|
|
6dfe0682d7 | ||
|
|
d15b495f0a | ||
|
|
c9df0e7409 | ||
|
|
866e8ff7ff | ||
|
|
d786d27cd7 | ||
| 34c07e0d9f | |||
|
|
fb2f3eb412 | ||
| 2c9886b251 | |||
|
|
d93fd3286c | ||
| 782562eef8 | |||
|
|
f361a5189a | ||
|
|
3751ef1c93 | ||
|
|
d5e003c86c | ||
|
|
8b2b0ca07f | ||
|
|
ab1a69b8f8 | ||
|
|
a564e04219 | ||
| 82c32ff58c | |||
|
|
a29c1778fc | ||
|
|
2ee687959e | ||
| 794e54c88b | |||
|
|
e226f16292 | ||
|
|
8ea6b43a73 | ||
|
|
cb213df6b6 | ||
|
|
2c87f5488d | ||
| 078f5624b2 | |||
|
|
348204abb5 | ||
| 44c10b88a5 | |||
|
|
48114149fb | ||
| a4b7b27a65 | |||
|
|
5eb2f1c5cf | ||
|
|
65927077aa | ||
| 77e2be2714 | |||
|
|
0c02512814 | ||
| f422b9ff7d | |||
|
|
9f900ba597 | ||
| 660270d49b | |||
|
|
97612703b7 | ||
| 9f442a7b8e | |||
|
|
5bb8b35c73 | ||
| 8fb9ba8406 | |||
|
|
a6c2513d9f | ||
|
|
70ef90a529 | ||
|
|
f728842a5a | ||
| 38abaa58c5 | |||
|
|
4837a99ac6 | ||
| 35d75ea019 | |||
|
|
3e130e6bdf | ||
|
|
d7fbf52647 | ||
| 0b8651a30a | |||
|
|
f5c97757b6 | ||
|
|
3c0e5bebc2 | ||
|
|
c5962d533b | ||
|
|
e9e25793d8 | ||
|
|
17f74d6675 | ||
|
|
264c51de2e | ||
| c5b3055bfe | |||
|
|
ac942480cb | ||
| 92a1d1a30f | |||
|
|
be9ef684e4 | ||
| f2c0bf1ddd | |||
|
|
792666c6ec | ||
|
|
2951e70c47 | ||
|
|
63b3f25b33 | ||
|
|
cefa964a3a | ||
|
|
38cde9d700 | ||
|
|
fff52596b1 | ||
|
|
3ce09a0194 | ||
|
|
961f92d9c6 | ||
|
|
a1cf4983ff | ||
|
|
c9e6f4f436 | ||
|
|
7d9f2a7792 | ||
|
|
a223500509 |
177
Jenkinsfile
vendored
177
Jenkinsfile
vendored
@@ -6,6 +6,8 @@
|
||||
// You should have received a copy of the license along with this
|
||||
// work. If not, see <https://creativecommons.org/licenses/by-nc-sa/4.0/>.
|
||||
|
||||
|
||||
|
||||
// configurable settings:
|
||||
// use to send email if workflow problem
|
||||
def _MaintainerName = "CHACHA"
|
||||
@@ -17,7 +19,7 @@ def _bPreRelease = false
|
||||
// toogle Draft flag on Gitea release system => If False, TAG is not created
|
||||
def _bDraft = false
|
||||
// release content / changelog management
|
||||
def _bAutoChangelog = false //Not supported yet
|
||||
def _bAutoChangelog = true //Not supported yet
|
||||
def _ReleaseContent_Title = "_CI/CD Automatic Release_"
|
||||
// full rebuild toogle
|
||||
def _bFullRebuilt = true
|
||||
@@ -94,12 +96,12 @@ pipeline {
|
||||
PY_PROJECT_VERSION = "__NOTSET__"
|
||||
PY_PROJECT_VERSION_STRIPPED = "__NOTSET__"
|
||||
}
|
||||
|
||||
|
||||
stages {
|
||||
|
||||
|
||||
stage("Prepare") {
|
||||
steps {
|
||||
script{
|
||||
script {
|
||||
if (_bFullRebuilt) {
|
||||
// start by cleaning the workspace (not using cleanWs() because we want to keep the directory itself)
|
||||
// => this is needed to fetch it again with custom options
|
||||
@@ -108,8 +110,7 @@ pipeline {
|
||||
else {
|
||||
sh("find ~/. -name . ! -path './TEST_ENV/*' ! -path './BUILD_ENV/*' -o -prune -exec rm -rf -- {} +")
|
||||
}
|
||||
if(_GIT_BRANCH!="master")
|
||||
{
|
||||
if(_GIT_BRANCH!="master") {
|
||||
_bPreRelease = true
|
||||
}
|
||||
}
|
||||
@@ -120,51 +121,113 @@ pipeline {
|
||||
echo("_GITEA_BASE_URL: . . . . . . . . $_GITEA_BASE_URL")
|
||||
echo("GIT_COMMIT:. . . . . . . . . . . $GIT_COMMIT ")
|
||||
echo("_PROJECT_USER_NAME:. . . . . . . $_PROJECT_USER_NAME")
|
||||
echo("_GITEA_PROJECT_NAME: . . . . . . $_PROJECT_NAME")
|
||||
echo("_PROJECT_NAME: . . . . . . . . . $_PROJECT_NAME")
|
||||
echo("_MaintainerEmail:. . . . . . . . $_MaintainerEmail")
|
||||
echo("_MaintainerName:. . . . . . . . $_MaintainerName")
|
||||
|
||||
|
||||
sh("virtualenv --pip=embed --setuptools=embed --wheel=embed --no-periodic-update --activators bash,python BUILD_ENV")
|
||||
sh("virtualenv --pip=embed --setuptools=embed --wheel=embed --no-periodic-update --activators bash,python TEST_ENV")
|
||||
sh("virtualenv --pip=embed --setuptools=embed --wheel=embed --no-periodic-update --activators bash,python TOOLS_ENV")
|
||||
|
||||
sh(". ~/BUILD_ENV/bin/activate && pip install --upgrade setuptools build pip copier jinja2-slug toml \"setuptools-git-versioning<2\"")
|
||||
|
||||
|
||||
sh(". ~/BUILD_ENV/bin/activate && pip install --upgrade setuptools build pip copier jinja2-slug toml")
|
||||
|
||||
sh(". ~/TOOLS_ENV/bin/activate && pip install simple_rest_client requests")
|
||||
|
||||
script {
|
||||
if(_PROJECT_NAME!="pygitversionhelper") {
|
||||
sh(". ~/TOOLS_ENV/bin/activate && pip install git+https://chacha.ddns.net/gitea/chacha/pygitversionhelper.git@master")
|
||||
}
|
||||
else
|
||||
{
|
||||
//TODO: need to install pygitversionhelper deps from a better way...
|
||||
sh(". ~/TOOLS_ENV/bin/activate && pip install packaging")
|
||||
}
|
||||
}
|
||||
sh("git config --global user.email $_MaintainerEmail")
|
||||
sh("git config --global user.name $_MaintainerName")
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
stage("GetCode") {
|
||||
steps {
|
||||
dir("gitrepo") {
|
||||
// manually checkout the repository, with All branches and tags
|
||||
// manually checkout the repository, with All branches and tags
|
||||
// => because we might need them for version and changelog computing
|
||||
checkout([ $class: "GitSCM",
|
||||
branches: [[name: GIT_BRANCH]],
|
||||
extensions: [[$class: "CloneOption", noTags: false, shallow: false, depth: 0, reference: '']],
|
||||
userRemoteConfigs: [[credentialsId: _SCMCredentials, url: GIT_URL]]])
|
||||
script{
|
||||
if(_GIT_BRANCH=="master")
|
||||
{
|
||||
if(sh(returnStdout: true, script: "git tag --points-at HEAD").trim().isEmpty())
|
||||
{
|
||||
error("master push/merge must have an explicit tag release number")
|
||||
script {
|
||||
|
||||
withCredentials([usernamePassword(credentialsId: _SCMCredentials, passwordVariable: 'GIT_PASSWORD', usernameVariable: 'GIT_USERNAME')]) {
|
||||
sh("git remote set-url origin https://${GIT_USERNAME}:${GIT_PASSWORD}@chacha.ddns.net/gitea/${_PROJECT_USER_NAME}/${_PROJECT_NAME}.git")
|
||||
}
|
||||
|
||||
if(_GIT_BRANCH=="master") {
|
||||
if(sh(returnStdout: true, script: "git tag --points-at HEAD").trim().isEmpty()) {
|
||||
|
||||
BUMPED_VERSION = sh(script: """#!/bin/sh -
|
||||
|. ~/TOOLS_ENV/bin/activate
|
||||
|exec python - << '__EOWRAPPER__'
|
||||
|
|
||||
|import re
|
||||
|
|
||||
|try:
|
||||
| from pygitversionhelper import gitversionhelper
|
||||
|except ImportError:
|
||||
| from src.pygitversionhelper import gitversionhelper
|
||||
|
|
||||
|lastcommit=gitversionhelper.commit.getLast(same_branch=True)
|
||||
|msg=gitversionhelper.commit.getMessage(lastcommit)
|
||||
|
|
||||
|_match=re.search(r"\\s*(?:#)?\\s*(?<=new-tag:)(?:\\s*)(?P<TAG>\\S*)",msg)
|
||||
|print(_match.group("TAG"),end="")
|
||||
|
|
||||
|__EOWRAPPER__
|
||||
""".stripMargin(),
|
||||
returnStdout: true).trim()
|
||||
|
||||
if(BUMPED_VERSION.isEmpty()) {
|
||||
echo "master push/merge must have an explicit tag release number, stopping pipeline"
|
||||
currentBuild.getRawBuild().getExecutor().doStop()
|
||||
}
|
||||
else {
|
||||
echo "new-tag requested in commit message: $BUMPED_VERSION"
|
||||
|
||||
sh("git tag $BUMPED_VERSION")
|
||||
sh("git push origin --tags")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// using git to get the latest tag on this branch
|
||||
def latestTag = sh(returnStdout: true, script: "git tag --sort=-creatordate | head -n 1").trim()
|
||||
|
||||
latestTag = sh(script: """#!/bin/sh -
|
||||
|. ~/TOOLS_ENV/bin/activate
|
||||
|exec python - << '__EOWRAPPER__'
|
||||
|
|
||||
|try:
|
||||
| from pygitversionhelper import gitversionhelper
|
||||
|except ImportError:
|
||||
| from src.pygitversionhelper import gitversionhelper
|
||||
|
|
||||
|print(gitversionhelper.tag.getLastTag(same_branch=True),end ="")
|
||||
|
|
||||
|__EOWRAPPER__
|
||||
""".stripMargin(),
|
||||
returnStdout: true)
|
||||
echo("latestTag:. . . . . . . . . . . . $latestTag ")
|
||||
|
||||
// using setuptools_git_versioning to get the generated version number based on Git tags and logs
|
||||
// TODO: read dev_template from toml
|
||||
// get current (or bumped) version number from git history
|
||||
PY_PROJECT_VERSION = sh(script: """#!/bin/sh -
|
||||
|. ~/BUILD_ENV/bin/activate
|
||||
|. ~/TOOLS_ENV/bin/activate
|
||||
|exec python - << '__EOWRAPPER__'
|
||||
|
|
||||
|from setuptools_git_versioning import version_from_git
|
||||
|try:
|
||||
| from pygitversionhelper import gitversionhelper
|
||||
|except ImportError:
|
||||
| from src.pygitversionhelper import gitversionhelper
|
||||
|
|
||||
|print(str(version_from_git(tag_filter="^\\d+\\.\\d+\\.\\d+\$",dev_template = "{tag}.post{ccount}")),end ="")
|
||||
|print(gitversionhelper.version.getCurrentVersion(formated_output=True,version_std="PEP440",bump_type="dev",bump_dev_strategy="post"),end ="")
|
||||
|
|
||||
|__EOWRAPPER__
|
||||
""".stripMargin(),
|
||||
@@ -172,16 +235,18 @@ pipeline {
|
||||
echo("PY_PROJECT_VERSION: . . . . . . . . . $PY_PROJECT_VERSION")
|
||||
PY_PROJECT_VERSION_STRIPPED=ExtractBaseVersion(PY_PROJECT_VERSION)
|
||||
|
||||
// Manually pushing a new tag with version string guessed by gitversionhelper
|
||||
// because setuptools-git-versioning / setuptools_scm cant fing tag on other branches, so will guess a wrong version without this tag.
|
||||
if(latestTag!=PY_PROJECT_VERSION) {
|
||||
sh("git tag $PY_PROJECT_VERSION")
|
||||
sh("git push origin --tags")
|
||||
}
|
||||
|
||||
// specific handling to test the template itself
|
||||
// => little hacky... creating a new git repo with a commit/tag corresponding to HEAD of the official one
|
||||
if(_PROJECT_NAME=="pyChaChaDummyProject") //specific case to test the template itself
|
||||
{
|
||||
sh("git config --global user.email $_MaintainerEmail")
|
||||
sh("git config --global user.name $_MaintainerName")
|
||||
|
||||
if(_PROJECT_NAME=="pyChaChaDummyProject") { //specific case to test the template itself
|
||||
sh("rm -Rf ~/_gitrepo || true")
|
||||
|
||||
//sh(". ~/BUILD_ENV/bin/activate && python -m copier --vcs-ref HEAD --prereleases --defaults ./ ~/_gitrepo")
|
||||
sh(script: """#!/bin/sh -
|
||||
|. ~/BUILD_ENV/bin/activate
|
||||
|exec python - << '__EOWRAPPER__'
|
||||
@@ -240,7 +305,7 @@ pipeline {
|
||||
steps {
|
||||
// no need for a build-env: setuptools is already creating one
|
||||
dir("gitrepo") {
|
||||
script{
|
||||
script {
|
||||
// actually doing the package build
|
||||
sh(". ~/BUILD_ENV/bin/activate && python -m build .")
|
||||
}
|
||||
@@ -269,16 +334,16 @@ pipeline {
|
||||
}
|
||||
}
|
||||
post {
|
||||
always {
|
||||
dir("gitrepo") {
|
||||
publishHTML([
|
||||
reportDir: "helpers-results/quality_check",
|
||||
reportFiles: "report.html",
|
||||
reportName: "quality-report",
|
||||
allowMissing: false,
|
||||
alwaysLinkToLastBuild: true,
|
||||
keepAll: true])
|
||||
}
|
||||
always {
|
||||
dir("gitrepo") {
|
||||
publishHTML([
|
||||
reportDir: "helpers-results/quality_check",
|
||||
reportFiles: "report.html",
|
||||
reportName: "quality-report",
|
||||
allowMissing: false,
|
||||
alwaysLinkToLastBuild: true,
|
||||
keepAll: true])
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -388,20 +453,27 @@ pipeline {
|
||||
def GITEA_LOGIN_TOKEN=credentials("GiteaCHACHAPush")
|
||||
}
|
||||
steps {
|
||||
sh("python3 -m pip install simple_rest_client requests")
|
||||
dir("gitrepo") {
|
||||
script {
|
||||
def CurrentDateTime=java.time.LocalDateTime.now()
|
||||
withCredentials([string( credentialsId: _MkDocsWebCredentials,variable: 'MKDOCSTOKEN' )])
|
||||
{
|
||||
sh(script: """#!/usr/bin/env python3
|
||||
|from simple_rest_client.api import API
|
||||
|from simple_rest_client.resource import Resource
|
||||
withCredentials([string( credentialsId: _MkDocsWebCredentials,variable: 'MKDOCSTOKEN' )]) {
|
||||
sh(script: """#!/bin/sh -
|
||||
|. ~/TOOLS_ENV/bin/activate
|
||||
|exec python - << '__EOWRAPPER__'
|
||||
|
|
||||
|import json
|
||||
|import glob
|
||||
|import requests
|
||||
|import shutil
|
||||
|
|
||||
|from simple_rest_client.api import API
|
||||
|from simple_rest_client.resource import Resource
|
||||
|
|
||||
|try:
|
||||
| from pygitversionhelper import gitversionhelper
|
||||
|except ImportError:
|
||||
| from src.pygitversionhelper import gitversionhelper
|
||||
|
|
||||
|from urllib.parse import urljoin
|
||||
|
|
||||
|class GiteaRepoCommits(Resource):
|
||||
@@ -478,8 +550,9 @@ pipeline {
|
||||
|response=requests.post("http://${_MkDocsWebURL}/API.php?REQ=pushDoc",data=reqData,files=files)
|
||||
|if response.status_code != 200:
|
||||
| raise RuntimeError(f"Wrong server response: {response.status_code}")
|
||||
|
|
||||
""".stripMargin())
|
||||
|
|
||||
|__EOWRAPPER__
|
||||
""".stripMargin())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
59
README.md
59
README.md
@@ -3,46 +3,33 @@
|
||||
|
||||

|
||||
|
||||
# Python project template
|
||||
# pyGitVersionHelper
|
||||
|
||||
A nice template to start a blank python projet.
|
||||
|
||||
This template automate a lot of handy things and allow CI/CD automatic releases generation.
|
||||
_A tiny library to help versioning management of git python projects_
|
||||
|
||||
It is also collectings data to feed Jenkins build.
|
||||
Because a good developer is a lazy developer and version management in CI/CD can be very time consuming.
|
||||
|
||||
Checkout [Latest Documentation](https://chacha.ddns.net/mkdocs-web/chacha/pychachadummyproject/{{branch}}/latest/).
|
||||
|
||||
Checkout [Latest Documentation](https://chacha.ddns.net/mkdocs-web/chacha/pygitversionhelper/{{branch}}/latest/).
|
||||
|
||||
## Features
|
||||
|
||||
### Generic pipeline skeleton:
|
||||
- Prepare
|
||||
- GetCode
|
||||
- BuildPackage
|
||||
- Install
|
||||
- CheckCode
|
||||
- PlotMetrics
|
||||
- RunUnitTests
|
||||
- GenDOC
|
||||
- PostRelease
|
||||
- list tags
|
||||
- get last tag
|
||||
- get last version
|
||||
- get current version (bumped)
|
||||
- convert / switch from SemVer to PEP440 (both ways)
|
||||
- automatic version format detection (SemVer by default)
|
||||
|
||||
### CI/CD Environment
|
||||
- Jenkins
|
||||
- Gitea (with patch for dynamic Readme variables: https://chacha.ddns.net/gitea/chacha/GiteaMarkupVariable)
|
||||
- Docker
|
||||
- MkDocsWeb
|
||||
|
||||
### CI/CD Helper libs
|
||||
- VirtualEnv
|
||||
- Changelog generation based on commits
|
||||
- copier
|
||||
- pylint + pylint_json2html
|
||||
- mypy
|
||||
- unittest + xmlrunner + junitparser + junit2htmlreport
|
||||
- mkdocs
|
||||
## Options
|
||||
- restrict to same branch
|
||||
- both SemVer and PEP440 support
|
||||
- custom output format
|
||||
- configurable default bump type: major, minor, patch or dev
|
||||
- configurable default bump strategy: post, pre-patch, pre-minor, pre-major
|
||||
- ignore non-version tag
|
||||
- force version format
|
||||
|
||||
### Python project
|
||||
- Full .toml implementation
|
||||
- .whl automatic generation
|
||||
- dynamic versionning using git repository
|
||||
- embedded unit-test
|
||||
## Process
|
||||
- full CI/CD developpment: Gitea / Jenkins + few python libs (pytlint, coverage, unittest, mkdocs)
|
||||
- documentation generated mkdocs and self-hosted
|
||||
- CI/CD on Linux, manually tested in Windows environnement
|
||||
@@ -1,16 +1,128 @@
|
||||
# Usage
|
||||
|
||||
## Pulvinar dolor
|
||||
Donec dapibus est fermentum justo volutpat condimentum. Integer quis nunc neque. Donec dictum vehicula justo, in facilisis ex tincidunt in.
|
||||
Vivamus sollicitudin sem dui, id mollis orci facilisis ut. Proin sed pulvinar dolor. Donec volutpat commodo urna imperdiet pulvinar. Fusce eget aliquam risus.
|
||||
Vivamus viverra luctus ex, in finibus mi. Nullam elementum dapibus mollis. Ut suscipit volutpat ex, quis feugiat lacus consectetur eu.
|
||||
## Installation
|
||||
|
||||
## Condimentum faucibus
|
||||
Quisque auctor egestas sem, luctus suscipit ex maximus vitae. Duis facilisis augue et condimentum faucibus.
|
||||
Donec cursus, enim a sagittis egestas, lectus lorem eleifend libero, at tincidunt leo magna at libero.
|
||||
Nunc eros velit, suscipit luctus tempor vel, finibus et est. Curabitur efficitur pretium pulvinar.
|
||||
Donec urna lectus, vulputate quis turpis sed, placerat congue urna. Phasellus aliquet fermentum quam, non auctor elit porta nec. Morbi eu ligula at nisl ultricies condimentum vitae id ante.
|
||||
From master repository:
|
||||
|
||||
## Aliquam lacinia
|
||||
In volutpat lorem ex, et fringilla nibh faucibus quis. Mauris et arcu elementum, auctor dui vitae, egestas arcu. Duis sit amet aliquam quam.
|
||||
Phasellus a odio turpis. Etiam tristique mi eu enim varius, eget facilisis est vestibulum. Aliquam lacinia nec purus sed luctus. Cras at laoreet erat.
|
||||
python -m pip install git+https://chacha.ddns.net/gitea/chacha/pygitversionhelper.git@master
|
||||
|
||||
From local .whl file:
|
||||
|
||||
python -m pip install pygitversionhelper-<VERSION>-py3-none-any.whl
|
||||
|
||||
From public repository:
|
||||
|
||||
TBD
|
||||
|
||||
## Import in your project
|
||||
|
||||
Add this line on the top of your python script:
|
||||
|
||||
#from pygitversionhelper import gitversionhelper
|
||||
|
||||
[optionnal] If you need to catch exception from this module:
|
||||
|
||||
#from pygitversionhelper import gitversionhelperException
|
||||
|
||||
## Basic API
|
||||
|
||||
All the API commands are static so it is not needed to create instantiate any object.
|
||||
|
||||
They are all executed in the current active directory.
|
||||
|
||||
One easy way to change directory:
|
||||
|
||||
import os
|
||||
os.chdir("<YOUR DIRECTORY>")
|
||||
|
||||
### sublib: repository
|
||||
|
||||
To check if a repository is dirty:
|
||||
|
||||
if gitversionhelper.repository.isDirty():
|
||||
print("repository is dirty")
|
||||
|
||||
### sublib: tag
|
||||
|
||||
List all tags [default to taggerdate order]:
|
||||
|
||||
for tag in gitversionhelper.tag.getTags():
|
||||
print(f"found tag: {tag}")
|
||||
|
||||
List all tags [using git refname order]:
|
||||
|
||||
for tag in gitversionhelper.tag.getTags("v:refname"):
|
||||
print(f"found tag: {tag}")
|
||||
|
||||
Get the last tag:
|
||||
|
||||
print(f"most recent repository tag: {gitversionhelper.tag.getLastTag()}")
|
||||
|
||||
Get the last tag [only on same branch]:
|
||||
|
||||
print(f"most recent repository tag: {gitversionhelper.tag.getLastTag(same_branch=True)}")
|
||||
|
||||
|
||||
Get the distance from HEAD to last tag:
|
||||
|
||||
print(f"number of commit since last tag: {gitversionhelper.tag.getDistanceFromTag()}")
|
||||
|
||||
Get the distance from HEAD to last tag [only on same branch]:
|
||||
|
||||
print(f"number of commit since last tag: {gitversionhelper.tag.getDistanceFromTag(same_branch=True)}")
|
||||
|
||||
### sublib: version
|
||||
|
||||
Get the last found version in the repository [return MetaVersion object]:
|
||||
|
||||
print(f"most recent repository version: {gitversionhelper.version.getLastVersion()}")
|
||||
|
||||
Get the last found version in the repository [return formated string]:
|
||||
|
||||
print(f"most recent repository version: {gitversionhelper.version.getLastVersion(formated_output=True)}")
|
||||
|
||||
Others kwargs available to this function:
|
||||
|
||||
* version_std: string to force a version standard for rendering ["PEP440" or "SemVer"]
|
||||
* same_branch: boolean to force searching on same branch
|
||||
* ignore\_unknown\_tags: boolean to allow unknown tag to be ignored
|
||||
|
||||
Get the current version of the repository, automatically bump it if the last one is not tagged [returns MetaVersion object]:
|
||||
|
||||
print(f"current repository version: {gitversionhelper.version.getCurrentVersion()}")
|
||||
|
||||
Or with formated output:
|
||||
|
||||
print(f"current repository version: {gitversionhelper.version.getCurrentVersion(formated_output=True)}")
|
||||
|
||||
Typical usage in CI/CD env:
|
||||
|
||||
bumped_version = gitversionhelper.version.getCurrentVersion( formated_output=True, \
|
||||
version_std="PEP440", \
|
||||
bump_type="dev", \
|
||||
bump_dev_strategy="post")
|
||||
print(f"current repository version: {bumped_version}")
|
||||
|
||||
kwargs available to this function:
|
||||
|
||||
* All same args as getLastVersion()
|
||||
* bump_type: if version need to be pump, allow to configure next release update type: __major, minor, patch, dev__
|
||||
* bump\_dev\_strategy: if bump\_type is dev, allow to choose dev update strategy: __post, pre-patch, pre-minor, pre-major__
|
||||
|
||||
A version object can also be manually formated:
|
||||
|
||||
_version = gitversionhelper.tag.getCurrentVersion()
|
||||
_version.doFormatVersion()
|
||||
#or
|
||||
gitversionhelper.version.doFormatVersion(_version)
|
||||
|
||||
kwargs available to those function:
|
||||
- output_format: string to choose a rendering format ["Auto","PEP440" or "SemVer"]
|
||||
|
||||
## Limitations
|
||||
|
||||
There is unfortunately some technical limitation :
|
||||
|
||||
* MultiThreading and async behavior is not tested.
|
||||
* Multiple tag on the same commit is not supported.
|
||||
* Branch filter when searching for a version is only tested with -no-ff strategy
|
||||
@@ -7,13 +7,12 @@
|
||||
# work. If not, see <https://creativecommons.org/licenses/by-nc-sa/4.0/>.
|
||||
|
||||
[build-system]
|
||||
requires = ["setuptools>=63", "wheel", "setuptools-git-versioning<2"]
|
||||
requires = ["setuptools>=63", "wheel", "setuptools_scm"]
|
||||
build-backend = "setuptools.build_meta"
|
||||
|
||||
[tool.setuptools-git-versioning]
|
||||
enabled = true
|
||||
dev_template = "{tag}.post{ccount}"
|
||||
tag_filter = "^\\d+\\.\\d+\\.\\d+$"
|
||||
[tool.setuptools_scm]
|
||||
version_scheme= "post-release"
|
||||
# tag_regex="^(?:v)?(?P<version>\\d+\\.\\d+\\.\\d+)([\\.\\-\\+])?(?:.*)?"
|
||||
|
||||
[project]
|
||||
name = "pygitversionhelper"
|
||||
@@ -36,6 +35,7 @@ classifiers = [
|
||||
]
|
||||
dependencies = [
|
||||
'importlib-metadata; python_version<"3.9"',
|
||||
'packaging'
|
||||
]
|
||||
dynamic = ["version"]
|
||||
|
||||
@@ -49,13 +49,9 @@ where = ["src"]
|
||||
[tool.setuptools.package-data]
|
||||
"pygitversionhelper.data" = ["*.*"]
|
||||
|
||||
#[tool.setuptools_scm]
|
||||
#write_to = "src/pygitversionhelper/_version.py"
|
||||
#version_scheme = "python-simplified-semver"
|
||||
|
||||
[project.urls]
|
||||
Homepage = "https://chacha.ddns.net/gitea/chacha/pygitversionhelper"
|
||||
Documentation = "https://chacha.ddns.net/gitea/chacha/pygitversionhelper/wiki"
|
||||
Homepage = "https://chacha.ddns.net/gitea/chacha/pygitversionhelper/"
|
||||
Documentation = "https://chacha.ddns.net/mkdocs-web/chacha/pygitversionhelper/master/latest/"
|
||||
Tracker = "https://chacha.ddns.net/gitea/chacha/pygitversionhelper/issues"
|
||||
|
||||
[project.optional-dependencies]
|
||||
|
||||
@@ -5,27 +5,28 @@
|
||||
#
|
||||
# You should have received a copy of the license along with this
|
||||
# work. If not, see <https://creativecommons.org/licenses/by-nc-sa/4.0/>.
|
||||
|
||||
"""
|
||||
This project try to help doing handy operations with git when
|
||||
dealing with project versioning and tags on python project -
|
||||
or at leat for project using PEP440 versionning or SemVer.
|
||||
at leat for project using PEP440 or SemVer standards.
|
||||
|
||||
Goal is to keep it compact and not covering too much other things.
|
||||
This is the reason it is one single file with nested classes.
|
||||
=> Design is on purpose poorly expandable to keep the scope maintainable.
|
||||
One requirement is to keep it compact and to not cover too much fancy features.
|
||||
This is the reason why it is one single file with nested classes.
|
||||
|
||||
=> Design is on-purpose poorly expandable to keep the scope light.
|
||||
|
||||
This library is maid for repository using tag as version.
|
||||
Support for non-version tags is optional and not well tested.
|
||||
|
||||
|
||||
This module is the main gitversionhelper file, containing all the code.
|
||||
This module is the main project file, containing all the code.
|
||||
|
||||
Read the read me for more information.
|
||||
Check the unittest s for usage samples.
|
||||
|
||||
Note: _Other Parameters_ are **kwargs
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
import os
|
||||
import subprocess
|
||||
@@ -35,11 +36,7 @@ import logging
|
||||
|
||||
from packaging.version import VERSION_PATTERN as packaging_VERSION_PATTERN
|
||||
|
||||
# Only imports the below statements during type checking
|
||||
if TYPE_CHECKING:
|
||||
from typing import Union
|
||||
|
||||
def _exec(cmd: str, root: str | os.PathLike | None = None) -> list[str]:
|
||||
def _exec(cmd: str, root: str | os.PathLike | None = None, raw:bool = False) -> list[str]:
|
||||
"""
|
||||
helper function to handle system cmd execution
|
||||
Args:
|
||||
@@ -49,13 +46,16 @@ def _exec(cmd: str, root: str | os.PathLike | None = None) -> list[str]:
|
||||
a list of command's return lines
|
||||
|
||||
"""
|
||||
p = subprocess.run(cmd.split(), text=True, cwd=root, capture_output=True, check=False, timeout=2)
|
||||
p = subprocess.run(cmd, text=True, cwd=root, capture_output=True, check=False, timeout=2,shell=True)
|
||||
if re.search("not a git repository",p.stderr):
|
||||
raise gitversionhelper.repository.notAGitRepository()
|
||||
if re.search("fatal:",p.stderr):
|
||||
if re.search("fatal:",p.stderr): #pragma: nocover
|
||||
raise gitversionhelper.unknownGITFatalError(p.stderr)
|
||||
if int(p.returncode) < 0:
|
||||
if int(p.returncode) < 0: #pragma: nocover
|
||||
raise gitversionhelper.unknownGITError(p.stderr)
|
||||
|
||||
if raw:
|
||||
return p.stdout
|
||||
lines = p.stdout.splitlines()
|
||||
return [line.rstrip() for line in lines if line.rstrip()]
|
||||
|
||||
@@ -110,6 +110,103 @@ class gitversionhelper: # pylint: disable=too-few-public-methods
|
||||
True if it is dirty
|
||||
"""
|
||||
return bool(_exec("git status --short"))
|
||||
class commit:
|
||||
"""
|
||||
class containing methods focusing on commits
|
||||
"""
|
||||
__OptDict = {"same_branch": "same_branch",
|
||||
"merged_output":"merged_output"}
|
||||
|
||||
class commitException(gitversionhelperException):
|
||||
"""
|
||||
generic commit exception
|
||||
"""
|
||||
|
||||
class commitNotFound(commitException):
|
||||
"""
|
||||
tag not found exception
|
||||
"""
|
||||
|
||||
@classmethod
|
||||
def getMessagesSinceTag(cls,tag:str,**kwargs) -> str:
|
||||
"""
|
||||
retrieve a commits message history from repository
|
||||
from LastCommit to the given tag
|
||||
Keyword Arguments:
|
||||
merged_output: output one single merged string
|
||||
same_branch(bool): force searching only in the same branch
|
||||
Returns:
|
||||
the commit message
|
||||
"""
|
||||
current_commit_id=cls.getLast(**kwargs)
|
||||
tag_commit_id=cls.getFromTag(tag)
|
||||
|
||||
if ((cls.__OptDict["same_branch"] in kwargs) and (kwargs[cls.__OptDict["same_branch"]] is True)):
|
||||
commits = _exec(f"git rev-list --first-parent --ancestry-path {tag_commit_id}..{current_commit_id}")
|
||||
else:
|
||||
commits = _exec(f"git rev-list --ancestry-path {tag_commit_id}..{current_commit_id}")
|
||||
result=[]
|
||||
for commit in commits:
|
||||
result.append(cls.getMessage(commit))
|
||||
|
||||
if ((cls.__OptDict["merged_output"] in kwargs) and (kwargs[cls.__OptDict["merged_output"]] is True)):
|
||||
print("JOIN")
|
||||
return os.linesep.join(result)
|
||||
return result
|
||||
|
||||
@classmethod
|
||||
def getMessage(cls, commit_hash:str) -> str:
|
||||
"""
|
||||
retrieve a commit message from repository
|
||||
Args:
|
||||
commit_hash: id of the commit
|
||||
Returns:
|
||||
the commit message
|
||||
"""
|
||||
try:
|
||||
res=_exec(f"git log -z --pretty=\"tformat:%B%-C()\" -n 1 {commit_hash}",None,True).rstrip('\x00')
|
||||
except gitversionhelper.unknownGITFatalError as _e:
|
||||
raise cls.commitNotFound("no commit found in commit history") from _e
|
||||
|
||||
return res.replace('\r\n','\n').replace('\n','\r\n')
|
||||
|
||||
@classmethod
|
||||
def getFromTag(cls,tag:str) -> str:
|
||||
"""
|
||||
retrieve a commit from repository associated to a tag
|
||||
Args:
|
||||
tag: tag of the commit
|
||||
Returns:
|
||||
the commit Id
|
||||
"""
|
||||
try:
|
||||
res=_exec(f"git rev-list -n 1 {tag}")
|
||||
except gitversionhelper.unknownGITFatalError as _e:
|
||||
raise cls.commitNotFound("no commit found in commit history") from _e
|
||||
if len(res)==0:
|
||||
raise cls.commitNotFound("no commit found in commit history")
|
||||
return res[0]
|
||||
|
||||
@classmethod
|
||||
def getLast(cls,**kwargs) -> str:
|
||||
"""
|
||||
retrieve last commit from repository
|
||||
Keyword Arguments:
|
||||
same_branch(bool): force searching only in the same branch
|
||||
Returns:
|
||||
the commit Id
|
||||
"""
|
||||
if ((cls.__OptDict["same_branch"] in kwargs) and (kwargs[cls.__OptDict["same_branch"]] is True)):
|
||||
try:
|
||||
res = _exec("git rev-parse HEAD")
|
||||
except gitversionhelper.unknownGITFatalError as _e:
|
||||
raise cls.commitNotFound("no commit found in commit history") from _e
|
||||
else:
|
||||
res = _exec("git for-each-ref --sort=-committerdate refs/heads/ --count 1 --format=\"%(objectname)\"")
|
||||
|
||||
if len(res)==0:
|
||||
raise cls.commitNotFound("no commit found in commit history")
|
||||
return res[0]
|
||||
|
||||
class tag:
|
||||
"""
|
||||
@@ -134,7 +231,7 @@ class gitversionhelper: # pylint: disable=too-few-public-methods
|
||||
"""
|
||||
|
||||
@classmethod
|
||||
def getTags(cls,sort:str = "taggerdate",**kwargs) -> list[str]:
|
||||
def getTags(cls,sort:str = "taggerdate",**kwargs) -> list[str|None]:
|
||||
"""
|
||||
retrieve all tags from a repository
|
||||
Args:
|
||||
@@ -142,10 +239,10 @@ class gitversionhelper: # pylint: disable=too-few-public-methods
|
||||
Returns:
|
||||
the tags list
|
||||
"""
|
||||
|
||||
|
||||
if sort not in cls.__validGitTagSort:
|
||||
raise gitversionhelper.wrongArguments("sort option not in allowed list")
|
||||
|
||||
|
||||
if ((cls.__OptDict["same_branch"] in kwargs) and (kwargs[cls.__OptDict["same_branch"]] is True)):
|
||||
currentBranch = _exec("git rev-parse --abbrev-ref HEAD")
|
||||
return list(reversed(_exec(f"git tag --merged {currentBranch[0]} --sort={sort}")))
|
||||
@@ -169,7 +266,7 @@ class gitversionhelper: # pylint: disable=too-few-public-methods
|
||||
|
||||
if len(res)==0:
|
||||
raise cls.tagNotFound("no tag found in commit history")
|
||||
if len(res)!=1:
|
||||
if len(res)!=1: #pragma: nocover
|
||||
raise cls.moreThanOneTag("multiple tags on same commit is unsupported")
|
||||
return res[0]
|
||||
|
||||
@@ -201,9 +298,8 @@ class gitversionhelper: # pylint: disable=too-few-public-methods
|
||||
r"(?:-(?P<prerelease>(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)"\
|
||||
r"(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?"\
|
||||
r"(?:\+(?P<buildmetadata>[0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$",
|
||||
"regex_preversion_num": r"(?:\.)(?P<num>(?:\d+(?!\w))+)",
|
||||
"regex_build_num" : r"(?:\.)(?P<num>(?:\d+(?!\w))+)"
|
||||
|
||||
"regex_preversion_num": r"(?:\.)(?P<num>(?:\d+(?!\w))+)",
|
||||
"regex_build_num" : r"(?:\.)(?P<num>(?:\d+(?!\w))+)"
|
||||
},
|
||||
"PEP440" : { "regex" : packaging_VERSION_PATTERN,
|
||||
"Auto" : None
|
||||
@@ -220,6 +316,11 @@ class gitversionhelper: # pylint: disable=too-few-public-methods
|
||||
no valid version found exception
|
||||
"""
|
||||
|
||||
class PreAndPostVersionUnsupported(versionException):
|
||||
"""
|
||||
pre and post release can not be present at the same time
|
||||
"""
|
||||
|
||||
class MetaVersion:
|
||||
"""
|
||||
generic version object
|
||||
@@ -284,7 +385,7 @@ class gitversionhelper: # pylint: disable=too-few-public-methods
|
||||
raise gitversionhelper.wrongArguments(f"invalid {cls.__OptDict['bump_type']} requested")
|
||||
return BumpType
|
||||
|
||||
def bump(self,amount:int=1,**kwargs) -> MetaVersion | str : # pylint: disable=R0912
|
||||
def bump(self,amount:int=1,**kwargs) -> gitversionhelper.version.MetaVersion | str : # pylint: disable=R0912
|
||||
"""
|
||||
bump the version to the next one
|
||||
Keyword Arguments:
|
||||
@@ -303,7 +404,8 @@ class gitversionhelper: # pylint: disable=too-few-public-methods
|
||||
_v.pre_count = _v.pre_count + amount
|
||||
else:
|
||||
_v.post_count = _v.post_count + amount
|
||||
elif BumpDevStrategy in ["pre-patch","pre-minor","pre-major"]:
|
||||
#elif BumpDevStrategy in ["pre-patch","pre-minor","pre-major"]:
|
||||
else:
|
||||
if _v.post_count > 0:
|
||||
_v.post_count = _v.post_count + amount
|
||||
else:
|
||||
@@ -313,7 +415,8 @@ class gitversionhelper: # pylint: disable=too-few-public-methods
|
||||
elif BumpDevStrategy == "pre-minor":
|
||||
_v.minor = _v.minor + 1
|
||||
_v.patch = 0
|
||||
elif BumpDevStrategy == "pre-major":
|
||||
#elif BumpDevStrategy == "pre-major":
|
||||
else:
|
||||
_v.major = _v.major + 1
|
||||
_v.minor = 0
|
||||
_v.patch = 0
|
||||
@@ -323,12 +426,13 @@ class gitversionhelper: # pylint: disable=too-few-public-methods
|
||||
_v.major = _v.major + amount
|
||||
elif BumpType == "minor":
|
||||
_v.minor = _v.minor + amount
|
||||
elif BumpType == "patch":
|
||||
#elif BumpType == "patch":
|
||||
else:
|
||||
_v.patch = _v.patch + amount
|
||||
_v.pre_count=0
|
||||
_v.post_count=0
|
||||
_v.raw=_v.doFormatVersion(**kwargs)
|
||||
|
||||
|
||||
if ((self.__OptDict["formated_output"] in kwargs) and (kwargs[self.__OptDict["formated_output"]] is True)):
|
||||
return _v.doFormatVersion(**kwargs)
|
||||
return _v
|
||||
@@ -336,6 +440,8 @@ class gitversionhelper: # pylint: disable=too-few-public-methods
|
||||
def doFormatVersion(self,**kwargs) -> str:
|
||||
"""
|
||||
output a formated version string
|
||||
Keyword Arguments:
|
||||
output_format: output format to render ("Auto" or "PEP440" or "SemVer")
|
||||
Returns:
|
||||
formated version string
|
||||
"""
|
||||
@@ -366,6 +472,8 @@ class gitversionhelper: # pylint: disable=too-few-public-methods
|
||||
version_std(str): the given version_std (can be None)
|
||||
same_branch(bool): force searching only in the same branch
|
||||
formated_output(bool) : output a formated version string
|
||||
bump_type(str): the given bump_type (can be None)
|
||||
bump_dev_strategy(str): the given bump_dev_strategy (can be None)
|
||||
Returns:
|
||||
the last version
|
||||
"""
|
||||
@@ -387,7 +495,15 @@ class gitversionhelper: # pylint: disable=too-few-public-methods
|
||||
return _v
|
||||
|
||||
@classmethod
|
||||
def _parseTag(cls,tag,**kwargs):
|
||||
def getCurrentFormatedVersion(cls,**kwargs) -> str :
|
||||
"""
|
||||
Same as getCurrentVersion() with formated_output kwarg activated
|
||||
"""
|
||||
kwargs["formated_output"]=True
|
||||
return cls.getCurrentVersion(kwargs)
|
||||
|
||||
@classmethod
|
||||
def _parseTag(cls,tag,**kwargs): # pylint: disable=R0914, R0912, R0915
|
||||
"""get the last version from tags
|
||||
Arguments:
|
||||
tag: the tag to be parsed
|
||||
@@ -448,7 +564,11 @@ class gitversionhelper: # pylint: disable=too-few-public-methods
|
||||
VersionStd = "PEP440"
|
||||
|
||||
if not bFound :
|
||||
raise gitversionhelper.version.noValidVersion("no valid version found in tags")
|
||||
raise gitversionhelper.version.noValidVersion("no valid version found in tags")
|
||||
|
||||
if pre_count > 0 and post_count > 0:
|
||||
raise cls.PreAndPostVersionUnsupported("can not parse a version with both pre" \
|
||||
" and post release number.")
|
||||
return cls.MetaVersion(VersionStd, major, minor, patch, pre_count, post_count, tag)
|
||||
|
||||
@classmethod
|
||||
@@ -473,18 +593,18 @@ class gitversionhelper: # pylint: disable=too-few-public-methods
|
||||
_v=None
|
||||
try:
|
||||
_v=cls._parseTag(lastTag,**kwargs)
|
||||
except gitversionhelper.version.noValidVersion:
|
||||
except gitversionhelper.version.noValidVersion as _ex:
|
||||
if ((cls.__OptDict["ignore_unknown_tags"] in kwargs) and (kwargs[cls.__OptDict["ignore_unknown_tags"]] is True)):
|
||||
tags = gitversionhelper.tag.getTags(sort= "taggerdate",**kwargs)
|
||||
_v=None
|
||||
_v = None
|
||||
for _tag in tags:
|
||||
try:
|
||||
_v=cls._parseTag(_tag,**kwargs)
|
||||
break;
|
||||
except:
|
||||
break
|
||||
except gitversionhelper.version.noValidVersion:
|
||||
continue
|
||||
if _v is None:
|
||||
raise gitversionhelper.version.noValidVersion()
|
||||
raise gitversionhelper.version.noValidVersion() from _ex
|
||||
|
||||
if ((cls.__OptDict["formated_output"] in kwargs) and (kwargs[cls.__OptDict["formated_output"]] is True)):
|
||||
return _v.doFormatVersion(**kwargs)
|
||||
@@ -494,6 +614,8 @@ class gitversionhelper: # pylint: disable=too-few-public-methods
|
||||
def doFormatVersion(cls,inputversion:MetaVersion,**kwargs) -> str:
|
||||
"""
|
||||
output a formated version string
|
||||
Keyword Arguments:
|
||||
output_format: output format to render ("Auto" or "PEP440" or "SemVer")
|
||||
Args:
|
||||
inputversion: version to be rendered
|
||||
Returns:
|
||||
@@ -517,7 +639,8 @@ class gitversionhelper: # pylint: disable=too-few-public-methods
|
||||
if OutputFormat is None:
|
||||
OutputFormat = "{major}.{minor}.{patch}{revpattern}{revcount}"
|
||||
if post_count > 0 and pre_count > 0:
|
||||
raise RuntimeError("pre and post release can not be present at the same time")
|
||||
raise gitversionhelper.version.PreAndPostVersionUnsupported("cannot output a version with both pre " \
|
||||
"and post release number.")
|
||||
if VersionStd == "PEP440":
|
||||
if post_count > 0:
|
||||
revpattern=".post"
|
||||
@@ -525,7 +648,8 @@ class gitversionhelper: # pylint: disable=too-few-public-methods
|
||||
elif pre_count > 0:
|
||||
revpattern=".pre"
|
||||
revcount=f"{pre_count}"
|
||||
elif VersionStd == "SemVer":
|
||||
#elif VersionStd == "SemVer":
|
||||
else:
|
||||
if post_count > 0:
|
||||
revpattern="+post"
|
||||
revcount=f".{post_count}"
|
||||
|
||||
@@ -12,9 +12,9 @@ import tempfile
|
||||
import os
|
||||
import pathlib
|
||||
import re
|
||||
import copy
|
||||
import time
|
||||
|
||||
from contextlib import redirect_stdout,redirect_stderr
|
||||
import subprocess
|
||||
|
||||
print(__name__)
|
||||
print(__package__)
|
||||
@@ -39,7 +39,7 @@ class Test_gitversionhelper(unittest.TestCase):
|
||||
os.system("git add .")
|
||||
os.system("git commit -m \"first commit\"")
|
||||
os.system(f"git tag {tag}")
|
||||
|
||||
|
||||
_v = pygitversionhelper.gitversionhelper.version.getLastVersion(**kwargs)
|
||||
|
||||
self.assertIsInstance(_v, pygitversionhelper.gitversionhelper.version.MetaVersion)
|
||||
@@ -56,6 +56,7 @@ class Test_gitversionhelper(unittest.TestCase):
|
||||
self.assertEqual(int(_m.group("MAJ")),_v.major)
|
||||
self.assertEqual(int(_m.group("MIN")),_v.minor)
|
||||
self.assertEqual(int(_m.group("PATCH")),_v.patch)
|
||||
|
||||
return _v
|
||||
|
||||
def _test_version_format(self,_v:pygitversionhelper.gitversionhelper.version.MetaVersion,tag:str,**kwargs):
|
||||
@@ -66,6 +67,17 @@ class Test_gitversionhelper(unittest.TestCase):
|
||||
def _test_version_readback_simple(self,tag:str,**kwargs):
|
||||
_v=self._test_version_readback(tag,**kwargs)
|
||||
self._test_version_format(_v,tag,**kwargs)
|
||||
|
||||
def test_nominal__version__formated_output(self):
|
||||
_v = pygitversionhelper.gitversionhelper.version.MetaVersion("PEP440",
|
||||
1,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
"1.0.0")
|
||||
self.assertEqual("1.0.1", _v.bump(formated_output=True))
|
||||
self.assertEqual("2.0.0", _v.bump(formated_output=True,bump_type="major"))
|
||||
|
||||
def test_nominal__version__auto_1(self):
|
||||
self._test_version_readback_simple("0.0.1")
|
||||
@@ -85,6 +97,7 @@ class Test_gitversionhelper(unittest.TestCase):
|
||||
self._test_version_readback_simple("1.1.1")
|
||||
def test_nominal__version__auto_9(self):
|
||||
self._test_version_readback_simple("1.2.1")
|
||||
|
||||
def test_nominal__version__auto_PEP440_post(self):
|
||||
self._test_version_readback_simple("1.2.1.post1")
|
||||
def test_nominal__version__auto_PEP440_pre(self):
|
||||
@@ -189,7 +202,7 @@ class Test_gitversionhelper(unittest.TestCase):
|
||||
_v=self._test_version_readback("1.2.1-toto",version_std="SemVer")
|
||||
self._test_version_format(_v,"1.2.1-pre.1",version_std="SemVer")
|
||||
|
||||
def test_nominal__version___pump_SemVer(self,**kwargs):
|
||||
def test_nominal__version___pump_SemVer(self):
|
||||
_v = self._test_version_readback("1.0.0",version_std="SemVer")
|
||||
|
||||
_v = _v.bump()
|
||||
@@ -315,7 +328,7 @@ class Test_gitversionhelper(unittest.TestCase):
|
||||
self.assertEqual(_v.post_count, 2)
|
||||
self.assertEqual(_v.doFormatVersion(), "2.1.5+post.2")
|
||||
|
||||
def test_nominal__version___pump_PEP440(self,**kwargs):
|
||||
def test_nominal__version___pump_PEP440(self):
|
||||
_v = self._test_version_readback("1.0.0",version_std="PEP440")
|
||||
|
||||
_v = _v.bump()
|
||||
@@ -497,8 +510,7 @@ class Test_gitversionhelper(unittest.TestCase):
|
||||
self.assertEqual(_v.major,0)
|
||||
self.assertEqual(_v.minor,1)
|
||||
self.assertEqual(_v.patch,0)
|
||||
|
||||
|
||||
|
||||
def test_nominal__version___AUTO_bump_commits(self):
|
||||
with open("demofile.txt", "w+t") as tmpFile:
|
||||
tmpFile.write("testvalue")
|
||||
@@ -637,11 +649,38 @@ class Test_gitversionhelper(unittest.TestCase):
|
||||
with open("demofile.txt", "w+t") as tmpFile:
|
||||
tmpFile.write("testvalue225")
|
||||
|
||||
with self.assertRaises(pygitversionhelper.gitversionhelper.repository.repositoryDirty) as context:
|
||||
with self.assertRaises(pygitversionhelper.gitversionhelper.repository.repositoryDirty) :
|
||||
pygitversionhelper.gitversionhelper.version.getCurrentVersion()
|
||||
|
||||
|
||||
def test_defect__version_post_and_pre_output(self):
|
||||
_v = pygitversionhelper.gitversionhelper.version.MetaVersion("PEP440",
|
||||
0,
|
||||
0,
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
"0.0.1.pre1.post1")
|
||||
with self.assertRaises(pygitversionhelper.gitversionhelper.version.PreAndPostVersionUnsupported) :
|
||||
_v.doFormatVersion()
|
||||
|
||||
_v = pygitversionhelper.gitversionhelper.version.MetaVersion("SemVer",
|
||||
0,
|
||||
0,
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
"0.0.1-pre.1+post.1")
|
||||
with self.assertRaises(pygitversionhelper.gitversionhelper.version.PreAndPostVersionUnsupported) :
|
||||
_v.doFormatVersion()
|
||||
|
||||
def test_defect__version_post_and_pre_parse(self):
|
||||
with self.assertRaises(pygitversionhelper.gitversionhelper.version.PreAndPostVersionUnsupported) :
|
||||
pygitversionhelper.gitversionhelper.version._parseTag("0.0.1.pre1.post1")
|
||||
|
||||
|
||||
def test_defect__git__wrongargument_sortargs(self):
|
||||
with self.assertRaises(pygitversionhelper.gitversionhelper.wrongArguments) as context:
|
||||
with self.assertRaises(pygitversionhelper.gitversionhelper.wrongArguments) :
|
||||
pygitversionhelper.gitversionhelper.tag.getTags(sort="toto")
|
||||
|
||||
def test_defect__git__notagfound(self):
|
||||
@@ -649,10 +688,10 @@ class Test_gitversionhelper(unittest.TestCase):
|
||||
tmpFile.write("testvalue")
|
||||
os.system("git add .")
|
||||
os.system("git commit -m \"first commit\"")
|
||||
with self.assertRaises(pygitversionhelper.gitversionhelper.tag.tagNotFound) as context:
|
||||
with self.assertRaises(pygitversionhelper.gitversionhelper.tag.tagNotFound) :
|
||||
pygitversionhelper.gitversionhelper.tag.getLastTag()
|
||||
|
||||
""" This test is impossible to do
|
||||
""" This test is impossible to do because current implementation can only return one tag
|
||||
def test_defect__git_multipletagsfound(self):
|
||||
with open("demofile.txt", "w+t") as tmpFile:
|
||||
tmpFile.write("testvalue")
|
||||
@@ -660,18 +699,18 @@ class Test_gitversionhelper(unittest.TestCase):
|
||||
os.system("git commit -m \"first commit\"")
|
||||
os.system(f"git tag 0.1.0")
|
||||
os.system(f"git tag 0.2.0")
|
||||
with self.assertRaises(pygitversionhelper.gitversionhelper.tag.moreThanOneTag) as context:
|
||||
pygitversionhelper.gitversionhelper.tag.getLastTag()
|
||||
with self.assertRaises(pygitversionhelper.gitversionhelper.tag.moreThanOneTag) :
|
||||
pygitversionhelper.gitversionhelper.tag.getLastTag(same_branch=True)
|
||||
"""
|
||||
|
||||
def test_defect__wrongargument_bump_type(self):
|
||||
_v=self._test_version_readback("0.1.1",version_std="PEP440")
|
||||
with self.assertRaises(pygitversionhelper.gitversionhelper.wrongArguments) as context:
|
||||
with self.assertRaises(pygitversionhelper.gitversionhelper.wrongArguments) :
|
||||
pygitversionhelper.gitversionhelper.version.getCurrentVersion(bump_type="toto")
|
||||
|
||||
def test_defect__wrongargument_bump_dev_strategy(self):
|
||||
_v=self._test_version_readback("0.1.1",version_std="PEP440")
|
||||
with self.assertRaises(pygitversionhelper.gitversionhelper.wrongArguments) as context:
|
||||
with self.assertRaises(pygitversionhelper.gitversionhelper.wrongArguments) :
|
||||
pygitversionhelper.gitversionhelper.version.getCurrentVersion(bump_dev_strategy="toto")
|
||||
|
||||
def test_nominal__tag__getDistanceFromTag(self):
|
||||
@@ -956,8 +995,12 @@ class Test_gitversionhelper(unittest.TestCase):
|
||||
os.system("git commit -m \"2nd commit\"")
|
||||
os.system(f"git tag INVALIDTAG")
|
||||
|
||||
with self.assertRaises(pygitversionhelper.gitversionhelper.version.noValidVersion) as context:
|
||||
with self.assertRaises(pygitversionhelper.gitversionhelper.version.noValidVersion) :
|
||||
_v = pygitversionhelper.gitversionhelper.version.getLastVersion()
|
||||
|
||||
|
||||
with self.assertRaises(pygitversionhelper.gitversionhelper.version.noValidVersion) :
|
||||
_v = pygitversionhelper.gitversionhelper.version.getLastVersion(ignore_unknown_tags=True)
|
||||
|
||||
def test_defect__tag__invalidtag_inbetween(self):
|
||||
with open("demofile.txt", "w+t") as tmpFile:
|
||||
@@ -972,7 +1015,7 @@ class Test_gitversionhelper(unittest.TestCase):
|
||||
os.system("git commit -m \"commit\"")
|
||||
os.system(f"git tag INVALIDTAG")
|
||||
|
||||
with self.assertRaises(pygitversionhelper.gitversionhelper.version.noValidVersion) as context:
|
||||
with self.assertRaises(pygitversionhelper.gitversionhelper.version.noValidVersion) :
|
||||
_v = pygitversionhelper.gitversionhelper.version.getLastVersion()
|
||||
|
||||
_v = pygitversionhelper.gitversionhelper.version.getLastVersion(ignore_unknown_tags=True)
|
||||
@@ -1001,7 +1044,7 @@ class Test_gitversionhelper(unittest.TestCase):
|
||||
os.system("git commit -m \"commit\"")
|
||||
os.system(f"git tag INVALIDTA4")
|
||||
|
||||
with self.assertRaises(pygitversionhelper.gitversionhelper.version.noValidVersion) as context:
|
||||
with self.assertRaises(pygitversionhelper.gitversionhelper.version.noValidVersion) :
|
||||
_v = pygitversionhelper.gitversionhelper.version.getLastVersion()
|
||||
|
||||
_v = pygitversionhelper.gitversionhelper.version.getLastVersion(ignore_unknown_tags=True)
|
||||
@@ -1040,7 +1083,7 @@ class Test_gitversionhelper(unittest.TestCase):
|
||||
os.system("git commit -m \"commit\"")
|
||||
os.system(f"git tag INVALIDTA5")
|
||||
|
||||
with self.assertRaises(pygitversionhelper.gitversionhelper.version.noValidVersion) as context:
|
||||
with self.assertRaises(pygitversionhelper.gitversionhelper.version.noValidVersion) :
|
||||
_v = pygitversionhelper.gitversionhelper.version.getLastVersion()
|
||||
|
||||
_v = pygitversionhelper.gitversionhelper.version.getLastVersion(ignore_unknown_tags=True)
|
||||
@@ -1073,6 +1116,210 @@ class Test_gitversionhelper(unittest.TestCase):
|
||||
self.assertEqual(_v.minor,1)
|
||||
self.assertEqual(_v.patch,3)
|
||||
|
||||
def test_nominal__commit_getLast(self):
|
||||
# sleeps are needed because commit date have a 1-sec accuracy :-/
|
||||
with open("demofile.txt", "w+t") as tmpFile:
|
||||
tmpFile.write("testvalue")
|
||||
os.system("git add .")
|
||||
os.system("git commit -m \"commit\"")
|
||||
time.sleep(1)
|
||||
|
||||
initial_commit = pygitversionhelper.gitversionhelper.commit.getLast()
|
||||
|
||||
with open("demofile.txt", "w+t") as tmpFile:
|
||||
tmpFile.write("testvalue2")
|
||||
os.system("git add .")
|
||||
os.system("git commit -m \"commit\"")
|
||||
time.sleep(1)
|
||||
|
||||
second_commit = pygitversionhelper.gitversionhelper.commit.getLast()
|
||||
|
||||
self.assertNotEqual(initial_commit,second_commit)
|
||||
|
||||
with open("demofile.txt", "w+t") as tmpFile:
|
||||
tmpFile.write("testvalue3")
|
||||
os.system("git add .")
|
||||
os.system("git commit -m \"commit\"")
|
||||
time.sleep(1)
|
||||
|
||||
third_commit = pygitversionhelper.gitversionhelper.commit.getLast()
|
||||
|
||||
self.assertNotEqual(initial_commit,third_commit)
|
||||
self.assertNotEqual(second_commit,third_commit)
|
||||
|
||||
|
||||
os.system("git checkout -b dev")
|
||||
|
||||
with open("demofile.txt", "w+t") as tmpFile:
|
||||
tmpFile.write("testvalue4")
|
||||
os.system("git add .")
|
||||
os.system("git commit -m \"commit\"")
|
||||
time.sleep(1)
|
||||
|
||||
fourth_commit = pygitversionhelper.gitversionhelper.commit.getLast()
|
||||
|
||||
self.assertNotEqual(initial_commit,fourth_commit)
|
||||
self.assertNotEqual(second_commit,fourth_commit)
|
||||
self.assertNotEqual(third_commit,fourth_commit)
|
||||
|
||||
os.system("git switch master")
|
||||
|
||||
fourth2_commit = pygitversionhelper.gitversionhelper.commit.getLast()
|
||||
self.assertEqual(fourth2_commit,fourth_commit)
|
||||
|
||||
fourth3_commit = pygitversionhelper.gitversionhelper.commit.getLast(same_branch=True)
|
||||
|
||||
self.assertNotEqual(fourth3_commit,fourth_commit)
|
||||
self.assertEqual(fourth3_commit,third_commit)
|
||||
|
||||
with open("demofile.txt", "w+t") as tmpFile:
|
||||
tmpFile.write("testvalue5")
|
||||
os.system("git add .")
|
||||
os.system("git commit -m \"commit\"")
|
||||
time.sleep(1)
|
||||
|
||||
fifth_commit = pygitversionhelper.gitversionhelper.commit.getLast()
|
||||
self.assertNotEqual(fifth_commit,fourth3_commit)
|
||||
|
||||
os.system("git switch dev")
|
||||
|
||||
fifth2_commit = pygitversionhelper.gitversionhelper.commit.getLast()
|
||||
self.assertEqual(fifth2_commit,fifth_commit)
|
||||
|
||||
fifth2_commit = pygitversionhelper.gitversionhelper.commit.getLast(same_branch=True)
|
||||
self.assertEqual(fifth2_commit,fourth_commit)
|
||||
|
||||
def test_defect__commit_notfound(self):
|
||||
|
||||
with self.assertRaises(pygitversionhelper.gitversionhelper.commit.commitNotFound) :
|
||||
pygitversionhelper.gitversionhelper.commit.getLast()
|
||||
|
||||
with self.assertRaises(pygitversionhelper.gitversionhelper.commit.commitNotFound) :
|
||||
pygitversionhelper.gitversionhelper.commit.getLast(same_branch=True)
|
||||
|
||||
def test_nominal__commit_getFromTag(self):
|
||||
with open("demofile.txt", "w+t") as tmpFile:
|
||||
tmpFile.write("testvalue")
|
||||
os.system("git add .")
|
||||
os.system("git commit -m \"commit\"")
|
||||
|
||||
initial_commit = pygitversionhelper.gitversionhelper.commit.getLast()
|
||||
|
||||
with open("demofile.txt", "w+t") as tmpFile:
|
||||
tmpFile.write("testvalue2")
|
||||
os.system("git add .")
|
||||
os.system("git commit -m \"commit2\"")
|
||||
os.system(f"git tag TAG")
|
||||
|
||||
second_commit = pygitversionhelper.gitversionhelper.commit.getLast()
|
||||
self.assertNotEqual(second_commit,initial_commit)
|
||||
second_commit2 = pygitversionhelper.gitversionhelper.commit.getFromTag("TAG")
|
||||
self.assertEqual(second_commit,second_commit2)
|
||||
self.assertNotEqual(second_commit2,initial_commit)
|
||||
|
||||
def test_defect__commit_notfound2(self):
|
||||
|
||||
with self.assertRaises(pygitversionhelper.gitversionhelper.commit.commitNotFound) :
|
||||
pygitversionhelper.gitversionhelper.commit.getFromTag("TAG")
|
||||
|
||||
with open("demofile.txt", "w+t") as tmpFile:
|
||||
tmpFile.write("testvalue")
|
||||
os.system("git add .")
|
||||
os.system("git commit -m \"commit\"")
|
||||
|
||||
with self.assertRaises(pygitversionhelper.gitversionhelper.commit.commitNotFound) :
|
||||
pygitversionhelper.gitversionhelper.commit.getFromTag("TAG")
|
||||
|
||||
os.system(f"git tag OTHER_TAG")
|
||||
|
||||
with self.assertRaises(pygitversionhelper.gitversionhelper.commit.commitNotFound) :
|
||||
pygitversionhelper.gitversionhelper.commit.getFromTag("TAG")
|
||||
|
||||
def test_nominal__commit_getMessage(self):
|
||||
commit_message="AAAABBB CCCCDDDD".replace('\r\n','\n').replace('\n','\r\n')
|
||||
with open("demofile.txt", "w+t") as tmpFile:
|
||||
tmpFile.write("testvalue")
|
||||
os.system("git add .")
|
||||
os.system(f"git commit -m \"{commit_message}\"")
|
||||
commit = pygitversionhelper.gitversionhelper.commit.getLast()
|
||||
message = pygitversionhelper.gitversionhelper.commit.getMessage(commit)
|
||||
self.assertEqual(message,commit_message)
|
||||
|
||||
def test_nominal__commit_getMessage2(self):
|
||||
commit_message="""AAAABBB
|
||||
CCCCDDDD
|
||||
-f dfsds dfsdfs $""".replace('\r\n','\n').replace('\n','\r\n')
|
||||
|
||||
with open("demofile.txt", "w+t") as tmpFile:
|
||||
tmpFile.write("testvalue")
|
||||
os.system("git add .")
|
||||
|
||||
cmd = "git commit -m".split()
|
||||
cmd.append(commit_message)
|
||||
subprocess.run(cmd,text=True,check=True)
|
||||
|
||||
commit = pygitversionhelper.gitversionhelper.commit.getLast()
|
||||
message = pygitversionhelper.gitversionhelper.commit.getMessage(commit)
|
||||
|
||||
self.assertEqual(message,commit_message)
|
||||
|
||||
def test_nominal__commit_getMessagesSinceLastTag(self):
|
||||
commit_message1="1.1 update this"+ os.linesep+\
|
||||
"1.1 fix that" + os.linesep+\
|
||||
"1.1 test"
|
||||
commit_message1=commit_message1.replace('\r\n','\n').replace('\n','\r\n')
|
||||
with open("demofile.txt", "w+t") as tmpFile:
|
||||
tmpFile.write("testvalue")
|
||||
os.system("git add .")
|
||||
|
||||
cmd = "git commit -m".split()
|
||||
cmd.append(commit_message1)
|
||||
subprocess.run(cmd,text=True,check=True)
|
||||
os.system(f"git tag 0.1.1")
|
||||
|
||||
commit_message2="2.1 update this"+ os.linesep+\
|
||||
"2.1 fix that" + os.linesep+\
|
||||
"2.1 test"
|
||||
commit_message2=commit_message2.replace('\r\n','\n').replace('\n','\r\n')
|
||||
with open("demofile.txt", "w+t") as tmpFile:
|
||||
tmpFile.write("testvalue2")
|
||||
os.system("git add .")
|
||||
|
||||
cmd = "git commit -m".split()
|
||||
cmd.append(commit_message2)
|
||||
subprocess.run(cmd,text=True,check=True)
|
||||
|
||||
commit_message3="3.1 update this"+ os.linesep+\
|
||||
"3.1 fix that" + os.linesep+\
|
||||
"3.1 test"
|
||||
commit_message3=commit_message3.replace('\r\n','\n').replace('\n','\r\n')
|
||||
with open("demofile.txt", "w+t") as tmpFile:
|
||||
tmpFile.write("testvalue3")
|
||||
os.system("git add .")
|
||||
|
||||
cmd = "git commit -m".split()
|
||||
cmd.append(commit_message3)
|
||||
subprocess.run(cmd,text=True,check=True)
|
||||
|
||||
commit_message4="4.1 update this" + os.linesep+\
|
||||
"4.1 fix that" + os.linesep+\
|
||||
"4.1 test"
|
||||
commit_message4=commit_message4.replace('\r\n','\n').replace('\n','\r\n')
|
||||
with open("demofile.txt", "w+t") as tmpFile:
|
||||
tmpFile.write("testvalue4")
|
||||
os.system("git add .")
|
||||
|
||||
cmd = "git commit -m".split()
|
||||
cmd.append(commit_message4)
|
||||
subprocess.run(cmd,text=True,check=True)
|
||||
|
||||
res = pygitversionhelper.gitversionhelper.commit.getMessagesSinceTag("0.1.1")
|
||||
self.assertEqual( [commit_message4,commit_message3,commit_message2],res)
|
||||
|
||||
res = pygitversionhelper.gitversionhelper.commit.getMessagesSinceTag("0.1.1",merged_output=True)
|
||||
self.assertEqual( os.linesep.join([commit_message4,commit_message3,commit_message2]),res)
|
||||
|
||||
|
||||
def tearDown(self):
|
||||
os.chdir("/")
|
||||
|
||||
@@ -1083,7 +1330,7 @@ class Test_gitversionhelperNoRepo(unittest.TestCase):
|
||||
os.chdir(self.TmpWorkingDirPath)
|
||||
|
||||
def test_defect__norepo(self):
|
||||
with self.assertRaises(pygitversionhelper.gitversionhelper.repository.notAGitRepository) as context:
|
||||
with self.assertRaises(pygitversionhelper.gitversionhelper.repository.notAGitRepository) :
|
||||
_v = pygitversionhelper.gitversionhelper.version.getCurrentVersion()
|
||||
|
||||
def tearDown(self):
|
||||
|
||||
Reference in New Issue
Block a user