Skip to content

Commit

Permalink
Extract Git::Patches, Git::Repository and Git::Remote
Browse files Browse the repository at this point in the history
  • Loading branch information
mmozuras committed Sep 10, 2014
1 parent 90f5bb7 commit 064f42f
Show file tree
Hide file tree
Showing 16 changed files with 121 additions and 60 deletions.
20 changes: 11 additions & 9 deletions lib/pronto.rb
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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)
Expand All @@ -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

Expand All @@ -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

Expand Down
17 changes: 3 additions & 14 deletions lib/pronto/git/line.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand All @@ -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]
Expand Down
18 changes: 18 additions & 0 deletions lib/pronto/git/patches.rb
Original file line number Diff line number Diff line change
@@ -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
10 changes: 10 additions & 0 deletions lib/pronto/git/remote.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module Pronto
module Git
class Remote < Struct.new(:remote)
def github_slug
match = /.*github.com(:|\/)(?<slug>.*).git/.match(remote.url)
match[:slug] if match
end
end
end
end
50 changes: 50 additions & 0 deletions lib/pronto/git/repository.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion lib/pronto/message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
8 changes: 0 additions & 8 deletions lib/pronto/rugged/remote.rb

This file was deleted.

1 change: 0 additions & 1 deletion spec/pronto/formatter/github_formattter_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
require 'spec_helper'
require 'ostruct'

module Pronto
module Formatter
Expand Down
1 change: 0 additions & 1 deletion spec/pronto/formatter/json_formatter_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
require 'spec_helper'
require 'ostruct'

module Pronto
module Formatter
Expand Down
1 change: 0 additions & 1 deletion spec/pronto/formatter/text_formatter_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
require 'spec_helper'
require 'ostruct'

module Pronto
module Formatter
Expand Down
2 changes: 1 addition & 1 deletion spec/pronto/git/line_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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] }

Expand Down
23 changes: 23 additions & 0 deletions spec/pronto/git/remote_spec.rb
Original file line number Diff line number Diff line change
@@ -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) { '[email protected]: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
Empty file.
21 changes: 0 additions & 21 deletions spec/pronto/rugged/remote_spec.rb

This file was deleted.

4 changes: 2 additions & 2 deletions spec/pronto_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@
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

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
Expand Down
3 changes: 2 additions & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require 'rspec'
require 'rspec/its'
require 'pry'
require 'ostruct'

require 'pronto'

Expand All @@ -10,7 +11,7 @@
end

def repository
Rugged::Repository.init_at('.')
Pronto::Git::Repository.new('.')
end

def load_fixture(fixture_name)
Expand Down

0 comments on commit 064f42f

Please sign in to comment.