Commit 3011ac4150e355da9e5956cb223d19e18425e0eb

Authored by Dmitriy Zaporozhets
1 parent 9f4fde04

Refactoring and cleanup. Removed unused commit finders

app/controllers/repositories_controller.rb
... ... @@ -17,7 +17,7 @@ class RepositoriesController < ProjectResourceController
17 17 end
18 18  
19 19 def stats
20   - @stats = Gitlab::GitStats.new(@repository.raw, @repository.root_ref)
  20 + @stats = Gitlab::Git::Stats.new(@repository.raw, @repository.root_ref)
21 21 @graph = @stats.graph
22 22 end
23 23  
... ...
app/controllers/wikis_controller.rb
... ... @@ -49,9 +49,9 @@ class WikisController < ProjectResourceController
49 49 end
50 50  
51 51 def history
52   - unless @wiki = @gollum_wiki.find_page(params[:id])
53   - redirect_to project_wiki_path(@project, :home), notice: "Page not found"
54   - end
  52 + @wiki = @gollum_wiki.find_page(params[:id])
  53 +
  54 + redirect_to(project_wiki_path(@project, :home), notice: "Page not found") unless @wiki
55 55 end
56 56  
57 57 def destroy
... ...
app/models/gollum_wiki.rb
... ... @@ -47,12 +47,6 @@ class GollumWiki
47 47 wiki.pages.map { |page| WikiPage.new(self, page, true) }
48 48 end
49 49  
50   - # Returns the last 30 Commit objects across the entire
51   - # repository.
52   - def recent_history
53   - Gitlab::Git::Commit.fresh_commits(wiki.repo, 30)
54   - end
55   -
56 50 # Finds a page within the repository based on a tile
57 51 # or slug.
58 52 #
... ...
app/models/network/graph.rb
... ... @@ -25,10 +25,9 @@ module Network
25 25 def collect_commits
26 26 refs_cache = build_refs_cache
27 27  
28   - find_commits(count_to_display_commit_in_center)
29   - .map do |commit|
30   - # Decorate with app/model/network/commit.rb
31   - Network::Commit.new(commit, refs_cache[commit.id])
  28 + find_commits(count_to_display_commit_in_center).map do |commit|
  29 + # Decorate with app/model/network/commit.rb
  30 + Network::Commit.new(commit, refs_cache[commit.id])
32 31 end
33 32 end
34 33  
... ... @@ -93,15 +92,13 @@ module Network
93 92 end
94 93  
95 94 def find_commits(skip = 0)
96   - Grit::Commit.find_all(
97   - @repo,
98   - nil,
99   - {
100   - date_order: true,
101   - max_count: self.class.max_count,
102   - skip: skip
103   - }
104   - )
  95 + opts = {
  96 + date_order: true,
  97 + max_count: self.class.max_count,
  98 + skip: skip
  99 + }
  100 +
  101 + Grit::Commit.find_all(@repo, opts, nil)
105 102 end
106 103  
107 104 def commits_sort_by_ref
... ...
lib/gitlab/git/commit.rb
... ... @@ -12,70 +12,6 @@ module Gitlab
12 12 delegate :parents, :diffs, :tree, :stats, :to_patch,
13 13 to: :raw_commit
14 14  
15   - class << self
16   - def serialize_keys
17   - %w(id authored_date committed_date author_name author_email committer_name committer_email message parent_ids)
18   - end
19   -
20   - def find_or_first(repo, commit_id = nil, root_ref)
21   - commit = if commit_id
22   - repo.commit(commit_id)
23   - else
24   - repo.commits(root_ref).first
25   - end
26   -
27   - Commit.new(commit) if commit
28   - end
29   -
30   - def fresh_commits(repo, n = 10)
31   - commits = repo.heads.map do |h|
32   - repo.commits(h.name, n).map { |c| Commit.new(c, h) }
33   - end.flatten.uniq { |c| c.id }
34   -
35   - commits.sort! do |x, y|
36   - y.committed_date <=> x.committed_date
37   - end
38   -
39   - commits[0...n]
40   - end
41   -
42   - def commits_with_refs(repo, n = 20)
43   - commits = repo.branches.map { |ref| Commit.new(ref.commit, ref) }
44   -
45   - commits.sort! do |x, y|
46   - y.committed_date <=> x.committed_date
47   - end
48   -
49   - commits[0..n]
50   - end
51   -
52   - def commits_since(repo, date)
53   - commits = repo.heads.map do |h|
54   - repo.log(h.name, nil, since: date).each { |c| Commit.new(c, h) }
55   - end.flatten.uniq { |c| c.id }
56   -
57   - commits.sort! do |x, y|
58   - y.committed_date <=> x.committed_date
59   - end
60   -
61   - commits
62   - end
63   -
64   - def commits(repo, ref, path = nil, limit = nil, offset = nil)
65   - if path
66   - repo.log(ref, path, max_count: limit, skip: offset)
67   - elsif limit && offset
68   - repo.commits(ref, limit, offset)
69   - else
70   - repo.commits(ref)
71   - end.map{ |c| Commit.new(c) }
72   - end
73   -
74   - def commits_between(repo, from, to)
75   - repo.commits_between(from, to).map { |c| Commit.new(c) }
76   - end
77   - end
78   -
79 15 def initialize(raw_commit, head = nil)
80 16 raise "Nil as raw commit passed" unless raw_commit
81 17  
... ... @@ -88,6 +24,10 @@ module Gitlab
88 24 @head = head
89 25 end
90 26  
  27 + def serialize_keys
  28 + %w(id authored_date committed_date author_name author_email committer_name committer_email message parent_ids)
  29 + end
  30 +
91 31 def sha
92 32 id
93 33 end
... ... @@ -143,7 +83,7 @@ module Gitlab
143 83 def to_hash
144 84 hash = {}
145 85  
146   - keys = Commit.serialize_keys
  86 + keys = serialize_keys
147 87  
148 88 keys.each do |key|
149 89 hash[key] = send(key)
... ... @@ -172,7 +112,7 @@ module Gitlab
172 112 end
173 113  
174 114 def init_from_hash(hash)
175   - Commit.serialize_keys.each do |key|
  115 + serialize_keys.each do |key|
176 116 send(:"#{key}=", hash[key])
177 117 end
178 118 end
... ...
lib/gitlab/git/repository.rb
... ... @@ -48,31 +48,41 @@ module Gitlab
48 48 end
49 49  
50 50 def commit(commit_id = nil)
51   - Gitlab::Git::Commit.find_or_first(repo, commit_id, root_ref)
52   - end
  51 + commit = if commit_id
  52 + repo.commit(commit_id)
  53 + else
  54 + repo.commits(root_ref).first
  55 + end
53 56  
54   - def fresh_commits(n = 10)
55   - Gitlab::Git::Commit.fresh_commits(repo, n)
  57 + Commit.new(commit) if commit
56 58 end
57 59  
58 60 def commits_with_refs(n = 20)
59   - Gitlab::Git::Commit.commits_with_refs(repo, n)
60   - end
  61 + commits = repo.branches.map { |ref| Commit.new(ref.commit, ref) }
  62 +
  63 + commits.sort! do |x, y|
  64 + y.committed_date <=> x.committed_date
  65 + end
61 66  
62   - def commits_since(date)
63   - Gitlab::Git::Commit.commits_since(repo, date)
  67 + commits[0..n]
64 68 end
65 69  
66 70 def commits(ref, path = nil, limit = nil, offset = nil)
67   - Gitlab::Git::Commit.commits(repo, ref, path, limit, offset)
  71 + if path
  72 + repo.log(ref, path, max_count: limit, skip: offset)
  73 + elsif limit && offset
  74 + repo.commits(ref, limit, offset)
  75 + else
  76 + repo.commits(ref)
  77 + end.map{ |c| Commit.new(c) }
68 78 end
69 79  
70   - def last_commit_for(ref, path = nil)
71   - commits(ref, path, 1).first
  80 + def commits_between(from, to)
  81 + repo.commits_between(from, to).map { |c| Commit.new(c) }
72 82 end
73 83  
74   - def commits_between(from, to)
75   - Gitlab::Git::Commit.commits_between(repo, from, to)
  84 + def last_commit_for(ref, path = nil)
  85 + commits(ref, path, 1).first
76 86 end
77 87  
78 88 # Returns an Array of branch names
... ...
lib/gitlab/git/stats.rb 0 → 100644
... ... @@ -0,0 +1,75 @@
  1 +module Gitlab
  2 + module Git
  3 + class Stats
  4 + attr_accessor :repo, :ref
  5 +
  6 + def initialize repo, ref
  7 + @repo, @ref = repo, ref
  8 + end
  9 +
  10 + def authors
  11 + @authors ||= collect_authors
  12 + end
  13 +
  14 + def commits_count
  15 + @commits_count ||= repo.commit_count(ref)
  16 + end
  17 +
  18 + def files_count
  19 + args = [ref, '-r', '--name-only' ]
  20 + repo.git.run(nil, 'ls-tree', nil, {}, args).split("\n").count
  21 + end
  22 +
  23 + def authors_count
  24 + authors.size
  25 + end
  26 +
  27 + def graph
  28 + @graph ||= build_graph
  29 + end
  30 +
  31 + protected
  32 +
  33 + def collect_authors
  34 + shortlog = repo.git.shortlog({e: true, s: true }, ref)
  35 +
  36 + authors = []
  37 +
  38 + lines = shortlog.split("\n")
  39 +
  40 + lines.each do |line|
  41 + data = line.split("\t")
  42 + commits = data.first
  43 + author = Grit::Actor.from_string(data.last)
  44 +
  45 + authors << OpenStruct.new(
  46 + name: author.name,
  47 + email: author.email,
  48 + commits: commits.to_i
  49 + )
  50 + end
  51 +
  52 + authors.sort_by(&:commits).reverse
  53 + end
  54 +
  55 + def build_graph n = 4
  56 + from, to = (Date.today - n.weeks), Date.today
  57 + args = ['--all', "--since=#{from.to_s(:date)}", '--format=%ad' ]
  58 + rev_list = repo.git.run(nil, 'rev-list', nil, {}, args).split("\n")
  59 +
  60 + commits_dates = rev_list.values_at(* rev_list.each_index.select {|i| i.odd?})
  61 + commits_dates = commits_dates.map { |date_str| Time.parse(date_str).to_date.to_s(:date) }
  62 +
  63 + commits_per_day = from.upto(to).map do |day|
  64 + commits_dates.count(day.to_date.to_s(:date))
  65 + end
  66 +
  67 + OpenStruct.new(
  68 + labels: from.upto(to).map { |day| day.stamp('Aug 23') },
  69 + commits: commits_per_day,
  70 + weeks: n
  71 + )
  72 + end
  73 + end
  74 + end
  75 +end
... ...
lib/gitlab/git/tree.rb
... ... @@ -38,7 +38,7 @@ module Gitlab
38 38 end
39 39  
40 40 def readme
41   - @readme ||= entries.find { |c| c.is_a?(Grit::Blob) and c.name =~ /^readme/i }
  41 + @readme ||= blobs.find { |c| c.name =~ /^readme/i }
42 42 end
43 43  
44 44 protected
... ...
lib/gitlab/git_stats.rb
... ... @@ -1,73 +0,0 @@
1   -module Gitlab
2   - class GitStats
3   - attr_accessor :repo, :ref
4   -
5   - def initialize repo, ref
6   - @repo, @ref = repo, ref
7   - end
8   -
9   - def authors
10   - @authors ||= collect_authors
11   - end
12   -
13   - def commits_count
14   - @commits_count ||= repo.commit_count(ref)
15   - end
16   -
17   - def files_count
18   - args = [ref, '-r', '--name-only' ]
19   - repo.git.run(nil, 'ls-tree', nil, {}, args).split("\n").count
20   - end
21   -
22   - def authors_count
23   - authors.size
24   - end
25   -
26   - def graph
27   - @graph ||= build_graph
28   - end
29   -
30   - protected
31   -
32   - def collect_authors
33   - shortlog = repo.git.shortlog({e: true, s: true }, ref)
34   -
35   - authors = []
36   -
37   - lines = shortlog.split("\n")
38   -
39   - lines.each do |line|
40   - data = line.split("\t")
41   - commits = data.first
42   - author = Grit::Actor.from_string(data.last)
43   -
44   - authors << OpenStruct.new(
45   - name: author.name,
46   - email: author.email,
47   - commits: commits.to_i
48   - )
49   - end
50   -
51   - authors.sort_by(&:commits).reverse
52   - end
53   -
54   - def build_graph n = 4
55   - from, to = (Date.today - n.weeks), Date.today
56   - args = ['--all', "--since=#{from.to_s(:date)}", '--format=%ad' ]
57   - rev_list = repo.git.run(nil, 'rev-list', nil, {}, args).split("\n")
58   -
59   - commits_dates = rev_list.values_at(* rev_list.each_index.select {|i| i.odd?})
60   - commits_dates = commits_dates.map { |date_str| Time.parse(date_str).to_date.to_s(:date) }
61   -
62   - commits_per_day = from.upto(to).map do |day|
63   - commits_dates.count(day.to_date.to_s(:date))
64   - end
65   -
66   - OpenStruct.new(
67   - labels: from.upto(to).map { |day| day.stamp('Aug 23') },
68   - commits: commits_per_day,
69   - weeks: n
70   - )
71   - end
72   - end
73   -end