Skip to content
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

Ensure branch exists locally and remotely before comparing it #677

Merged
merged 3 commits into from
Aug 12, 2021
Merged
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
15 changes: 15 additions & 0 deletions spec/support/outputs/git_ensure_pushed.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
echo "-----> Ensuring everything is pushed to git"
if \[ \$\(git branch --list master \| wc -l\) -eq 0 \]; then
echo "! Branch master doesn't exist"
exit 1
fi

if \[ \$\(git branch -r --list origin/master \| wc -l\) -eq 0 \]; then
echo "! Branch origin/master doesn't exist"
exit 1
fi

if \[ \$\(git log origin/master..master \| wc -l\) -ne 0 \]; then
echo "! Your branch master needs to be pushed to origin before deploying"
exit 1
fi
6 changes: 6 additions & 0 deletions spec/tasks/git_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,10 @@
expect { invoke_all }.to output(output_file('git_revision')).to_stdout
end
end

describe 'git:ensure_pushed' do
it 'git ensure pushed' do
expect { invoke_all }.to output(output_file('git_ensure_pushed')).to_stdout
end
end
end
12 changes: 12 additions & 0 deletions tasks/mina/git.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
set :remove_git_dir, true
set :remote, 'origin'
set :git_not_pushed_message, -> { "Your branch #{fetch(:branch)} needs to be pushed to #{fetch(:remote)} before deploying" }
set :git_local_branch_missing_message, -> { "Branch #{fetch(:branch)} doesn't exist" }
set :git_remote_branch_missing_message, -> { "Branch #{fetch(:remote)}/#{fetch(:branch)} doesn't exist" }

namespace :git do
desc 'Clones the Git repository to the current path.'
Expand Down Expand Up @@ -48,6 +50,16 @@
run :local do
comment %{Ensuring everything is pushed to git}
command %{
if [ $(git branch --list #{fetch(:branch)} | wc -l) -eq 0 ]; then
echo "! #{fetch(:git_local_branch_missing_message)}"
exit 1
fi
lovro-bikic marked this conversation as resolved.
Show resolved Hide resolved

if [ $(git branch -r --list #{fetch(:remote)}/#{fetch(:branch)} | wc -l) -eq 0 ]; then
echo "! #{fetch(:git_remote_branch_missing_message)}"
exit 1
fi

if [ $(git log #{fetch(:remote)}/#{fetch(:branch)}..#{fetch(:branch)} | wc -l) -ne 0 ]; then
echo "! #{fetch(:git_not_pushed_message)}"
exit 1
Expand Down