#!/usr/bin/ruby -w module GitTopic; end module GitTopic::Completion class << self def user ENV['USER'] end def branches `git branch -a --no-color`.split( "\n" ).map do |l| l.gsub( /->.*$/, '' )[ 2..-1 ] end end def others_review_branches branches.map do |b| b =~ %r{^remotes/origin/review/(.*)} && $1 end.reject do |b| b =~ %r{^#{user}/} end.compact end def my_reject_review_and_all_wip_branches branches.map do |b| b =~ %r{^(wip)/#{user}/(.*)} || b =~ %r{^remotes/origin/(rejected)/#{user}/(.*)} || b =~ %r{^remotes/origin/(review)/#{user}/(.*)} b =~ %r{^remotes/origin/(wip)/(\S*?)/(.*)} suggestion = [$1,($2 unless $2 == user), $3].compact.join("/") suggestion unless suggestion.strip.empty? end.compact end def all_reject_and_review_branches branches.map do |b| b =~ %r{^remotes/origin/(rejected|review)/(\S*)/(.*)} suggestion = [$1,($2 unless $2 == user), $3].compact.join("/") suggestion unless suggestion.strip.empty? end.compact end def complete suggestions = case ARGV.shift # TODO 2: if we let accept/reject take args, simply return # others_review_branches here when "accept" when "reject" when "comment" when "done" # nothing when "comments" all_reject_and_review_branches when "work-on" my_reject_review_and_all_wip_branches when "review" others_review_branches end || [] suggestions.each{ |s| puts s } end end end GitTopic::Completion.complete