Skip to content

Instantly share code, notes, and snippets.

@cjohansen
Last active December 10, 2015 22:09
Show Gist options
  • Save cjohansen/4500470 to your computer and use it in GitHub Desktop.
Save cjohansen/4500470 to your computer and use it in GitHub Desktop.
hgrep - historic grep. Find the last version where some string occurred in a file in a git repository.
#!/usr/bin/env ruby
if ARGV.length < 2
puts "#{File.basename(__FILE__)} file pattern [grep options]"
exit 1
end
`git reflog #{ARGV[0]}`.split("\n").each do |ref|
oid = ref.split(" ").first
if `git cat-file -p #{oid}:#{ARGV[0]} | grep #{ARGV[1]}` != ""
puts "Found match for '#{ARGV[1]}' in #{oid}:#{ARGV[0]}"
puts `git cat-file -p #{oid}:#{ARGV[0]} | grep #{ARGV[2..-1].join(' ')} #{ARGV[1]}`
exit 0
end
end
puts "No match for '#{ARGV[1]}' in #{ARGV[0]}"
exit 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment