Last active
January 16, 2020 11:26
-
-
Save Glutexo/f4e52e2275643937f271ea98ab2c2e9c to your computer and use it in GitHub Desktop.
Get revision history of a git branch
This file contains hidden or 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 | |
# Usage: git-rev-history.rb [branch] | |
# Output: | |
# branch@{0} 4be30be6eb844a40ca4af301cb8b2359a5afcd30 | |
# … up to @{9} | |
#!/usr/bin/env ruby | |
def command(*args) | |
stdout = nil | |
IO.popen(*args) do |io| | |
stdout = io.gets | |
end | |
raise RuntimeError unless $?.success? | |
stdout | |
end | |
def git(*args) | |
command(['git', *args]) | |
end | |
def git_rev_parse(rev, params = {}) | |
args = ['rev-parse'] | |
args << '--abbrev-ref' if params[:abbrev_ref] | |
args << rev | |
git(*args).chomp | |
end | |
def current_branch | |
begin | |
git_rev_parse('HEAD', abbrev_ref: true) | |
rescue RuntimeError | |
end | |
end | |
def history_revision(branch, i) | |
"#{branch}@{#{i}}" | |
end | |
def argv | |
$<.argv | |
end | |
branch = (argv[0] or current_branch) | |
exit(1) unless branch | |
(0...10).each do |i| | |
hist_rev = history_revision(branch, i) | |
out = [hist_rev, git_rev_parse(hist_rev)] | |
puts(out.join(' ')) | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment