diff options
Diffstat (limited to 'tool/auto-style.rb')
-rwxr-xr-x[-rw-r--r--] | tool/auto-style.rb | 31 |
1 files changed, 25 insertions, 6 deletions
diff --git a/tool/auto-style.rb b/tool/auto-style.rb index d2b007bd51..71139c8eb8 100644..100755 --- a/tool/auto-style.rb +++ b/tool/auto-style.rb @@ -15,8 +15,10 @@ class Git @branch = branch # GitHub may not fetch github.event.pull_request.base.sha at checkout - git('fetch', '--depth=1', 'origin', @oldrev) - git('fetch', '--depth=100', 'origin', @newrev) + git('log', '--format=%H', '-1', @oldrev, out: IO::NULL, err: [:child, :out]) or + git('fetch', '--depth=1', 'origin', @oldrev) + git('log', '--format=%H', '-1', "#@newrev~99", out: IO::NULL, err: [:child, :out]) or + git('fetch', '--depth=100', 'origin', @newrev) with_clean_env do @revs = {} @@ -66,12 +68,14 @@ class Git private - def git(*args) + def git(*args, **opts) cmd = ['git', *args].shelljoin puts "+ #{cmd}" - unless with_clean_env { system(cmd) } + ret = with_clean_env { system('git', *args, **opts) } + unless ret or opts[:err] abort "Failed to run: #{cmd}" end + ret end def with_clean_env @@ -173,6 +177,10 @@ IGNORED_FILES = [ %r{\Asample/trick[^/]*/}, ] +DIFFERENT_STYLE_FILES = %w[ + addr2line.c io_buffer.c prism*.c scheduler.c +] + oldrev, newrev, pushref = ARGV unless dry_run = pushref.empty? branch = IO.popen(['git', 'rev-parse', '--symbolic', '--abbrev-ref', pushref], &:read).strip @@ -194,7 +202,7 @@ if files.empty? exit end -trailing = eofnewline = expandtab = false +trailing = eofnewline = expandtab = indent = false edited_files = files.select do |f| src = File.binread(f) rescue next @@ -202,6 +210,8 @@ edited_files = files.select do |f| trailing0 = false expandtab0 = false + indent0 = false + src.gsub!(/^.*$/).with_index do |line, lineno| trailing = trailing0 = true if line.sub!(/[ \t]+$/, '') line @@ -225,7 +235,15 @@ edited_files = files.select do |f| end end - if trailing0 or eofnewline0 or expandtab0 + if File.fnmatch?("*.[ch]", f, File::FNM_PATHNAME) && + !DIFFERENT_STYLE_FILES.any? {|pat| File.fnmatch?(pat, f, File::FNM_PATHNAME)} + indent0 = true if src.gsub!(/^\w+\([^\n]*?\)\K[ \t]*(?=\{( *\\)?$)/, '\1' "\n") + indent0 = true if src.gsub!(/^([ \t]*)\}\K[ \t]*(?=else\b.*?( *\\)?$)/, '\2' "\n" '\1') + indent0 = true if src.gsub!(/^[ \t]*\}\n\K\n+(?=[ \t]*else\b)/, '') + indent ||= indent0 + end + + if trailing0 or eofnewline0 or expandtab0 or indent0 File.binwrite(f, src) true end @@ -236,6 +254,7 @@ else msg = [('remove trailing spaces' if trailing), ('append newline at EOF' if eofnewline), ('expand tabs' if expandtab), + ('adjust indents' if indent), ].compact message = "* #{msg.join(', ')}. [ci skip]" if expandtab |