Skip to content
Browse files

`pull-request` editor message defaults to single commit message

Closes #136
  • Loading branch information...
1 parent 990db02 commit 961f4b5fa1c933437055e4069a6891c5a2ec0a81 @mislav mislav committed Feb 7, 2012
Showing with 28 additions and 10 deletions.
  1. +19 −5 lib/hub/commands.rb
  2. +4 −0 lib/hub/context.rb
  3. +5 −5 test/hub_test.rb
View
24 lib/hub/commands.rb
@@ -125,7 +125,7 @@ def pull_request(args)
remote_branch = "#{head_project.remote}/#{options[:head]}"
options[:head] = "#{head_project.owner}:#{options[:head]}"
- if !force and tracked_branch and local_commits = git_command("rev-list --cherry #{remote_branch}...")
+ if !force and tracked_branch and local_commits = rev_list(remote_branch, nil)
$stderr.puts "Aborted: #{local_commits.split("\n").size} commits are not yet pushed to #{remote_branch}"
warn "(use `-f` to force submit a pull request anyway)"
abort
@@ -138,10 +138,25 @@ def pull_request(args)
unless options[:title] or options[:issue]
base_branch = "#{base_project.remote}/#{options[:base]}"
- changes = git_command "log --no-color --pretty=medium --cherry %s...%s" %
- [base_branch, remote_branch]
+ commits = rev_list(base_branch, remote_branch).to_s.split("\n")
+
+ case commits.size
+ when 0
+ default_message = commit_summary = nil
+ when 1
+ format = '%w(78,0,0)%s%n%+b'
+ default_message = git_command "show -s --format='#{format}' #{commits.first}"
+ commit_summary = nil
+ else
+ format = '%h (%aN, %ar)%n%w(78,3,3)%s%n%+b'
+ default_message = nil
+ commit_summary = git_command "log --no-color --format='%s' --cherry %s...%s" %
+ [format, base_branch, remote_branch]
+ end
- options[:title], options[:body] = pullrequest_editmsg(changes) { |msg|
+ options[:title], options[:body] = pullrequest_editmsg(commit_summary) { |msg|
+ msg.puts default_message if default_message
+ msg.puts ""
msg.puts "# Requesting a pull to #{base_project.owner}:#{options[:base]} from #{options[:head]}"
msg.puts "#"
msg.puts "# Write a message for this pull request. The first block"
@@ -898,7 +913,6 @@ def create_pullrequest(options)
def pullrequest_editmsg(changes)
message_file = File.join(git_dir, 'PULLREQ_EDITMSG')
File.open(message_file, 'w') { |msg|
- msg.puts
yield msg
if changes
msg.puts "#\n# Changes:\n#"
View
4 lib/hub/context.rb
@@ -431,6 +431,10 @@ def git_alias_for(name)
git_config "alias.#{name}"
end
+ def rev_list(a, b)
+ git_command("rev-list --cherry-pick --right-only --no-merges #{a}...#{b}")
+ end
+
PWD = Dir.pwd
def current_dir
View
10 test/hub_test.rb
@@ -816,7 +816,7 @@ def test_pullrequest
def test_pullrequest_with_unpushed_commits
stub_tracking('master', 'mislav', 'master')
- stub_command_output "rev-list --cherry mislav/master...", "+abcd1234\n+bcde2345"
+ stub_command_output "rev-list --cherry-pick --right-only --no-merges mislav/master...", "+abcd1234\n+bcde2345"
expected = "Aborted: 2 commits are not yet pushed to mislav/master\n" <<
"(use `-f` to force submit a pull request anyway)\n"
@@ -839,7 +839,7 @@ def test_pullrequest_from_branch
def test_pullrequest_from_tracking_branch
stub_branch('refs/heads/feature')
stub_tracking('feature', 'mislav', 'yay-feature')
- stub_command_output "rev-list --cherry mislav/master...", nil
+ stub_command_output "rev-list --cherry-pick --right-only --no-merges mislav/master...", nil
stub_request(:post, "https://#{auth}github.com/api/v2/json/pulls/defunkt/hub").
with(:body => { 'pull' => {'base' => "master", 'head' => "mislav:yay-feature", 'title' => "hereyougo"} }).
@@ -856,7 +856,7 @@ def test_pullrequest_enterprise_no_tracking
stub_github_token('789xyz', 'git.my.org')
stub_branch('refs/heads/feature')
stub_tracking_nothing('feature')
- stub_command_output "rev-list --cherry origin/feature...", nil
+ stub_command_output "rev-list --cherry-pick --right-only --no-merges origin/feature...", nil
stub_request(:post, "https://#{auth('myfiname', '789xyz')}git.my.org/api/v2/json/pulls/defunkt/hub").
with(:body => { 'pull' => {'base' => "master", 'head' => "myfiname:feature", 'title' => "hereyougo"} }).
@@ -914,7 +914,7 @@ def test_pullrequest_explicit_base_with_repo
def test_pullrequest_existing_issue
stub_branch('refs/heads/myfix')
stub_tracking('myfix', 'mislav', 'awesomefix')
- stub_command_output "rev-list --cherry mislav/awesomefix...", nil
+ stub_command_output "rev-list --cherry-pick --right-only --no-merges mislav/awesomefix...", nil
stub_request(:post, "https://#{auth}github.com/api/v2/json/pulls/defunkt/hub").
with(:body => { 'pull' => {'base' => "master", 'head' => "mislav:awesomefix", 'issue' => '92'} }).
@@ -927,7 +927,7 @@ def test_pullrequest_existing_issue
def test_pullrequest_existing_issue_url
stub_branch('refs/heads/myfix')
stub_tracking('myfix', 'mislav', 'awesomefix')
- stub_command_output "rev-list --cherry mislav/awesomefix...", nil
+ stub_command_output "rev-list --cherry-pick --right-only --no-merges mislav/awesomefix...", nil
stub_request(:post, "https://#{auth}github.com/api/v2/json/pulls/mojombo/hub").
with(:body => { 'pull' => {'base' => "master", 'head' => "mislav:awesomefix", 'issue' => '92'} }).

0 comments on commit 961f4b5

Please sign in to comment.
Something went wrong with that request. Please try again.