Skip to content

Instantly share code, notes, and snippets.

@cjohansen
Last active Dec 10, 2015
Embed
What would you like to do?
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