Created
December 1, 2014 01:48
-
-
Save quark-zju/fe164294fc478fdb9fff to your computer and use it in GitHub Desktop.
Quick & dirty way to sync git to remote
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# Find git root | |
while !File.exists?('.git/config') | |
Dir.chdir('..') | |
raise 'git root not found' if Dir.pwd == '/' | |
end | |
args = ARGV | |
if args.empty? | |
args = File.read('.pushf').split rescue [] | |
end | |
args.each do |arg| | |
# Run git push | |
hostname, path = arg.split(':') | |
if !path || path.empty? | |
path = File.basename(Dir.pwd) | |
end | |
if path.empty? | |
puts 'unsupported' | |
exit 1 | |
end | |
if system 'git', 'push', '-f', "#{hostname}:#{path}" | |
system 'git', 'push', '--tags', '-f', "#{hostname}:#{path}" | |
else | |
system "ssh #{hostname} '[ ! -e #{path}/.git ] && mkdir -p #{path} && cd #{path} && git init && git config receive.denyCurrentBranch ignore'" | |
system 'git', 'push', '-f', "#{hostname}:#{path}" | |
end | |
head = `git rev-parse HEAD`.chomp | |
system "ssh #{hostname} 'cd #{path} && echo #{head} > .git/HEAD && git reset --hard HEAD'" | |
end | |
if args.empty? | |
puts 'git pushf host1[:path] [host2[:path]] ...' | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment