Skip to content

Instantly share code, notes, and snippets.

@ewalk153
Created October 12, 2017 15:26
Show Gist options
  • Select an option

  • Save ewalk153/9441033666b0efee949b216f26e6cb03 to your computer and use it in GitHub Desktop.

Select an option

Save ewalk153/9441033666b0efee949b216f26e6cb03 to your computer and use it in GitHub Desktop.
Command line script to find a specific commit
#!/usr/bin/env ruby
#find a commit from a file and a line
require 'optparse'
options = {}
OptionParser.new do |opts|
opts.banner = "Usage: #{$0} [options] filename:linenumber"
opts.on("-l") do |v|
options[:local_open] = true
end
end.parse!
def failure
puts "Error: must provide with and line number filename:linenumber"
exit(1)
end
failure if ARGV[0].nil?
filename, line = ARGV[0].split(":")
failure if line.nil?
commit_data = `git blame #{filename} -L #{line},#{line} --incremental`
commit_line, author, author_email, _ = commit_data.split("\n")
commit = commit_line.split(' ').first
remote = `git remote -v | grep origin`.split("\n").first.split(' ')[1].gsub('.git', '')
if options[:local_open]
exec("git show #{commit}")
else
exec("open '#{remote}/commit/#{commit}'")
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment