added ruby scripts to show progress of a piped command and contributors to a git project

This commit is contained in:
Sami Samhuri 2010-05-30 11:33:46 -07:00
parent 216dea5ca9
commit 7bbe74e4ac
2 changed files with 88 additions and 0 deletions

41
git-contributors Executable file
View file

@ -0,0 +1,41 @@
#!/usr/bin/env ruby
def git_dir?
File.exists?('.git') || `git rev-parse --git-dir >/dev/null`
end
def authors
`git log | grep '^Author:' | sed -e 's/Author: //g' | sort -f | uniq`.split(/\n/)
end
def authors_by_field n
raise "invalid field #{n}, expected 0 or 1" unless n == 0 || n == 1
other = (n-1).abs
authors.inject({}) do |as, a|
parts = a.split(' <')
parts[1].sub!(/>$/, '')
(as[parts[n]] ||= []) << parts[other]
as
end
end
def authors_by_name; authors_by_field(0) end
def authors_by_email; authors_by_field(1) end
def main
exit 1 unless git_dir?
sort = :name
sort = ARGV.first.to_sym if ARGV.length > 0
if sort == :name
authors_by_name.each do |name, emails|
puts "#{name} <#{emails.join(', ')}>"
end
else
authors_by_email.each do |email, names|
puts "#{email}: #{names.first} (aka #{names[1..-1].join(' aka ')})"
end
end
end
main if $0 == __FILE__

47
progress Executable file
View file

@ -0,0 +1,47 @@
#!/usr/bin/env ruby
STDERR.sync = true
$bytes = true
$max = nil
$count = 0
until ARGV.empty?
case (arg = ARGV.shift)
when '-l'
$bytes = false
when '-b'
$bytes = true
when /\A(\d+)([kmg]?)\Z/
units = {'k'=>2**10, 'm'=>2**20, 'g'=>2**10, nil=>1}
$max = $1.to_i * units[$2]
else
raise "Unrecognized argument: `#{arg}'"
end
end
$max = STDIN.stat.size if $bytes and STDIN.stat.file? and $max.nil?
Thread.new{
last_count = nil
while true
if $count != last_count
if $max
STDERR.print "\r#{$count}/#{$max} [#{$count*100/$max}%]"
else
STDERR.print "\r#{$count}"
end
last_count = $count
end
sleep 1
end
}
begin
while data = ($bytes ? STDIN.read(2**12) : STDIN.gets)
STDOUT.print(data)
$count += $bytes ? data.length : 1
end
STDERR.print "\n"
rescue Errno::EPIPE
end