From a419af2ade5a389e1b55f4ed00a3ec4abd0ffcea Mon Sep 17 00:00:00 2001 From: cclecle Date: Mon, 27 Mar 2023 23:11:16 +0100 Subject: [PATCH 1/7] add ignore_merged option to commit search --- mkdocs.yml | 15 ++++-- src/pygitversionhelper/gitversionhelper.py | 20 ++++++-- test/test_gitversionhelper.py | 58 +++++++++++++++++++++- 3 files changed, 83 insertions(+), 10 deletions(-) diff --git a/mkdocs.yml b/mkdocs.yml index 438ff03..aa37e6e 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -1,11 +1,11 @@ docs_dir: docs site_name: pygitversionhelper -site_url: 'https://chacha.ddns.net/mkdocs-web/chacha/pygitversionhelper/latest/' -site_description: 'A simple simple git version helper in python.' +site_url: https://chacha.ddns.net/mkdocs-web/chacha/pygitversionhelper/latest/ +site_description: A simple simple git version helper in python. site_author: prune -repo_url: 'https://chacha.ddns.net/gitea/chacha/pygitversionhelper' +repo_url: https://chacha.ddns.net/gitea/chacha/pygitversionhelper use_directory_urls: false -copyright: 'CC BY-NC-SA 4.0' +copyright: CC BY-NC-SA 4.0 theme: name: material features: @@ -57,6 +57,13 @@ plugins: show_root_full_path: false merge_init_into_class: true separate_signature: true +- with-pdf: + cover_subtitle: User Manual + cover_logo: C:\Users\chacha\git\pygitversionhelper\docs-static\Library.jpg + verbose: false + exclude_pages: + - LICENSE + output_path: C:\Users\chacha\git\pygitversionhelper\helpers-results\doc_gen\site\pdf\manual.pdf markdown_extensions: - def_list - tables diff --git a/src/pygitversionhelper/gitversionhelper.py b/src/pygitversionhelper/gitversionhelper.py index 4765769..e9245f7 100644 --- a/src/pygitversionhelper/gitversionhelper.py +++ b/src/pygitversionhelper/gitversionhelper.py @@ -57,6 +57,7 @@ if TYPE_CHECKING: "merged_output": Optional[bool], "ignore_unknown_tags": Optional[bool], "output_format": Optional[str], + "ignore_merged": Optional[bool], }, total=False, ) @@ -161,7 +162,9 @@ class gitversionhelper: # pylint: disable=too-few-public-methods the commit message """ current_commit_id = cls.getLast(**kwargs) + print(f"current_commit_id = {current_commit_id}") tag_commit_id = cls.getFromTag(tag) + print(f"tag_commit_id = {tag_commit_id}") if ("same_branch" in kwargs) and (kwargs["same_branch"] is True): commits = _exec(f"git rev-list --first-parent --ancestry-path {tag_commit_id}..{current_commit_id}") @@ -216,16 +219,23 @@ class gitversionhelper: # pylint: disable=too-few-public-methods """retrieve last commit from repository Keyword Arguments: kwargs/same_branch (bool): force searching only in the same branch + kwargs/ignore_merged (bool): ignore merged commits Returns: the commit Id """ + str_cmd: str if ("same_branch" in kwargs) and (kwargs["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 + str_cmd = "git rev-list --max-count=1 --date-order HEAD --first-parent" # ok else: - res = _exec('git for-each-ref --sort=-committerdate refs/heads/ --count 1 --format="%(objectname)"') + str_cmd = "git log --format=%H --all -n1" + + if ("ignore_merged" in kwargs) and (kwargs["ignore_merged"] is True): + str_cmd = str_cmd + " --no-merges" # ok + + try: + res = _exec(str_cmd) + 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") diff --git a/test/test_gitversionhelper.py b/test/test_gitversionhelper.py index 17f8dfd..0842d6a 100644 --- a/test/test_gitversionhelper.py +++ b/test/test_gitversionhelper.py @@ -1009,6 +1009,16 @@ class Test_gitversionhelper(unittest.TestCase): self.assertEqual(_v.minor, 3) self.assertEqual(_v.patch, 0) + with open("demofile.txt", "w+t") as tmpFile: + tmpFile.write("testvalue4") + os.system("git add .") + os.system('git commit -m "4th commit"') + + with open("demofile.txt", "w+t") as tmpFile: + tmpFile.write("testvalue5") + os.system("git add .") + os.system('git commit -m "5th commit"') + os.system("git switch master") _v = pygitversionhelper.gitversionhelper.version.getLastVersion(version_std="PEP440") @@ -1404,6 +1414,17 @@ class Test_gitversionhelper(unittest.TestCase): cmd = "git commit -m".split() cmd.append(commit_message5) subprocess.run(cmd, text=True, check=True) + os.system(f"git tag 0.1.1.post4") + + commit_message6 = "6.1 update this" + os.linesep + "6.1 fix that" + os.linesep + "6.1 test" + commit_message6 = commit_message6.replace("\r\n", "\n").replace("\n", "\r\n") + with open("demofile.txt", "w+t") as tmpFile: + tmpFile.write("testvalue6") + os.system("git add .") + + cmd = "git commit -m".split() + cmd.append(commit_message6) + subprocess.run(cmd, text=True, check=True) cmd = "git switch master".split() subprocess.run(cmd, text=True, check=True) @@ -1412,7 +1433,42 @@ class Test_gitversionhelper(unittest.TestCase): self.assertEqual(os.linesep.join([commit_message4, commit_message3, commit_message2]), res) res = pygitversionhelper.gitversionhelper.commit.getMessagesSinceTag("0.1.1", merged_output=True) - self.assertEqual(os.linesep.join([commit_message5, commit_message4, commit_message3, commit_message2]), res) + self.assertEqual(os.linesep.join([commit_message6, commit_message5, commit_message4, commit_message3, commit_message2]), res) + + time.sleep(1) + + merge_message = "automerge" + cmd = "git merge --no-ff dev -m".split() + cmd.append(merge_message) + subprocess.run(cmd, text=True, check=True) + + res = pygitversionhelper.gitversionhelper.commit.getMessagesSinceTag("0.1.1", merged_output=True, same_branch=True) + self.assertEqual(os.linesep.join([merge_message, commit_message4, commit_message3, commit_message2]), res) + + res = pygitversionhelper.gitversionhelper.commit.getMessagesSinceTag("0.1.1", merged_output=True) + self.assertEqual( + set( + merge_message.splitlines() + + commit_message6.splitlines() + + commit_message5.splitlines() + + commit_message4.splitlines() + + commit_message3.splitlines() + + commit_message2.splitlines() + ), + set(res.splitlines()), + ) + + res = pygitversionhelper.gitversionhelper.commit.getMessagesSinceTag("0.1.1", merged_output=True, ignore_merged=True) + self.assertEqual( + set( + commit_message6.splitlines() + + commit_message5.splitlines() + + commit_message4.splitlines() + + commit_message3.splitlines() + + commit_message2.splitlines() + ), + set(res.splitlines()), + ) def tearDown(self): os.chdir("/") From d3bf6ddaa6f6112aa49b734c134fb19b191f4bda Mon Sep 17 00:00:00 2001 From: cclecle Date: Mon, 27 Mar 2023 23:18:42 +0100 Subject: [PATCH 2/7] add ignore_merged to git messages --- src/pygitversionhelper/gitversionhelper.py | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/src/pygitversionhelper/gitversionhelper.py b/src/pygitversionhelper/gitversionhelper.py index e9245f7..0e720a0 100644 --- a/src/pygitversionhelper/gitversionhelper.py +++ b/src/pygitversionhelper/gitversionhelper.py @@ -158,6 +158,7 @@ class gitversionhelper: # pylint: disable=too-few-public-methods Keyword Arguments: kwargs/merged_output (bool): Output one single merged string kwargs/same_branch (bool): Force searching only in the same branch + kwargs/ignore_merged (bool): ignore merged commits Returns: the commit message """ @@ -166,10 +167,20 @@ class gitversionhelper: # pylint: disable=too-few-public-methods tag_commit_id = cls.getFromTag(tag) print(f"tag_commit_id = {tag_commit_id}") + str_cmd: str if ("same_branch" in kwargs) and (kwargs["same_branch"] is True): - commits = _exec(f"git rev-list --first-parent --ancestry-path {tag_commit_id}..{current_commit_id}") + str_cmd = 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}") + str_cmd = f"git rev-list --ancestry-path {tag_commit_id}..{current_commit_id}" + + if ("ignore_merged" in kwargs) and (kwargs["ignore_merged"] is True): + str_cmd = str_cmd + " --no-merges" # ok + + try: + commits = _exec(str_cmd) + except gitversionhelper.unknownGITFatalError as _e: + raise cls.commitNotFound("no commit found in commit history") from _e + result = [] for commit in commits: result.append(cls.getMessage(commit)) @@ -188,7 +199,7 @@ class gitversionhelper: # pylint: disable=too-few-public-methods """ try: res = _exec( - f'git log -z --pretty="tformat:%B%-C()" -n 1 {commit_hash}', + f'git log -z --pretty="tformat:%B%-C()" -n 1 {commit_hash}', # ok None, True, ) @@ -207,9 +218,10 @@ class gitversionhelper: # pylint: disable=too-few-public-methods the commit Id """ try: - res = _exec(f"git rev-list -n 1 {tag}") + res = _exec(f"git rev-list -n 1 {tag}") # ok 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] @@ -227,7 +239,7 @@ class gitversionhelper: # pylint: disable=too-few-public-methods if ("same_branch" in kwargs) and (kwargs["same_branch"] is True): str_cmd = "git rev-list --max-count=1 --date-order HEAD --first-parent" # ok else: - str_cmd = "git log --format=%H --all -n1" + str_cmd = "git log --format=%H --all -n1" # ok if ("ignore_merged" in kwargs) and (kwargs["ignore_merged"] is True): str_cmd = str_cmd + " --no-merges" # ok From e06a3b00ae2e7c6fbb5c7ec8d827e580286ce7e8 Mon Sep 17 00:00:00 2001 From: cclecle Date: Mon, 27 Mar 2023 23:27:21 +0100 Subject: [PATCH 3/7] fix wrong getMessagesSinceTag() command --- src/pygitversionhelper/gitversionhelper.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/pygitversionhelper/gitversionhelper.py b/src/pygitversionhelper/gitversionhelper.py index 0e720a0..e2180f9 100644 --- a/src/pygitversionhelper/gitversionhelper.py +++ b/src/pygitversionhelper/gitversionhelper.py @@ -169,9 +169,9 @@ class gitversionhelper: # pylint: disable=too-few-public-methods str_cmd: str if ("same_branch" in kwargs) and (kwargs["same_branch"] is True): - str_cmd = f"git rev-list --first-parent --ancestry-path {tag_commit_id}..{current_commit_id}" + str_cmd = f"git rev-list --first-parent {tag_commit_id}..{current_commit_id}" # ok else: - str_cmd = f"git rev-list --ancestry-path {tag_commit_id}..{current_commit_id}" + str_cmd = f"git rev-list {tag_commit_id}..{current_commit_id}" # ok if ("ignore_merged" in kwargs) and (kwargs["ignore_merged"] is True): str_cmd = str_cmd + " --no-merges" # ok From 0ef5a8f4636ddfd61439348061176eb804deb0f8 Mon Sep 17 00:00:00 2001 From: cclecle Date: Mon, 27 Mar 2023 23:46:34 +0100 Subject: [PATCH 4/7] feature: add changelog ! --- Jenkinsfile | 19 ++++++++++++++----- helpers/complexity_check.py | 1 - 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/Jenkinsfile b/Jenkinsfile index a097e14..07a0f19 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -184,16 +184,14 @@ pipeline { 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 twine") + sh(". ~/TOOLS_ENV/bin/activate && pip install simple_rest_client requests twine packaging) script { if(_PROJECT_NAME!="pygitversionhelper") { sh(". ~/TOOLS_ENV/bin/activate && pip install pygitversionhelper") } - else - { - //TODO: need to install pygitversionhelper deps from a better way... - sh(". ~/TOOLS_ENV/bin/activate && pip install packaging") + if(_PROJECT_NAME!="pychangelogfactory") { + sh(". ~/TOOLS_ENV/bin/activate && pip install pychangelogfactory") } } sh("git config --global user.email $_MaintainerEmail") @@ -574,6 +572,11 @@ pipeline { |from simple_rest_client.resource import Resource | |try: + | from pychangelogfactory import ChangelogFactory + |except ImportError: + | from src.pychangelogfactory import ChangelogFactory + | + |try: | from pygitversionhelper import gitversionhelper |except ImportError: | from src.pygitversionhelper import gitversionhelper @@ -619,6 +622,12 @@ pipeline { |ReleaseContent = "${_ReleaseContent_Title}" + "\\n" \\ | + "\\n" \\ | + "Reference documentation: [mkdocs page](https://chacha.ddns.net/mkdocs-web/${_PROJECT_USER_NAME}/${PY_PROJECT_NAME}/${_GIT_BRANCH}/${PY_PROJECT_VERSION_STRIPPED}/) " + | + |LastTag=gitversionhelper.tag.getLastTag(same_branch=True) + |CommitHistory=gitversionhelper.commit.getMessagesSinceTag(LastTag, merged_output=True, ignore_merged=True) + |Changelog = ChangelogFactory(CommitHistory).RenderFullChangelog() + | + |ReleaseContent = ReleaseContent + "\\n"+ "\\n"+ "#Changelog\\n" + Changelog | |data={ | "body": ReleaseContent, diff --git a/helpers/complexity_check.py b/helpers/complexity_check.py index 529e872..a61959a 100644 --- a/helpers/complexity_check.py +++ b/helpers/complexity_check.py @@ -19,7 +19,6 @@ from radon.cli import Config from radon.cli.harvest import CCHarvester, HCHarvester, MIHarvester from .helper_base import helper_withresults_base -from pprint import pprint class complexity_check(helper_withresults_base): From c6ed7ef0a1c162a379cf5ffc79c2129f3fc8c3e8 Mon Sep 17 00:00:00 2001 From: cclecle Date: Mon, 27 Mar 2023 23:48:18 +0100 Subject: [PATCH 5/7] fix wrong typo in jenkinsfile --- Jenkinsfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Jenkinsfile b/Jenkinsfile index 07a0f19..6f4f2ff 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -184,7 +184,7 @@ pipeline { 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 twine packaging) + sh(". ~/TOOLS_ENV/bin/activate && pip install simple_rest_client requests twine packaging") script { if(_PROJECT_NAME!="pygitversionhelper") { From a5f39d6cb2e6da097ccbae68cc0349d70a8833b4 Mon Sep 17 00:00:00 2001 From: cclecle Date: Tue, 28 Mar 2023 00:04:31 +0100 Subject: [PATCH 6/7] update changelog --- Jenkinsfile | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Jenkinsfile b/Jenkinsfile index 6f4f2ff..ce443dc 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -627,7 +627,10 @@ pipeline { |CommitHistory=gitversionhelper.commit.getMessagesSinceTag(LastTag, merged_output=True, ignore_merged=True) |Changelog = ChangelogFactory(CommitHistory).RenderFullChangelog() | - |ReleaseContent = ReleaseContent + "\\n"+ "\\n"+ "#Changelog\\n" + Changelog + |ReleaseContent = ReleaseContent + "\\n"+ "\\n"+ "## Changelog:\\n" + Changelog + | + |if not Changelog: + | ReleaseContent = ReleaseContent + "code/project maintainance" | |data={ | "body": ReleaseContent, From e1c1e643c3bb146cd7e8fac1069b78c6ac166575 Mon Sep 17 00:00:00 2001 From: cclecle Date: Tue, 28 Mar 2023 00:08:18 +0100 Subject: [PATCH 7/7] include others changes in changelog --- Jenkinsfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Jenkinsfile b/Jenkinsfile index ce443dc..620b2f3 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -625,7 +625,7 @@ pipeline { | |LastTag=gitversionhelper.tag.getLastTag(same_branch=True) |CommitHistory=gitversionhelper.commit.getMessagesSinceTag(LastTag, merged_output=True, ignore_merged=True) - |Changelog = ChangelogFactory(CommitHistory).RenderFullChangelog() + |Changelog = ChangelogFactory(CommitHistory).RenderFullChangelog(include_unknown=True) | |ReleaseContent = ReleaseContent + "\\n"+ "\\n"+ "## Changelog:\\n" + Changelog |