Commit 03f41e2820d76d272aa7357cf726b5d131bb80e0
1 parent
0c5795a4
Exists in
master
and in
4 other branches
Gitlab::Git::Tree & Blob added
Showing
9 changed files
with
65 additions
and
26 deletions
Show diff stats
app/controllers/refs_controller.rb
| @@ -30,7 +30,7 @@ class RefsController < ProjectResourceController | @@ -30,7 +30,7 @@ class RefsController < ProjectResourceController | ||
| 30 | end | 30 | end |
| 31 | 31 | ||
| 32 | def logs_tree | 32 | def logs_tree |
| 33 | - contents = @tree.contents | 33 | + contents = @tree.entries |
| 34 | @logs = contents.map do |content| | 34 | @logs = contents.map do |content| |
| 35 | file = params[:path] ? File.join(params[:path], content.name) : content.name | 35 | file = params[:path] ? File.join(params[:path], content.name) : content.name |
| 36 | last_commit = @repo.commits(@commit.id, file, 1).last | 36 | last_commit = @repo.commits(@commit.id, file, 1).last |
app/helpers/tree_helper.rb
| @@ -3,9 +3,9 @@ module TreeHelper | @@ -3,9 +3,9 @@ module TreeHelper | ||
| 3 | # their corresponding partials | 3 | # their corresponding partials |
| 4 | # | 4 | # |
| 5 | # contents - A Grit::Tree object for the current tree | 5 | # contents - A Grit::Tree object for the current tree |
| 6 | - def render_tree(contents) | 6 | + def render_tree(tree) |
| 7 | # Render Folders before Files/Submodules | 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 | tree = "" | 10 | tree = "" |
| 11 | 11 | ||
| @@ -91,5 +91,4 @@ module TreeHelper | @@ -91,5 +91,4 @@ module TreeHelper | ||
| 91 | file = File.join(tree.path, "..") | 91 | file = File.join(tree.path, "..") |
| 92 | tree_join(tree.ref, file) | 92 | tree_join(tree.ref, file) |
| 93 | end | 93 | end |
| 94 | - | ||
| 95 | end | 94 | end |
app/models/merge_request.rb
| @@ -152,7 +152,7 @@ class MergeRequest < ActiveRecord::Base | @@ -152,7 +152,7 @@ class MergeRequest < ActiveRecord::Base | ||
| 152 | end | 152 | end |
| 153 | 153 | ||
| 154 | def commits | 154 | def commits |
| 155 | - load_commits(st_commits) || [] | 155 | + load_commits(st_commits || []) |
| 156 | end | 156 | end |
| 157 | 157 | ||
| 158 | def probably_merged? | 158 | def probably_merged? |
app/models/tree.rb
| 1 | class Tree | 1 | class Tree |
| 2 | - attr_accessor :path, :tree, :ref | 2 | + attr_accessor :raw |
| 3 | 3 | ||
| 4 | def initialize(repository, sha, ref = nil, path = nil) | 4 | def initialize(repository, sha, ref = nil, path = nil) |
| 5 | @raw = Gitlab::Git::Tree.new(repository, sha, ref, path) | 5 | @raw = Gitlab::Git::Tree.new(repository, sha, ref, path) |
| 6 | end | 6 | end |
| 7 | 7 | ||
| 8 | - def invalid? | ||
| 9 | - @raw.nil? | ||
| 10 | - end | ||
| 11 | - | ||
| 12 | def method_missing(m, *args, &block) | 8 | def method_missing(m, *args, &block) |
| 13 | @raw.send(m, *args, &block) | 9 | @raw.send(m, *args, &block) |
| 14 | end | 10 | end |
app/views/commits/_diffs.html.haml
| @@ -16,8 +16,8 @@ | @@ -16,8 +16,8 @@ | ||
| 16 | - unless @suppress_diff | 16 | - unless @suppress_diff |
| 17 | - diffs.each_with_index do |diff, i| | 17 | - diffs.each_with_index do |diff, i| |
| 18 | - next if diff.diff.empty? | 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 | - next unless file | 21 | - next unless file |
| 22 | .file{id: "diff-#{i}"} | 22 | .file{id: "diff-#{i}"} |
| 23 | .header | 23 | .header |
| @@ -25,7 +25,7 @@ | @@ -25,7 +25,7 @@ | ||
| 25 | %span= diff.old_path | 25 | %span= diff.old_path |
| 26 | 26 | ||
| 27 | - if @commit.parent_ids.present? | 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 | View file @ | 29 | View file @ |
| 30 | %span.commit-short-id= @commit.short_id(6) | 30 | %span.commit-short-id= @commit.short_id(6) |
| 31 | - else | 31 | - else |
| @@ -43,7 +43,7 @@ | @@ -43,7 +43,7 @@ | ||
| 43 | - if file.text? | 43 | - if file.text? |
| 44 | = render "commits/text_file", diff: diff, index: i | 44 | = render "commits/text_file", diff: diff, index: i |
| 45 | - elsif file.image? | 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 | = render "commits/image", diff: diff, old_file: old_file, file: file, index: i | 47 | = render "commits/image", diff: diff, old_file: old_file, file: file, index: i |
| 48 | - else | 48 | - else |
| 49 | %p.nothing_here_message No preview for this file type | 49 | %p.nothing_here_message No preview for this file type |
app/views/tree/_tree.html.haml
lib/extracts_path.rb
| @@ -104,7 +104,7 @@ module ExtractsPath | @@ -104,7 +104,7 @@ module ExtractsPath | ||
| 104 | 104 | ||
| 105 | @tree = Tree.new(@project.repository, @commit.id, @ref, @path) | 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 | rescue RuntimeError, NoMethodError, InvalidPathError | 108 | rescue RuntimeError, NoMethodError, InvalidPathError |
| 109 | not_found! | 109 | not_found! |
| 110 | end | 110 | end |
| @@ -0,0 +1,30 @@ | @@ -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 | module Gitlab | 1 | module Gitlab |
| 2 | module Git | 2 | module Git |
| 3 | class Tree | 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 | def initialize(repository, sha, ref = nil, path = nil) | 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 | # Load tree from repository | 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 | end | 18 | end |
| 15 | 19 | ||
| 16 | def empty? | 20 | def empty? |
| 17 | data.blank? | 21 | data.blank? |
| 18 | end | 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 | end | 30 | end |
| 23 | 31 | ||
| 24 | def is_blob? | 32 | def is_blob? |
| 25 | - tree.is_a?(Grit::Blob) | 33 | + raw_tree.is_a?(Grit::Blob) |
| 26 | end | 34 | end |
| 27 | 35 | ||
| 28 | def up_dir? | 36 | def up_dir? |
| @@ -30,7 +38,13 @@ module Gitlab | @@ -30,7 +38,13 @@ module Gitlab | ||
| 30 | end | 38 | end |
| 31 | 39 | ||
| 32 | def readme | 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 | end | 48 | end |
| 35 | end | 49 | end |
| 36 | end | 50 | end |