Class: Grit::Blob

Inherits:
Object
  • Object
show all
Defined in:
lib/grit/blob.rb,
lib/grit_ext/blob.rb

Constant Summary collapse

DEFAULT_MIME_TYPE =
"text/plain"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id


6
7
8
# File 'lib/grit/blob.rb', line 6

def id
  @id
end

#modeObject (readonly)

Returns the value of attribute mode


7
8
9
# File 'lib/grit/blob.rb', line 7

def mode
  @mode
end

#nameObject (readonly)

Returns the value of attribute name


8
9
10
# File 'lib/grit/blob.rb', line 8

def name
  @name
end

Class Method Details

.blame(repo, commit, file) ⇒ Object

The blame information for the given file at the given commit

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


57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/grit/blob.rb', line 57

def self.blame(repo, commit, file)
  data = repo.git.blame({:p => true}, commit, '--', file)

  commits = {}
  blames = []
  info = nil

  data.split("\n").each do |line|
    parts = line.split(/\s+/, 2)
    case parts.first
      when /^[0-9A-Fa-f]{40}$/
        case line
          when /^([0-9A-Fa-f]{40}) (\d+) (\d+) (\d+)$/
            _, id, origin_line, final_line, group_lines = *line.match(/^([0-9A-Fa-f]{40}) (\d+) (\d+) (\d+)$/)
            info = {:id => id}
            blames << [nil, []]
          when /^([0-9A-Fa-f]{40}) (\d+) (\d+)$/
            _, id, origin_line, final_line = *line.match(/^([0-9A-Fa-f]{40}) (\d+) (\d+)$/)
            info = {:id => id}
        end
      when /^(author|committer)/
        case parts.first
          when /^(.+)-mail$/
            info["#{$1}_email".intern] = parts.last
          when /^(.+)-time$/
            info["#{$1}_date".intern] = Time.at(parts.last.to_i)
          when /^(author|committer)$/
            info[$1.intern] = parts.last
        end
      when /^filename/
        info[:filename] = parts.last
      when /^summary/
        info[:summary] = parts.last
      when ''
        c = commits[info[:id]]
        unless c
          c = Commit.create(repo, :id => info[:id],
                                  :author => Actor.from_string(info[:author] + ' ' + info[:author_email]),
                                  :authored_date => info[:author_date],
                                  :committer => Actor.from_string(info[:committer] + ' ' + info[:committer_email]),
                                  :committed_date => info[:committer_date],
                                  :message => info[:summary])
          commits[info[:id]] = c
        end
        _, text = *line.match(/^\t(.*)$/)
        blames.last[0] = c
        blames.last[1] << text
        info = nil
    end
  end

  blames
end

.create(repo, atts) ⇒ Object

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)


15
16
17
# File 'lib/grit/blob.rb', line 15

def self.create(repo, atts)
  self.allocate.create_initialize(repo, atts)
end

.old_blameObject

The blame information for the given file at the given commit

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


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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/grit_ext/blob.rb', line 16

def self.blame(repo, commit, file)
  data = repo.git.blame({:p => true}, commit, '--', file)

  commits = {}
  blames = []
  info = nil

  data.split("\n").each do |line|
    parts = line.split(/\s+/, 2)
    case parts.first
      when /^[0-9A-Fa-f]{40}$/
        case line
          when /^([0-9A-Fa-f]{40}) (\d+) (\d+) (\d+)$/
            _, id, origin_line, final_line, group_lines = *line.match(/^([0-9A-Fa-f]{40}) (\d+) (\d+) (\d+)$/)
            info = {:id => id}
            blames << [nil, []]
          when /^([0-9A-Fa-f]{40}) (\d+) (\d+)$/
            _, id, origin_line, final_line = *line.match(/^([0-9A-Fa-f]{40}) (\d+) (\d+)$/)
            info = {:id => id}
        end
      when /^(author|committer)/
        case parts.first
          when /^(.+)-mail$/
            info["#{$1}_email".intern] = parts.last
          when /^(.+)-time$/
            info["#{$1}_date".intern] = Time.at(parts.last.to_i)
          when /^(author|committer)$/
            info[$1.intern] = parts.last
        end
      when /^filename/
        info[:filename] = parts.last
      when /^summary/
        info[:summary] = parts.last
      when ''
        c = commits[info[:id]]
        unless c
          c = Commit.create(repo, :id => info[:id],
                                  :author => Actor.from_string(info[:author] + ' ' + info[:author_email]),
                                  :authored_date => info[:author_date],
                                  :committer => Actor.from_string(info[:committer] + ' ' + info[:committer_email]),
                                  :committed_date => info[:committer_date],
                                  :message => info[:summary])
          commits[info[:id]] = c
        end
        _, text = *line.match(/^\t(.*)$/)
        blames.last[0] = c
        blames.last[1] << text
        info = nil
    end
  end

  blames
end

Instance Method Details

#<=>(other) ⇒ Object

Compares blobs by name


121
122
123
# File 'lib/grit/blob.rb', line 121

def <=>(other)
  name <=> other.name
end

#basenameObject


111
112
113
# File 'lib/grit/blob.rb', line 111

def basename
  File.basename(name)
end

#create_initialize(repo, atts) ⇒ Object

Initializer for Blob.create

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

Returns Grit::Blob (unbaked)


24
25
26
27
28
29
30
# File 'lib/grit/blob.rb', line 24

def create_initialize(repo, atts)
  @repo = repo
  atts.each do |k, v|
    instance_variable_set("@#{k}".to_sym, v)
  end
  self
end

#dataObject

The binary contents of this blob.

Returns String


42
43
44
# File 'lib/grit/blob.rb', line 42

def data
  @data ||= @repo.git.cat_file({:p => true}, id)
end

#inspectObject

Pretty object inspection


116
117
118
# File 'lib/grit/blob.rb', line 116

def inspect
  %Q{#<Grit::Blob "#{@id}">}
end

#mime_typeObject

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

Returns String


49
50
51
52
# File 'lib/grit/blob.rb', line 49

def mime_type
  guesses = MIME::Types.type_for(self.name) rescue []
  guesses.first ? guesses.first.simplified : DEFAULT_MIME_TYPE
end

#old_dataObject

The binary contents of this blob.

Returns String


5
6
7
# File 'lib/grit_ext/blob.rb', line 5

def data
  @data ||= @repo.git.cat_file({:p => true}, id)
end

#old_nameObject

Returns the value of attribute name


4
5
6
# File 'lib/grit_ext/blob.rb', line 4

def name
  @name
end

#sizeObject

The size of this blob in bytes

Returns Integer


35
36
37
# File 'lib/grit/blob.rb', line 35

def size
  @size ||= @repo.git.cat_file({:s => true}, id).chomp.to_i
end