Commit cd06d6edacac73ea798d05754fb47c110d9ae0de

Authored by Dmitriy Zaporozhets
1 parent 9988282e

Project model refactored

app/models/commit.rb
... ... @@ -20,6 +20,66 @@ class Commit
20 20 :id,
21 21 :to => :commit
22 22  
  23 +
  24 + class << self
  25 + def find_or_first(repo, commit_id = nil)
  26 + commit = if commit_id
  27 + repo.commit(commit_id)
  28 + else
  29 + repo.commits.first
  30 + end
  31 + Commit.new(commit) if commit
  32 + end
  33 +
  34 + def fresh_commits(repo, n = 10)
  35 + commits = repo.heads.map do |h|
  36 + repo.commits(h.name, n).map { |c| Commit.new(c, h) }
  37 + end.flatten.uniq { |c| c.id }
  38 +
  39 + commits.sort! do |x, y|
  40 + y.committed_date <=> x.committed_date
  41 + end
  42 +
  43 + commits[0...n]
  44 + end
  45 +
  46 + def commits_with_refs(repo, n = 20)
  47 + commits = repo.branches.map { |ref| Commit.new(ref.commit, ref) }
  48 +
  49 + commits.sort! do |x, y|
  50 + y.committed_date <=> x.committed_date
  51 + end
  52 +
  53 + commits[0..n]
  54 + end
  55 +
  56 + def commits_since(repo, date)
  57 + commits = repo.heads.map do |h|
  58 + repo.log(h.name, nil, :since => date).each { |c| Commit.new(c, h) }
  59 + end.flatten.uniq { |c| c.id }
  60 +
  61 + commits.sort! do |x, y|
  62 + y.committed_date <=> x.committed_date
  63 + end
  64 +
  65 + commits
  66 + end
  67 +
  68 + def commits(repo, ref, path = nil, limit = nil, offset = nil)
  69 + if path
  70 + repo.log(ref, path, :max_count => limit, :skip => offset)
  71 + elsif limit && offset
  72 + repo.commits(ref, limit, offset)
  73 + else
  74 + repo.commits(ref)
  75 + end.map{ |c| Commit.new(c) }
  76 + end
  77 +
  78 + def commits_between(repo, from, to)
  79 + repo.commits_between(from, to).map { |c| Commit.new(c) }
  80 + end
  81 + end
  82 +
23 83 def persisted?
24 84 false
25 85 end
... ...
app/models/project.rb
... ... @@ -69,7 +69,7 @@ class Project &lt; ActiveRecord::Base
69 69 :project => self,
70 70 :action => Event::Pushed,
71 71 :data => data,
72   - :author_id => Key.find_by_identifier(author_key_id).user.id
  72 + :author_id => data[:user_id]
73 73 )
74 74 end
75 75  
... ... @@ -259,60 +259,27 @@ class Project &lt; ActiveRecord::Base
259 259 end
260 260  
261 261 def commit(commit_id = nil)
262   - commit = if commit_id
263   - repo.commit(commit_id)
264   - else
265   - repo.commits.first
266   - end
267   - Commit.new(commit) if commit
  262 + Commit.find_or_first(repo, commit_id)
268 263 end
269 264  
270 265 def fresh_commits(n = 10)
271   - commits = heads.map do |h|
272   - repo.commits(h.name, n).map { |c| Commit.new(c, h) }
273   - end.flatten.uniq { |c| c.id }
274   -
275   - commits.sort! do |x, y|
276   - y.committed_date <=> x.committed_date
277   - end
278   -
279   - commits[0...n]
  266 + Commit.fresh_commits(repo, n)
280 267 end
281 268  
282 269 def commits_with_refs(n = 20)
283   - commits = repo.branches.map { |ref| Commit.new(ref.commit, ref) }
284   -
285   - commits.sort! do |x, y|
286   - y.committed_date <=> x.committed_date
287   - end
288   -
289   - commits[0..n]
  270 + Commit.commits_with_refs(repo, n)
290 271 end
291 272  
292 273 def commits_since(date)
293   - commits = heads.map do |h|
294   - repo.log(h.name, nil, :since => date).each { |c| Commit.new(c, h) }
295   - end.flatten.uniq { |c| c.id }
296   -
297   - commits.sort! do |x, y|
298   - y.committed_date <=> x.committed_date
299   - end
300   -
301   - commits
  274 + Commit.commits_since(repo, date)
302 275 end
303 276  
304 277 def commits(ref, path = nil, limit = nil, offset = nil)
305   - if path
306   - repo.log(ref, path, :max_count => limit, :skip => offset)
307   - elsif limit && offset
308   - repo.commits(ref, limit, offset)
309   - else
310   - repo.commits(ref)
311   - end.map{ |c| Commit.new(c) }
  278 + Commit.commits(repo, ref, path, limit, offset)
312 279 end
313 280  
314 281 def commits_between(from, to)
315   - repo.commits_between(from, to).map { |c| Commit.new(c) }
  282 + Commit.commits_between(repo, from, to)
316 283 end
317 284  
318 285 def project_id
... ...
app/models/repository.rb
... ... @@ -1,13 +0,0 @@
1   -require File.join(Rails.root, "lib", "gitlabhq", "git_host")
2   -
3   -class Repository
4   - attr_accessor :project
5   -
6   - def self.default_ref
7   - "master"
8   - end
9   -
10   - def self.access_options
11   - {}
12   - end
13   -end
db/fixtures/development/004_teams.rb
1 1 UsersProject.seed(:id, [
2   - { :id => 1, :project_id => 1, :user_id => 1, :project_access => Project::PROJECT_RWA, :repo_access => Repository::REPO_RW },
3   - { :id => 2, :project_id => 1, :user_id => 2, :project_access => Project::PROJECT_RW, :repo_access => Repository::REPO_N },
4   - { :id => 3, :project_id => 1, :user_id => 3, :project_access => Project::PROJECT_RW, :repo_access => Repository::REPO_N },
5   - { :id => 4, :project_id => 1, :user_id => 4, :project_access => Project::PROJECT_R, :repo_access => Repository::REPO_N },
6   - { :id => 5, :project_id => 1, :user_id => 5, :project_access => Project::PROJECT_R, :repo_access => Repository::REPO_N },
  2 + { :id => 1, :project_id => 1, :user_id => 1, :project_access => UsersProject::MASTER },
  3 + { :id => 2, :project_id => 1, :user_id => 2, :project_access => UsersProject::REPORTERW},
  4 + { :id => 3, :project_id => 1, :user_id => 3, :project_access => UsersProject::REPORTERW},
  5 + { :id => 4, :project_id => 1, :user_id => 4, :project_access => UsersProject::REPORTER},
  6 + { :id => 5, :project_id => 1, :user_id => 5, :project_access => UsersProject::REPORTER},
7 7  
8   - { :id => 6, :project_id => 2, :user_id => 1, :project_access => Project::PROJECT_RWA, :repo_access => Repository::REPO_RW },
9   - { :id => 7, :project_id => 2, :user_id => 2, :project_access => Project::PROJECT_R, :repo_access => Repository::REPO_N },
10   - { :id => 8, :project_id => 2, :user_id => 3, :project_access => Project::PROJECT_R, :repo_access => Repository::REPO_N },
11   - { :id => 9, :project_id => 2, :user_id => 4, :project_access => Project::PROJECT_RWA, :repo_access => Repository::REPO_N },
12   - { :id => 11, :project_id => 2, :user_id => 5, :project_access => Project::PROJECT_RWA, :repo_access => Repository::REPO_N },
  8 + { :id => 6, :project_id => 2, :user_id => 1, :project_access => UsersProject::MASTER },
  9 + { :id => 7, :project_id => 2, :user_id => 2, :project_access => UsersProject::REPORTER},
  10 + { :id => 8, :project_id => 2, :user_id => 3, :project_access => UsersProject::REPORTER},
  11 + { :id => 9, :project_id => 2, :user_id => 4, :project_access => UsersProject::MASTER},
  12 + { :id => 11, :project_id => 2, :user_id => 5, :project_access => UsersProject::MASTER},
13 13  
14   - { :id => 12, :project_id => 3, :user_id => 1, :project_access => Project::PROJECT_RWA, :repo_access => Repository::REPO_RW },
15   - { :id => 13, :project_id => 3, :user_id => 2, :project_access => Project::PROJECT_R, :repo_access => Repository::REPO_N },
16   - { :id => 14, :project_id => 3, :user_id => 3, :project_access => Project::PROJECT_RWA, :repo_access => Repository::REPO_N },
17   - { :id => 15, :project_id => 3, :user_id => 4, :project_access => Project::PROJECT_R, :repo_access => Repository::REPO_N },
18   - { :id => 16, :project_id => 3, :user_id => 5, :project_access => Project::PROJECT_RWA, :repo_access => Repository::REPO_N }
  14 + { :id => 12, :project_id => 3, :user_id => 1, :project_access => UsersProject::MASTER },
  15 + { :id => 13, :project_id => 3, :user_id => 2, :project_access => UsersProject::REPORTER},
  16 + { :id => 14, :project_id => 3, :user_id => 3, :project_access => UsersProject::MASTER},
  17 + { :id => 15, :project_id => 3, :user_id => 4, :project_access => UsersProject::REPORTER},
  18 + { :id => 16, :project_id => 3, :user_id => 5, :project_access => UsersProject::MASTER}
19 19 ])
20 20  
21 21  
... ...