Skip to content

Instantly share code, notes, and snippets.

@goulvench
Created May 15, 2020 16:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save goulvench/2727fd739becf41be354a46b104c21e6 to your computer and use it in GitHub Desktop.
Save goulvench/2727fd739becf41be354a46b104c21e6 to your computer and use it in GitHub Desktop.
# frozen_string_literal: true
namespace :git do
desc 'Check that local changes have been pushed to the origin repository'
task :check_if_up_to_date do
# skip if no git dir detected
git_dir = `git rev-parse --git-dir`.strip
next if git_dir.empty?
time = fetch(:uptodate_time, 300)
branch = fetch(:uptodate_branch, 'master')
remote_repository = fetch(:uptodate_remote_repository, 'origin')
# skip if remote is not yet configured
remote_reference_file = File.join(git_dir, 'refs', 'remotes', remote_repository, branch)
next unless File.exist?(remote_reference_file)
# fetch remote references and skip on error
fetch_file = File.join(git_dir, 'FETCH_HEAD')
unless File.exist?(fetch_file) && Time.now - File.mtime(fetch_file) < time
puts "Fetching references from #{remote_repository} repository ..."
next unless system("git fetch #{remote_repository}")
end
# get commit for local and remote reference
local_commit = `git rev-parse #{branch}`.strip
remote_commit = `git rev-parse #{remote_repository}/#{branch}`.strip
# compare local and remote commit
if local_commit != remote_commit
puts "Local '#{branch}' branch is not synchronized with '#{remote_repository}' repository."
ask(:continue, 'type "y" to proceed anyway')
abort unless fetch(:continue) == 'y'
end
end
after 'git:check', 'git:check_if_up_to_date'
end
## Instructions
# Copy this into lib/capistrano/tasks/git.rake
# and make sure that all tasks in this folder are loaded by adding
# or uncommenting the following line in your Capfile:
# Dir.glob('lib/capistrano/tasks/*.rake').each { |r| import r }
#
# Now, deployment will ask for confirmation if you haven't pushed
# your local changes to the deployment branch.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment