add binarysearch for commit_count by hx

This commit is contained in:
houxiang 2015-11-23 16:51:12 +08:00
parent fdedcbfa81
commit 56519389d2
1 changed files with 36 additions and 9 deletions

View File

@ -42,7 +42,7 @@ class RepositoriesController < ApplicationController
include RepositoriesHelper
helper :project_score
#@root_path = RepositoriesHelper::ROOT_PATH
$g=Gitlab.client
rescue_from Redmine::Scm::Adapters::CommandFailed, :with => :show_error_command_failed
def new
@ -315,14 +315,20 @@ update
#Modified by young
# (show_error_not_found; return) unless @entries
g = Gitlab.client
count = 0
(0..100).each do |page|
if g.commits(@project.gpid,:page => page).count == 0
break
else
count = count + g.commits(@project.gpid,:page => page).count
end
end
# count = 0
# (0..100).each do |page|
# if g.commits(@project.gpid,:page => page).count == 0
# break
# else
# count = count + g.commits(@project.gpid,:page => page).count
# end
# end
#add by hx
pages , last_page_count = binarySearch(@project.gpid)
count = (pages-1)*20 + last_page_count
@changesets = g.commits(@project.gpid)
# @changesets = @repository.latest_changesets(@path, @rev)
# @changesets_count = @repository.latest_changesets(@path, @rev).count
@ -349,6 +355,27 @@ update
alias_method :browse, :show
#add by hx
def binarySearch(project_id ,left=0 , right=30 )
#如果right页面有数据,直接递归将right改left,right=right+100
if $g.commits(project_id,:page=>right.to_s).count>0
binarySearch(project_id,right,right+100)
end
#取自己默认页面数的中间页面数
median = (left + right)/2
#判断是否为最后一个页面
if $g.commits(project_id,:page=>median.to_s).count >0 && $g.commits(project_id,:page=>(median+1).to_s).count==0
#返回前面的页面数,和最后一个页面的commits_count
return median , $g.commits(project_id,:page=>median.to_s)
#左边查找
elsif $g.commits(project_id,:page=>median.to_s).count == 0
binarySearch(project_id,left,median-1)
#右边查找
elsif $g.commits(project_id,:page=>(median+1).to_s).count > 0
binarySearch(project_id,median+1,right)
end
end
def changes
@entry = @repository.entry(@path, @rev)