2019-08-22 08:13:34 -04:00
|
|
|
#!/usr/bin/env ruby --disable-gems
|
|
|
|
# Add the following line to your `.git/hooks/pre-commit`:
|
|
|
|
#
|
|
|
|
# $ ruby --disable-gems misc/expand_tabs.rb
|
|
|
|
#
|
|
|
|
|
|
|
|
require 'shellwords'
|
|
|
|
require 'tmpdir'
|
|
|
|
ENV['LC_ALL'] = 'C'
|
|
|
|
|
|
|
|
class Git
|
|
|
|
def initialize(oldrev, newrev)
|
|
|
|
@oldrev = oldrev
|
|
|
|
@newrev = newrev
|
|
|
|
end
|
|
|
|
|
|
|
|
# ["foo/bar.c", "baz.h", ...]
|
|
|
|
def updated_paths
|
|
|
|
with_clean_env do
|
|
|
|
IO.popen(['git', 'diff', '--cached', '--name-only', @newrev], &:readlines).each(&:chomp!)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# [0, 1, 4, ...]
|
|
|
|
def updated_lines(file)
|
|
|
|
lines = []
|
2019-08-25 15:53:15 -04:00
|
|
|
revs_pattern = ("0"*40) + " "
|
2019-08-22 08:13:34 -04:00
|
|
|
with_clean_env { IO.popen(['git', 'blame', '-l', '--', file], &:readlines) }.each_with_index do |line, index|
|
2019-08-25 15:53:15 -04:00
|
|
|
if line.b.start_with?(revs_pattern)
|
2019-08-22 08:13:34 -04:00
|
|
|
lines << index
|
|
|
|
end
|
|
|
|
end
|
|
|
|
lines
|
|
|
|
end
|
|
|
|
|
|
|
|
def add(file)
|
|
|
|
git('add', file)
|
|
|
|
end
|
|
|
|
|
|
|
|
def toplevel
|
|
|
|
IO.popen(['git', 'rev-parse', '--show-toplevel'], &:read).chomp
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def git(*args)
|
|
|
|
cmd = ['git', *args].shelljoin
|
|
|
|
unless with_clean_env { system(cmd) }
|
|
|
|
abort "Failed to run: #{cmd}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def with_clean_env
|
|
|
|
git_dir = ENV.delete('GIT_DIR') # this overcomes '-C' or pwd
|
|
|
|
yield
|
|
|
|
ensure
|
|
|
|
ENV['GIT_DIR'] = git_dir if git_dir
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
DEFAULT_GEM_LIBS = %w[
|
2021-05-25 07:19:11 -04:00
|
|
|
abbrev
|
|
|
|
base64
|
|
|
|
benchmark
|
2019-08-22 08:13:34 -04:00
|
|
|
bundler
|
|
|
|
cmath
|
|
|
|
csv
|
2021-05-25 07:19:11 -04:00
|
|
|
debug
|
|
|
|
delegate
|
|
|
|
did_you_mean
|
|
|
|
drb
|
|
|
|
english
|
|
|
|
erb
|
2019-08-22 08:13:34 -04:00
|
|
|
fileutils
|
2021-05-25 07:19:11 -04:00
|
|
|
find
|
2019-08-22 08:13:34 -04:00
|
|
|
forwardable
|
2021-05-25 07:19:11 -04:00
|
|
|
getoptlong
|
2019-08-22 08:13:34 -04:00
|
|
|
ipaddr
|
|
|
|
irb
|
|
|
|
logger
|
|
|
|
mutex_m
|
2021-05-25 07:19:11 -04:00
|
|
|
net-http
|
|
|
|
net-protocol
|
|
|
|
observer
|
|
|
|
open3
|
|
|
|
open-uri
|
|
|
|
optparse
|
2019-08-22 08:13:34 -04:00
|
|
|
ostruct
|
2021-05-25 07:19:11 -04:00
|
|
|
pp
|
|
|
|
prettyprint
|
2019-08-22 08:13:34 -04:00
|
|
|
prime
|
2021-05-25 07:19:11 -04:00
|
|
|
pstore
|
2019-08-22 08:13:34 -04:00
|
|
|
rdoc
|
2021-05-25 07:19:11 -04:00
|
|
|
readline
|
|
|
|
reline
|
|
|
|
resolv
|
|
|
|
resolv-replace
|
2019-08-22 08:13:34 -04:00
|
|
|
rexml
|
2021-05-25 07:19:11 -04:00
|
|
|
rinda
|
2019-08-22 08:13:34 -04:00
|
|
|
rss
|
2021-05-25 07:19:11 -04:00
|
|
|
rubygems
|
2019-08-22 08:13:34 -04:00
|
|
|
scanf
|
2021-05-25 07:19:11 -04:00
|
|
|
securerandom
|
|
|
|
set
|
|
|
|
shellwords
|
|
|
|
singleton
|
|
|
|
tempfile
|
2019-08-22 08:13:34 -04:00
|
|
|
thwait
|
2021-05-25 07:19:11 -04:00
|
|
|
time
|
|
|
|
timeout
|
|
|
|
tmpdir
|
|
|
|
un
|
|
|
|
tsort
|
|
|
|
uri
|
|
|
|
weakref
|
|
|
|
yaml
|
2019-08-22 08:13:34 -04:00
|
|
|
]
|
|
|
|
|
|
|
|
DEFAULT_GEM_EXTS = %w[
|
|
|
|
bigdecimal
|
2021-05-25 07:19:11 -04:00
|
|
|
cgi
|
2019-08-22 08:13:34 -04:00
|
|
|
date
|
2021-05-25 07:19:11 -04:00
|
|
|
digest
|
2019-08-22 08:13:34 -04:00
|
|
|
etc
|
|
|
|
fcntl
|
|
|
|
fiddle
|
2021-05-25 07:19:11 -04:00
|
|
|
io-console
|
|
|
|
io-nonblock
|
|
|
|
io-wait
|
2019-08-22 08:13:34 -04:00
|
|
|
json
|
2021-05-25 07:19:11 -04:00
|
|
|
nkf
|
2019-08-22 08:13:34 -04:00
|
|
|
openssl
|
2021-05-25 07:19:11 -04:00
|
|
|
pathname
|
2019-08-22 08:13:34 -04:00
|
|
|
psych
|
2021-05-25 07:19:11 -04:00
|
|
|
racc
|
|
|
|
readline-ext
|
2019-08-22 08:13:34 -04:00
|
|
|
stringio
|
|
|
|
strscan
|
2021-05-25 07:19:11 -04:00
|
|
|
syslog
|
|
|
|
win32ole
|
2019-08-22 08:13:34 -04:00
|
|
|
zlib
|
|
|
|
]
|
|
|
|
|
|
|
|
EXPANDTAB_IGNORED_FILES = [
|
|
|
|
# default gems whose master is GitHub
|
|
|
|
%r{\Abin/(?!erb)\w+\z},
|
|
|
|
*DEFAULT_GEM_LIBS.flat_map { |lib|
|
|
|
|
[
|
|
|
|
%r{\Alib/#{lib}/},
|
|
|
|
%r{\Alib/#{lib}\.gemspec\z},
|
|
|
|
%r{\Alib/#{lib}\.rb\z},
|
|
|
|
%r{\Atest/#{lib}/},
|
|
|
|
]
|
|
|
|
},
|
|
|
|
*DEFAULT_GEM_EXTS.flat_map { |ext|
|
|
|
|
[
|
|
|
|
%r{\Aext/#{ext}/},
|
|
|
|
%r{\Atest/#{ext}/},
|
|
|
|
]
|
|
|
|
},
|
|
|
|
|
|
|
|
# vendoring (ccan)
|
|
|
|
%r{\Accan/},
|
|
|
|
|
|
|
|
# vendoring (onigmo)
|
|
|
|
%r{\Aenc/},
|
|
|
|
%r{\Ainclude/ruby/onigmo\.h\z},
|
|
|
|
%r{\Areg.+\.(c|h)\z},
|
|
|
|
|
|
|
|
# explicit or implicit `c-file-style: "linux"`
|
|
|
|
%r{\Aaddr2line\.c\z},
|
|
|
|
%r{\Amissing/},
|
|
|
|
%r{\Astrftime\.c\z},
|
|
|
|
%r{\Avsnprintf\.c\z},
|
|
|
|
]
|
|
|
|
|
|
|
|
git = Git.new('HEAD^', 'HEAD')
|
|
|
|
|
|
|
|
Dir.chdir(git.toplevel) do
|
|
|
|
paths = git.updated_paths
|
2019-08-23 00:57:19 -04:00
|
|
|
paths.select! {|f|
|
|
|
|
(f.end_with?('.c') || f.end_with?('.h') || f == 'insns.def') && EXPANDTAB_IGNORED_FILES.all? { |re| !f.match(re) }
|
2019-08-22 08:13:34 -04:00
|
|
|
}
|
|
|
|
files = paths.select {|n| File.file?(n)}
|
|
|
|
exit if files.empty?
|
|
|
|
|
|
|
|
files.each do |f|
|
|
|
|
src = File.binread(f) rescue next
|
|
|
|
|
|
|
|
expanded = false
|
|
|
|
updated_lines = git.updated_lines(f)
|
2019-08-23 00:57:19 -04:00
|
|
|
unless updated_lines.empty?
|
2019-08-22 08:13:34 -04:00
|
|
|
src.gsub!(/^.*$/).with_index do |line, lineno|
|
|
|
|
if updated_lines.include?(lineno) && line.start_with?("\t") # last-committed line with hard tabs
|
|
|
|
expanded = true
|
|
|
|
line.sub(/\A\t+/) { |tabs| ' ' * (8 * tabs.length) }
|
|
|
|
else
|
|
|
|
line
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if expanded
|
|
|
|
File.binwrite(f, src)
|
|
|
|
git.add(f)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|