Class Grit::Blob
In: lib/grit/blob.rb
Parent: Object

Methods

<=>   basename   blame   create   create_initialize   data   inspect   mime_type   size  

Constants

DEFAULT_MIME_TYPE = "text/plain"

Attributes

id  [R] 
mode  [R] 
name  [R] 

Public Class methods

The blame information for the given file at the given commit

Returns Array: [Grit::Commit, Array: [<line>]]

[Source]

     # File lib/grit/blob.rb, line 57
 57:     def self.blame(repo, commit, file)
 58:       data = repo.git.blame({:p => true}, commit, '--', file)
 59:       
 60:       commits = {}
 61:       blames = []
 62:       info = nil
 63:       
 64:       data.split("\n").each do |line|
 65:         parts = line.split(/\s+/, 2)
 66:         case parts.first
 67:           when /^[0-9A-Fa-f]{40}$/
 68:             case line
 69:               when /^([0-9A-Fa-f]{40}) (\d+) (\d+) (\d+)$/
 70:                 _, id, origin_line, final_line, group_lines = *line.match(/^([0-9A-Fa-f]{40}) (\d+) (\d+) (\d+)$/)
 71:                 info = {:id => id}
 72:                 blames << [nil, []]
 73:               when /^([0-9A-Fa-f]{40}) (\d+) (\d+)$/
 74:                 _, id, origin_line, final_line = *line.match(/^([0-9A-Fa-f]{40}) (\d+) (\d+)$/)
 75:                 info = {:id => id}
 76:             end
 77:           when /^(author|committer)/
 78:             case parts.first
 79:               when /^(.+)-mail$/
 80:                 info["#{$1}_email".intern] = parts.last
 81:               when /^(.+)-time$/
 82:                 info["#{$1}_date".intern] = Time.at(parts.last.to_i)
 83:               when /^(author|committer)$/
 84:                 info[$1.intern] = parts.last
 85:             end
 86:           when /^filename/
 87:             info[:filename] = parts.last
 88:           when /^summary/
 89:             info[:summary] = parts.last
 90:           when ''
 91:             c = commits[info[:id]]
 92:             unless c
 93:               c = Commit.create(repo, :id => info[:id],
 94:                                       :author => Actor.from_string(info[:author] + ' ' + info[:author_email]),
 95:                                       :authored_date => info[:author_date],
 96:                                       :committer => Actor.from_string(info[:committer] + ' ' + info[:committer_email]),
 97:                                       :committed_date => info[:committer_date],
 98:                                       :message => info[:summary])
 99:               commits[info[:id]] = c
100:             end
101:             _, text = *line.match(/^\t(.*)$/)
102:             blames.last[0] = c
103:             blames.last[1] << text
104:             info = nil
105:         end
106:       end
107: 
108:       blames
109:     end

Create an unbaked Blob containing just the specified attributes

  +repo+ is the Repo
  +atts+ is a Hash of instance variable data

Returns Grit::Blob (unbaked)

[Source]

    # File lib/grit/blob.rb, line 15
15:     def self.create(repo, atts)
16:       self.allocate.create_initialize(repo, atts)
17:     end

Public Instance methods

Compares blobs by name

[Source]

     # File lib/grit/blob.rb, line 121
121:     def <=>(other)
122:       name <=> other.name
123:     end

[Source]

     # File lib/grit/blob.rb, line 111
111:     def basename
112:       File.basename(name)
113:     end

Initializer for Blob.create

  +repo+ is the Repo
  +atts+ is a Hash of instance variable data

Returns Grit::Blob (unbaked)

[Source]

    # File lib/grit/blob.rb, line 24
24:     def create_initialize(repo, atts)
25:       @repo = repo
26:       atts.each do |k, v|
27:         instance_variable_set("@#{k}".to_sym, v)
28:       end
29:       self
30:     end

The binary contents of this blob.

Returns String

[Source]

    # File lib/grit/blob.rb, line 42
42:     def data
43:       @data ||= @repo.git.cat_file({:p => true}, id)
44:     end

Pretty object inspection

[Source]

     # File lib/grit/blob.rb, line 116
116:     def inspect
117:       %Q{#<Grit::Blob "#{@id}">}
118:     end

The mime type of this file (based on the filename)

Returns String

[Source]

    # File lib/grit/blob.rb, line 49
49:     def mime_type
50:       guesses = MIME::Types.type_for(self.name) rescue []
51:       guesses.first ? guesses.first.simplified : DEFAULT_MIME_TYPE
52:     end

The size of this blob in bytes

Returns Integer

[Source]

    # File lib/grit/blob.rb, line 35
35:     def size
36:       @size ||= @repo.git.cat_file({:s => true}, id).chomp.to_i
37:     end

[Validate]