summaryrefslogtreecommitdiff
path: root/lib/gitlab/git/tree.rb
blob: e6b500ba18cf2b8683c2412faed01106e00da5d3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
module Gitlab
  module Git
    class Tree
      attr_accessor :repository, :sha, :path, :ref, :raw_tree, :id

      def initialize(repository, sha, ref = nil, path = nil)
        @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)
      end

      def exists?
        raw_tree
      end

      def empty?
        data.blank?
      end

      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?
        raw_tree.is_a?(Grit::Blob)
      end

      def up_dir?
        path.present?
      end

      def readme
        @readme ||= blobs.find { |c| c.name =~ /^readme/i }
      end

      protected

      def entries
        raw_tree.contents
      end
    end
  end
end