Skip to content

Instantly share code, notes, and snippets.

@luke-gru
Created June 18, 2011 19:43
Show Gist options
  • Save luke-gru/1033444 to your computer and use it in GitHub Desktop.
Save luke-gru/1033444 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
#puts "what dir are you in?"
#$gitdir = $stdin.gets.chomp #should work off of cd and then check for .git dir
if File.exists?('.git')
$gitdir = `pwd`.chomp
else
begin
# Dir::chdir($gitdir)
rd, wr = IO::pipe
pid = Process.fork do
$stdout.reopen(wr)
rd.close
exec("git rev-parse --git-dir")
end
wr.close
rd.each do |line|
$gitdir = "#{line}".chomp.gsub(/\.git/i, '')
end
Process.wait(pid)
end
end
class Gruber_parser
def latest_commit?
begin
# Dir::chdir($gitdir)
rd, wr = IO::pipe
pid = Process.fork do
$stdout.reopen(wr)
rd.close
exec("git rev-parse HEAD")
end
wr.close
rd.each do |line|
@commit = "#{line}"
end
Process.wait(pid)
end
require 'grit'
begin
repo = Grit::Repo.new($gitdir)
rescue Grit::InvalidGitRepositoryError => e
puts "#{e} is not a git repo"
exit 1
end
sha = repo.commits[0]
if @commit.match(/.*#{sha}.*$/)
puts "You're on the latest commit"
else
#make method to check how many commits away from the latest you are
puts ":("
end
end
def get_branch
begin
# Dir::chdir($gitdir)
rd, wr = IO::pipe
pid = Process.fork do
$stdout.reopen(wr)
rd.close
exec("git branch")
end
wr.close
rd.each do |line|
@branch = "#{line}".chomp
end
Process.wait(pid)
end
return @branch
end
def remote_ahead?
get_branch
begin
# Dir::chdir($gitdir)
rd, wr = IO::pipe
pid = Process.fork do
$stdout.reopen(wr)
rd.close
exec("git remote -v update > /dev/null 2>&1")
end
wr.close
rd.each do |line|
@all_lines = "#{line}"
@up_to_date = "#{line}" unless line !~ /up to date/
end
Process.wait(pid)
end
if @up_to_date != ''
puts "Remote tracking branch for #{@branch}: up to date"
else
print @all_lines
end
end
def latest_remote_branch
begin
# Dir::chdir($gitdir)
rd, wr = IO::pipe
pid = Process.fork do
$stdout.reopen(wr)
rd.close
exec("git for-each-ref --format='%(committerdate)%09%(refname)' --sort=-committerdate refs/remotes/")
end
wr.close
rd.each do |line|
@latest_remote_b = "#{line}" unless line =~ /HEAD/
end
Process.wait(pid)
end
puts @latest_remote_b
end
def type_of_object(object)
begin
# Dir::chdir($gitdir)
rd, wr = IO::pipe
pid = Process.fork do
$stdout.reopen(wr)
rd.close
exec("git cat-file -t #{object}")
end
wr.close
rd.each do |line|
@type_of_object = "#{line}".chomp
end
Process.wait(pid)
end
return @type_of_object
end
def inspect_sha
require 'grit'
begin
repo = Grit::Repo.new($gitdir)
rescue Grit::InvalidGitRepositoryError => e
puts "#{e} is not a git repo"
exit 1
end
sha = ARGV.first
type_of_object(sha)
type_of_obj = @type_of_object
grit_obj = repo.commits(sha, 1).first
puts
puts "========================================"
print "inspecting #{type_of_obj}: #{sha}\n"
if type_of_obj == 'commit'
tree = grit_obj.tree
sha_of_tree = tree.id
puts " root tree: #{sha_of_tree}"
puts "========================================"
puts
tree.contents.each do |obj|
print "----------------------------------------\n"
p obj
print " " + "#{obj.name}" + " " + "#{obj.mode}\n"
end
elsif type_of_obj == 'tree'
tree = repo.tree(sha)
puts "========================================"
puts
tree.contents.each do |obj|
print "----------------------------------------\n"
p obj
print " " + "#{obj.name}" + " " + "#{obj.mode}\n"
end
elsif type_of_obj == 'blob'
blob = repo.blob(sha)
puts "========================================"
puts
puts blob.data
end
end
ARGV.each do |options|
if options =~ %r(\A-[-]?l(atest)?c(ommit)?$)
parser = Gruber_parser.new
parser.latest_commit?
elsif options =~ %r(\A-[-]?r(emote)?a(head)?$)
parser = Gruber_parser.new
parser.remote_ahead?
elsif options =~ %r(\A-[-]?lr(emote)?(b)?(r)?(anch)?$)
parser = Gruber_parser.new
parser.latest_remote_branch
elsif options =~ %r(\A[\d\D]{40}$)
parser = Gruber_parser.new
parser.inspect_sha
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment