Class: Dolt::Git::Blame::PorcelainParser
- Inherits:
-
Object
- Object
- Dolt::Git::Blame::PorcelainParser
- Defined in:
- lib/dolt/git/blame.rb
Instance Method Summary collapse
- #extract(lines, thing) ⇒ Object
- #extract_hash(lines, type) ⇒ Object
- #extract_header(lines) ⇒ Object
- #extract_lines(lines, num) ⇒ Object
-
#initialize(output) ⇒ PorcelainParser
constructor
A new instance of PorcelainParser.
- #is_header?(line) ⇒ Boolean
- #parse ⇒ Object
Constructor Details
#initialize(output) ⇒ PorcelainParser
Returns a new instance of PorcelainParser.
34 35 36 37 |
# File 'lib/dolt/git/blame.rb', line 34 def initialize(output) @output = output @commits = {} end |
Instance Method Details
#extract(lines, thing) ⇒ Object
106 107 108 |
# File 'lib/dolt/git/blame.rb', line 106 def extract(lines, thing) lines.shift.split("#{thing} ")[1] end |
#extract_hash(lines, type) ⇒ Object
97 98 99 100 101 102 103 104 |
# File 'lib/dolt/git/blame.rb', line 97 def extract_hash(lines, type) { :name => extract(lines, "#{type}"), :mail => extract(lines, "#{type}-mail").gsub(/[<>]/, ""), :time => (Time.at(extract(lines, "#{type}-time").to_i) + Time.zone_offset(extract(lines, "#{type}-tz"))) } end |
#extract_header(lines) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/dolt/git/blame.rb', line 62 def extract_header(lines) header = lines.shift pieces = header.scan(/^([0-9a-f]{40}) (\d+) (\d+) (\d+)$/).first header = { :oid => pieces.first, :num_lines => pieces[3].to_i } if lines.first =~ /^author/ header[:author] = extract_hash(lines, :author) header[:committer] = extract_hash(lines, :committer) header[:summary] = extract(lines, "summary") throwaway = lines.shift until throwaway =~ /^filename/ @commits[header[:oid]] = header else header[:author] = @commits[header[:oid]][:author] header[:committer] = @commits[header[:oid]][:committer] header[:summary] = @commits[header[:oid]][:summary] end header end |
#extract_lines(lines, num) ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/dolt/git/blame.rb', line 82 def extract_lines(lines, num) extracted = [] num.times do if extracted.length > 0 line = lines.shift # Header for next line end content = lines.shift # Actual content extracted.push(content[1..content.length]) # 8 spaces padding end extracted end |
#is_header?(line) ⇒ Boolean
58 59 60 |
# File 'lib/dolt/git/blame.rb', line 58 def is_header?(line) line =~ /^[0-9a-f]{40} \d+ \d+ \d+$/ end |
#parse ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/dolt/git/blame.rb', line 39 def parse lines = @output.split("\n") chunks = [] while lines.length > 0 chunk = extract_header(lines) affected_lines = extract_lines(lines, chunk[:num_lines]) if chunks.last && chunk[:oid] == chunks.last[:oid] chunks.last[:lines].concat(affected_lines) else chunk[:lines] = affected_lines chunks << chunk end end chunks end |