-
Notifications
You must be signed in to change notification settings - Fork 280
Include changes in last commit when running pre-commit hooks after git commit --amend
#167
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
d8c3ff8
f30dd0c
b42eac4
fdb1230
21b2321
55d41cf
4796111
005d67a
30f4576
3eec82f
b367eac
68a659d
c7dfa99
1253bfd
808b7d9
735545e
fbd04da
d2dacb7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,6 +7,63 @@ | |
| let(:input) { double('input') } | ||
| let(:context) { described_class.new(config, args, input) } | ||
|
|
||
| describe '#amendment?' do | ||
| subject { context.amendment? } | ||
|
|
||
| before do | ||
| Overcommit::Utils.stub(:parent_command).and_return(command) | ||
| end | ||
|
|
||
| context 'when amending a commit using `git commit --amend`' do | ||
| let(:command) { 'git commit --amend' } | ||
|
|
||
| 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'` | ||
| `git config alias.other-amend 'commit --amend'` | ||
| example.run | ||
| end | ||
| end | ||
|
|
||
| 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 | ||
| context 'using `git commit`' do | ||
| let(:command) { 'git commit' } | ||
|
|
||
| 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 | ||
|
|
||
| describe '#setup_environment' do | ||
| subject { context.setup_environment } | ||
|
|
||
|
|
@@ -261,6 +318,10 @@ | |
| describe '#modified_files' do | ||
| subject { context.modified_files } | ||
|
|
||
| before do | ||
| context.stub(:amendment?).and_return(false) | ||
| end | ||
|
|
||
| it 'does not include submodules' do | ||
| submodule = repo do | ||
| `touch foo` | ||
|
|
@@ -324,5 +385,80 @@ | |
|
|
||
| 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'` | ||
| FileUtils.touch('other-file') | ||
| `git add other-file` | ||
| example.run | ||
| end | ||
| end | ||
|
|
||
| before do | ||
| context.stub(:amendment?).and_return(true) | ||
| end | ||
|
|
||
| it { should =~ [File.expand_path('some-file'), File.expand_path('other-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(:amendment?).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"` | ||
| File.open(modified_file, 'a') { |f| f.puts 4 } | ||
| `git add #{modified_file}` | ||
| example.run | ||
| end | ||
| end | ||
|
|
||
| before do | ||
| context.stub(:amendment?).and_return(true) | ||
| end | ||
|
|
||
| it { should == Set.new(1..4) } | ||
| end | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we add tests that ensure we're properly merging the two result sets (already committed files and newly staged files) when we are amending with additional staged files?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure, done in d2dacb7 |
||
| end | ||
| end | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should also add a quick test ensuring we don't match silly aliases like:
...which is possible if you see my comment above about hypens in aliases.