Commit 49b024f5f5b88d406b895f050943db1e75adfa2a

Authored by Dmitriy Zaporozhets
1 parent bb06e905

Use Gitlab::Git:: for git features across application

app/controllers/blame_controller.rb
... ... @@ -8,7 +8,6 @@ class BlameController < ProjectResourceController
8 8 before_filter :require_non_empty_project
9 9  
10 10 def show
11   - @repo = @project.repo
12   - @blame = Grit::Blob.blame(@repo, @commit.id, @path)
  11 + @blame = Gitlab::Git::Blame.new(project.repository, @commit.id, @path)
13 12 end
14 13 end
... ...
app/controllers/compare_controller.rb
... ... @@ -8,12 +8,12 @@ class CompareController < ProjectResourceController
8 8 end
9 9  
10 10 def show
11   - result = Commit.compare(project, params[:from], params[:to])
  11 + compare = Gitlab::Git::Compare.new(project.repository, params[:from], params[:to])
12 12  
13   - @commits = result[:commits]
14   - @commit = result[:commit]
15   - @diffs = result[:diffs]
16   - @refs_are_same = result[:same]
  13 + @commits = compare.commits
  14 + @commit = compare.commit
  15 + @diffs = compare.diffs
  16 + @refs_are_same = compare.same
17 17 @line_notes = []
18 18 end
19 19  
... ...
app/models/commit.rb
... ... @@ -8,6 +8,10 @@ class Commit
8 8 #
9 9 DIFF_SAFE_SIZE = 100
10 10  
  11 + def self.decorate(commits)
  12 + commits.map { |c| self.new(c) }
  13 + end
  14 +
11 15 attr_accessor :raw
12 16  
13 17 def initialize(raw_commit)
... ...
app/models/gollum_wiki.rb
... ... @@ -114,5 +114,4 @@ class GollumWiki
114 114 def path_to_repo
115 115 @path_to_repo ||= File.join(Gitlab.config.gitlab_shell.repos_path, "#{path_with_namespace}.git")
116 116 end
117   -
118 117 end
... ...
app/models/repository.rb
... ... @@ -13,13 +13,13 @@ class Repository
13 13  
14 14 def commits(ref, path = nil, limit = nil, offset = nil)
15 15 commits = raw_repository.commits(ref, path, limit, offset)
16   - commits = decorate_commits(commits) if commits.present?
  16 + commits = Commit.decorate(commits) if commits.present?
17 17 commits
18 18 end
19 19  
20 20 def commits_between(target, source)
21 21 commits = raw_repository.commits_between(target, source)
22   - commits = decorate_commits(commits) if commits.present?
  22 + commits = Commit.decorate(commits) if commits.present?
23 23 commits
24 24 end
25 25  
... ... @@ -32,10 +32,4 @@ class Repository
32 32  
33 33 super
34 34 end
35   -
36   - protected
37   -
38   - def decorate_commits(commits)
39   - commits.map { |c| Commit.new(c) }
40   - end
41 35 end
... ...
app/models/wiki_page.rb
... ... @@ -79,14 +79,14 @@ class WikiPage
79 79 def version
80 80 return nil unless persisted?
81 81  
82   - @version ||= Commit.new(@page.version)
  82 + @version ||= Commit.new(Gitlab::Git::Commit.new(@page.version))
83 83 end
84 84  
85 85 # Returns an array of Gitlab Commit instances.
86 86 def versions
87 87 return [] unless persisted?
88 88  
89   - @page.versions.map { |v| Commit.new(v) }
  89 + @page.versions.map { |v| Commit.new(Gitlab::Git::Commit.new(v)) }
90 90 end
91 91  
92 92 # Returns the Date that this latest version was
... ...
app/views/blame/show.html.haml
... ... @@ -6,7 +6,7 @@
6 6 %i.icon-angle-right
7 7 = link_to project_tree_path(@project, @ref) do
8 8 = @project.name
9   - - @tree.breadcrumbs(6) do |link|
  9 + - tree_breadcrumbs(@tree, 6) do |link|
10 10 \/
11 11 %li= link
12 12 .clear
... ...
app/views/compare/show.html.haml
... ... @@ -16,7 +16,7 @@
16 16 %div.ui-box
17 17 %h5.title
18 18 Commits (#{@commits.count})
19   - %ul.well-list= render @commits
  19 + %ul.well-list= render Commit.decorate(@commits)
20 20  
21 21 - unless @diffs.empty?
22 22 %h4 Diff
... ...
app/views/wikis/show.html.haml
... ... @@ -13,5 +13,4 @@
13 13 = preserve do
14 14 = render_wiki_content(@wiki)
15 15  
16   -- commit = Commit.new(@wiki.version)
17   -%p.time Last edited by #{commit_author_link(commit, avatar: true, size: 16)} #{time_ago_in_words @wiki.created_at} ago
  16 +%p.time Last edited by #{commit_author_link(@wiki.version, avatar: true, size: 16)} #{time_ago_in_words @wiki.created_at} ago
... ...
lib/gitlab/git/commit.rb
1   -# Gitlab::Git::Gitlab::Git::Commit is a wrapper around native Grit::Commit object
  1 +# Gitlab::Git::Commit is a wrapper around native Grit::Commit object
2 2 # We dont want to use grit objects inside app/
3 3 # It helps us easily migrate to rugged in future
4 4 module Gitlab
5 5 module Git
6   - class Gitlab::Git::Commit
  6 + class Commit
7 7 attr_accessor :raw_commit, :head, :refs
8 8  
9 9 delegate :message, :authored_date, :committed_date, :parents, :sha,
... ... @@ -18,12 +18,12 @@ module Gitlab
18 18 repo.commits(root_ref).first
19 19 end
20 20  
21   - Gitlab::Git::Commit.new(commit) if commit
  21 + Commit.new(commit) if commit
22 22 end
23 23  
24 24 def fresh_commits(repo, n = 10)
25 25 commits = repo.heads.map do |h|
26   - repo.commits(h.name, n).map { |c| Gitlab::Git::Commit.new(c, h) }
  26 + repo.commits(h.name, n).map { |c| Commit.new(c, h) }
27 27 end.flatten.uniq { |c| c.id }
28 28  
29 29 commits.sort! do |x, y|
... ... @@ -34,7 +34,7 @@ module Gitlab
34 34 end
35 35  
36 36 def commits_with_refs(repo, n = 20)
37   - commits = repo.branches.map { |ref| Gitlab::Git::Commit.new(ref.commit, ref) }
  37 + commits = repo.branches.map { |ref| Commit.new(ref.commit, ref) }
38 38  
39 39 commits.sort! do |x, y|
40 40 y.committed_date <=> x.committed_date
... ... @@ -45,7 +45,7 @@ module Gitlab
45 45  
46 46 def commits_since(repo, date)
47 47 commits = repo.heads.map do |h|
48   - repo.log(h.name, nil, since: date).each { |c| Gitlab::Git::Commit.new(c, h) }
  48 + repo.log(h.name, nil, since: date).each { |c| Commit.new(c, h) }
49 49 end.flatten.uniq { |c| c.id }
50 50  
51 51 commits.sort! do |x, y|
... ... @@ -62,41 +62,11 @@ module Gitlab
62 62 repo.commits(ref, limit, offset)
63 63 else
64 64 repo.commits(ref)
65   - end.map{ |c| Gitlab::Git::Commit.new(c) }
  65 + end.map{ |c| Commit.new(c) }
66 66 end
67 67  
68 68 def commits_between(repo, from, to)
69   - repo.commits_between(from, to).map { |c| Gitlab::Git::Commit.new(c) }
70   - end
71   -
72   - def compare(project, from, to)
73   - result = {
74   - commits: [],
75   - diffs: [],
76   - commit: nil,
77   - same: false
78   - }
79   -
80   - return result unless from && to
81   -
82   - first = project.repository.commit(to.try(:strip))
83   - last = project.repository.commit(from.try(:strip))
84   -
85   - if first && last
86   - result[:same] = (first.id == last.id)
87   - result[:commits] = project.repo.commits_between(last.id, first.id).map {|c| Gitlab::Git::Commit.new(c)}
88   -
89   - # Dont load diff for 100+ commits
90   - result[:diffs] = if result[:commits].size > 100
91   - []
92   - else
93   - project.repo.diff(last.id, first.id) rescue []
94   - end
95   -
96   - result[:commit] = Gitlab::Git::Commit.new(first)
97   - end
98   -
99   - result
  69 + repo.commits_between(from, to).map { |c| Commit.new(c) }
100 70 end
101 71 end
102 72  
... ... @@ -142,7 +112,7 @@ module Gitlab
142 112  
143 113 def prev_commit
144 114 @prev_commit ||= if parents.present?
145   - Gitlab::Git::Commit.new(parents.first)
  115 + Commit.new(parents.first)
146 116 else
147 117 nil
148 118 end
... ... @@ -156,7 +126,7 @@ module Gitlab
156 126 #
157 127 # Cuts out the header and stats from #to_patch and returns only the diff.
158 128 def to_diff
159   - # see Grit::Gitlab::Git::Commit#show
  129 + # see Grit::Commit#show
160 130 patch = to_patch
161 131  
162 132 # discard lines before the diff
... ...
spec/support/test_env.rb
... ... @@ -17,15 +17,18 @@ module TestEnv
17 17 repos_path = Rails.root.join('tmp', 'test-git-base-path')
18 18 Gitlab.config.gitlab_shell.stub(repos_path: repos_path)
19 19  
  20 + Gitlab::Shell.any_instance.stub(:add_repository) do |path|
  21 + create_temp_repo(File.join(repos_path, "#{path}.git"))
  22 + end
  23 +
20 24 Gitlab::Shell.any_instance.stub(
21   - add_repository: ->(path) { create_temp_repo(File.join(repos_path, "#{path}.git")) },
22 25 mv_repository: true,
23 26 remove_repository: true,
24 27 add_key: true,
25 28 remove_key: true
26 29 )
27 30  
28   - fake_satellite = double(
  31 + fake_satellite = stub(
29 32 exists?: true,
30 33 destroy: true,
31 34 create: true
... ...