add ignore_merged option to commit search

This commit is contained in:
cclecle
2023-03-27 23:11:16 +01:00
parent 5f9d513c59
commit a419af2ade
3 changed files with 83 additions and 10 deletions

View File

@@ -1,11 +1,11 @@
docs_dir: docs docs_dir: docs
site_name: pygitversionhelper site_name: pygitversionhelper
site_url: 'https://chacha.ddns.net/mkdocs-web/chacha/pygitversionhelper/latest/' site_url: https://chacha.ddns.net/mkdocs-web/chacha/pygitversionhelper/latest/
site_description: 'A simple simple git version helper in python.' site_description: A simple simple git version helper in python.
site_author: prune 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 use_directory_urls: false
copyright: 'CC BY-NC-SA 4.0' copyright: CC BY-NC-SA 4.0
theme: theme:
name: material name: material
features: features:
@@ -57,6 +57,13 @@ plugins:
show_root_full_path: false show_root_full_path: false
merge_init_into_class: true merge_init_into_class: true
separate_signature: 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: markdown_extensions:
- def_list - def_list
- tables - tables

View File

@@ -57,6 +57,7 @@ if TYPE_CHECKING:
"merged_output": Optional[bool], "merged_output": Optional[bool],
"ignore_unknown_tags": Optional[bool], "ignore_unknown_tags": Optional[bool],
"output_format": Optional[str], "output_format": Optional[str],
"ignore_merged": Optional[bool],
}, },
total=False, total=False,
) )
@@ -161,7 +162,9 @@ class gitversionhelper: # pylint: disable=too-few-public-methods
the commit message the commit message
""" """
current_commit_id = cls.getLast(**kwargs) current_commit_id = cls.getLast(**kwargs)
print(f"current_commit_id = {current_commit_id}")
tag_commit_id = cls.getFromTag(tag) 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): 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}") 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 """retrieve last commit from repository
Keyword Arguments: Keyword Arguments:
kwargs/same_branch (bool): force searching only in the same branch kwargs/same_branch (bool): force searching only in the same branch
kwargs/ignore_merged (bool): ignore merged commits
Returns: Returns:
the commit Id the commit Id
""" """
str_cmd: str
if ("same_branch" in kwargs) and (kwargs["same_branch"] is True): if ("same_branch" in kwargs) and (kwargs["same_branch"] is True):
try: str_cmd = "git rev-list --max-count=1 --date-order HEAD --first-parent" # ok
res = _exec("git rev-parse HEAD")
except gitversionhelper.unknownGITFatalError as _e:
raise cls.commitNotFound("no commit found in commit history") from _e
else: 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: if len(res) == 0:
raise cls.commitNotFound("no commit found in commit history") raise cls.commitNotFound("no commit found in commit history")

View File

@@ -1009,6 +1009,16 @@ class Test_gitversionhelper(unittest.TestCase):
self.assertEqual(_v.minor, 3) self.assertEqual(_v.minor, 3)
self.assertEqual(_v.patch, 0) 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") os.system("git switch master")
_v = pygitversionhelper.gitversionhelper.version.getLastVersion(version_std="PEP440") _v = pygitversionhelper.gitversionhelper.version.getLastVersion(version_std="PEP440")
@@ -1404,6 +1414,17 @@ class Test_gitversionhelper(unittest.TestCase):
cmd = "git commit -m".split() cmd = "git commit -m".split()
cmd.append(commit_message5) cmd.append(commit_message5)
subprocess.run(cmd, text=True, check=True) 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() cmd = "git switch master".split()
subprocess.run(cmd, text=True, check=True) 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) self.assertEqual(os.linesep.join([commit_message4, commit_message3, commit_message2]), res)
res = pygitversionhelper.gitversionhelper.commit.getMessagesSinceTag("0.1.1", merged_output=True) 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): def tearDown(self):
os.chdir("/") os.chdir("/")