Skip to content

Commit

Permalink
Get rid of Rugged monkey patches, move them to Pronto::Git namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
mmozuras committed Sep 10, 2014
1 parent 6b09caa commit 90f5bb7
Show file tree
Hide file tree
Showing 9 changed files with 150 additions and 141 deletions.
11 changes: 4 additions & 7 deletions lib/pronto.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
require 'rugged'
require 'pronto/rugged/patch'
require 'pronto/rugged/diff/delta'
require 'pronto/rugged/diff/line'
require 'pronto/git/patch'
require 'pronto/git/line'
require 'pronto/rugged/remote'
require 'pronto/rugged/commit'

require 'pronto/plugin'
require 'pronto/message'
Expand All @@ -18,11 +16,10 @@
module Pronto
def self.run(commit = 'master', repo_path = '.', formatter = nil)
repo = Rugged::Repository.new(repo_path)
# TODO: Remove this hack when regression is fixed
Rugged::Tree.define_singleton_method(:repo) { repo }
commit ||= 'master'
merge_base = repo.merge_base(commit, repo.head.target)
patches = repo.diff(merge_base, repo.head.target)
# TODO This could be cleaner
patches = repo.diff(merge_base, repo.head.target).map { |patch| Git::Patch.new(patch, repo) }

result = run_all_runners(patches, merge_base)

Expand Down
86 changes: 86 additions & 0 deletions lib/pronto/git/line.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
module Pronto
module Git
class Line < Struct.new(:line, :patch, :hunk)
def addition?
line.addition?
end

def deletion?
line.deletion?
end

def content
line.content
end

def new_lineno
line.new_lineno
end

def old_lineno
line.old_lineno
end

def line_origin
line.line_origin
end

def position
hunk_index = patch.hunks.find_index { |h| h.header == hunk.header }
line_index = patch.lines.find_index(line)

line_index + hunk_index + 1
end

def commit
@commit ||= begin
repo.lookup(commit_sha) if commit_sha
end
end

def commit_sha
blame[:final_commit_id] if blame
end

def commit_line
@commit_line ||= begin
# TODO: Rugged does not seem to support diffing against multiple parents
diff = commit.diff(reverse: true) unless commit.parents.count != 1
patches = diff ? diff.patches : []

# TODO This could be cleaner
patches = patches.map { |patch| Git::Patch.new(patch, repo) }

commit_patch = patches.find do |p|
patch.new_file_full_path == p.new_file_full_path
end

lines = commit_patch ? commit_patch.lines : []
result = lines.find { |l| blame[:orig_start_line_number] == l.new_lineno }

result || line # no commit_line means that it was just added
end
end

def ==(other)
line.content == other.content &&
line_origin == other.line_origin &&
old_lineno == other.old_lineno &&
new_lineno == other.new_lineno
end

private

def repo
patch.repo
end

def blame
@blame ||= Rugged::Blame.new(repo, patch.delta.new_file[:path],
min_line: new_lineno, max_line: new_lineno,
track_copies_same_file: true,
track_copies_any_commit_copies: true)[0]
end
end
end
end
44 changes: 44 additions & 0 deletions lib/pronto/git/patch.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
require 'pathname'

module Pronto
module Git
class Patch < Struct.new(:patch, :repo)
def delta
patch.delta
end

def hunks
patch.hunks
end

def additions
patch.stat[0]
end

def deletions
patch.stat[1]
end

def lines
@lines ||= begin
patch.map do |hunk|
hunk.lines.map { |line| Line.new(line, self, hunk) }
end.flatten.compact
end
end

def added_lines
lines.select(&:addition?)
end

def deleted_lines
lines.select(&:deletion?)
end

def new_file_full_path
repo_path = Pathname.new(repo.path).parent
repo_path.join(patch.delta.new_file[:path])
end
end
end
end
8 changes: 0 additions & 8 deletions lib/pronto/rugged/commit.rb

This file was deleted.

16 changes: 0 additions & 16 deletions lib/pronto/rugged/diff/delta.rb

This file was deleted.

61 changes: 0 additions & 61 deletions lib/pronto/rugged/diff/line.rb

This file was deleted.

33 changes: 0 additions & 33 deletions lib/pronto/rugged/patch.rb

This file was deleted.

16 changes: 16 additions & 0 deletions spec/pronto/git/line_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
require 'spec_helper'

module Pronto
module Git
describe Line do
let(:diff) { repository.diff('88558b7', '88558b7~5') }
let(:patch) { Patch.new(diff.patches.last, repository) }
let(:line) { patch.lines[2] }

describe '#position' do
subject { line.position }
it { should == 3 }
end
end
end
end
16 changes: 0 additions & 16 deletions spec/pronto/rugged/diff/line_spec.rb
Original file line number Diff line number Diff line change
@@ -1,16 +0,0 @@
require 'spec_helper'

module Rugged
class Diff
describe Line do
let(:diff) { repository.diff('88558b7', '88558b7~5') }
let(:patch) { diff.patches.last }
let(:line) { patch.lines[2] }

describe '#position' do
subject { line.position }
it { should == 3 }
end
end
end
end

0 comments on commit 90f5bb7

Please sign in to comment.