diff --git a/lib/pronto.rb b/lib/pronto.rb index fc17346a..ff21c938 100644 --- a/lib/pronto.rb +++ b/lib/pronto.rb @@ -1,7 +1,10 @@ require 'rugged' + +require 'pronto/git/repository' +require 'pronto/git/patches' require 'pronto/git/patch' require 'pronto/git/line' -require 'pronto/rugged/remote' +require 'pronto/git/remote' require 'pronto/plugin' require 'pronto/message' @@ -15,13 +18,12 @@ module Pronto def self.run(commit = 'master', repo_path = '.', formatter = nil) - repo = Rugged::Repository.new(repo_path) commit ||= 'master' - merge_base = repo.merge_base(commit, 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) + repo = Git::Repository.new(repo_path) + patches = repo.diff(commit) + + result = run_all_runners(patches) formatter ||= default_formatter formatter.format(result, repo) @@ -33,7 +35,7 @@ def self.gem_names true elsif gem.name != 'pronto' runner_path = File.join(gem.full_gem_path, "lib/pronto/#{gem.name}.rb") - File.exists?(runner_path) + File.exist?(runner_path) end end @@ -44,9 +46,9 @@ def self.gem_names private - def self.run_all_runners(patches, commit) + def self.run_all_runners(patches) Runner.runners.map do |runner| - runner.new.run(patches, commit) + runner.new.run(patches, patches.commit) end.flatten.compact end diff --git a/lib/pronto/git/line.rb b/lib/pronto/git/line.rb index 841ca27d..aaae7309 100644 --- a/lib/pronto/git/line.rb +++ b/lib/pronto/git/line.rb @@ -32,24 +32,13 @@ def position 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) } + patches = repo.show_commit(commit_sha) commit_patch = patches.find do |p| patch.new_file_full_path == p.new_file_full_path @@ -58,7 +47,7 @@ def commit_line 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 + result || self # no commit_line means that it was just added end end @@ -76,7 +65,7 @@ def repo end def blame - @blame ||= Rugged::Blame.new(repo, patch.delta.new_file[:path], + @blame ||= Rugged::Blame.new(repo.rugged, 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] diff --git a/lib/pronto/git/patches.rb b/lib/pronto/git/patches.rb new file mode 100644 index 00000000..041b6cb4 --- /dev/null +++ b/lib/pronto/git/patches.rb @@ -0,0 +1,18 @@ +module Pronto + module Git + class Patches + include Enumerable + + attr_reader :commit, :repo + + def initialize(repo, commit, patches) + @commit = commit + @patches = patches.map { |patch| Git::Patch.new(patch, repo) } + end + + def each(&block) + @patches.each(&block) + end + end + end +end diff --git a/lib/pronto/git/remote.rb b/lib/pronto/git/remote.rb new file mode 100644 index 00000000..d837e837 --- /dev/null +++ b/lib/pronto/git/remote.rb @@ -0,0 +1,10 @@ +module Pronto + module Git + class Remote < Struct.new(:remote) + def github_slug + match = /.*github.com(:|\/)(?.*).git/.match(remote.url) + match[:slug] if match + end + end + end +end diff --git a/lib/pronto/git/repository.rb b/lib/pronto/git/repository.rb new file mode 100644 index 00000000..4d76cd67 --- /dev/null +++ b/lib/pronto/git/repository.rb @@ -0,0 +1,50 @@ +module Pronto + module Git + class Repository + def initialize(path) + @repo = Rugged::Repository.new(path) + end + + def remotes + @repo.remotes.map { |remote| Remote.new(remote) } + end + + def diff(commit) + merge_base = merge_base(commit) + patches = @repo.diff(merge_base, head) + Patches.new(self, merge_base, patches) + end + + def show_commit(sha) + return [] unless sha + + commit = @repo.lookup(sha) + return [] if commit.parents.count != 1 + + # TODO: Rugged does not seem to support diffing against multiple parents + diff = commit.diff(reverse: true) + return [] if diff.nil? + + Patches.new(self, sha, diff.patches) + end + + def path + @repo.path + end + + def rugged + @repo + end + + private + + def merge_base(commit) + @repo.merge_base(commit, head) + end + + def head + @repo.head.target + end + end + end +end diff --git a/lib/pronto/message.rb b/lib/pronto/message.rb index 33dbe362..a8fcafa6 100644 --- a/lib/pronto/message.rb +++ b/lib/pronto/message.rb @@ -18,7 +18,7 @@ def initialize(path, line, level, msg, commit_sha = nil) end def repo - line.patch.delta.repo if line + line.patch.repo if line end end end diff --git a/lib/pronto/rugged/remote.rb b/lib/pronto/rugged/remote.rb deleted file mode 100644 index 4f05cddd..00000000 --- a/lib/pronto/rugged/remote.rb +++ /dev/null @@ -1,8 +0,0 @@ -module Rugged - class Remote - def github_slug - match = /.*github.com(:|\/)(?.*).git/.match(url) - match[:slug] if match - end - end -end diff --git a/spec/pronto/formatter/github_formattter_spec.rb b/spec/pronto/formatter/github_formattter_spec.rb index 0c9f10b6..faf5759a 100644 --- a/spec/pronto/formatter/github_formattter_spec.rb +++ b/spec/pronto/formatter/github_formattter_spec.rb @@ -1,5 +1,4 @@ require 'spec_helper' -require 'ostruct' module Pronto module Formatter diff --git a/spec/pronto/formatter/json_formatter_spec.rb b/spec/pronto/formatter/json_formatter_spec.rb index bb12b3f3..bf2498b9 100644 --- a/spec/pronto/formatter/json_formatter_spec.rb +++ b/spec/pronto/formatter/json_formatter_spec.rb @@ -1,5 +1,4 @@ require 'spec_helper' -require 'ostruct' module Pronto module Formatter diff --git a/spec/pronto/formatter/text_formatter_spec.rb b/spec/pronto/formatter/text_formatter_spec.rb index a921d13a..e4912e04 100644 --- a/spec/pronto/formatter/text_formatter_spec.rb +++ b/spec/pronto/formatter/text_formatter_spec.rb @@ -1,5 +1,4 @@ require 'spec_helper' -require 'ostruct' module Pronto module Formatter diff --git a/spec/pronto/git/line_spec.rb b/spec/pronto/git/line_spec.rb index 2eb60913..d8637f49 100644 --- a/spec/pronto/git/line_spec.rb +++ b/spec/pronto/git/line_spec.rb @@ -3,7 +3,7 @@ module Pronto module Git describe Line do - let(:diff) { repository.diff('88558b7', '88558b7~5') } + let(:diff) { repository.rugged.diff('88558b7', '88558b7~5') } let(:patch) { Patch.new(diff.patches.last, repository) } let(:line) { patch.lines[2] } diff --git a/spec/pronto/git/remote_spec.rb b/spec/pronto/git/remote_spec.rb new file mode 100644 index 00000000..e2452df4 --- /dev/null +++ b/spec/pronto/git/remote_spec.rb @@ -0,0 +1,23 @@ +require 'spec_helper' + +module Pronto + module Git + describe Remote do + let(:remote) { Remote.new(double(url: url)) } + + describe '#github_slug' do + subject { remote.github_slug } + + context 'ssh' do + let(:url) { 'git@github.com:mmozuras/pronto.git' } + it { should == 'mmozuras/pronto' } + end + + context 'http' do + let(:url) { 'https://github.com/mmozuras/pronto.git' } + it { should == 'mmozuras/pronto' } + end + end + end + end +end diff --git a/spec/pronto/rugged/diff/line_spec.rb b/spec/pronto/rugged/diff/line_spec.rb deleted file mode 100644 index e69de29b..00000000 diff --git a/spec/pronto/rugged/remote_spec.rb b/spec/pronto/rugged/remote_spec.rb deleted file mode 100644 index fe80e27b..00000000 --- a/spec/pronto/rugged/remote_spec.rb +++ /dev/null @@ -1,21 +0,0 @@ -require 'spec_helper' - -module Rugged - describe Remote do - let(:remote) { repository.remotes.create_anonymous(url) } - - describe '#github_slug' do - subject { remote.github_slug } - - context 'ssh' do - let(:url) { 'git@github.com:mmozuras/pronto.git' } - it { should == 'mmozuras/pronto' } - end - - context 'http' do - let(:url) { 'https://github.com/mmozuras/pronto.git' } - it { should == 'mmozuras/pronto' } - end - end - end -end diff --git a/spec/pronto_spec.rb b/spec/pronto_spec.rb index b9c2e0b5..5cf0f294 100644 --- a/spec/pronto_spec.rb +++ b/spec/pronto_spec.rb @@ -21,7 +21,7 @@ context 'with good path' do let(:gems) { [double(name: 'good', full_gem_path: '/good')] } before do - File.stub(:exists?).with('/good/lib/pronto/good.rb').and_return(true) + File.stub(:exist?).with('/good/lib/pronto/good.rb').and_return(true) end it { should include('good') } end @@ -29,7 +29,7 @@ context 'with bad path' do let(:gems) { [double(name: 'bad', full_gem_path: '/bad')] } before do - File.stub(:exists?).with('/bad/lib/pronto/bad.rb').and_return(false) + File.stub(:exist?).with('/bad/lib/pronto/bad.rb').and_return(false) end it { should_not include('bad') } end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index b2e08334..dcede2e2 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,6 +1,7 @@ require 'rspec' require 'rspec/its' require 'pry' +require 'ostruct' require 'pronto' @@ -10,7 +11,7 @@ end def repository - Rugged::Repository.init_at('.') + Pronto::Git::Repository.new('.') end def load_fixture(fixture_name)