-
Notifications
You must be signed in to change notification settings - Fork 280
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix AuthorName/AuthorEmail to respect env variables
When committing, the `GIT_AUTHOR_{NAME,EMAIL}` environment variables take precedence over Git configuration values. Ensure these are recognized by the hook. This required a change to the commit integration spec since GIT_AUTHOR_NAME/GIT_AUTHOR_EMAIL is set by git before it executes the hook, so even if you set the local configuration to an empty string it will fall back to using the global configuration.
- Loading branch information
Showing
6 changed files
with
101 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,14 +2,20 @@ module Overcommit::Hook::PreCommit | |
# Checks the format of an author's email address. | ||
class AuthorEmail < Base | ||
def run | ||
result = execute(%w[git config --get user.email]) | ||
email = result.stdout.chomp | ||
email = | ||
if ENV.key?('GIT_AUTHOR_EMAIL') | ||
ENV['GIT_AUTHOR_EMAIL'] | ||
else | ||
result = execute(%w[git config --get user.email]) | ||
result.stdout.chomp | ||
end | ||
|
||
unless email =~ /#{config['pattern']}/ | ||
return :fail, | ||
"Author has an invalid email address: '#{email}'\n" \ | ||
'Set your email with ' \ | ||
'`git config --global user.email [email protected]`' | ||
'`git config --global user.email [email protected]` ' \ | ||
'or via the GIT_AUTHOR_EMAIL environment variable' | ||
end | ||
|
||
:pass | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,50 +6,66 @@ | |
subject { described_class.new(config, context) } | ||
let(:result) { double('result') } | ||
|
||
before do | ||
result.stub(:stdout).and_return(email) | ||
subject.stub(:execute).and_return(result) | ||
end | ||
|
||
context 'when user has no email' do | ||
let(:email) { '' } | ||
shared_examples_for 'author email check' do | ||
context 'when user has no email' do | ||
let(:email) { '' } | ||
|
||
it { should fail_hook } | ||
end | ||
it { should fail_hook } | ||
end | ||
|
||
context 'when user has an invalid email' do | ||
let(:email) { 'Invalid Email' } | ||
context 'when user has an invalid email' do | ||
let(:email) { 'Invalid Email' } | ||
|
||
it { should fail_hook } | ||
end | ||
it { should fail_hook } | ||
end | ||
|
||
context 'when user has a valid email' do | ||
let(:email) { '[email protected]' } | ||
context 'when user has a valid email' do | ||
let(:email) { '[email protected]' } | ||
|
||
it { should pass } | ||
end | ||
it { should pass } | ||
end | ||
|
||
context 'when a custom pattern is specified' do | ||
let(:config) do | ||
super().merge(Overcommit::Configuration.new( | ||
'PreCommit' => { | ||
'AuthorEmail' => { | ||
'pattern' => '^[^@]+@brigade\.com$' | ||
context 'when a custom pattern is specified' do | ||
let(:config) do | ||
super().merge(Overcommit::Configuration.new( | ||
'PreCommit' => { | ||
'AuthorEmail' => { | ||
'pattern' => '^[^@]+@brigade\.com$' | ||
} | ||
} | ||
} | ||
)) | ||
end | ||
)) | ||
end | ||
|
||
context 'and the email does not match the pattern' do | ||
let(:email) { '[email protected]' } | ||
context 'and the email does not match the pattern' do | ||
let(:email) { '[email protected]' } | ||
|
||
it { should fail_hook } | ||
it { should fail_hook } | ||
end | ||
|
||
context 'and the email matches the pattern' do | ||
let(:email) { '[email protected]' } | ||
|
||
it { should pass } | ||
end | ||
end | ||
end | ||
|
||
context 'and the email matches the pattern' do | ||
let(:email) { '[email protected]' } | ||
context 'when email is set via config' do | ||
before do | ||
result.stub(:stdout).and_return(email) | ||
subject.stub(:execute).and_return(result) | ||
end | ||
|
||
it { should pass } | ||
it_should_behave_like 'author email check' | ||
end | ||
|
||
context 'when email is set via environment variable' do | ||
around do |example| | ||
Overcommit::Utils.with_environment 'GIT_AUTHOR_EMAIL' => email do | ||
example.run | ||
end | ||
end | ||
|
||
it_should_behave_like 'author email check' | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters