From 90f5bb7830d7c3d93cebc964741b268eccb1c499 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mindaugas=20Moz=C5=ABras?= Date: Tue, 29 Jul 2014 22:24:46 +0300 Subject: [PATCH] Get rid of Rugged monkey patches, move them to Pronto::Git namespace --- lib/pronto.rb | 11 ++-- lib/pronto/git/line.rb | 86 ++++++++++++++++++++++++++++ lib/pronto/git/patch.rb | 44 ++++++++++++++ lib/pronto/rugged/commit.rb | 8 --- lib/pronto/rugged/diff/delta.rb | 16 ------ lib/pronto/rugged/diff/line.rb | 61 -------------------- lib/pronto/rugged/patch.rb | 33 ----------- spec/pronto/git/line_spec.rb | 16 ++++++ spec/pronto/rugged/diff/line_spec.rb | 16 ------ 9 files changed, 150 insertions(+), 141 deletions(-) create mode 100644 lib/pronto/git/line.rb create mode 100644 lib/pronto/git/patch.rb delete mode 100644 lib/pronto/rugged/commit.rb delete mode 100644 lib/pronto/rugged/diff/delta.rb delete mode 100644 lib/pronto/rugged/diff/line.rb delete mode 100644 lib/pronto/rugged/patch.rb create mode 100644 spec/pronto/git/line_spec.rb diff --git a/lib/pronto.rb b/lib/pronto.rb index 57a05e9b..fc17346a 100644 --- a/lib/pronto.rb +++ b/lib/pronto.rb @@ -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' @@ -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) diff --git a/lib/pronto/git/line.rb b/lib/pronto/git/line.rb new file mode 100644 index 00000000..841ca27d --- /dev/null +++ b/lib/pronto/git/line.rb @@ -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 diff --git a/lib/pronto/git/patch.rb b/lib/pronto/git/patch.rb new file mode 100644 index 00000000..dc9e8cc9 --- /dev/null +++ b/lib/pronto/git/patch.rb @@ -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 diff --git a/lib/pronto/rugged/commit.rb b/lib/pronto/rugged/commit.rb deleted file mode 100644 index 60f14bcc..00000000 --- a/lib/pronto/rugged/commit.rb +++ /dev/null @@ -1,8 +0,0 @@ -module Rugged - class Commit - def show - # TODO: Rugged does not seem to support diffing against multiple parents - diff(reverse: true) unless parents.count != 1 - end - end -end diff --git a/lib/pronto/rugged/diff/delta.rb b/lib/pronto/rugged/diff/delta.rb deleted file mode 100644 index 32f48624..00000000 --- a/lib/pronto/rugged/diff/delta.rb +++ /dev/null @@ -1,16 +0,0 @@ -require 'pathname' - -module Rugged - class Diff - class Delta - def repo - diff.tree.repo - end - - def new_file_full_path - repo_path = Pathname.new(repo.path).parent - repo_path.join(new_file[:path]) - end - end - end -end diff --git a/lib/pronto/rugged/diff/line.rb b/lib/pronto/rugged/diff/line.rb deleted file mode 100644 index 8b139c8b..00000000 --- a/lib/pronto/rugged/diff/line.rb +++ /dev/null @@ -1,61 +0,0 @@ -module Rugged - class Diff - class Line - def patch - hunk.delta - end - - def position - hunk_index = patch.hunks.find_index { |h| h.header == hunk.header } - line_index = patch.lines.find_index(self) - - 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 - diff = commit.show - patches = diff ? diff.patches : [] - 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 || self # no commit_line means that it was just added - end - end - - def ==(other) - content == other.content && - line_origin == other.line_origin && - old_lineno == other.old_lineno && - new_lineno == other.new_lineno - end - - private - - def repo - patch.diff.tree.repo - end - - def blame - @blame ||= 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 diff --git a/lib/pronto/rugged/patch.rb b/lib/pronto/rugged/patch.rb deleted file mode 100644 index 9727026b..00000000 --- a/lib/pronto/rugged/patch.rb +++ /dev/null @@ -1,33 +0,0 @@ -module Rugged - class Patch - def additions - stat[0] - end - - def deletions - stat[1] - end - - def added_lines - lines.select(&:addition?) - end - - def deleted_lines - lines.select(&:deletion?) - end - - def new_file_full_path - delta.new_file_full_path - end - - def lines - map do |hunk| - hunk.lines.map do |line| - # TODO: Remove this hack when regression is fixed - line.define_singleton_method(:hunk) { hunk } - line - end - end.flatten.compact - end - end -end diff --git a/spec/pronto/git/line_spec.rb b/spec/pronto/git/line_spec.rb new file mode 100644 index 00000000..2eb60913 --- /dev/null +++ b/spec/pronto/git/line_spec.rb @@ -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 diff --git a/spec/pronto/rugged/diff/line_spec.rb b/spec/pronto/rugged/diff/line_spec.rb index 984accb6..e69de29b 100644 --- a/spec/pronto/rugged/diff/line_spec.rb +++ b/spec/pronto/rugged/diff/line_spec.rb @@ -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