From d8c3ff89083287b9bd9f46e588e202997ca0ce47 Mon Sep 17 00:00:00 2001 From: Josh Hagins Date: Tue, 7 Apr 2015 20:50:05 -0400 Subject: [PATCH 01/18] Add method Utils.parent_command Returns the name of the command corresponding to the parent of the current process, which will be the calling git command when running hooks. --- lib/overcommit/utils.rb | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/lib/overcommit/utils.rb b/lib/overcommit/utils.rb index 540937f3..20bcfaf4 100644 --- a/lib/overcommit/utils.rb +++ b/lib/overcommit/utils.rb @@ -101,6 +101,11 @@ def in_path?(cmd) false end + # Return the parent command that triggered this hook run + def parent_command + `ps -ocommand= -p #{Process.ppid}`.chomp + end + # Execute a command in a subprocess, capturing exit status and output from # both standard and error streams. # From f30dd0cedbb7e9768fb1c74a895318e723a456d9 Mon Sep 17 00:00:00 2001 From: Josh Hagins Date: Tue, 7 Apr 2015 20:51:45 -0400 Subject: [PATCH 02/18] Add spec for Utils.parent_command --- spec/overcommit/utils_spec.rb | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/spec/overcommit/utils_spec.rb b/spec/overcommit/utils_spec.rb index 91bb32ef..215a1aee 100644 --- a/spec/overcommit/utils_spec.rb +++ b/spec/overcommit/utils_spec.rb @@ -121,6 +121,16 @@ # rubocop:enable Metrics/LineLength end + describe '.parent_command' do + subject { described_class.parent_command } + + before do + Process.stub(:ppid) { Process.pid } + end + + it { should =~ /rspec/ } + end + describe '.execute' do let(:arguments) { %w[echo -n Hello World] } subject { described_class.execute(arguments) } From b42eac48db90c85088c99a35c122e8eedc122658 Mon Sep 17 00:00:00 2001 From: Josh Hagins Date: Tue, 7 Apr 2015 20:52:08 -0400 Subject: [PATCH 03/18] Add helper method HookContext::PreCommit#amend? Returns true if the current hook run was initiated by amending a commit, false otherwise. --- lib/overcommit/hook_context/pre_commit.rb | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/lib/overcommit/hook_context/pre_commit.rb b/lib/overcommit/hook_context/pre_commit.rb index 289eaffe..53d15118 100644 --- a/lib/overcommit/hook_context/pre_commit.rb +++ b/lib/overcommit/hook_context/pre_commit.rb @@ -82,6 +82,15 @@ def modified_lines_in_file(file) Overcommit::GitRepo.extract_modified_lines(file, staged: true) end + # Returns whether this hook run was triggered by `git commit --amend` + def amend? + cmd = Overcommit::Utils.last_command + amend_alias = `git config --get-regexp '^alias\\.' '--amend'`. + slice(/(?<=alias\.)\w+/) + + (/--amend|git #{amend_alias}/ =~ cmd) != nil + end + private # Clears the working tree so that the stash can be applied. From fdb12303f40fe27941b0dbc15cdd9d8a846a993a Mon Sep 17 00:00:00 2001 From: Josh Hagins Date: Tue, 7 Apr 2015 20:53:07 -0400 Subject: [PATCH 04/18] Add specs for HookContext::PreCommit#amend? --- .../hook_context/pre_commit_spec.rb | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/spec/overcommit/hook_context/pre_commit_spec.rb b/spec/overcommit/hook_context/pre_commit_spec.rb index b1af2d0e..e098d44c 100644 --- a/spec/overcommit/hook_context/pre_commit_spec.rb +++ b/spec/overcommit/hook_context/pre_commit_spec.rb @@ -7,6 +7,41 @@ let(:input) { double('input') } let(:context) { described_class.new(config, args, input) } + describe '#amend?' do + subject { context.amend? } + + context 'when amending a commit using `git commit --amend`' do + before do + Overcommit::Utils.stub(:last_command).and_return('git commit --amend') + end + + it { should == true } + end + + context 'when amending a commit using a git alias' do + around do |example| + repo do + `git config alias.amend 'commit --amend'` + example.run + end + end + + before do + Overcommit::Utils.stub(:last_command).and_return('git amend') + end + + it { should == true } + end + + context 'when not amending a commit' do + before do + Overcommit::Utils.stub(:last_command).and_return('git commit') + end + + it { should == false } + end + end + describe '#setup_environment' do subject { context.setup_environment } From 21b2321fdf1a461cc7655baaabfcfb43f434441f Mon Sep 17 00:00:00 2001 From: Josh Hagins Date: Tue, 7 Apr 2015 21:22:31 -0400 Subject: [PATCH 05/18] Use Utils.parent_command, not last_command --- lib/overcommit/hook_context/pre_commit.rb | 2 +- spec/overcommit/hook_context/pre_commit_spec.rb | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/overcommit/hook_context/pre_commit.rb b/lib/overcommit/hook_context/pre_commit.rb index 53d15118..48a6f1fe 100644 --- a/lib/overcommit/hook_context/pre_commit.rb +++ b/lib/overcommit/hook_context/pre_commit.rb @@ -84,7 +84,7 @@ def modified_lines_in_file(file) # Returns whether this hook run was triggered by `git commit --amend` def amend? - cmd = Overcommit::Utils.last_command + cmd = Overcommit::Utils.parent_command amend_alias = `git config --get-regexp '^alias\\.' '--amend'`. slice(/(?<=alias\.)\w+/) diff --git a/spec/overcommit/hook_context/pre_commit_spec.rb b/spec/overcommit/hook_context/pre_commit_spec.rb index e098d44c..c63050a9 100644 --- a/spec/overcommit/hook_context/pre_commit_spec.rb +++ b/spec/overcommit/hook_context/pre_commit_spec.rb @@ -12,7 +12,7 @@ context 'when amending a commit using `git commit --amend`' do before do - Overcommit::Utils.stub(:last_command).and_return('git commit --amend') + Overcommit::Utils.stub(:parent_command).and_return('git commit --amend') end it { should == true } @@ -27,7 +27,7 @@ end before do - Overcommit::Utils.stub(:last_command).and_return('git amend') + Overcommit::Utils.stub(:parent_command).and_return('git amend') end it { should == true } @@ -35,7 +35,7 @@ context 'when not amending a commit' do before do - Overcommit::Utils.stub(:last_command).and_return('git commit') + Overcommit::Utils.stub(:parent_command).and_return('git commit') end it { should == false } From 55d41cf2edfb826526488b698cdb3051ef7925e4 Mon Sep 17 00:00:00 2001 From: Josh Hagins Date: Tue, 7 Apr 2015 21:24:10 -0400 Subject: [PATCH 06/18] Disable ClassLength warning for HookContext::PreCommit --- lib/overcommit/hook_context/pre_commit.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/overcommit/hook_context/pre_commit.rb b/lib/overcommit/hook_context/pre_commit.rb index 48a6f1fe..7fde1476 100644 --- a/lib/overcommit/hook_context/pre_commit.rb +++ b/lib/overcommit/hook_context/pre_commit.rb @@ -7,7 +7,7 @@ module Overcommit::HookContext # This includes staged files, which lines of those files have been modified, # etc. It is also responsible for saving/restoring the state of the repo so # hooks only inspect staged changes. - class PreCommit < Base + class PreCommit < Base # rubocop:disable ClassLength # Stash unstaged contents of files so hooks don't see changes that aren't # about to be committed. def setup_environment From 4796111d946ea6ee37311e271d11322448ec56c7 Mon Sep 17 00:00:00 2001 From: Josh Hagins Date: Tue, 7 Apr 2015 21:26:09 -0400 Subject: [PATCH 07/18] Move HookContext::PreCommit#amend? to top of class --- lib/overcommit/hook_context/pre_commit.rb | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/lib/overcommit/hook_context/pre_commit.rb b/lib/overcommit/hook_context/pre_commit.rb index 7fde1476..50640ed0 100644 --- a/lib/overcommit/hook_context/pre_commit.rb +++ b/lib/overcommit/hook_context/pre_commit.rb @@ -8,6 +8,15 @@ module Overcommit::HookContext # etc. It is also responsible for saving/restoring the state of the repo so # hooks only inspect staged changes. class PreCommit < Base # rubocop:disable ClassLength + # Returns whether this hook run was triggered by `git commit --amend` + def amend? + cmd = Overcommit::Utils.parent_command + amend_alias = `git config --get-regexp '^alias\\.' '--amend'`. + slice(/(?<=alias\.)\w+/) + + (/--amend|git #{amend_alias}/ =~ cmd) != nil + end + # Stash unstaged contents of files so hooks don't see changes that aren't # about to be committed. def setup_environment @@ -82,15 +91,6 @@ def modified_lines_in_file(file) Overcommit::GitRepo.extract_modified_lines(file, staged: true) end - # Returns whether this hook run was triggered by `git commit --amend` - def amend? - cmd = Overcommit::Utils.parent_command - amend_alias = `git config --get-regexp '^alias\\.' '--amend'`. - slice(/(?<=alias\.)\w+/) - - (/--amend|git #{amend_alias}/ =~ cmd) != nil - end - private # Clears the working tree so that the stash can be applied. From 005d67a9132a5a57820d7eab4b9b8bec33da7fc7 Mon Sep 17 00:00:00 2001 From: Josh Hagins Date: Tue, 7 Apr 2015 21:27:27 -0400 Subject: [PATCH 08/18] Cache HookContext::PreCommit#amend? value --- lib/overcommit/hook_context/pre_commit.rb | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/lib/overcommit/hook_context/pre_commit.rb b/lib/overcommit/hook_context/pre_commit.rb index 50640ed0..14ad3273 100644 --- a/lib/overcommit/hook_context/pre_commit.rb +++ b/lib/overcommit/hook_context/pre_commit.rb @@ -10,11 +10,16 @@ module Overcommit::HookContext class PreCommit < Base # rubocop:disable ClassLength # Returns whether this hook run was triggered by `git commit --amend` def amend? - cmd = Overcommit::Utils.parent_command - amend_alias = `git config --get-regexp '^alias\\.' '--amend'`. - slice(/(?<=alias\.)\w+/) + if @amended.nil? + cmd = Overcommit::Utils.parent_command + @amended = !(/--amend/ =~ cmd).nil? - (/--amend|git #{amend_alias}/ =~ cmd) != nil + amend_alias = `git config --get-regexp '^alias\\.' '--amend'`. + slice(/(?<=alias\.)\w+/) + + @amended ||= !(/git #{amend_alias}/ =~ cmd).nil? unless amend_alias.nil? + end + @amended end # Stash unstaged contents of files so hooks don't see changes that aren't From 30f45760db05c1a4a02e9fd9f37c8012bfd43c25 Mon Sep 17 00:00:00 2001 From: Josh Hagins Date: Tue, 7 Apr 2015 21:28:05 -0400 Subject: [PATCH 09/18] Include modifications from last commit if amending --- lib/overcommit/hook_context/pre_commit.rb | 25 ++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/lib/overcommit/hook_context/pre_commit.rb b/lib/overcommit/hook_context/pre_commit.rb index 14ad3273..10088c71 100644 --- a/lib/overcommit/hook_context/pre_commit.rb +++ b/lib/overcommit/hook_context/pre_commit.rb @@ -78,7 +78,16 @@ def cleanup_environment # Get a list of added, copied, or modified files that have been staged. # Renames and deletions are ignored, since there should be nothing to check. def modified_files - @modified_files ||= Overcommit::GitRepo.modified_files(staged: true) + unless @modified_files + @modified_files = Overcommit::GitRepo.modified_files(staged: true) + + # Include files modified in last commit if amending + if amend? + subcmd = 'show --format=%n' + @modified_files += Overcommit::GitRepo.modified_files(subcmd: subcmd) + end + end + @modified_files end # @deprecated @@ -92,8 +101,18 @@ def modified_lines(file) # changed in a specified file. def modified_lines_in_file(file) @modified_lines ||= {} - @modified_lines[file] ||= - Overcommit::GitRepo.extract_modified_lines(file, staged: true) + unless @modified_lines[file] + @modified_lines[file] = + Overcommit::GitRepo.extract_modified_lines(file, staged: true) + + # Include lines modified in last commit if amending + if amend? + subcmd = 'show --format=%n' + @modified_lines[file] += + Overcommit::GitRepo.extract_modified_lines(file, subcmd: subcmd) + end + end + @modified_lines[file] end private From 3eec82feb0f07f1a415a263d7c4872c5a7c80db4 Mon Sep 17 00:00:00 2001 From: Josh Hagins Date: Tue, 7 Apr 2015 21:46:38 -0400 Subject: [PATCH 10/18] Test #modified_files when amending last commit --- .../hook_context/pre_commit_spec.rb | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/spec/overcommit/hook_context/pre_commit_spec.rb b/spec/overcommit/hook_context/pre_commit_spec.rb index c63050a9..2cfa56fd 100644 --- a/spec/overcommit/hook_context/pre_commit_spec.rb +++ b/spec/overcommit/hook_context/pre_commit_spec.rb @@ -296,6 +296,10 @@ describe '#modified_files' do subject { context.modified_files } + before do + context.stub(:amend?).and_return(false) + end + it 'does not include submodules' do submodule = repo do `touch foo` @@ -359,5 +363,22 @@ it { should be_empty } end + + context 'when amending last commit' do + around do |example| + repo do + FileUtils.touch('some-file') + `git add some-file` + `git commit -m 'Initial commit'` + example.run + end + end + + before do + context.stub(:amend?).and_return(true) + end + + it { should == [File.expand_path('some-file')] } + end end end From b367eac55f01ebcb79c264bca46c471cdfd99894 Mon Sep 17 00:00:00 2001 From: Josh Hagins Date: Tue, 7 Apr 2015 21:47:00 -0400 Subject: [PATCH 11/18] Add specs for HookContext::PreCommit#modified_lines_in_file --- .../hook_context/pre_commit_spec.rb | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/spec/overcommit/hook_context/pre_commit_spec.rb b/spec/overcommit/hook_context/pre_commit_spec.rb index 2cfa56fd..16f6f1aa 100644 --- a/spec/overcommit/hook_context/pre_commit_spec.rb +++ b/spec/overcommit/hook_context/pre_commit_spec.rb @@ -381,4 +381,58 @@ it { should == [File.expand_path('some-file')] } end end + + describe '#modified_lines_in_file' do + let(:modified_file) { 'some-file' } + subject { context.modified_lines_in_file(modified_file) } + + before do + context.stub(:amend?).and_return(false) + end + + context 'when file contains a trailing newline' do + around do |example| + repo do + File.open(modified_file, 'w') { |f| (1..3).each { |i| f.write("#{i}\n") } } + `git add #{modified_file}` + example.run + end + end + + it { should == Set.new(1..3) } + end + + context 'when file does not contain a trailing newline' do + around do |example| + repo do + File.open(modified_file, 'w') do |f| + (1..2).each { |i| f.write("#{i}\n") } + f.write(3) + end + + `git add #{modified_file}` + example.run + end + end + + it { should == Set.new(1..3) } + end + + context 'when amending last commit' do + around do |example| + repo do + File.open(modified_file, 'w') { |f| (1..3).each { |i| f.write("#{i}\n") } } + `git add #{modified_file}` + `git commit -m "Add files"` + example.run + end + end + + before do + context.stub(:amend?).and_return(true) + end + + it { should == Set.new(1..3) } + end + end end From 68a659d2bfb2ba8be159e60ff06d53050f54a857 Mon Sep 17 00:00:00 2001 From: Josh Hagins Date: Wed, 8 Apr 2015 00:56:38 -0400 Subject: [PATCH 12/18] DRY up 'before' blocks in #amend? specs --- .../overcommit/hook_context/pre_commit_spec.rb | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/spec/overcommit/hook_context/pre_commit_spec.rb b/spec/overcommit/hook_context/pre_commit_spec.rb index 16f6f1aa..83b261bc 100644 --- a/spec/overcommit/hook_context/pre_commit_spec.rb +++ b/spec/overcommit/hook_context/pre_commit_spec.rb @@ -10,15 +10,19 @@ describe '#amend?' do subject { context.amend? } + before do + Overcommit::Utils.stub(:parent_command).and_return(command) + end + context 'when amending a commit using `git commit --amend`' do - before do - Overcommit::Utils.stub(:parent_command).and_return('git commit --amend') - end + let(:command) { 'git commit --amend' } it { should == true } end context 'when amending a commit using a git alias' do + let(:command) { 'git amend' } + around do |example| repo do `git config alias.amend 'commit --amend'` @@ -26,17 +30,11 @@ end end - before do - Overcommit::Utils.stub(:parent_command).and_return('git amend') - end - it { should == true } end context 'when not amending a commit' do - before do - Overcommit::Utils.stub(:parent_command).and_return('git commit') - end + let(:command) { 'git commit' } it { should == false } end From c7dfa9996b1be1f82b58c979368712f89e317542 Mon Sep 17 00:00:00 2001 From: Josh Hagins Date: Wed, 8 Apr 2015 00:57:15 -0400 Subject: [PATCH 13/18] Add spec for git alias containing '--amend' --- .../hook_context/pre_commit_spec.rb | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/spec/overcommit/hook_context/pre_commit_spec.rb b/spec/overcommit/hook_context/pre_commit_spec.rb index 83b261bc..7bc47da8 100644 --- a/spec/overcommit/hook_context/pre_commit_spec.rb +++ b/spec/overcommit/hook_context/pre_commit_spec.rb @@ -34,9 +34,24 @@ end context 'when not amending a commit' do - let(:command) { 'git commit' } + context 'using `git commit`' do + let(:command) { 'git commit' } - it { should == false } + it { should == false } + end + + context 'using a git alias containing "--amend"' do + let(:command) { 'git no--amend' } + + around do |example| + repo do + `git config alias.no--amend commit` + example.run + end + end + + it { should == false } + end end end From 1253bfd307162f02e3210a5f979d7aeb2772760d Mon Sep 17 00:00:00 2001 From: Josh Hagins Date: Wed, 8 Apr 2015 00:59:15 -0400 Subject: [PATCH 14/18] Rename #amend? to #amendment? --- lib/overcommit/hook_context/pre_commit.rb | 6 +++--- spec/overcommit/hook_context/pre_commit_spec.rb | 12 ++++++------ 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/lib/overcommit/hook_context/pre_commit.rb b/lib/overcommit/hook_context/pre_commit.rb index 10088c71..713ccfe1 100644 --- a/lib/overcommit/hook_context/pre_commit.rb +++ b/lib/overcommit/hook_context/pre_commit.rb @@ -9,7 +9,7 @@ module Overcommit::HookContext # hooks only inspect staged changes. class PreCommit < Base # rubocop:disable ClassLength # Returns whether this hook run was triggered by `git commit --amend` - def amend? + def amendment? if @amended.nil? cmd = Overcommit::Utils.parent_command @amended = !(/--amend/ =~ cmd).nil? @@ -82,7 +82,7 @@ def modified_files @modified_files = Overcommit::GitRepo.modified_files(staged: true) # Include files modified in last commit if amending - if amend? + if amendment? subcmd = 'show --format=%n' @modified_files += Overcommit::GitRepo.modified_files(subcmd: subcmd) end @@ -106,7 +106,7 @@ def modified_lines_in_file(file) Overcommit::GitRepo.extract_modified_lines(file, staged: true) # Include lines modified in last commit if amending - if amend? + if amendment? subcmd = 'show --format=%n' @modified_lines[file] += Overcommit::GitRepo.extract_modified_lines(file, subcmd: subcmd) diff --git a/spec/overcommit/hook_context/pre_commit_spec.rb b/spec/overcommit/hook_context/pre_commit_spec.rb index 7bc47da8..b32e6f3d 100644 --- a/spec/overcommit/hook_context/pre_commit_spec.rb +++ b/spec/overcommit/hook_context/pre_commit_spec.rb @@ -7,8 +7,8 @@ let(:input) { double('input') } let(:context) { described_class.new(config, args, input) } - describe '#amend?' do - subject { context.amend? } + describe '#amendment?' do + subject { context.amendment? } before do Overcommit::Utils.stub(:parent_command).and_return(command) @@ -310,7 +310,7 @@ subject { context.modified_files } before do - context.stub(:amend?).and_return(false) + context.stub(:amendment?).and_return(false) end it 'does not include submodules' do @@ -388,7 +388,7 @@ end before do - context.stub(:amend?).and_return(true) + context.stub(:amendment?).and_return(true) end it { should == [File.expand_path('some-file')] } @@ -400,7 +400,7 @@ subject { context.modified_lines_in_file(modified_file) } before do - context.stub(:amend?).and_return(false) + context.stub(:amendment?).and_return(false) end context 'when file contains a trailing newline' do @@ -442,7 +442,7 @@ end before do - context.stub(:amend?).and_return(true) + context.stub(:amendment?).and_return(true) end it { should == Set.new(1..3) } From 808b7d92e4afb7dcb9f7eeb18e3cb5ec3888ef1a Mon Sep 17 00:00:00 2001 From: Josh Hagins Date: Wed, 8 Apr 2015 01:38:56 -0400 Subject: [PATCH 15/18] Clean up #amendment? and add comments for clarity --- lib/overcommit/hook_context/pre_commit.rb | 27 +++++++++++++++-------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/lib/overcommit/hook_context/pre_commit.rb b/lib/overcommit/hook_context/pre_commit.rb index 713ccfe1..d717a6de 100644 --- a/lib/overcommit/hook_context/pre_commit.rb +++ b/lib/overcommit/hook_context/pre_commit.rb @@ -10,16 +10,25 @@ module Overcommit::HookContext class PreCommit < Base # rubocop:disable ClassLength # Returns whether this hook run was triggered by `git commit --amend` def amendment? - if @amended.nil? - cmd = Overcommit::Utils.parent_command - @amended = !(/--amend/ =~ cmd).nil? - - amend_alias = `git config --get-regexp '^alias\\.' '--amend'`. - slice(/(?<=alias\.)\w+/) + return @amendment unless @amendment.nil? + + cmd = Overcommit::Utils.parent_command + amend_pattern = 'commit(\s.*)?\s--amend(\s|$)' + + return @amendment if + # True if the command is a commit with the --amend flag + @amendment = !(/\s#{amend_pattern}/ =~ cmd).nil? + + # Check for git aliases that call `commit --amend` + `git config --get-regexp '^alias\\.' '#{amend_pattern}'`. + scan(/alias\.([-\w]+)/). # Extract the alias + each do |amend_alias| + return @amendment if + # True if the command uses a git alias for `commit --amend` + @amendment = !(/git\s+#{amend_alias}/ =~ cmd).nil? + end - @amended ||= !(/git #{amend_alias}/ =~ cmd).nil? unless amend_alias.nil? - end - @amended + @amendment end # Stash unstaged contents of files so hooks don't see changes that aren't From 735545ea6687b0ac2160e7dfe3c956f93057bb53 Mon Sep 17 00:00:00 2001 From: Josh Hagins Date: Wed, 8 Apr 2015 01:52:43 -0400 Subject: [PATCH 16/18] Add spec for multiple git aliases that call 'commit --amend' --- spec/overcommit/hook_context/pre_commit_spec.rb | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/spec/overcommit/hook_context/pre_commit_spec.rb b/spec/overcommit/hook_context/pre_commit_spec.rb index b32e6f3d..79dbbb6d 100644 --- a/spec/overcommit/hook_context/pre_commit_spec.rb +++ b/spec/overcommit/hook_context/pre_commit_spec.rb @@ -21,16 +21,25 @@ end context 'when amending a commit using a git alias' do - let(:command) { 'git amend' } - around do |example| repo do `git config alias.amend 'commit --amend'` + `git config alias.other-amend 'commit --amend'` example.run end end - it { should == true } + context 'when using one of multiple aliases' do + let(:command) { 'git amend' } + + it { should == true } + end + + context 'when using another of multiple aliases' do + let(:command) { 'git other-amend' } + + it { should == true } + end end context 'when not amending a commit' do From fbd04dae3967a625f58598349d8d4e1d6537693b Mon Sep 17 00:00:00 2001 From: Josh Hagins Date: Wed, 8 Apr 2015 01:53:57 -0400 Subject: [PATCH 17/18] Extract alias from match array --- lib/overcommit/hook_context/pre_commit.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/overcommit/hook_context/pre_commit.rb b/lib/overcommit/hook_context/pre_commit.rb index d717a6de..c7c82ec7 100644 --- a/lib/overcommit/hook_context/pre_commit.rb +++ b/lib/overcommit/hook_context/pre_commit.rb @@ -22,10 +22,10 @@ def amendment? # Check for git aliases that call `commit --amend` `git config --get-regexp '^alias\\.' '#{amend_pattern}'`. scan(/alias\.([-\w]+)/). # Extract the alias - each do |amend_alias| + each do |match| return @amendment if # True if the command uses a git alias for `commit --amend` - @amendment = !(/git\s+#{amend_alias}/ =~ cmd).nil? + @amendment = !(/git\s+#{match[0]}/ =~ cmd).nil? end @amendment From d2dacb74b16f57e62c271a0fa4b108f8b35e25a5 Mon Sep 17 00:00:00 2001 From: Josh Hagins Date: Wed, 8 Apr 2015 02:00:56 -0400 Subject: [PATCH 18/18] Check that result sets are merged when amending --- spec/overcommit/hook_context/pre_commit_spec.rb | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/spec/overcommit/hook_context/pre_commit_spec.rb b/spec/overcommit/hook_context/pre_commit_spec.rb index 79dbbb6d..0d4cd8fb 100644 --- a/spec/overcommit/hook_context/pre_commit_spec.rb +++ b/spec/overcommit/hook_context/pre_commit_spec.rb @@ -392,6 +392,8 @@ FileUtils.touch('some-file') `git add some-file` `git commit -m 'Initial commit'` + FileUtils.touch('other-file') + `git add other-file` example.run end end @@ -400,7 +402,7 @@ context.stub(:amendment?).and_return(true) end - it { should == [File.expand_path('some-file')] } + it { should =~ [File.expand_path('some-file'), File.expand_path('other-file')] } end end @@ -446,6 +448,8 @@ File.open(modified_file, 'w') { |f| (1..3).each { |i| f.write("#{i}\n") } } `git add #{modified_file}` `git commit -m "Add files"` + File.open(modified_file, 'a') { |f| f.puts 4 } + `git add #{modified_file}` example.run end end @@ -454,7 +458,7 @@ context.stub(:amendment?).and_return(true) end - it { should == Set.new(1..3) } + it { should == Set.new(1..4) } end end end