Skip to content

Instantly share code, notes, and snippets.

@andrewrlee
Last active August 29, 2015 14:17
Show Gist options
  • Select an option

  • Save andrewrlee/12999075a40d201bb122 to your computer and use it in GitHub Desktop.

Select an option

Save andrewrlee/12999075a40d201bb122 to your computer and use it in GitHub Desktop.
Trash script for showing git status in recursive directories
#!/usr/bin/ruby
dirty = ARGV.include? "-d"
change_dir = ARGV.include? "-cd"
search = ARGV.find{|x| ! x.start_with? "-"}
repos = Dir.glob("/home/alee/workspaces/**/.git/").collect()
class String
def brown; "\033[33m#{self.strip! || self}\033[0m" end
def magenta; "\033[35m#{self.strip! || self}\033[0m" end
end
def print_items (common_dir, data, dirty_only)
data.select! {|i| i[:changes] > 0 || !dirty_only}
puts "\n #{data.count} Repos found in #{common_dir}:"
data.each do |i|
isDirty = i[:changes] > 0
indicator = isDirty ? "*" : "_"
puts ("\t(%s) %s: %s\n\t%s" % [ indicator.brown, i[:norm_path], i[:branch].brown, i[:path].magenta])
end
puts "\n"
end
common_dir = repos.inject{|l,s| l=l.chop while l!=s[0...l.length];l}
data = repos.collect do |r|
ENV["GIT_DIR"] = r
path = File.dirname r
repo_name = File.basename path
{:repo_name => repo_name,
:branch => `git rev-parse --abbrev-ref HEAD`,
:changes => `git --git-dir=#{r} --work-tree=#{path} status --porcelain`.split(/\n/).count,
:norm_path => repo_name.sub(common_dir, ''),
:path => File.dirname(r) }
end
data.sort_by! {|i| i[:norm_path].downcase}
data.select! {|i| i[:norm_path].include? search} unless search.nil?
if change_dir && data.length == 1
Dir.chdir("#{data[0][:path]}")
exec 'bash'
else
print_items(common_dir, data, dirty)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment