Commit dab072c1ab609917c3472ae663f1db49af75e56e
1 parent
99bd0015
Exists in
master
and in
4 other branches
refactor code: repository.rb
Showing
4 changed files
with
82 additions
and
56 deletions
Show diff stats
app/controllers/application_controller.rb
| ... | ... | @@ -64,7 +64,7 @@ class ApplicationController < ActionController::Base |
| 64 | 64 | else |
| 65 | 65 | @branch = params[:branch].blank? ? nil : params[:branch] |
| 66 | 66 | @tag = params[:tag].blank? ? nil : params[:tag] |
| 67 | - @ref = @branch || @tag || "master" | |
| 67 | + @ref = @branch || @tag || Repository.default_ref | |
| 68 | 68 | end |
| 69 | 69 | end |
| 70 | 70 | ... | ... |
app/controllers/projects_controller.rb
| ... | ... | @@ -108,7 +108,7 @@ class ProjectsController < ApplicationController |
| 108 | 108 | if params[:commit_id] |
| 109 | 109 | @commit = @repo.commits(params[:commit_id]).first |
| 110 | 110 | else |
| 111 | - @commit = @repo.commits(@ref || "master").first | |
| 111 | + @commit = @repo.commits(@ref).first | |
| 112 | 112 | end |
| 113 | 113 | |
| 114 | 114 | @tree = @commit.tree | ... | ... |
app/models/project.rb
| ... | ... | @@ -46,6 +46,21 @@ class Project < ActiveRecord::Base |
| 46 | 46 | |
| 47 | 47 | scope :public_only, where(:private_flag => false) |
| 48 | 48 | |
| 49 | + def repository | |
| 50 | + @repository ||= Repository.new(self) | |
| 51 | + end | |
| 52 | + | |
| 53 | + delegate :repo, | |
| 54 | + :tags, | |
| 55 | + :repo_exists?, | |
| 56 | + :commit, | |
| 57 | + :commits, | |
| 58 | + :tree, | |
| 59 | + :heads, | |
| 60 | + :commits_since, | |
| 61 | + :fresh_commits, | |
| 62 | + :to => :repository, :prefix => nil | |
| 63 | + | |
| 49 | 64 | def to_param |
| 50 | 65 | code |
| 51 | 66 | end |
| ... | ... | @@ -114,18 +129,6 @@ class Project < ActiveRecord::Base |
| 114 | 129 | GITOSIS["base_path"] + path + ".git" |
| 115 | 130 | end |
| 116 | 131 | |
| 117 | - def repo | |
| 118 | - @repo ||= Grit::Repo.new(path_to_repo) | |
| 119 | - end | |
| 120 | - | |
| 121 | - def tags | |
| 122 | - repo.tags.map(&:name).sort.reverse | |
| 123 | - end | |
| 124 | - | |
| 125 | - def repo_exists? | |
| 126 | - repo rescue false | |
| 127 | - end | |
| 128 | - | |
| 129 | 132 | def last_activity |
| 130 | 133 | updates(1).first |
| 131 | 134 | rescue |
| ... | ... | @@ -146,48 +149,6 @@ class Project < ActiveRecord::Base |
| 146 | 149 | end[0...n] |
| 147 | 150 | end |
| 148 | 151 | |
| 149 | - def commit(commit_id = nil) | |
| 150 | - if commit_id | |
| 151 | - repo.commits(commit_id).first | |
| 152 | - else | |
| 153 | - repo.commits.first | |
| 154 | - end | |
| 155 | - end | |
| 156 | - | |
| 157 | - def heads | |
| 158 | - @heads ||= repo.heads | |
| 159 | - end | |
| 160 | - | |
| 161 | - def fresh_commits(n = 10) | |
| 162 | - commits = heads.map do |h| | |
| 163 | - repo.commits(h.name, n) | |
| 164 | - end.flatten.uniq { |c| c.id } | |
| 165 | - | |
| 166 | - commits.sort! do |x, y| | |
| 167 | - y.committed_date <=> x.committed_date | |
| 168 | - end | |
| 169 | - | |
| 170 | - commits[0...n] | |
| 171 | - end | |
| 172 | - | |
| 173 | - def commits_since(date) | |
| 174 | - commits = heads.map do |h| | |
| 175 | - repo.log(h.name, nil, :since => date) | |
| 176 | - end.flatten.uniq { |c| c.id } | |
| 177 | - | |
| 178 | - commits.sort! do |x, y| | |
| 179 | - y.committed_date <=> x.committed_date | |
| 180 | - end | |
| 181 | - | |
| 182 | - commits | |
| 183 | - end | |
| 184 | - | |
| 185 | - def tree(fcommit, path = nil) | |
| 186 | - fcommit = commit if fcommit == :head | |
| 187 | - tree = fcommit.tree | |
| 188 | - path ? (tree / path) : tree | |
| 189 | - end | |
| 190 | - | |
| 191 | 152 | def check_limit |
| 192 | 153 | unless owner.can_create_project? |
| 193 | 154 | errors[:base] << ("Your own projects limit is #{owner.projects_limit}! Please contact administrator to increase it") | ... | ... |
| ... | ... | @@ -0,0 +1,65 @@ |
| 1 | +class Repository | |
| 2 | + attr_accessor :project | |
| 3 | + | |
| 4 | + def self.default_ref | |
| 5 | + "master" | |
| 6 | + end | |
| 7 | + | |
| 8 | + def initialize(project) | |
| 9 | + @project = project | |
| 10 | + end | |
| 11 | + | |
| 12 | + def repo | |
| 13 | + @repo ||= Grit::Repo.new(project.path_to_repo) | |
| 14 | + end | |
| 15 | + | |
| 16 | + def tags | |
| 17 | + repo.tags.map(&:name).sort.reverse | |
| 18 | + end | |
| 19 | + | |
| 20 | + def repo_exists? | |
| 21 | + repo rescue false | |
| 22 | + end | |
| 23 | + | |
| 24 | + def commit(commit_id = nil) | |
| 25 | + if commit_id | |
| 26 | + repo.commits(commit_id).first | |
| 27 | + else | |
| 28 | + repo.commits.first | |
| 29 | + end | |
| 30 | + end | |
| 31 | + | |
| 32 | + def tree(fcommit, path = nil) | |
| 33 | + fcommit = commit if fcommit == :head | |
| 34 | + tree = fcommit.tree | |
| 35 | + path ? (tree / path) : tree | |
| 36 | + end | |
| 37 | + | |
| 38 | + def fresh_commits(n = 10) | |
| 39 | + commits = heads.map do |h| | |
| 40 | + repo.commits(h.name, n) | |
| 41 | + end.flatten.uniq { |c| c.id } | |
| 42 | + | |
| 43 | + commits.sort! do |x, y| | |
| 44 | + y.committed_date <=> x.committed_date | |
| 45 | + end | |
| 46 | + | |
| 47 | + commits[0...n] | |
| 48 | + end | |
| 49 | + | |
| 50 | + def heads | |
| 51 | + @heads ||= repo.heads | |
| 52 | + end | |
| 53 | + | |
| 54 | + def commits_since(date) | |
| 55 | + commits = heads.map do |h| | |
| 56 | + repo.log(h.name, nil, :since => date) | |
| 57 | + end.flatten.uniq { |c| c.id } | |
| 58 | + | |
| 59 | + commits.sort! do |x, y| | |
| 60 | + y.committed_date <=> x.committed_date | |
| 61 | + end | |
| 62 | + | |
| 63 | + commits | |
| 64 | + end | |
| 65 | +end | ... | ... |