Skip to content

Instantly share code, notes, and snippets.

@kronn
Last active July 20, 2018 09:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kronn/6e6cac7510f1113c0e0e to your computer and use it in GitHub Desktop.
Save kronn/6e6cac7510f1113c0e0e to your computer and use it in GitHub Desktop.
a little script to review feature-branches
#!/usr/bin/env ruby
# rubocop:disable Style/SpecialGlobalVars
begin
require 'ruby-progressbar'
rescue LoadError
system 'gem', 'install', 'ruby-progressbar'
exec "#{$0} #{ARGV.join(' ')}"
end
def determine_current_branch
branch = `git branch | grep '^*' | sed 's!\*\ !!'`.chomp
if just_pulled? && branch == 'master'
# format "branch" like base..HEAD
`git reflog -1 --parents | cut -d ' ' -f 1,2`.chomp.split.reverse.join('..')
elsif git_flow?
prefix = `git config gitflow.prefix.feature | sed 's!\/!\\\/!'`.chomp
branch.gsub(prefix, '')
else
branch
end
end
def integration_branch
@integration_branch ||=
begin
if git_flow?
`git config gitflow.branch.develop`.chomp
else
'master'
end
end
end
def git_flow?
@git_flow ||= begin
`git config --get gitflow.branch.develop`
$?.success?
end
end
def just_pulled?
`git reflog -1 | grep 'pull'`
$?.success?
end
ENV['PAGER'] ||= "#{`which less`.chomp} -E"
feature = ARGV[0] || determine_current_branch
base = ARGV[1] || integration_branch
if feature =~ /(?<base>[0-9a-f]{5,8})\.{2}(?<branch>[0-9a-f]{5,8})/
range_given = true
full_branch = feature # used for message
commit_range = feature
matches = $~
feature = matches[:branch]
base = matches[:base]
else
range_given = false
full_branch = git_flow? ? "feature/#{feature}" : feature
commit_range = "#{base}..#{full_branch}"
end
puts "preparing to review #{full_branch}"
$stdout.sync = true
if git_flow? && !range_given && feature != determine_current_branch
base_branch = base.gsub(/\.+$/, '')
system('git', 'checkout', base_branch)
system('git', 'pull', '--rebase')
system('git', 'flow', 'feature', 'track', feature)
end
system('git', 'log', '--graph', '--oneline', '--decorate', commit_range)
puts
puts 'start review now?'
answer = STDIN.gets.chomp
commits = `git log --reverse --format=format:%H #{commit_range}`.split
commit_hash = 'commit 0000000000000000000000000000000000000000'
progressbar = ProgressBar.create(
format: commit_hash + ' %a |%w>%i| %c/%C | %E ',
total: commits.size,
starting_at: 0
)
commits.each do |commit|
system 'clear'
progressbar.increment
system 'git', 'show', '--patch-with-stat', commit
answer = STDIN.gets.chomp
end
if git_flow? && !range_given && !just_pulled?
puts 'clean up review?'
answer = STDIN.gets.chomp
system 'git', 'checkout', integration_branch
system "git branch -d feature/#{feature}"
end
# rubocop:enable Style/SpecialGlobalVars
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment