Created
October 10, 2012 15:00
-
-
Save konstantinbe/3866165 to your computer and use it in GitHub Desktop.
Backing Up Repos
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 | |
# | |
# Copyright (c) 2012 Konstantin Bender. | |
# | |
# Permission is hereby granted, free of charge, to any person obtaining a copy | |
# of this software and associated documentation files (the "Software"), to deal | |
# in the Software without restriction, including without limitation the rights | |
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
# copies of the Software, and to permit persons to whom the Software is | |
# furnished to do so, subject to the following conditions: | |
# | |
# The above copyright notice and this permission notice shall be included in | |
# all copies or substantial portions of the Software. | |
# | |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
# THE SOFTWARE. | |
# This script pushes (forced) all branches of the repo to a backup repo defined | |
# in the (global or local) git config file under `backup.url`. Each pushed | |
# branch is prefixed with the name of the remote repo followed by a slash | |
# followed by the original branch name. | |
# | |
# The main use case is to have a backup either of full private branches | |
# or commits that are not yet pushed to origin. You need only one private | |
# backup repo for any number of projects. | |
# | |
# INSTALLATION: Put this script somewhere in your path and make it executable | |
# (chmod +x git-backup). | |
# | |
# EXAMPLE: Lets say I'm working on a project called 'milk' and 'sugar' with the | |
# following origin urls: | |
# | |
# git@github.com:konstantinbe/milk.git | |
# git@github.com:konstantinbe/sugar.git | |
# | |
# Create a backup repo: | |
# | |
# git@github.com:konstantinbe/backup.git | |
# | |
# Configure git for backup: | |
# | |
# git config --global backup.url git@github.com:konstantinbe/backup.git | |
# | |
# Now at any time call 'git backup' from within a local repo. In our case: | |
# | |
# cd ~/projects/milk | |
# ... do some work ... | |
# git backup | |
# | |
# cd ~/projects/sugar | |
# ... do some work ... | |
# git backup | |
# | |
# Each local branch will be prefixed with the repo name and pushed (forced) to | |
# the backup repo, which in our case might look as follows: | |
# | |
# git ls-remote --heads git@github.com:konstantinbe/backup.git | |
# | |
# 423fd3adace9db89b16004745df9150e5e09620e refs/heads/master | |
# 32c79ed0f051ab73a9ce863f8f1b447e375814f4 refs/heads/milk/dictionary | |
# 32c8d8d93883b469d800dfd99369a1400172c1c9 refs/heads/milk/gh-pages | |
# 717c4623c9591b719a6037edbb8eaaf9381df97a refs/heads/milk/master | |
# e74a68957311c205f7e511fcd277a422061aa21c refs/heads/sugar/gh-pages | |
# e74a68957311c205f7e511fcd277a422061aa21c refs/heads/sugar/master | |
# | |
# ---------------------------------------------------------------- constants --- | |
OK = "\33[0m\33[32mOK\33[0m" | |
FAILED = "\33[0m\33[31mFAILED\33[0m" | |
ORIGIN = `git config remote.origin.url`.chomp | |
BACKUP = `git config backup.url`.chomp | |
PREFIX = ORIGIN.scan(/\/(.*)\.git/).first.first.to_s | |
# ---------------------------------------------------------------- functions --- | |
def check(condition) | |
unless condition | |
puts FAILED | |
exit 1 | |
end | |
end | |
def run(command) | |
success = system command | |
check success | |
end | |
def put(string) | |
print string | |
STDOUT.flush | |
end | |
# ------------------------------------------------------------------- script --- | |
refspecs = `git branch`.chomp.map do |branch| | |
name = branch.scan(/\S+$/).first.first.to_s | |
"#{name}:#{PREFIX}/#{name}" | |
end | |
put "Backing up #{refspecs.length} branch(es) ... " | |
run "git push --quiet --force #{BACKUP} #{refspecs.join ' '}" | |
puts OK |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment