From b9e8f6e892db97ac09a0cbaa1bdbb5862729e98f Mon Sep 17 00:00:00 2001 From: Seth Vargo Date: Thu, 13 Nov 2014 17:47:08 -0500 Subject: [PATCH] Interpret the current branch to push to Heroku --- plugins/pushes/heroku/push.rb | 21 ++++++++- test/unit/plugins/pushes/heroku/push_test.rb | 45 ++++++++++++++++++-- 2 files changed, 61 insertions(+), 5 deletions(-) diff --git a/plugins/pushes/heroku/push.rb b/plugins/pushes/heroku/push.rb index cab46de7a..17dc75a04 100644 --- a/plugins/pushes/heroku/push.rb +++ b/plugins/pushes/heroku/push.rb @@ -10,6 +10,9 @@ module VagrantPlugins # Expand any paths relative to the root dir = File.expand_path(config.dir, env.root_path) + # Get the current branch + branch = git_branch(dir) + # Verify git is installed verify_git_bin!(config.git_bin) @@ -22,7 +25,7 @@ module VagrantPlugins end # Push to Heroku - git_push_heroku(config.remote, config.branch, dir) + git_push_heroku(config.remote, branch, dir) end # Verify that git is installed. @@ -49,6 +52,20 @@ module VagrantPlugins "#{path}/.git" end + # The name of the current git branch. + # @param [String] path + # @return [String] + def git_branch(path) + result = execute!("git", + "--git-dir", git_dir(path), + "--work-tree", path, + "branch", + ) + + # Returns something like "* master" + result.stdout.sub("*", "").strip + end + # Push to the Heroku remote. # @param [String] remote # @param [String] branch @@ -56,7 +73,7 @@ module VagrantPlugins execute!("git", "--git-dir", git_dir(path), "--work-tree", path, - "push", remote, branch, + "push", remote, "#{branch}:master", ) end diff --git a/test/unit/plugins/pushes/heroku/push_test.rb b/test/unit/plugins/pushes/heroku/push_test.rb index 1bec15927..ab5a64eee 100644 --- a/test/unit/plugins/pushes/heroku/push_test.rb +++ b/test/unit/plugins/pushes/heroku/push_test.rb @@ -17,17 +17,20 @@ describe VagrantPlugins::HerokuPush::Push do dir: "lib", git_bin: "git", remote: "heroku", - branch: "master", ) end subject { described_class.new(env, config) } describe "#push" do + let(:branch) { "master" } + let(:root_path) { "/handy/dandy" } let(:dir) { "#{root_path}/#{config.dir}" } before do + allow(subject).to receive(:git_branch) + .and_return(branch) allow(subject).to receive(:verify_git_bin!) allow(subject).to receive(:verify_git_repo!) allow(subject).to receive(:has_git_remote?) @@ -78,7 +81,7 @@ describe VagrantPlugins::HerokuPush::Push do it "pushes to heroku" do expect(subject).to receive(:git_push_heroku) - .with(config.remote, config.branch, dir) + .with(config.remote, branch, dir) subject.push end end @@ -157,7 +160,7 @@ describe VagrantPlugins::HerokuPush::Push do .with("git", "--git-dir", "#{dir}/.git", "--work-tree", dir, - "push", "bacon", "hamlet", + "push", "bacon", "hamlet:master", ) subject.git_push_heroku("bacon", "hamlet", dir) end @@ -232,6 +235,42 @@ describe VagrantPlugins::HerokuPush::Push do end end + describe "#git_branch" do + let(:stdout) { "" } + let(:process) { double("process", stdout: stdout) } + + before do + allow(subject).to receive(:execute!) + .and_return(process) + end + + let(:branch) { subject.git_branch("/path") } + + context "when the branch is prefixed with a star" do + let(:stdout) { "*bacon" } + + it "returns the correct name" do + expect(branch).to eq("bacon") + end + end + + context "when the branch is prefixed with a star space" do + let(:stdout) { "* bacon" } + + it "returns the correct name" do + expect(branch).to eq("bacon") + end + end + + context "when the branch is not prefixed" do + let(:stdout) { "bacon" } + + it "returns the correct name" do + expect(branch).to eq("bacon") + end + end + end + describe "#execute!" do let(:exit_code) { 0 } let(:stdout) { "This is the output" }