11from __future__ import annotations
22
33import os
4- from enum import Enum
5- from os import linesep
64from pathlib import Path
75from tempfile import NamedTemporaryFile
86
97from commitizen import cmd , out
108from commitizen .exceptions import GitCommandError
119
12- UNIX_EOL = "\n "
13- WINDOWS_EOL = "\r \n "
14-
15-
16- class EOLTypes (Enum ):
17- """The EOL type from `git config core.eol`."""
18-
19- LF = "lf"
20- CRLF = "crlf"
21- NATIVE = "native"
22-
23- def get_eol_for_open (self ) -> str :
24- """Get the EOL character for `open()`."""
25- map = {
26- EOLTypes .CRLF : WINDOWS_EOL ,
27- EOLTypes .LF : UNIX_EOL ,
28- EOLTypes .NATIVE : linesep ,
29- }
30-
31- return map [self ]
10+ _UNIX_EOL = "\n "
11+ _WINDOWS_EOL = "\r \n "
3212
3313
3414class GitObject :
@@ -37,9 +17,7 @@ class GitObject:
3717 date : str
3818
3919 def __eq__ (self , other ) -> bool :
40- if not hasattr (other , "rev" ):
41- return False
42- return self .rev == other .rev # type: ignore
20+ return hasattr (other , "rev" ) and self .rev == other .rev # type: ignore
4321
4422
4523class GitCommit (GitObject ):
@@ -101,13 +79,11 @@ def tag(
10179 # according to https://git-scm.com/book/en/v2/Git-Basics-Tagging,
10280 # we're not able to create lightweight tag with message.
10381 # by adding message, we make it a annotated tags
104- c = cmd .run (f'git tag { _opt } "{ tag if _opt == "" or msg is None else msg } "' )
105- return c
82+ return cmd .run (f'git tag { _opt } "{ tag if _opt == "" or msg is None else msg } "' )
10683
10784
10885def add (* args : str ) -> cmd .Command :
109- c = cmd .run (f"git add { ' ' .join (args )} " )
110- return c
86+ return cmd .run (f"git add { ' ' .join (args )} " )
11187
11288
11389def commit (
@@ -141,22 +117,19 @@ def get_commits(
141117 """Get the commits between start and end."""
142118 git_log_entries = _get_log_as_str_list (start , end , args )
143119 git_commits = []
144- for rev_and_commit in git_log_entries :
145- if not rev_and_commit :
146- continue
120+ for rev_and_commit in filter (None , git_log_entries ):
147121 rev , parents , title , author , author_email , * body_list = rev_and_commit .split (
148122 "\n "
149123 )
150- if rev_and_commit :
151- git_commit = GitCommit (
152- rev = rev .strip (),
153- title = title .strip (),
154- body = "\n " .join (body_list ).strip (),
155- author = author ,
156- author_email = author_email ,
157- parents = [p for p in parents .strip ().split (" " ) if p ],
158- )
159- git_commits .append (git_commit )
124+ git_commit = GitCommit (
125+ rev = rev .strip (),
126+ title = title .strip (),
127+ body = "\n " .join (body_list ).strip (),
128+ author = author ,
129+ author_email = author_email ,
130+ parents = [p for p in parents .strip ().split (" " ) if p ],
131+ )
132+ git_commits .append (git_commit )
160133 return git_commits
161134
162135
@@ -170,8 +143,7 @@ def get_filenames_in_commit(git_reference: str = ""):
170143 c = cmd .run (f"git show --name-only --pretty=format: { git_reference } " )
171144 if c .return_code == 0 :
172145 return c .out .strip ().split ("\n " )
173- else :
174- raise GitCommandError (c .err )
146+ raise GitCommandError (c .err )
175147
176148
177149def get_tags (
@@ -240,9 +212,9 @@ def get_tag_names() -> list[str | None]:
240212
241213def find_git_project_root () -> Path | None :
242214 c = cmd .run ("git rev-parse --show-toplevel" )
243- if not c .err :
244- return Path ( c . out . strip ())
245- return None
215+ if c .err :
216+ return None
217+ return Path ( c . out . strip ())
246218
247219
248220def is_staging_clean () -> bool :
@@ -253,32 +225,19 @@ def is_staging_clean() -> bool:
253225
254226def is_git_project () -> bool :
255227 c = cmd .run ("git rev-parse --is-inside-work-tree" )
256- if c .out .strip () == "true" :
257- return True
258- return False
228+ return c .out .strip () == "true"
259229
260230
261- def get_eol_style () -> EOLTypes :
231+ def get_eol_for_open () -> str :
232+ # See: https://git-scm.com/docs/git-config#Documentation/git-config.txt-coreeol
262233 c = cmd .run ("git config core.eol" )
263234 eol = c .out .strip ().lower ()
264235
265- # We enumerate the EOL types of the response of
266- # `git config core.eol`, and map it to our enumration EOLTypes.
267- #
268- # It is just like the variant of the "match" syntax.
269- map = {
270- "lf" : EOLTypes .LF ,
271- "crlf" : EOLTypes .CRLF ,
272- "native" : EOLTypes .NATIVE ,
273- }
274-
275- # If the response of `git config core.eol` is in the map:
276- if eol in map :
277- return map [eol ]
278- else :
279- # The default value is "native".
280- # https://git-scm.com/docs/git-config#Documentation/git-config.txt-coreeol
281- return map ["native" ]
236+ if eol == "lf" :
237+ return _UNIX_EOL
238+ if eol == "crlf" :
239+ return _WINDOWS_EOL
240+ return os .linesep
282241
283242
284243def get_core_editor () -> str | None :
@@ -288,9 +247,9 @@ def get_core_editor() -> str | None:
288247 return None
289248
290249
291- def smart_open (* args , ** kargs ):
250+ def smart_open (* args , ** kwargs ):
292251 """Open a file with the EOL style determined from Git."""
293- return open (* args , newline = get_eol_style (). get_eol_for_open (), ** kargs )
252+ return open (* args , newline = get_eol_for_open (), ** kwargs )
294253
295254
296255def _get_log_as_str_list (start : str | None , end : str , args : str ) -> list [str ]:
@@ -300,10 +259,7 @@ def _get_log_as_str_list(start: str | None, end: str, args: str) -> list[str]:
300259 git_log_cmd = (
301260 f"git -c log.showSignature=False log --pretty={ log_format } { delimiter } { args } "
302261 )
303- if start :
304- command = f"{ git_log_cmd } { start } ..{ end } "
305- else :
306- command = f"{ git_log_cmd } { end } "
262+ command = f"{ git_log_cmd } { f'{ start } ..{ end } ' if start else end } "
307263 c = cmd .run (command )
308264 if c .return_code != 0 :
309265 raise GitCommandError (c .err )
0 commit comments