Commit 3a1c6cccc5389f74b59863e6da8b3d1babade015

Authored by Dmitriy Zaporozhets
2 parents 07157799 97a4d8ae

Merge branch 'bug/500_tag_name' into 'master'

Fix 500 on tags page

Fix bug when grit parse tags wrong and raise exception on tags page.
Fixes #993
Gemfile
... ... @@ -29,7 +29,7 @@ gem 'omniauth-github'
29 29  
30 30 # Extracting information from a git repository
31 31 # Provide access to Gitlab::Git library
32   -gem "gitlab_git", "~> 5.0.0"
  32 +gem "gitlab_git", git: 'https://gitlab.com/gitlab-org/gitlab_git.git', ref: '39bc4f043414f1e49f802ed633fa20e6400bfa49'
33 33  
34 34 # Ruby/Rack Git Smart-HTTP Server Handler
35 35 gem 'gitlab-grack', '~> 2.0.0.pre', require: 'grack'
... ...
Gemfile.lock
... ... @@ -5,6 +5,17 @@ GIT
5 5 specs:
6 6 github-markup (0.7.6)
7 7  
  8 +GIT
  9 + remote: https://gitlab.com/gitlab-org/gitlab_git.git
  10 + revision: 39bc4f043414f1e49f802ed633fa20e6400bfa49
  11 + ref: 39bc4f043414f1e49f802ed633fa20e6400bfa49
  12 + specs:
  13 + gitlab_git (5.1.0.pre)
  14 + activesupport (~> 4.0.0)
  15 + gitlab-grit (~> 2.6.1)
  16 + gitlab-linguist (~> 3.0.0)
  17 + rugged (~> 0.19.0)
  18 +
8 19 GEM
9 20 remote: https://rubygems.org/
10 21 specs:
... ... @@ -176,10 +187,6 @@ GEM
176 187 charlock_holmes (~> 0.6.6)
177 188 escape_utils (~> 0.2.4)
178 189 mime-types (~> 1.19)
179   - gitlab_git (5.0.0)
180   - activesupport (~> 4.0.0)
181   - gitlab-grit (~> 2.6.1)
182   - gitlab-linguist (~> 3.0.0)
183 190 gitlab_meta (6.0)
184 191 gitlab_omniauth-ldap (1.0.4)
185 192 net-ldap (~> 0.3.1)
... ... @@ -417,6 +424,7 @@ GEM
417 424 ruby-hmac (0.4.0)
418 425 ruby-progressbar (1.2.0)
419 426 rubyntlm (0.1.1)
  427 + rugged (0.19.0)
420 428 safe_yaml (0.9.7)
421 429 sanitize (2.0.6)
422 430 nokogiri (>= 1.4.4)
... ... @@ -574,7 +582,7 @@ DEPENDENCIES
574 582 gitlab-gollum-lib (~> 1.1.0)
575 583 gitlab-grack (~> 2.0.0.pre)
576 584 gitlab-linguist (~> 3.0.0)
577   - gitlab_git (~> 5.0.0)
  585 + gitlab_git!
578 586 gitlab_meta (= 6.0)
579 587 gitlab_omniauth-ldap (= 1.0.4)
580 588 gon (~> 5.0.0)
... ...
app/controllers/projects/tags_controller.rb
... ... @@ -8,7 +8,7 @@ class Projects::TagsController < Projects::ApplicationController
8 8 before_filter :authorize_admin_project!, only: [:destroy]
9 9  
10 10 def index
11   - @tags = Kaminari.paginate_array(@repository.tags).page(params[:page]).per(30)
  11 + @tags = Kaminari.paginate_array(@repository.tags.reverse).page(params[:page]).per(30)
12 12 end
13 13  
14 14 def create
... ...
app/helpers/gitlab_markdown_helper.rb
... ... @@ -166,13 +166,13 @@ module GitlabMarkdownHelper
166 166  
167 167 def file_exists?(path)
168 168 return false if path.nil? || path.empty?
169   - return @repository.blob_at(current_ref, path).present? || Tree.new(@repository, current_ref, path).entries.any?
  169 + return @repository.blob_at(current_ref, path).present? || @repository.tree(:head, path).entries.any?
170 170 end
171 171  
172 172 # Check if the path is pointing to a directory(tree) or a file(blob)
173 173 # eg. doc/api is directory and doc/README.md is file
174 174 def local_path(path)
175   - return "tree" if Tree.new(@repository, current_ref, path).entries.any?
  175 + return "tree" if @repository.tree(:head, path).entries.any?
176 176 return "raw" if @repository.blob_at(current_ref, path).image?
177 177 return "blob"
178 178 end
... ...
app/models/commit.rb
... ... @@ -16,29 +16,31 @@ class Commit
16 16 DIFF_HARD_LIMIT_FILES = 500
17 17 DIFF_HARD_LIMIT_LINES = 10000
18 18  
19   - def self.decorate(commits)
20   - commits.map { |c| self.new(c) }
21   - end
  19 + class << self
  20 + def decorate(commits)
  21 + commits.map { |c| self.new(c) }
  22 + end
22 23  
23   - # Calculate number of lines to render for diffs
24   - def self.diff_line_count(diffs)
25   - diffs.reduce(0){|sum, d| sum + d.diff.lines.count}
26   - end
  24 + # Calculate number of lines to render for diffs
  25 + def diff_line_count(diffs)
  26 + diffs.reduce(0){|sum, d| sum + d.diff.lines.count}
  27 + end
27 28  
28   - def self.diff_suppress?(diffs, line_count = nil)
29   - # optimize - check file count first
30   - return true if diffs.size > DIFF_SAFE_FILES
  29 + def diff_suppress?(diffs, line_count = nil)
  30 + # optimize - check file count first
  31 + return true if diffs.size > DIFF_SAFE_FILES
31 32  
32   - line_count ||= Commit::diff_line_count(diffs)
33   - line_count > DIFF_SAFE_LINES
34   - end
  33 + line_count ||= Commit::diff_line_count(diffs)
  34 + line_count > DIFF_SAFE_LINES
  35 + end
35 36  
36   - def self.diff_force_suppress?(diffs, line_count = nil)
37   - # optimize - check file count first
38   - return true if diffs.size > DIFF_HARD_LIMIT_FILES
  37 + def diff_force_suppress?(diffs, line_count = nil)
  38 + # optimize - check file count first
  39 + return true if diffs.size > DIFF_HARD_LIMIT_FILES
39 40  
40   - line_count ||= Commit::diff_line_count(diffs)
41   - line_count > DIFF_HARD_LIMIT_LINES
  41 + line_count ||= Commit::diff_line_count(diffs)
  42 + line_count > DIFF_HARD_LIMIT_LINES
  43 + end
42 44 end
43 45  
44 46 attr_accessor :raw
... ...
app/models/repository.rb
... ... @@ -57,7 +57,7 @@ class Repository
57 57  
58 58 def recent_branches(limit = 20)
59 59 branches.sort do |a, b|
60   - b.commit.committed_date <=> a.commit.committed_date
  60 + commit(b.target).committed_date <=> commit(a.target).committed_date
61 61 end[0..limit]
62 62 end
63 63  
... ... @@ -163,7 +163,19 @@ class Repository
163 163  
164 164 def readme
165 165 Rails.cache.fetch(cache_key(:readme)) do
166   - Tree.new(self, self.root_ref).readme
  166 + tree(:head).readme
167 167 end
168 168 end
  169 +
  170 + def head_commit
  171 + commit(self.root_ref)
  172 + end
  173 +
  174 + def tree(sha = :head, path = nil)
  175 + if sha == :head
  176 + sha = head_commit.sha
  177 + end
  178 +
  179 + Tree.new(self, sha, path)
  180 + end
169 181 end
... ...
app/models/tree.rb
... ... @@ -23,4 +23,8 @@ class Tree
23 23 def submodules
24 24 @entries.select(&:submodule?)
25 25 end
  26 +
  27 + def sorted_entries
  28 + trees + blobs + submodules
  29 + end
26 30 end
... ...
app/views/projects/branches/_branch.html.haml
1   -- commit = Commit.new(Gitlab::Git::Commit.new(branch.commit))
  1 +- commit = @repository.commit(branch.target)
2 2 %li
3 3 %h4
4 4 = link_to project_commits_path(@project, branch.name) do
... ... @@ -19,10 +19,14 @@
19 19 = link_to project_branch_path(@project, branch.name), class: 'btn grouped btn-small remove-row', method: :delete, data: { confirm: 'Removed branch cannot be restored. Are you sure?'}, remote: true do
20 20 %i.icon-trash
21 21  
22   - %p
23   - = link_to project_commit_path(@project, commit.id), class: 'commit_short_id' do
24   - = commit.short_id
25   - = image_tag avatar_icon(commit.author_email), class: "avatar s16", alt: ''
26   - %span.light
27   - = gfm escape_once(truncate(commit.title, length: 40))
28   - #{time_ago_with_tooltip(commit.committed_date)}
  22 + - if commit
  23 + %p
  24 + = link_to project_commit_path(@project, commit.id), class: 'commit_short_id' do
  25 + = commit.short_id
  26 + = image_tag avatar_icon(commit.author_email), class: "avatar s16", alt: ''
  27 + %span.light
  28 + = gfm escape_once(truncate(commit.title, length: 40))
  29 + #{time_ago_with_tooltip(commit.committed_date)}
  30 + - else
  31 + %p
  32 + Cant find HEAD commit for this branch
... ...
app/views/projects/tags/_tag.html.haml 0 → 100644
... ... @@ -0,0 +1,22 @@
  1 +- commit = @repository.commit(tag.target)
  2 +%li
  3 + %h4
  4 + = link_to project_commits_path(@project, tag.name), class: "" do
  5 + %i.icon-tag
  6 + = tag.name
  7 + .pull-right
  8 + %small.cdark
  9 + %i.icon-calendar
  10 + #{time_ago_with_tooltip(commit.committed_date)}
  11 + %p.prepend-left-20
  12 + = link_to commit.short_id(8), project_commit_path(@project, commit), class: "monospace"
  13 + &ndash;
  14 + = link_to_gfm truncate(commit.title, length: 70), project_commit_path(@project, commit.id), class: "cdark"
  15 +
  16 + %span.pull-right
  17 + - if can? current_user, :download_code, @project
  18 + = render 'projects/repositories/download_archive', ref: tag.name, btn_class: 'grouped btn-group-small'
  19 + - if can?(current_user, :admin_project, @project)
  20 + = link_to project_tag_path(@project, tag.name), class: 'btn btn-small remove-row grouped', method: :delete, data: { confirm: 'Removed tag cannot be restored. Are you sure?'}, remote: true do
  21 + %i.icon-trash
  22 +
... ...
app/views/projects/tags/index.html.haml
... ... @@ -13,29 +13,7 @@
13 13 - unless @tags.empty?
14 14 %ul.bordered-list
15 15 - @tags.each do |tag|
16   - - commit = Commit.new(Gitlab::Git::Commit.new(tag.commit))
17   - %li
18   - %h4
19   - = link_to project_commits_path(@project, tag.name), class: "" do
20   - %i.icon-tag
21   - = tag.name
22   - %small
23   - = truncate(tag.message || '', length: 70)
24   - .pull-right
25   - %small.cdark
26   - %i.icon-calendar
27   - #{time_ago_with_tooltip(commit.committed_date)}
28   - %p.prepend-left-20
29   - = link_to commit.short_id(8), project_commit_path(@project, commit), class: "monospace"
30   - &ndash;
31   - = link_to_gfm truncate(commit.title, length: 70), project_commit_path(@project, commit.id), class: "cdark"
32   -
33   - %span.pull-right
34   - - if can? current_user, :download_code, @project
35   - = render 'projects/repositories/download_archive', ref: tag.name, btn_class: 'grouped btn-group-small'
36   - - if can?(current_user, :admin_project, @project)
37   - = link_to project_tag_path(@project, tag.name), class: 'btn btn-small remove-row grouped', method: :delete, data: { confirm: 'Removed tag cannot be restored. Are you sure?'}, remote: true do
38   - %i.icon-trash
  16 + = render 'tag', tag: tag
39 17  
40 18 = paginate @tags, theme: 'gitlab'
41 19  
... ...
lib/api/entities.rb
... ... @@ -79,7 +79,16 @@ module API
79 79 end
80 80  
81 81 class RepoObject < Grape::Entity
82   - expose :name, :commit
  82 + expose :name
  83 +
  84 + expose :commit do |repo_obj, options|
  85 + if repo_obj.respond_to?(:commit)
  86 + repo_obj.commit
  87 + elsif options[:project]
  88 + options[:project].repository.commit(repo_obj.target)
  89 + end
  90 + end
  91 +
83 92 expose :protected do |repo, options|
84 93 if options[:project]
85 94 options[:project].protected_branch? repo.name
... ... @@ -87,6 +96,16 @@ module API
87 96 end
88 97 end
89 98  
  99 + class RepoTreeObject < Grape::Entity
  100 + expose :id, :name, :type
  101 +
  102 + expose :mode do |obj, options|
  103 + filemode = obj.mode.to_s(8)
  104 + filemode = "0" + filemode if filemode.length < 6
  105 + filemode
  106 + end
  107 + end
  108 +
90 109 class RepoCommit < Grape::Entity
91 110 expose :id, :short_id, :title, :author_name, :author_email, :created_at
92 111 end
... ...
lib/api/repositories.rb
... ... @@ -82,7 +82,7 @@ module API
82 82 # Example Request:
83 83 # GET /projects/:id/repository/tags
84 84 get ":id/repository/tags" do
85   - present user_project.repo.tags.sort_by(&:name).reverse, with: Entities::RepoObject
  85 + present user_project.repo.tags.sort_by(&:name).reverse, with: Entities::RepoObject, project: user_project
86 86 end
87 87  
88 88 # Get a project repository commits
... ... @@ -141,15 +141,9 @@ module API
141 141 path = params[:path] || nil
142 142  
143 143 commit = user_project.repository.commit(ref)
144   - tree = Tree.new(user_project.repository, commit.id, path)
  144 + tree = user_project.repository.tree(commit.id, path)
145 145  
146   - trees = []
147   -
148   - %w(trees blobs submodules).each do |type|
149   - trees += tree.send(type).map { |t| {name: t.name, type: type.singularize, mode: t.mode, id: t.id} }
150   - end
151   -
152   - trees
  146 + present tree.sorted_entries, with: Entities::RepoTreeObject
153 147 end
154 148  
155 149 # Get a raw file contents
... ... @@ -233,4 +227,3 @@ module API
233 227 end
234 228 end
235 229 end
236   -
... ...
lib/extracts_path.rb
... ... @@ -117,7 +117,7 @@ module ExtractsPath
117 117 end
118 118  
119 119 def tree
120   - @tree ||= Tree.new(@repo, @commit.id, @path)
  120 + @tree ||= @repo.tree(@commit.id, @path)
121 121 end
122 122  
123 123 private
... ...