Commit 03f41e2820d76d272aa7357cf726b5d131bb80e0

Authored by Dmitriy Zaporozhets
1 parent 0c5795a4

Gitlab::Git::Tree & Blob added

app/controllers/refs_controller.rb
... ... @@ -30,7 +30,7 @@ class RefsController < ProjectResourceController
30 30 end
31 31  
32 32 def logs_tree
33   - contents = @tree.contents
  33 + contents = @tree.entries
34 34 @logs = contents.map do |content|
35 35 file = params[:path] ? File.join(params[:path], content.name) : content.name
36 36 last_commit = @repo.commits(@commit.id, file, 1).last
... ...
app/helpers/tree_helper.rb
... ... @@ -3,9 +3,9 @@ module TreeHelper
3 3 # their corresponding partials
4 4 #
5 5 # contents - A Grit::Tree object for the current tree
6   - def render_tree(contents)
  6 + def render_tree(tree)
7 7 # Render Folders before Files/Submodules
8   - folders, files = contents.partition { |v| v.kind_of?(Grit::Tree) }
  8 + folders, files = tree.trees, tree.blobs
9 9  
10 10 tree = ""
11 11  
... ... @@ -91,5 +91,4 @@ module TreeHelper
91 91 file = File.join(tree.path, "..")
92 92 tree_join(tree.ref, file)
93 93 end
94   -
95 94 end
... ...
app/models/merge_request.rb
... ... @@ -152,7 +152,7 @@ class MergeRequest < ActiveRecord::Base
152 152 end
153 153  
154 154 def commits
155   - load_commits(st_commits) || []
  155 + load_commits(st_commits || [])
156 156 end
157 157  
158 158 def probably_merged?
... ...
app/models/tree.rb
1 1 class Tree
2   - attr_accessor :path, :tree, :ref
  2 + attr_accessor :raw
3 3  
4 4 def initialize(repository, sha, ref = nil, path = nil)
5 5 @raw = Gitlab::Git::Tree.new(repository, sha, ref, path)
6 6 end
7 7  
8   - def invalid?
9   - @raw.nil?
10   - end
11   -
12 8 def method_missing(m, *args, &block)
13 9 @raw.send(m, *args, &block)
14 10 end
... ...
app/views/commits/_diffs.html.haml
... ... @@ -16,8 +16,8 @@
16 16 - unless @suppress_diff
17 17 - diffs.each_with_index do |diff, i|
18 18 - next if diff.diff.empty?
19   - - file = Tree.new(@repository, @commit.id, @ref, diff.new_path)
20   - - file = Tree.new(@repository, @commit.parent_id, @ref, diff.old_path) unless file
  19 + - file = Gitlab::Git::Blob.new(@repository, @commit.id, @ref, diff.new_path)
  20 + - file = Gitlab::Git::Blob.new(@repository, @commit.parent_id, @ref, diff.old_path) unless file.exists?
21 21 - next unless file
22 22 .file{id: "diff-#{i}"}
23 23 .header
... ... @@ -25,7 +25,7 @@
25 25 %span= diff.old_path
26 26  
27 27 - if @commit.parent_ids.present?
28   - = link_to project_tree_path(@project, tree_join(@commit.prev_commit_id, diff.new_path)), {:class => 'btn btn-tiny pull-right view-file'} do
  28 + = link_to project_tree_path(@project, tree_join(@commit.parent_id, diff.new_path)), {:class => 'btn btn-tiny pull-right view-file'} do
29 29 View file @
30 30 %span.commit-short-id= @commit.short_id(6)
31 31 - else
... ... @@ -43,7 +43,7 @@
43 43 - if file.text?
44 44 = render "commits/text_file", diff: diff, index: i
45 45 - elsif file.image?
46   - - old_file = Tree.new(@repository, @commit.parent_id, @ref, diff.old_path) if @commit.parent_id
  46 + - old_file = Gitlab::Git::Blob.new(@repository, @commit.parent_id, @ref, diff.old_path) if @commit.parent_id
47 47 = render "commits/image", diff: diff, old_file: old_file, file: file, index: i
48 48 - else
49 49 %p.nothing_here_message No preview for this file type
... ...
app/views/tree/_tree.html.haml
... ... @@ -32,7 +32,7 @@
32 32 %td
33 33 %td
34 34  
35   - = render_tree(tree.contents)
  35 + = render_tree(tree)
36 36  
37 37 - if tree.readme
38 38 = render "tree/readme", readme: tree.readme
... ...
lib/extracts_path.rb
... ... @@ -104,7 +104,7 @@ module ExtractsPath
104 104  
105 105 @tree = Tree.new(@project.repository, @commit.id, @ref, @path)
106 106  
107   - raise InvalidPathError if @tree.invalid?
  107 + raise InvalidPathError unless @tree.exists?
108 108 rescue RuntimeError, NoMethodError, InvalidPathError
109 109 not_found!
110 110 end
... ...
lib/gitlab/git/blob.rb 0 → 100644
... ... @@ -0,0 +1,30 @@
  1 +module Gitlab
  2 + module Git
  3 + class Blob
  4 + include Linguist::BlobHelper
  5 +
  6 + attr_accessor :raw_blob
  7 +
  8 + delegate :name, to: :raw_blob
  9 +
  10 + def initialize(repository, sha, ref, path)
  11 + @repository, @sha, @ref = repository, sha, ref
  12 +
  13 + @commit = @repository.commit(sha)
  14 + @raw_blob = @repository.tree(@commit, path)
  15 + end
  16 +
  17 + def data
  18 + if raw_blob
  19 + raw_blob.data
  20 + else
  21 + nil
  22 + end
  23 + end
  24 +
  25 + def exists?
  26 + @raw_blob
  27 + end
  28 + end
  29 + end
  30 +end
... ...
lib/gitlab/git/tree.rb
1 1 module Gitlab
2 2 module Git
3 3 class Tree
4   - include Linguist::BlobHelper
5   -
6   - attr_accessor :repository, :sha, :path, :ref, :raw_tree
  4 + attr_accessor :repository, :sha, :path, :ref, :raw_tree, :id
7 5  
8 6 def initialize(repository, sha, ref = nil, path = nil)
9   - @repository, @sha, @ref = repository, sha, ref
  7 + @repository, @sha, @ref, @path = repository, sha, ref, path
  8 +
  9 + @path = nil if @path.blank?
10 10  
11 11 # Load tree from repository
12   - @commit = @repository.commit(sha)
13   - @raw_tree = @repository.tree(@commit, path)
  12 + @commit = @repository.commit(@sha)
  13 + @raw_tree = @repository.tree(@commit, @path)
  14 + end
  15 +
  16 + def exists?
  17 + raw_tree
14 18 end
15 19  
16 20 def empty?
17 21 data.blank?
18 22 end
19 23  
20   - def data
21   - raw_tree.data
  24 + def trees
  25 + entries.select { |t| t.is_a?(Grit::Tree) }
  26 + end
  27 +
  28 + def blobs
  29 + entries.select { |t| t.is_a?(Grit::Blob) }
22 30 end
23 31  
24 32 def is_blob?
25   - tree.is_a?(Grit::Blob)
  33 + raw_tree.is_a?(Grit::Blob)
26 34 end
27 35  
28 36 def up_dir?
... ... @@ -30,7 +38,13 @@ module Gitlab
30 38 end
31 39  
32 40 def readme
33   - @readme ||= contents.find { |c| c.is_a?(Grit::Blob) and c.name =~ /^readme/i }
  41 + @readme ||= entries.find { |c| c.is_a?(Grit::Blob) and c.name =~ /^readme/i }
  42 + end
  43 +
  44 + protected
  45 +
  46 + def entries
  47 + raw_tree.contents
34 48 end
35 49 end
36 50 end
... ...