lib/git_topic/naming.rb in git-topic-0.2.5 vs lib/git_topic/naming.rb in git-topic-0.2.6

- old
+ new

@@ -19,28 +19,39 @@ def rejected_branch( topic ) "rejected/#{user}/#{strip_namespace topic}" end - def review_branch( topic, user=user ) - "review/#{user}/#{strip_namespace topic}" + def review_branch( topic, user=user, opts={} ) + rb = "review/#{user}/#{strip_namespace topic}" + if opts[:remote] + "origin/#{rb}" + else + rb + end end def remote_rejected_branch( topic, user=user ) "rejected/#{user}/#{strip_namespace topic}" end - def remote_branch( spec=current_branch ) + def remote_branch( spec=current_branch, opts={} ) parts = topic_parts( spec ) - remote_branches.find do |remote_branch| + rb = remote_branches.find do |remote_branch| bp = topic_parts( remote_branch ) parts.all? do |part, value| bp[part] == value end end + + if opts[:strip_remote] + rb.gsub %r{^origin/}, '' + else + rb + end end def find_remote_review_branch( topic ) others_review_branches.find{|b| b.index topic} @@ -80,11 +91,11 @@ first_part = (parts.first =~ /(wip|review|rejected)/) ? :namespace : :user p[ first_part ], p[:topic] = parts when 1 p[:topic] = parts.first else - raise "Unexpected topic: #{ref}" + return nil end if opts[:lookup] && p[:user].nil? remote_branches_organized.find do |namespace, v| v.find do |user, vv| @@ -121,18 +132,36 @@ def remote_branches @@remote_branches ||= capture_git( "branch -r --no-color" ).split( "\n" ).map{|b| b[2..-1]} end - def others_review_branches + def review_branches remote_branches.select do |b| b =~ %r{/review/} - end.reject do |b| + end + end + + def others_review_branches + review_branches.reject do |b| b =~ %r{/#{user}/} end end + def user_review_branches user=user + review_branches.select do |b| + b =~ %r{/#{user}/} + end + end + + # Returns review branches as a hash of namespace → user → topic* hashes, + # e.g. + # + # { + # :review => { 'davidjh' => [ 'topic1', 'topic2' ]}, + # :rejected => { 'davidjh' => [ 'topic3' ]} + # } + # def remote_branches_organized @@remote_branches_organized ||= ( rb = remote_branches.dup # Convert a bunch of remote branch names, like # origin/HEAD -> origin/masterr @@ -175,19 +204,33 @@ # no ‘,’, i.e. only one ref matches the commit !ref.strip.empty? && ref.index( ',' ).nil? end.strip[ 1..-2 ] # chomp the leading and trailing parenthesis end + def newest_pending_branch + return nil if user_review_branches.empty? + + commits_by_age = capture_git([ + "log --date-order --pretty=format:%d", + "^origin/master #{user_review_branches.join( ' ' )}", + ].join( " " )).split( "\n" ) + + commits_by_age.find do |ref| + # no ‘,’, i.e. only one ref matches the commit + !ref.strip.empty? && ref.index( ',' ).nil? + end.strip[ 1..-2 ] # chomp the leading and trailing parenthesis + end + def oldest_review_user_topic user_topic_name( oldest_review_branch ) end def on_review_branch? current_branch =~ %r{^review/} end end - def self.included( base ) + def self.included base base.extend ClassMethods end end