Commit c0a30b3a0c67d2120a2489a4226b54d92616878f

Authored by Ricardo Rauch
2 parents e2d0f42a d730e3ef

Merge branch 'master' of dev.gitlabhq.com:gitlabhq

app/controllers/application_controller.rb
@@ -64,7 +64,7 @@ class ApplicationController < ActionController::Base @@ -64,7 +64,7 @@ class ApplicationController < ActionController::Base
64 else 64 else
65 @branch = params[:branch].blank? ? nil : params[:branch] 65 @branch = params[:branch].blank? ? nil : params[:branch]
66 @tag = params[:tag].blank? ? nil : params[:tag] 66 @tag = params[:tag].blank? ? nil : params[:tag]
67 - @ref = @branch || @tag || "master" 67 + @ref = @branch || @tag || Repository.default_ref
68 end 68 end
69 end 69 end
70 70
app/controllers/commits_controller.rb
@@ -8,18 +8,18 @@ class CommitsController < ApplicationController @@ -8,18 +8,18 @@ class CommitsController < ApplicationController
8 before_filter :add_project_abilities 8 before_filter :add_project_abilities
9 before_filter :authorize_read_project! 9 before_filter :authorize_read_project!
10 before_filter :require_non_empty_project 10 before_filter :require_non_empty_project
  11 + before_filter :load_refs, :only => :index # load @branch, @tag & @ref
11 12
12 - def index  
13 - load_refs # load @branch, @tag & @ref  
14 13
  14 + def index
15 @repo = project.repo 15 @repo = project.repo
16 limit, offset = (params[:limit] || 20), (params[:offset] || 0) 16 limit, offset = (params[:limit] || 20), (params[:offset] || 0)
17 17
18 - if params[:path]  
19 - @commits = @repo.log(@ref, params[:path], :max_count => limit, :skip => offset)  
20 - else  
21 - @commits = @repo.commits(@ref, limit, offset)  
22 - end 18 + @commits = if params[:path]
  19 + @repo.log(@ref, params[:path], :max_count => limit, :skip => offset)
  20 + else
  21 + @repo.commits(@ref, limit, offset)
  22 + end
23 23
24 respond_to do |format| 24 respond_to do |format|
25 format.html # index.html.erb 25 format.html # index.html.erb
@@ -29,8 +29,8 @@ class CommitsController < ApplicationController @@ -29,8 +29,8 @@ class CommitsController < ApplicationController
29 29
30 def show 30 def show
31 @commit = project.repo.commits(params[:id]).first 31 @commit = project.repo.commits(params[:id]).first
32 - @notes = project.notes.where(:noteable_id => @commit.id, :noteable_type => "Commit").order("created_at DESC").limit(20)  
33 - @note = @project.notes.new(:noteable_id => @commit.id, :noteable_type => "Commit") 32 + @notes = project.commit_notes(@commit).fresh.limit(20)
  33 + @note = @project.build_commit_note(@commit)
34 34
35 respond_to do |format| 35 respond_to do |format|
36 format.html 36 format.html
app/controllers/projects_controller.rb
@@ -6,8 +6,8 @@ class ProjectsController < ApplicationController @@ -6,8 +6,8 @@ class ProjectsController < ApplicationController
6 before_filter :add_project_abilities 6 before_filter :add_project_abilities
7 before_filter :authorize_read_project!, :except => [:index, :new, :create] 7 before_filter :authorize_read_project!, :except => [:index, :new, :create]
8 before_filter :authorize_admin_project!, :only => [:edit, :update, :destroy] 8 before_filter :authorize_admin_project!, :only => [:edit, :update, :destroy]
9 -  
10 before_filter :require_non_empty_project, :only => [:blob, :tree] 9 before_filter :require_non_empty_project, :only => [:blob, :tree]
  10 + before_filter :load_refs, :only => :tree # load @branch, @tag & @ref
11 11
12 def index 12 def index
13 source = current_user.projects 13 source = current_user.projects
@@ -101,15 +101,13 @@ class ProjectsController < ApplicationController @@ -101,15 +101,13 @@ class ProjectsController < ApplicationController
101 # 101 #
102 102
103 def tree 103 def tree
104 - load_refs # load @branch, @tag & @ref  
105 -  
106 @repo = project.repo 104 @repo = project.repo
107 105
108 - if params[:commit_id]  
109 - @commit = @repo.commits(params[:commit_id]).first  
110 - else  
111 - @commit = @repo.commits(@ref || "master").first  
112 - end 106 + @commit = if params[:commit_id]
  107 + @repo.commits(params[:commit_id]).first
  108 + else
  109 + @repo.commits(@ref).first
  110 + end
113 111
114 @tree = @commit.tree 112 @tree = @commit.tree
115 @tree = @tree / params[:path] if params[:path] 113 @tree = @tree / params[:path] if params[:path]
app/models/project.rb
@@ -46,6 +46,25 @@ class Project < ActiveRecord::Base @@ -46,6 +46,25 @@ class Project < ActiveRecord::Base
46 46
47 scope :public_only, where(:private_flag => false) 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 + :url_to_repo,
  55 + :path_to_repo,
  56 + :update_gitosis_project,
  57 + :destroy_gitosis_project,
  58 + :tags,
  59 + :repo_exists?,
  60 + :commit,
  61 + :commits,
  62 + :tree,
  63 + :heads,
  64 + :commits_since,
  65 + :fresh_commits,
  66 + :to => :repository, :prefix => nil
  67 +
49 def to_param 68 def to_param
50 code 69 code
51 end 70 end
@@ -59,16 +78,12 @@ class Project < ActiveRecord::Base @@ -59,16 +78,12 @@ class Project < ActiveRecord::Base
59 notes.where(:noteable_type => ["", nil]) 78 notes.where(:noteable_type => ["", nil])
60 end 79 end
61 80
62 - def update_gitosis_project  
63 - Gitosis.new.configure do |c|  
64 - c.update_project(path, gitosis_writers)  
65 - end 81 + def build_commit_note(commit)
  82 + notes.new(:noteable_id => commit.id, :noteable_type => "Commit")
66 end 83 end
67 84
68 - def destroy_gitosis_project  
69 - Gitosis.new.configure do |c|  
70 - c.destroy_project(self)  
71 - end 85 + def commit_notes(commit)
  86 + notes.where(:noteable_id => commit.id, :noteable_type => "Commit")
72 end 87 end
73 88
74 def add_access(user, *access) 89 def add_access(user, *access)
@@ -106,26 +121,6 @@ class Project < ActiveRecord::Base @@ -106,26 +121,6 @@ class Project < ActiveRecord::Base
106 private_flag 121 private_flag
107 end 122 end
108 123
109 - def url_to_repo  
110 - "#{GITOSIS["git_user"]}@#{GITOSIS["host"]}:#{path}.git"  
111 - end  
112 -  
113 - def path_to_repo  
114 - GITOSIS["base_path"] + path + ".git"  
115 - end  
116 -  
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 def last_activity 124 def last_activity
130 updates(1).first 125 updates(1).first
131 rescue 126 rescue
@@ -146,48 +141,6 @@ class Project < ActiveRecord::Base @@ -146,48 +141,6 @@ class Project < ActiveRecord::Base
146 end[0...n] 141 end[0...n]
147 end 142 end
148 143
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 def check_limit 144 def check_limit
192 unless owner.can_create_project? 145 unless owner.can_create_project?
193 errors[:base] << ("Your own projects limit is #{owner.projects_limit}! Please contact administrator to increase it") 146 errors[:base] << ("Your own projects limit is #{owner.projects_limit}! Please contact administrator to increase it")
app/models/repository.rb 0 → 100644
@@ -0,0 +1,93 @@ @@ -0,0 +1,93 @@
  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 path
  13 + @path ||= project.path
  14 + end
  15 +
  16 + def project_id
  17 + project.id
  18 + end
  19 +
  20 + def repo
  21 + @repo ||= Grit::Repo.new(project.path_to_repo)
  22 + end
  23 +
  24 + def url_to_repo
  25 + "#{GITOSIS["git_user"]}@#{GITOSIS["host"]}:#{path}.git"
  26 + end
  27 +
  28 + def path_to_repo
  29 + GITOSIS["base_path"] + path + ".git"
  30 + end
  31 +
  32 + def update_gitosis_project
  33 + Gitosis.new.configure do |c|
  34 + c.update_project(path, project.gitosis_writers)
  35 + end
  36 + end
  37 +
  38 + def destroy_gitosis_project
  39 + Gitosis.new.configure do |c|
  40 + c.destroy_project(@project)
  41 + end
  42 + end
  43 +
  44 + def repo_exists?
  45 + repo rescue false
  46 + end
  47 +
  48 + def tags
  49 + repo.tags.map(&:name).sort.reverse
  50 + end
  51 +
  52 + def heads
  53 + @heads ||= repo.heads
  54 + end
  55 +
  56 + def tree(fcommit, path = nil)
  57 + fcommit = commit if fcommit == :head
  58 + tree = fcommit.tree
  59 + path ? (tree / path) : tree
  60 + end
  61 +
  62 + def commit(commit_id = nil)
  63 + if commit_id
  64 + repo.commits(commit_id).first
  65 + else
  66 + repo.commits.first
  67 + end
  68 + end
  69 +
  70 + def fresh_commits(n = 10)
  71 + commits = heads.map do |h|
  72 + repo.commits(h.name, n)
  73 + end.flatten.uniq { |c| c.id }
  74 +
  75 + commits.sort! do |x, y|
  76 + y.committed_date <=> x.committed_date
  77 + end
  78 +
  79 + commits[0...n]
  80 + end
  81 +
  82 + def commits_since(date)
  83 + commits = heads.map do |h|
  84 + repo.log(h.name, nil, :since => date)
  85 + end.flatten.uniq { |c| c.id }
  86 +
  87 + commits.sort! do |x, y|
  88 + y.committed_date <=> x.committed_date
  89 + end
  90 +
  91 + commits
  92 + end
  93 +end