@@ -11,9 +11,11 @@ class Git
1111
1212 def initialize ( oldrev , newrev , branch = nil )
1313 @oldrev = oldrev
14- @newrev = newrev . empty? ? 'HEAD' : newrev
14+ @newrev = ! newrev || newrev . empty? ? 'HEAD' : newrev
1515 @branch = branch
1616
17+ return unless oldrev
18+
1719 # GitHub may not fetch github.event.pull_request.base.sha at checkout
1820 git ( 'log' , '--format=%H' , '-1' , @oldrev , out : IO ::NULL , err : [ :child , :out ] ) or
1921 git ( 'fetch' , '--depth=1' , 'origin' , @oldrev )
@@ -59,7 +61,7 @@ def commit(log, *files)
5961 end
6062
6163 def push
62- git ( 'push' , 'origin' , @branch )
64+ git ( 'push' , 'origin' , @branch ) if @branch
6365 end
6466
6567 def diff
@@ -181,85 +183,97 @@ def with_clean_env
181183 addr2line.c io_buffer.c prism*.c scheduler.c
182184]
183185
184- oldrev , newrev , pushref = ARGV
185- unless dry_run = pushref . empty?
186- branch = IO . popen ( [ 'git' , 'rev-parse' , '--symbolic' , '--abbrev-ref' , pushref ] , &:read ) . strip
187- end
188- git = Git . new ( oldrev , newrev , branch )
186+ def adjust_styles ( files )
187+ trailing = eofnewline = expandtab = indent = false
189188
190- updated_files = git . updated_paths
191- files = updated_files . select { |l |
192- /^\d / !~ l and /\. bat\z / !~ l and
193- ( /\A (?:config|[Mm]akefile|GNUmakefile|README)/ =~ File . basename ( l ) or
194- /\A \z |\. (?:[chsy]|\d +|e?rb|tmpl|bas[eh]|z?sh|in|ma?k|def|src|trans|rdoc|ja|en|el|sed|awk|p[ly]|scm|mspec|html|rs)\z / =~ File . extname ( l ) )
195- }
196- files . select! { |n | File . file? ( n ) }
197- files . reject! do |f |
198- IGNORED_FILES . any? { |re | f . match ( re ) }
199- end
200- if files . empty?
201- puts "No files are an auto-style target:\n #{ updated_files . join ( "\n " ) } "
202- exit
203- end
189+ edited_files = files . select do |f |
190+ src = File . binread ( f ) rescue next
191+ eofnewline = eofnewline0 = true if src . sub! ( /(?<!\A |\n )\z / , "\n " )
204192
205- trailing = eofnewline = expandtab = indent = false
193+ trailing0 = false
194+ expandtab0 = false
195+ indent0 = false
206196
207- edited_files = files . select do |f |
208- src = File . binread ( f ) rescue next
209- eofnewline = eofnewline0 = true if src . sub! ( /(?<!\A |\n )\z / , "\n " )
197+ src . gsub! ( /^.*$/ ) . with_index do |line , lineno |
198+ trailing = trailing0 = true if line . sub! ( /[ \t ]+$/ , '' )
199+ line
200+ end
210201
211- trailing0 = false
212- expandtab0 = false
213- indent0 = false
202+ if f . end_with? ( '.c' ) || f . end_with? ( '.h' ) || f == 'insns.def'
203+ # If and only if unedited lines did not have tab indentation, prevent introducing tab indentation to the file.
204+ expandtab_allowed = src . each_line . with_index . all? do |line , lineno |
205+ !line . start_with? ( "\t " )
206+ end
214207
215- src . gsub! ( /^.*$/ ) . with_index do |line , lineno |
216- trailing = trailing0 = true if line . sub! ( /[ \t ]+$/ , '' )
217- line
218- end
208+ if expandtab_allowed
209+ src . gsub! ( /^.*$/ ) . with_index do |line , lineno |
210+ if line . start_with? ( "\t " ) # last-committed line with hard tabs
211+ expandtab = expandtab0 = true
212+ line . sub ( /\A \t +/ ) { |tabs | ' ' * ( 8 * tabs . length ) }
213+ else
214+ line
215+ end
216+ end
217+ end
218+ end
219219
220- if f . end_with? ( '.c' ) || f . end_with? ( '.h' ) || f == 'insns.def'
221- # If and only if unedited lines did not have tab indentation, prevent introducing tab indentation to the file.
222- expandtab_allowed = src . each_line . with_index . all? do |line , lineno |
223- !line . start_with? ( "\t " )
220+ if File . fnmatch? ( "*.[ch]" , f , File ::FNM_PATHNAME ) &&
221+ !DIFFERENT_STYLE_FILES . any? { |pat | File . fnmatch? ( pat , f , File ::FNM_PATHNAME ) }
222+ indent0 = true if src . gsub! ( /^\w +\( [^\n ]*?\) \K [ \t ]*(?=\{ ( *\\ )?$)/ , '\1' "\n " )
223+ indent0 = true if src . gsub! ( /^([ \t ]*)\} \K [ \t ]*(?=else\b .*?( *\\ )?$)/ , '\2' "\n " '\1' )
224+ indent0 = true if src . gsub! ( /^[ \t ]*\} \n \K \n +(?=[ \t ]*else\b )/ , '' )
225+ indent ||= indent0
224226 end
225227
226- if expandtab_allowed
227- src . gsub! ( /^.*$/ ) . with_index do |line , lineno |
228- if line . start_with? ( "\t " ) # last-committed line with hard tabs
229- expandtab = expandtab0 = true
230- line . sub ( /\A \t +/ ) { |tabs | ' ' * ( 8 * tabs . length ) }
231- else
232- line
233- end
234- end
228+ if trailing0 or eofnewline0 or expandtab0 or indent0
229+ File . binwrite ( f , src )
230+ true
235231 end
236232 end
237-
238- if File . fnmatch? ( "*.[ch]" , f , File ::FNM_PATHNAME ) &&
239- !DIFFERENT_STYLE_FILES . any? { |pat | File . fnmatch? ( pat , f , File ::FNM_PATHNAME ) }
240- indent0 = true if src . gsub! ( /^\w +\( [^\n ]*?\) \K [ \t ]*(?=\{ ( *\\ )?$)/ , '\1' "\n " )
241- indent0 = true if src . gsub! ( /^([ \t ]*)\} \K [ \t ]*(?=else\b .*?( *\\ )?$)/ , '\2' "\n " '\1' )
242- indent0 = true if src . gsub! ( /^[ \t ]*\} \n \K \n +(?=[ \t ]*else\b )/ , '' )
243- indent ||= indent0
233+ if edited_files . empty?
234+ return
235+ else
236+ msg = [ ( 'remove trailing spaces' if trailing ) ,
237+ ( 'append newline at EOF' if eofnewline ) ,
238+ ( 'expand tabs' if expandtab ) ,
239+ ( 'adjust indents' if indent ) ,
240+ ] . compact
241+ message = "* #{ msg . join ( ', ' ) } . [ci skip]"
242+ if expandtab
243+ message += "\n Please consider using misc/expand_tabs.rb as a pre-commit hook."
244+ end
245+ return message , edited_files
244246 end
247+ end
245248
246- if trailing0 or eofnewline0 or expandtab0 or indent0
247- File . binwrite ( f , src )
248- true
249+ oldrev , newrev , pushref = ARGV
250+ if ( dry_run = oldrev == '-n' ) or oldrev == '--'
251+ _ , *updated_files = ARGV
252+ git = Git . new ( nil , nil )
253+ else
254+ unless dry_run = pushref . empty?
255+ branch = IO . popen ( [ 'git' , 'rev-parse' , '--symbolic' , '--abbrev-ref' , pushref ] , &:read ) . strip
249256 end
257+ git = Git . new ( oldrev , newrev , branch )
258+
259+ updated_files = git . updated_paths
260+ end
261+
262+ files = updated_files . select { |l |
263+ /^\d / !~ l and /\. bat\z / !~ l and
264+ ( /\A (?:config|[Mm]akefile|GNUmakefile|README)/ =~ File . basename ( l ) or
265+ /\A \z |\. (?:[chsy]|\d +|e?rb|tmpl|bas[eh]|z?sh|in|ma?k|def|src|trans|rdoc|ja|en|el|sed|awk|p[ly]|scm|mspec|html|rs)\z / =~ File . extname ( l ) )
266+ }
267+ files . select! { |n | File . file? ( n ) }
268+ files . reject! do |f |
269+ IGNORED_FILES . any? { |re | f . match ( re ) }
250270end
251- if edited_files . empty?
271+
272+ if files . empty?
273+ puts "No files are an auto-style target:\n #{ updated_files . join ( "\n " ) } "
274+ elsif !( message , edited_files = adjust_styles ( files ) )
252275 puts "All edited lines are formatted well:\n #{ files . join ( "\n " ) } "
253276else
254- msg = [ ( 'remove trailing spaces' if trailing ) ,
255- ( 'append newline at EOF' if eofnewline ) ,
256- ( 'expand tabs' if expandtab ) ,
257- ( 'adjust indents' if indent ) ,
258- ] . compact
259- message = "* #{ msg . join ( ', ' ) } . [ci skip]"
260- if expandtab
261- message += "\n Please consider using misc/expand_tabs.rb as a pre-commit hook."
262- end
263277 if dry_run
264278 git . diff
265279 abort message
0 commit comments