Compare commits

...

2 Commits

Author SHA1 Message Date
cclecle
2951e70c47 fix: escapes git strings options 2023-03-19 10:15:23 +00:00
cclecle
63b3f25b33 fix quality warning + commit messages unittest 2023-03-19 10:03:50 +00:00
2 changed files with 25 additions and 20 deletions

View File

@@ -53,7 +53,7 @@ def _exec(cmd: str, root: str | os.PathLike | None = None, raw:bool = False) ->
raise gitversionhelper.unknownGITFatalError(p.stderr)
if int(p.returncode) < 0: #pragma: nocover
raise gitversionhelper.unknownGITError(p.stderr)
if raw:
return p.stdout
lines = p.stdout.splitlines()
@@ -126,7 +126,7 @@ class gitversionhelper: # pylint: disable=too-few-public-methods
"""
tag not found exception
"""
@classmethod
def getMessagesSinceTag(cls,tag:str,**kwargs) -> str:
"""
@@ -139,39 +139,39 @@ class gitversionhelper: # pylint: disable=too-few-public-methods
the commit message
"""
current_commit_id=cls.getLast(**kwargs)
tag_commit_id=cls.getFromTag(tag,**kwargs)
if ((cls.__OptDict["same_branch"] in kwargs) and (kwargs[cls.__OptDict["same_branch"]] is True)):
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,**kwargs))
if ((cls.__OptDict["merged_output"] in kwargs) and (kwargs[cls.__OptDict["merged_output"]] is True)):
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, id:str, **kwargs) -> str:
def getMessage(cls, hash:str) -> str:
"""
retrieve a commit message from repository
Args:
id: id of the commit
hash: id of the commit
Returns:
the commit message
"""
try:
res=_exec(f"git log -z --pretty=tformat:%B%-C() -n 1 {id}",None,True).rstrip('\x00')
res=_exec(f"git log -z --pretty=\"tformat:%B%-C()\" -n 1 {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,**kwargs) -> str:
def getFromTag(cls,tag:str) -> str:
"""
retrieve a commit from repository associated to a tag
Args:
@@ -196,14 +196,14 @@ class gitversionhelper: # pylint: disable=too-few-public-methods
Returns:
the commit Id
"""
if ((cls.__OptDict["same_branch"] in kwargs) and (kwargs[cls.__OptDict["same_branch"]] is True)):
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)")
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]

View File

@@ -1235,7 +1235,7 @@ class Test_gitversionhelper(unittest.TestCase):
pygitversionhelper.gitversionhelper.commit.getFromTag("TAG")
def test_nominal__commit_getMessage(self):
commit_message="AAAABBB CCCCDDDD"
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 .")
@@ -1247,7 +1247,8 @@ class Test_gitversionhelper(unittest.TestCase):
def test_nominal__commit_getMessage2(self):
commit_message="""AAAABBB
CCCCDDDD
-f dfsds dfsdfs $"""
-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 .")
@@ -1258,13 +1259,14 @@ class Test_gitversionhelper(unittest.TestCase):
commit = pygitversionhelper.gitversionhelper.commit.getLast()
message = pygitversionhelper.gitversionhelper.commit.getMessage(commit)
print(message)
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 .")
@@ -1277,6 +1279,7 @@ class Test_gitversionhelper(unittest.TestCase):
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 .")
@@ -1288,6 +1291,7 @@ class Test_gitversionhelper(unittest.TestCase):
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 .")
@@ -1299,6 +1303,7 @@ class Test_gitversionhelper(unittest.TestCase):
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 .")