Created
March 12, 2013 10:42
-
-
Save draftcode/5141919 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| #!/bin/sh | |
| DROPBOX_REPOSITORIES_PATH=`git config dropbox.folder` | |
| eval DROPBOX_REPOSITORIES_PATH=$DROPBOX_REPOSITORIES_PATH | |
| if [ ! "$DROPBOX_REPOSITORIES_PATH" -o ! -d "$DROPBOX_REPOSITORIES_PATH" ]; then | |
| echo "Do \"git config --global dropbox.folder \$HOME/Dropbox/repositories\" first" | |
| exit 1 | |
| fi | |
| shorten_home() { | |
| if [ ${1#$HOME} = $1 ]; then | |
| echo "$1" | |
| else | |
| echo "~${1#$HOME}" | |
| fi | |
| } | |
| cmd_clone() { | |
| if git rev-parse --is-inside-work-tree > /dev/null 2>&1; then | |
| echo "Do not clone inside other working tree." | |
| return 1 | |
| fi | |
| if [ -z "$1" ]; then | |
| echo "Specify an existing repository name." | |
| return 1 | |
| fi | |
| DROPBOX_REPO=$DROPBOX_REPOSITORIES_PATH/$1.git | |
| if [ ! -d $DROPBOX_REPO ]; then | |
| echo "Specify an existing repository name." | |
| return 1 | |
| fi | |
| CLONE_TO=`pwd`/$1 | |
| if [ -d $CLONE_TO ]; then | |
| echo "$2 is already cloned" | |
| return 1 | |
| fi | |
| echo "Clone $(shorten_home $DROPBOX_REPO)" | |
| echo "To $(shorten_home $CLONE_TO)" | |
| git clone $DROPBOX_REPO $CLONE_TO | |
| cd $CLONE_TO | |
| git remote rename origin dropbox | |
| return 0 | |
| } | |
| cmd_sync() { | |
| if ! git rev-parse --is-inside-work-tree > /dev/null 2>&1; then | |
| echo "Please invoke inside a working tree." | |
| return 1 | |
| fi | |
| cd `pwd`/`git rev-parse --show-cdup` | |
| DROPBOX_REPO=$DROPBOX_REPOSITORIES_PATH/$(basename `pwd`).git | |
| CURRENT_REPO=`pwd` | |
| if [ -z "$(git remote | grep dropbox)" ]; then | |
| if [ ! -d $DROPBOX_REPO ]; then | |
| echo "Create a mirror: $(shorten_home $DROPBOX_REPO)" | |
| git clone --mirror $CURRENT_REPO $DROPBOX_REPO | |
| git --git-dir=$DROPBOX_REPO remote rm origin | |
| git remote add dropbox $DROPBOX_REPO | |
| else | |
| echo "Remote repository exists: $(shorten_home $DROPBOX_REPO)" | |
| echo "Set the remote 'dropbox'" | |
| git remote add dropbox $DROPBOX_REPO | |
| git fetch dropbox && git push --all dropbox | |
| fi | |
| else | |
| REMOTE_INFO=$(git remote show -n dropbox | grep "Fetch URL:") | |
| CURRENT_REMOTE_REPO=${REMOTE_INFO## Fetch URL: } | |
| if [ $DROPBOX_REPO != $CURRENT_REMOTE_REPO ]; then | |
| echo "Current 'dropbox' remote is different from the assumption" | |
| echo "Current: $CURRENT_REMOTE_REPO" | |
| echo "Expect: $DROPBOX_REPO" | |
| return 1 | |
| fi | |
| git fetch dropbox && git push --all dropbox | |
| fi | |
| } | |
| main() { | |
| if [ -z "$1" ]; then | |
| COMMAND="sync" | |
| else | |
| COMMAND=$1 | |
| fi | |
| shift | |
| case "$COMMAND" in | |
| "clone" ) cmd_clone "$@" ;; | |
| "sync" ) cmd_sync "$@" ;; | |
| * ) echo "Unknown command $COMMAND"; return 1 ;; | |
| esac | |
| } | |
| main "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment