Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bin/overcommit
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ require 'overcommit/cli'

logger = Overcommit::Logger.new(STDOUT)

Overcommit::CLI.new(ARGV, logger).run
Overcommit::CLI.new(ARGV, STDIN, logger).run
8 changes: 5 additions & 3 deletions lib/overcommit/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ module Overcommit
# Responsible for parsing command-line options and executing appropriate
# application logic based on those options.
class CLI # rubocop:disable ClassLength
def initialize(arguments, logger)
def initialize(arguments, input, logger)
@arguments = arguments
@input = input
@log = logger
@options = {}
end
Expand Down Expand Up @@ -177,7 +178,8 @@ def sign_plugins
config = Overcommit::ConfigurationLoader.load_repo_config
context = Overcommit::HookContext.create(@options[:hook_to_sign],
config,
@arguments)
@arguments,
@input)
Overcommit::HookLoader::PluginHookLoader.new(config,
context,
log).update_signatures
Expand All @@ -186,7 +188,7 @@ def sign_plugins

def run_all
config = Overcommit::ConfigurationLoader.load_repo_config
context = Overcommit::HookContext.create('run-all', config, @arguments)
context = Overcommit::HookContext.create('run-all', config, @arguments, @input)
config.apply_environment!(context, ENV)

printer = Overcommit::Printer.new(log, context)
Expand Down
10 changes: 10 additions & 0 deletions lib/overcommit/hook/pre_push/base.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
require 'forwardable'

module Overcommit::Hook::PrePush
# Functionality common to all pre-push hooks.
class Base < Overcommit::Hook::Base
extend Forwardable

def_delegators :@context, :remote_name, :remote_url, :pushed_commits
end
end
4 changes: 2 additions & 2 deletions lib/overcommit/hook_context.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# Utility module which manages the creation of {HookContext}s.
module Overcommit::HookContext
def self.create(hook_type, config, args)
def self.create(hook_type, config, args, input)
hook_type_class = Overcommit::Utils.camel_case(hook_type)
underscored_hook_type = Overcommit::Utils.snake_case(hook_type)

require "overcommit/hook_context/#{underscored_hook_type}"

Overcommit::HookContext.const_get(hook_type_class).new(config, args)
Overcommit::HookContext.const_get(hook_type_class).new(config, args, input)
rescue LoadError, NameError => error
# Could happen when a symlink was created for a hook type Overcommit does
# not yet support.
Expand Down
8 changes: 7 additions & 1 deletion lib/overcommit/hook_context/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ module Overcommit::HookContext
class Base
# @param config [Overcommit::Configuration]
# @param args [Array<String>]
def initialize(config, args)
def initialize(config, args, input)
@config = config
@args = args
@input = input
end

# Returns the camel-cased type of this hook (e.g. PreCommit)
Expand Down Expand Up @@ -65,5 +66,10 @@ def modified_files
def modified_lines(_file)
Set.new
end

# Returns an array of lines passed to the hook via STDIN.
def input_lines
@input_lines ||= @input.read.split("\n")
end
end
end
26 changes: 26 additions & 0 deletions lib/overcommit/hook_context/pre_push.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
module Overcommit::HookContext
# Contains helpers related to contextual information used by pre-push hooks.
class PrePush < Base
attr_accessor :args

def remote_name
@args[0]
end

def remote_url
@args[1]
end

def pushed_commits
input_lines.map do |line|
PushedCommit.new(*line.split(' '))
end
end

PushedCommit = Struct.new(:local_ref, :local_sha1, :remote_ref, :remote_sha1) do
def to_s
"#{local_ref} #{local_sha1} #{remote_ref} #{remote_sha1}"
end
end
end
end
3 changes: 2 additions & 1 deletion spec/overcommit/cli_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
describe Overcommit::CLI do
describe '#run' do
let(:logger) { Overcommit::Logger.silent }
let(:cli) { described_class.new(arguments, logger) }
let(:input) { double('input') }
let(:cli) { described_class.new(arguments, input, logger) }
subject { cli.run }

before do
Expand Down
13 changes: 12 additions & 1 deletion spec/overcommit/hook_context/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
describe Overcommit::HookContext::Base do
let(:config) { double('config') }
let(:args) { [] }
let(:context) { described_class.new(config, args) }
let(:input) { double('input') }
let(:context) { described_class.new(config, args, input) }

describe '#hook_class_name' do
subject { context.hook_class_name }
Expand All @@ -12,4 +13,14 @@
subject.should == 'Base'
end
end

describe '#input_lines' do
subject { context.input_lines }

before do
input.stub(:read).and_return("line 1\nline 2\n")
end

it { should == ['line 1', 'line 2'] }
end
end
3 changes: 2 additions & 1 deletion spec/overcommit/hook_context/commit_msg_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
describe Overcommit::HookContext::CommitMsg do
let(:config) { double('config') }
let(:args) { [commit_message_file] }
let(:context) { described_class.new(config, args) }
let(:input) { double('input') }
let(:context) { described_class.new(config, args, input) }
let(:commit_msg) do
[
'# Please enter the commit message for your changes.',
Expand Down
3 changes: 2 additions & 1 deletion spec/overcommit/hook_context/post_checkout_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
describe Overcommit::HookContext::PostCheckout do
let(:config) { double('config') }
let(:args) { [previous_head, new_head, branch_flag] }
let(:input) { double('input') }
let(:previous_head) { random_hash }
let(:new_head) { random_hash }
let(:branch_flag) { '1' }
let(:context) { described_class.new(config, args) }
let(:context) { described_class.new(config, args, input) }

describe '#previous_head' do
subject { context.previous_head }
Expand Down
3 changes: 2 additions & 1 deletion spec/overcommit/hook_context/post_commit_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
describe Overcommit::HookContext::PostCommit do
let(:config) { double('config') }
let(:args) { [] }
let(:context) { described_class.new(config, args) }
let(:input) { double('input') }
let(:context) { described_class.new(config, args, input) }

describe '#modified_files' do
subject { context.modified_files }
Expand Down
3 changes: 2 additions & 1 deletion spec/overcommit/hook_context/post_merge_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
describe Overcommit::HookContext::PostMerge do
let(:config) { double('config') }
let(:args) { [] }
let(:context) { described_class.new(config, args) }
let(:input) { double('input') }
let(:context) { described_class.new(config, args, input) }

describe '#squash?' do
subject { context.squash? }
Expand Down
3 changes: 2 additions & 1 deletion spec/overcommit/hook_context/post_rewrite_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@

describe Overcommit::HookContext::PostRewrite do
let(:config) { double('config') }
let(:context) { described_class.new(config, args) }
let(:input) { double('input') }
let(:context) { described_class.new(config, args, input) }

describe '#amend?' do
subject { context.amend? }
Expand Down
3 changes: 2 additions & 1 deletion spec/overcommit/hook_context/pre_commit_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
describe Overcommit::HookContext::PreCommit do
let(:config) { double('config') }
let(:args) { [] }
let(:context) { described_class.new(config, args) }
let(:input) { double('input') }
let(:context) { described_class.new(config, args, input) }

describe '#setup_environment' do
subject { context.setup_environment }
Expand Down
46 changes: 46 additions & 0 deletions spec/overcommit/hook_context/pre_push_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
require 'spec_helper'
require 'overcommit/hook_context/pre_push'

describe Overcommit::HookContext::PrePush do
let(:config) { double('config') }
let(:args) { [remote_name, remote_url] }
let(:input) { double('input') }
let(:remote_name) { 'origin' }
let(:remote_url) { 'git@github.com:brigade/overcommit.git' }
let(:context) { described_class.new(config, args, input) }

describe '#remote_name' do
subject { context.remote_name }

it { should == remote_name }
end

describe '#remote_url' do
subject { context.remote_url }

it { should == remote_url }
end

describe '#pushed_commits' do
subject(:pushed_commits) { context.pushed_commits }

let(:local_ref) { 'refs/heads/master' }
let(:local_sha1) { random_hash }
let(:remote_ref) { 'refs/heads/master' }
let(:remote_sha1) { random_hash }

before do
input.stub(:read).and_return("#{local_ref} #{local_sha1} #{remote_ref} #{remote_sha1}\n")
end

it 'should parse commit info from the input' do
pushed_commits.length.should == 1
pushed_commits.each do |pushed_commit|
pushed_commit.local_ref.should == local_ref
pushed_commit.local_sha1.should == local_sha1
pushed_commit.remote_ref.should == remote_ref
pushed_commit.remote_sha1.should == remote_sha1
end
end
end
end
3 changes: 2 additions & 1 deletion spec/overcommit/hook_context/run_all_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
describe Overcommit::HookContext::RunAll do
let(:config) { double('config') }
let(:args) { [] }
let(:context) { described_class.new(config, args) }
let(:input) { double('input') }
let(:context) { described_class.new(config, args, input) }

describe '#modified_files' do
subject { context.modified_files }
Expand Down
8 changes: 6 additions & 2 deletions spec/overcommit/utils_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,17 @@
describe '.supported_hook_types' do
subject { described_class.supported_hook_types }

it { should =~ %w[commit-msg pre-commit post-checkout post-commit post-merge post-rewrite] }
# rubocop:disable Metrics/LineLength
it { should =~ %w[commit-msg pre-commit post-checkout post-commit post-merge post-rewrite pre-push] }
# rubocop:enable Metrics/LineLength
end

describe '.supported_hook_type_classes' do
subject { described_class.supported_hook_type_classes }

it { should =~ %w[CommitMsg PreCommit PostCheckout PostCommit PostMerge PostRewrite] }
# rubocop:disable Metrics/LineLength
it { should =~ %w[CommitMsg PreCommit PostCheckout PostCommit PostMerge PostRewrite PrePush] }
# rubocop:enable Metrics/LineLength
end

describe '.execute' do
Expand Down
2 changes: 1 addition & 1 deletion template-dir/hooks/overcommit-hook
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ begin

config = Overcommit::ConfigurationLoader.load_repo_config

context = Overcommit::HookContext.create(hook_type, config, ARGV)
context = Overcommit::HookContext.create(hook_type, config, ARGV, STDIN)
config.apply_environment!(context, ENV)

printer = Overcommit::Printer.new(logger, context)
Expand Down
1 change: 1 addition & 0 deletions template-dir/hooks/pre-push