Skip to content

Instantly share code, notes, and snippets.

@zeisler
Last active April 5, 2021 22:18
Show Gist options
  • Save zeisler/80bc427cb3955c650b6f96c5571439bc to your computer and use it in GitHub Desktop.
Save zeisler/80bc427cb3955c650b6f96c5571439bc to your computer and use it in GitHub Desktop.
git-recent
#!/usr/bin/env ruby
require 'bundler/inline'
gemfile do
source 'https://rubygems.org'
gem 'tty-prompt', '~> 0.18.1'
gem 'colorize', '~> 0.8.1'
gem 'time_ago_in_words', '~> 0.1'
gem 'git', '~> 1.5.0'
gem 'pry'
end
require 'csv'
prompt_for_branch = TTY::Prompt.new(interrupt: :exit)
current_repo_dir = `git rev-parse --show-toplevel`.chomp
g = Git.open(current_repo_dir)
current_branch = nil
Dir.chdir(current_repo_dir) do
current_branch = `git rev-parse --abbrev-ref HEAD`.chomp
end
git_script = %q[git for-each-ref --no-merged=master --count=50 --sort=-committerdate refs/heads/ --format="%(refname:short),%(committerdate),%(authorname)"]
branch_result = prompt_for_branch.select("Select Recent Branch", filter: true) do |menu|
CSV.parse(`#{git_script}`) do |row|
next if row[0] == current_branch
array_rows = row.to_a
array_rows[1] = Time.parse(array_rows[1]).ago_in_words.colorize(:yellow)
array_rows[2] = array_rows[2].colorize(:green)
menu.choice array_rows[0].ljust(50) + " " + array_rows[1]
end
end
selected_branch = branch_result.split(" ").first
begin
g.branch(selected_branch).checkout
rescue Git::GitExecuteError => e
puts e.message.split("2>&1:").last
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment