summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorDmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>2013-04-02 22:37:20 +0300
committerDmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>2013-04-02 22:37:20 +0300
commit03f41e2820d76d272aa7357cf726b5d131bb80e0 (patch)
tree8f9b317572899049d43de0d72194b9b50da1648e /lib
parent0c5795a49726402d2f2751d8b05d5bbb9dd23511 (diff)
downloadgitlab-ce-03f41e2820d76d272aa7357cf726b5d131bb80e0.tar.gz
Gitlab::Git::Tree & Blob added
Diffstat (limited to 'lib')
-rw-r--r--lib/extracts_path.rb2
-rw-r--r--lib/gitlab/git/blob.rb30
-rw-r--r--lib/gitlab/git/tree.rb34
3 files changed, 55 insertions, 11 deletions
diff --git a/lib/extracts_path.rb b/lib/extracts_path.rb
index c3e2441ff21..009c5fcada9 100644
--- a/lib/extracts_path.rb
+++ b/lib/extracts_path.rb
@@ -104,7 +104,7 @@ module ExtractsPath
@tree = Tree.new(@project.repository, @commit.id, @ref, @path)
- raise InvalidPathError if @tree.invalid?
+ raise InvalidPathError unless @tree.exists?
rescue RuntimeError, NoMethodError, InvalidPathError
not_found!
end
diff --git a/lib/gitlab/git/blob.rb b/lib/gitlab/git/blob.rb
new file mode 100644
index 00000000000..405cbddad90
--- /dev/null
+++ b/lib/gitlab/git/blob.rb
@@ -0,0 +1,30 @@
+module Gitlab
+ module Git
+ class Blob
+ include Linguist::BlobHelper
+
+ attr_accessor :raw_blob
+
+ delegate :name, to: :raw_blob
+
+ def initialize(repository, sha, ref, path)
+ @repository, @sha, @ref = repository, sha, ref
+
+ @commit = @repository.commit(sha)
+ @raw_blob = @repository.tree(@commit, path)
+ end
+
+ def data
+ if raw_blob
+ raw_blob.data
+ else
+ nil
+ end
+ end
+
+ def exists?
+ @raw_blob
+ end
+ end
+ end
+end
diff --git a/lib/gitlab/git/tree.rb b/lib/gitlab/git/tree.rb
index b81ce550f4c..8bcf71ea217 100644
--- a/lib/gitlab/git/tree.rb
+++ b/lib/gitlab/git/tree.rb
@@ -1,28 +1,36 @@
module Gitlab
module Git
class Tree
- include Linguist::BlobHelper
-
- attr_accessor :repository, :sha, :path, :ref, :raw_tree
+ attr_accessor :repository, :sha, :path, :ref, :raw_tree, :id
def initialize(repository, sha, ref = nil, path = nil)
- @repository, @sha, @ref = repository, sha, ref
+ @repository, @sha, @ref, @path = repository, sha, ref, path
+
+ @path = nil if @path.blank?
# Load tree from repository
- @commit = @repository.commit(sha)
- @raw_tree = @repository.tree(@commit, path)
+ @commit = @repository.commit(@sha)
+ @raw_tree = @repository.tree(@commit, @path)
+ end
+
+ def exists?
+ raw_tree
end
def empty?
data.blank?
end
- def data
- raw_tree.data
+ def trees
+ entries.select { |t| t.is_a?(Grit::Tree) }
+ end
+
+ def blobs
+ entries.select { |t| t.is_a?(Grit::Blob) }
end
def is_blob?
- tree.is_a?(Grit::Blob)
+ raw_tree.is_a?(Grit::Blob)
end
def up_dir?
@@ -30,7 +38,13 @@ module Gitlab
end
def readme
- @readme ||= contents.find { |c| c.is_a?(Grit::Blob) and c.name =~ /^readme/i }
+ @readme ||= entries.find { |c| c.is_a?(Grit::Blob) and c.name =~ /^readme/i }
+ end
+
+ protected
+
+ def entries
+ raw_tree.contents
end
end
end