From 5f8cdb86ac502432442167db5ff31f670ad95d3a Mon Sep 17 00:00:00 2001 From: Devin Howard Date: Mon, 8 Aug 2016 16:43:13 +0800 Subject: [PATCH] ensure git pushed function - code props to @fgarcia --- data/deploy.rb | 1 + docs/writing_your_own_tasks.md | 7 +++++++ lib/mina/dsl.rb | 9 +++++++++ 3 files changed, 17 insertions(+) diff --git a/data/deploy.rb b/data/deploy.rb index 048afa5d..ce10f824 100644 --- a/data/deploy.rb +++ b/data/deploy.rb @@ -42,6 +42,7 @@ desc "Deploys the current version to the server." task deploy: :environment do + ensure_git_pushed! deploy do # Put things that will set up an empty directory into a fully set-up # instance of your project. diff --git a/docs/writing_your_own_tasks.md b/docs/writing_your_own_tasks.md index c931f580..9a445293 100644 --- a/docs/writing_your_own_tasks.md +++ b/docs/writing_your_own_tasks.md @@ -115,3 +115,10 @@ Raises an error if variable is not set ``` ruby ensure!(:deploy_to) ``` + +### ensure_git_pushed! +Aborts the build if the local branch has not been pushed to the remote branch. + +``` ruby +ensure_git_pushed!(branch: :master, remote: :origin, message: "Your master branch needs to be pushed to origin before deploying") +``` diff --git a/lib/mina/dsl.rb b/lib/mina/dsl.rb index 0aafa8e2..06025ef8 100644 --- a/lib/mina/dsl.rb +++ b/lib/mina/dsl.rb @@ -15,6 +15,15 @@ def invoke(task, *args) Rake::Task[task].reenable end + def ensure_git_pushed!(opts = {}) + branch = opts[:branch] || fetch(:branch) + remote = opts[:remote] || fetch(:remote) + message = opts[:message].to_s || + "Your branch #{branch} needs to be pushed to #{remote} before deploying" + pending = system "[ $(git log #{remote}/#{branch}..#{branch} | wc -l) -ne 0 ]" + fail message if pending + end + def commands @commands ||= Commands.new end