10000 refactor(git): rename get tag function to distinguish return str and … · Lee-W/commitizen@9f93074 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9f93074

Browse files
committed
refactor(git): rename get tag function to distinguish return str and GitTag
1 parent f43cd6a commit 9f93074

File tree

2 files changed

+28
-30
lines changed

2 files changed

+28
-30
lines changed

commitizen/commands/init.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from commitizen import factory, out
66
from commitizen.cz import registry
77
from commitizen.config import BaseConfig, TomlConfig, IniConfig
8-
from commitizen.git import get_latest_tag, get_all_tags
8+
from commitizen.git import get_latest_tag_name, get_tag_names
99
from commitizen.defaults import config_files
1010

1111

@@ -57,7 +57,7 @@ def _ask_name(self) -> str:
5757
return name
5858

5959
def _ask_tag(self) -> str:
60-
latest_tag = get_latest_tag()
60+
latest_tag = get_latest_tag_name()
6161
if not latest_tag:
6262
out.error("No Existing Tag. Set tag to v0.0.1")
6363
return "0.0.1"
@@ -66,14 +66,14 @@ def _ask_tag(self) -> str:
6666
f"Is {latest_tag} the latest tag?", style=self.cz.style, default=False
6767
).ask()
6868
if not is_correct_tag:
69-
tags = get_all_tags()
69+
tags = get_tag_names()
7070
if not tags:
7171
out.error("No Existing Tag. Set tag to v0.0.1")
7272
return "0.0.1"
7373

7474
latest_tag = questionary.select(
7575
"Please choose the latest tag: ",
76-
choices=get_all_tags(),
76+
choices=get_tag_names(),
7777
style=self.cz.style,
7878
).ask()
7979

commitizen/git.py

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ def __eq__(self, other):
1515

1616
class GitCommit(GitObject):
1717
def __init__(self, rev, title, body=""):
18-
self.rev = rev
19-
self.title = title
20-
self.body = body
18+
self.rev = rev.strip()
19+
self.title = title.strip()
20+
self.body = body.strip()
2121

2222
@property
2323
def message(self):
@@ -28,9 +28,10 @@ def __repr__(self):
2828

2929

3030
class GitTag(GitObject):
31-
def __init__(self, name, rev):
32-
self.name = name
33-
self.rev = rev
31+
def __init__(self, name, rev, date):
32+
self.rev = rev.strip()
33+
self.name = name.strip()
34+
self.date = date.strip()
3435

3536
def __repr__(self):
3637
return f"{self.name} ({self.rev})"
@@ -85,19 +86,34 @@ def get_commits(
8586
return git_commits
8687

8788

89+
def get_tags(dateformat: str = "%Y-%m-%d") -> List[GitTag]:
90+
inner_delimiter = "---inner_delimiter---"
91+
formatter = (
92+
f"'%(refname:lstrip=2){inner_delimiter}"
93+
f"%(objectname){inner_delimiter}"
94+
f"%(committerdate:format:{dateformat})'"
95+
)
96+
c = cmd.run(f"git tag --format={formatter} --sort=-committerdate")
97+
if c.err or not c.out:
98+
return []
99+
100+
git_tags = [GitTag(*line.split(inner_delimiter)) for line in c.out.split("\n")[:-1]]
101+
return git_tags
102+
103+
88104
def tag_exist(tag: str) -> bool:
89105
c = cmd.run(f"git tag --list {tag}")
90106
return tag in c.out
91107

92108

93-
def get_latest_tag() -> Optional[str]:
109+
def get_latest_tag_name() -> Optional[str]:
94110
c = cmd.run("git describe --abbrev=0 --tags")
95111
if c.err:
96112
return None
97113
return c.out.strip()
98114

99115

100-
def get_all_tags() -> Optional[List[str]]:
116+
def get_tag_names() -> Optional[List[str]]:
101117
c = cmd.run("git tag --list")
102118
if c.err:
103119
return []
@@ -111,24 +127,6 @@ def find_git_project_root() -> Optional[Path]:
111127
return None
112128

113129

114-
def get_tags_with_rev() -> List[GitTag]:
115-
tag_delimiter = "---tag-delimiter---"
116-
c = cmd.run(
117-
(
118-
f"git tag --format='%(refname:lstrip=2){tag_delimiter}%(objectname)'"
119-
" --sort=-committerdate"
120-
)
121-
)
122-
if c.err or not c.out:
123-
return []
124-
125-
git_tags = []
126-
for line in c.out.split("\n")[:-1]:
127-
name, rev = line.split(tag_delimiter)
128-
git_tags.append(GitTag(name.strip(), rev.strip()))
129-
return git_tags
130-
131-
132130
def is_staging_clean() -> bool:
133131
"""Check if staing is clean"""
134132
c = cmd.run("git diff --no-ext-diff --name-only")

0 commit comments

Comments
 (0)
0