Last active
December 10, 2015 22:09
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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