44from datetime import datetime
55from subprocess import check_output
66
7- from .style import CommitStyle , BasicStyle , AtomStyle , AngularStyle
87from .providers import GitHub , GitLab
8+ from .style import AngularStyle , AtomStyle , BasicStyle , CommitStyle
99
1010
11- def bump (version , part = ' patch' ):
12- major , minor , patch = version .split ('.' , 2 )
13- patch = patch .split ('-' , 1 )
14- pre = ''
11+ def bump (version , part = " patch" ):
12+ major , minor , patch = version .split ("." , 2 )
13+ patch = patch .split ("-" , 1 )
14+ pre = ""
1515 if len (patch ) > 1 :
1616 patch , pre = patch
1717 else :
1818 patch = patch [0 ]
19- if part == ' major' :
19+ if part == " major" :
2020 major = str (int (major ) + 1 )
21- minor = patch = '0'
22- elif part == ' minor' :
21+ minor = patch = "0"
22+ elif part == " minor" :
2323 minor = str (int (minor ) + 1 )
24- patch = '0'
25- elif part == ' patch' and not pre :
24+ patch = "0"
25+ elif part == " patch" and not pre :
2626 patch = str (int (patch ) + 1 )
27- return '.' .join ((major , minor , patch ))
27+ return "." .join ((major , minor , patch ))
2828
2929
3030class Commit :
3131 def __init__ (
32- self , hash , author_name = '' , author_email = '' , author_date = '' ,
33- committer_name = '' , committer_email = '' , committer_date = '' ,
34- refs = '' , subject = '' , body = None , url = '' ):
32+ self ,
33+ hash ,
34+ author_name = "" ,
35+ author_email = "" ,
36+ author_date = "" ,
37+ committer_name = "" ,
38+ committer_email = "" ,
39+ committer_date = "" ,
40+ refs = "" ,
41+ subject = "" ,
42+ body = None ,
43+ url = "" ,
44+ ):
3545 self .hash = hash
3646 self .author_name = author_name
3747 self .author_email = author_email
@@ -43,11 +53,11 @@ def __init__(
4353 self .body = body or []
4454 self .url = url
4555
46- tag = ''
47- for ref in refs .split (',' ):
56+ tag = ""
57+ for ref in refs .split ("," ):
4858 ref = ref .strip ()
49- if ref .startswith (' tag: ' ):
50- tag = ref .replace (' tag: ' , '' )
59+ if ref .startswith (" tag: " ):
60+ tag = ref .replace (" tag: " , "" )
5161 break
5262 self .tag = self .version = tag
5363
@@ -60,34 +70,31 @@ def update_with_style(self, style):
6070 def update_with_provider (self , provider ):
6171 # set the commit url based on provider
6272 # FIXME: hardcoded 'commits'
63- if ' commits' in provider .REF :
64- self .url = provider .build_ref_url (' commits' , {' ref' : self .hash })
73+ if " commits" in provider .REF :
74+ self .url = provider .build_ref_url (" commits" , {" ref" : self .hash })
6575 else :
6676 # use default "commit" url (could be wrong)
67- self .url = '%s/%s/%s/commit/%s' % (
68- provider .url , provider .namespace ,
69- provider .project , self .hash )
77+ self .url = "%s/%s/%s/commit/%s" % (provider .url , provider .namespace , provider .project , self .hash )
7078
7179 # build commit text references from its subject and body
7280 for ref_type in provider .REF .keys ():
73- self .text_refs [ref_type ] = provider .get_refs (
74- ref_type , '\n ' .join ([self .subject ] + self .body ))
81+ self .text_refs [ref_type ] = provider .get_refs (ref_type , "\n " .join ([self .subject ] + self .body ))
7582
76- if ' issues' in self .text_refs :
77- self .text_refs [' issues_not_in_subject' ] = []
78- for issue in self .text_refs [' issues' ]:
83+ if " issues" in self .text_refs :
84+ self .text_refs [" issues_not_in_subject" ] = []
85+ for issue in self .text_refs [" issues" ]:
7986 if issue .ref not in self .subject :
80- self .text_refs [' issues_not_in_subject' ].append (issue )
87+ self .text_refs [" issues_not_in_subject" ].append (issue )
8188
8289
8390class Section :
84- def __init__ (self , type = '' , commits = None ):
91+ def __init__ (self , type = "" , commits = None ):
8592 self .type = type
8693 self .commits = commits or []
8794
8895
8996class Version :
90- def __init__ (self , tag = '' , date = '' , sections = None , commits = None , url = '' , compare_url = '' ):
97+ def __init__ (self , tag = "" , date = "" , sections = None , commits = None , url = "" , compare_url = "" ):
9198 self .tag = tag
9299 self .date = date
93100
@@ -105,49 +112,45 @@ def typed_sections(self):
105112
106113 @property
107114 def untyped_section (self ):
108- return self .sections_dict .get ('' , None )
115+ return self .sections_dict .get ("" , None )
109116
110117 @property
111118 def is_major (self ):
112- return self .tag .split ('.' , 1 )[1 ].startswith (' 0.0' )
119+ return self .tag .split ("." , 1 )[1 ].startswith (" 0.0" )
113120
114121 @property
115122 def is_minor (self ):
116- return self .tag .split ('.' , 2 )[2 ]
123+ return self .tag .split ("." , 2 )[2 ]
117124
118125
119126class Changelog :
120- MARKER = ' --GITOLOG MARKER--'
127+ MARKER = " --GITOLOG MARKER--"
121128 FORMAT = (
122- ' %H%n' # commit hash
123- ' %an%n' # author name
124- ' %ae%n' # author email
125- ' %ad%n' # author date
126- ' %cn%n' # committer name
127- ' %ce%n' # committer email
128- ' %cd%n' # committer date
129- ' %D%n' # tag
130- ' %s%n' # subject
131- ' %b%n' + MARKER # body
129+ " %H%n" # commit hash
130+ " %an%n" # author name
131+ " %ae%n" # author email
132+ " %ad%n" # author date
133+ " %cn%n" # committer name
134+ " %ce%n" # committer email
135+ " %cd%n" # committer date
136+ " %D%n" # tag
137+ " %s%n" # subject
138+ " %b%n" + MARKER # body
132139 )
133- STYLE = {
134- 'basic' : BasicStyle ,
135- 'angular' : AngularStyle ,
136- 'atom' : AtomStyle
137- }
140+ STYLE = {"basic" : BasicStyle , "angular" : AngularStyle , "atom" : AtomStyle }
138141
139142 def __init__ (self , repository , provider = None , style = None ):
140143 self .repository = repository
141144
142145 # set provider
143146 if not provider :
144147 remote_url = self .get_remote_url ()
145- split = remote_url .split ('/' )
146- provider_url = '/' .join (split [:3 ])
148+ split = remote_url .split ("/" )
149+ provider_url = "/" .join (split [:3 ])
147150 namespace , project = split [3 ], split [4 ]
148- if ' github' in provider_url :
151+ if " github" in provider_url :
149152 provider = GitHub (namespace , project , url = provider_url )
150- elif ' gitlab' in provider_url :
153+ elif " gitlab" in provider_url :
151154 provider = GitLab (namespace , project , url = provider_url )
152155 self .remote_url = remote_url
153156 self .provider = provider
@@ -157,8 +160,7 @@ def __init__(self, repository, provider=None, style=None):
157160 try :
158161 style = self .STYLE [style ]()
159162 except KeyError :
160- print ('gitolog: no such style available: %s, '
161- 'using default style' % style , file = sys .stderr )
163+ print ("gitolog: no such style available: %s, " "using default style" % style , file = sys .stderr )
162164 style = BasicStyle ()
163165 elif style is None :
164166 style = BasicStyle ()
@@ -175,70 +177,72 @@ def __init__(self, repository, provider=None, style=None):
175177 # apply dates to commits and group them by version
176178 dates = self .apply_versions_to_commits ()
177179 versions = self .group_commits_by_version (dates )
178- self .versions_list = versions [' as_list' ]
179- self .versions_dict = versions [' as_dict' ]
180+ self .versions_list = versions [" as_list" ]
181+ self .versions_dict = versions [" as_dict" ]
180182
181183 # guess the next version number based on last version and recent commits
182184 last_version = self .versions_list [0 ]
183185 if not last_version .tag and last_version .previous_version :
184186 last_tag = last_version .previous_version .tag
185187 major = minor = False
186188 for commit in last_version .commits :
187- if commit .style [' is_major' ]:
189+ if commit .style [" is_major" ]:
188190 major = True
189191 break
190- elif commit .style [' is_minor' ]:
192+ elif commit .style [" is_minor" ]:
191193 minor = True
192194 if major :
193- planned_tag = bump (last_tag , ' major' )
195+ planned_tag = bump (last_tag , " major" )
194196 elif minor :
195- planned_tag = bump (last_tag , ' minor' )
197+ planned_tag = bump (last_tag , " minor" )
196198 else :
197- planned_tag = bump (last_tag , ' patch' )
199+ planned_tag = bump (last_tag , " patch" )
198200 last_version .planned_tag = planned_tag
199201 last_version .url = self .provider .get_tag_url (tag = planned_tag )
200202 last_version .compare_url = self .provider .get_compare_url (
201- base = last_version .previous_version .tag , target = last_version .planned_tag )
203+ base = last_version .previous_version .tag , target = last_version .planned_tag
204+ )
202205
203206 def get_remote_url (self ):
204- git_url = check_output (
205- ['git' , 'config' , '--get' , 'remote.origin.url' ],
206- cwd = self .repository
207- ).decode ('utf-8' ).rstrip ('\n ' )
208- if git_url .startswith ('git@' ):
209- git_url = git_url .replace (':' , '/' , 1 ).replace ('git@' , 'https://' , 1 )
210- if git_url .endswith ('.git' ):
207+ git_url = (
208+ check_output (["git" , "config" , "--get" , "remote.origin.url" ], cwd = self .repository )
209+ .decode ("utf-8" )
210+ .rstrip ("\n " )
211+ )
212+ if git_url .startswith ("git@" ):
213+ git_url = git_url .replace (":" , "/" , 1 ).replace ("git@" , "https://" , 1 )
214+ if git_url .endswith (".git" ):
211215 git_url = git_url [:- 4 ]
212216 return git_url
213217
214218 def get_log (self ):
215- return check_output (
216- [ 'git' , 'log' , '--date=unix' , '--format=' + self . FORMAT ],
217- cwd = self . repository ). decode ( 'utf-8' )
219+ return check_output ([ "git" , "log" , "--date=unix" , "--format=" + self . FORMAT ], cwd = self . repository ). decode (
220+ "utf-8"
221+ )
218222
219223 def parse_commits (self ):
220- lines = self .raw_log .split (' \n ' )
224+ lines = self .raw_log .split (" \n " )
221225 size = len (lines ) - 1 # don't count last blank line
222226 commits = []
223227 pos = 0
224228 while pos < size :
225229 commit = Commit (
226230 hash = lines [pos ],
227- author_name = lines [pos + 1 ],
228- author_email = lines [pos + 2 ],
229- author_date = lines [pos + 3 ],
230- committer_name = lines [pos + 4 ],
231- committer_email = lines [pos + 5 ],
232- committer_date = lines [pos + 6 ],
233- refs = lines [pos + 7 ],
234- subject = lines [pos + 8 ],
235- body = [lines [pos + 9 ]]
231+ author_name = lines [pos + 1 ],
232+ author_email = lines [pos + 2 ],
233+ author_date = lines [pos + 3 ],
234+ committer_name = lines [pos + 4 ],
235+ committer_email = lines [pos + 5 ],
236+ committer_date = lines [pos + 6 ],
237+ refs = lines [pos + 7 ],
238+ subject = lines [pos + 8 ],
239+ body = [lines [pos + 9 ]],
236240 )
237241
238242 # append body lines
239243 nbl_index = 10
240- while lines [pos + nbl_index ] != self .MARKER :
241- commit .body .append (lines [pos + nbl_index ])
244+ while lines [pos + nbl_index ] != self .MARKER :
245+ commit .body .append (lines [pos + nbl_index ])
242246 nbl_index += 1
243247 pos += nbl_index + 1
244248
@@ -248,7 +252,7 @@ def parse_commits(self):
248252
249253 elif self .remote_url :
250254 # set the commit url based on remote_url (could be wrong)
251- commit .url = self .remote_url + ' /commit/' + commit .hash
255+ commit .url = self .remote_url + " /commit/" + commit .hash
252256
253257 # expand commit object with style parsing
254258 if self .style :
@@ -259,7 +263,7 @@ def parse_commits(self):
259263 return commits
260264
261265 def apply_versions_to_commits (self ):
262- versions_dates = {'' : None }
266+ versions_dates = {"" : None }
263267 version = None
264268 for commit in self .commits :
265269 if commit .version :
@@ -276,27 +280,25 @@ def group_commits_by_version(self, dates):
276280 next_version = None
277281 for commit in self .commits :
278282 if commit .version not in versions_dict :
279- version = versions_dict [commit .version ] = Version (
280- tag = commit .version , date = dates [commit .version ])
283+ version = versions_dict [commit .version ] = Version (tag = commit .version , date = dates [commit .version ])
281284 version .url = self .provider .get_tag_url (tag = commit .version )
282285 if next_version :
283286 version .next_version = next_version
284287 next_version .previous_version = version
285288 next_version .compare_url = self .provider .get_compare_url (
286- base = version .tag , target = next_version .tag or 'HEAD' )
289+ base = version .tag , target = next_version .tag or "HEAD"
290+ )
287291 next_version = version
288292 versions_list .append (version )
289293 versions_types_dict [commit .version ] = {}
290294 versions_dict [commit .version ].commits .append (commit )
291- if 'type' in commit .style \
292- and commit .style ['type' ] not in versions_types_dict [commit .version ]:
293- section = versions_types_dict [commit .version ][commit .style ['type' ]] = Section (
294- type = commit .style ['type' ])
295+ if "type" in commit .style and commit .style ["type" ] not in versions_types_dict [commit .version ]:
296+ section = versions_types_dict [commit .version ][commit .style ["type" ]] = Section (type = commit .style ["type" ])
295297 versions_dict [commit .version ].sections_list .append (section )
296298 versions_dict [commit .version ].sections_dict = versions_types_dict [commit .version ]
297- versions_types_dict [commit .version ][commit .style [' type' ]].commits .append (commit )
299+ versions_types_dict [commit .version ][commit .style [" type" ]].commits .append (commit )
298300 if next_version is not None :
299301 next_version .compare_url = self .provider .get_compare_url (
300- base = versions_list [- 1 ].commits [- 1 ].hash , target = next_version .tag or ' HEAD' )
301- return { 'as_list' : versions_list , 'as_dict' : versions_dict }
302-
302+ base = versions_list [- 1 ].commits [- 1 ].hash , target = next_version .tag or " HEAD"
303+ )
304+ return { "as_list" : versions_list , "as_dict" : versions_dict }
0 commit comments