From 961f4b5fa1c933437055e4069a6891c5a2ec0a81 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mislav=20Marohnic=CC=81?= Date: Tue, 7 Feb 2012 21:09:39 +0100 Subject: [PATCH] `pull-request` editor message defaults to single commit message Closes #136 --- lib/hub/commands.rb | 24 +++++++++++++++++++----- lib/hub/context.rb | 4 ++++ test/hub_test.rb | 10 +++++----- 3 files changed, 28 insertions(+), 10 deletions(-) diff --git a/lib/hub/commands.rb b/lib/hub/commands.rb index ed31f9f3..d49a21d4 100644 --- a/lib/hub/commands.rb +++ b/lib/hub/commands.rb @@ -125,7 +125,7 @@ module Hub 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 @@ module Hub 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 @@ help 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#" diff --git a/lib/hub/context.rb b/lib/hub/context.rb index 2b98d31c..ed71c7bb 100644 --- a/lib/hub/context.rb +++ b/lib/hub/context.rb @@ -431,6 +431,10 @@ module Hub 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 diff --git a/test/hub_test.rb b/test/hub_test.rb index e61b0c93..86ba5600 100644 --- a/test/hub_test.rb +++ b/test/hub_test.rb @@ -816,7 +816,7 @@ class HubTest < Test::Unit::TestCase 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 @@ class HubTest < Test::Unit::TestCase 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 @@ class HubTest < Test::Unit::TestCase 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 @@ class HubTest < Test::Unit::TestCase 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 @@ class HubTest < Test::Unit::TestCase 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'} }). -- GitLab