Skip to content

Added squash commits prompt to to-master #136

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

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ To get full `git help` and manpage support, do:

* `gitProcess.integrationBranch` : The name of the integration branch. Defaults to `master`, but can be set to `develop` or other.
* `gitProcess.keepLocalIntegrationBranch` : Controls asking about removing the local integration branch. Defaults to 'false' (i.e., do not assume the branch should be there).
* `gitProcess.squashCommits` : Controls asking about squashing commits when pushing to master. Defaults to 'false' (i.e., do not prompt user to squash commits). Does not support VIM as the interactive rebase editor at this time.
* `gitProcess.remoteName` : Explicitly sets the remote server name to use.
* `gitProcess.defaultRebaseSync`: Should `git sync` default to using rebase instead of merge? Defaults to 'true' (i.e., Sync using rebase.)

Expand Down
3 changes: 3 additions & 0 deletions lib/git-process/git_lib.rb
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,9 @@ def rev_list(start_revision, end_revision, opts ={})
args << "-#{opts[:num_revs]}" if opts[:num_revs]
args << '--oneline' if opts[:oneline]
args << "#{start_revision}..#{end_revision}"
if opts[:count]
args << '--count'
end
command('rev-list', args)
end

Expand Down
5 changes: 5 additions & 0 deletions lib/git-process/git_process.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@ def fetch_remote_changes(remote_name = nil)
def is_parked?
gitlib.is_parked?
end


def commits_since_master
Integer(gitlib.rev_list('origin/master','HEAD', :count => true))
end


private
Expand Down
34 changes: 32 additions & 2 deletions lib/git-process/rebase_to_master.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,15 @@ def verify_preconditions
raise UncommittedChangesError.new unless gitlib.status.clean?
raise ParkedChangesError.new(gitlib) if is_parked?
end



def should_squash_commits
if commits_since_master > 1
if ask_about_squashing_commits
gitlib.proc_rebase(gitlib.config.integration_branch, :interactive => 'origin/master')
end
end
end

def runner
if remote.exists?
Expand All @@ -48,7 +56,9 @@ def runner
unless @pr_number.nil? or @pr_number.empty?
checkout_pull_request
end


should_squash_commits if squash_commits_config_value.to_boolean

Syncer.rebase_sync(gitlib, true)
current = gitlib.branches.current.name
gitlib.push(remote.name, current, config.master_branch)
Expand Down Expand Up @@ -128,6 +138,26 @@ def close_pull_request
end
end

#noinspection RubyInstanceMethodNamingConvention
def squash_commits_config_value
gitlib.config['gitProcess.squashCommits']
end

def ask_about_squashing_commits
resp = ask("You should squash your commits before pushing to master. Do you need to do this? (Yn) ") do |q|
q.responses[:not_valid] = 'Please respond with either (y)es or (n)o. Defaults to (y)es.'
q.case = :down
q.default = 'Y'
q.validate = /y|n/i
end

if resp == 'n'
say("(You can turn off this message using \"git config gitProcess.squashCommits false\").")
false
else
true
end
end

private

Expand Down
27 changes: 27 additions & 0 deletions spec/rebase_to_master_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,33 @@ def create_process(base, opts = {})
end


it 'should prompt for squash when > 1 commit' do
gitlib.branch('fb', :base_branch => 'master')
change_file_and_commit('a', '')
clone_repo('fb') do |gl|
rtm = GitProc::RebaseToMaster.new(gl, :log_level => log_level, :keep => false)
rtm.config['gitProcess.squashCommits'] = 'true'
rtm.stub(:commits_since_master).and_return(2)
rtm.should_receive(:ask_about_squashing_commits)
commit_count.should == 2
rtm.should_squash_commits
end
end


it 'should not prompt for squash when < 2 commits' do
gitlib.branch('fb', :base_branch => 'master')
clone_repo('fb') do |gl|
rtm = GitProc::RebaseToMaster.new(gl, :log_level => log_level, :keep => false)
rtm.config['gitProcess.squashCommits'] = 'true'
rtm.stub(:commits_since_master).and_return(1)
rtm.should_not_receive(:ask_about_squashing_commits)
commit_count.should == 1
rtm.should_squash_commits
end
end


describe 'when used on _parking_' do
it 'should fail #rebase_to_master' do
gitlib.checkout('_parking_', :new_branch => 'master')
Expand Down