Breadcrumbs
Home / Programming / Unix / Shell / Bash Functions for GIT and DropboxBash Functions for GIT and Dropbox
I had recently mentioned an article on using GIT with Dropbox.
Using Git with Dropbox is very cool but things don't work exactly like they do when using a normal Git hosting company like GitHub. One major difference is creating and tracking remote branches.
I frequently work on projects from my desktop computer and my laptop. Using Dropbox makes this very convenient so I came up with a few bash functions that make using Git with Dropbox a little easier.
The code is documented but if you have any questions or suggestions, please leave them in the comments.
#!/usr/bin/env bash
#
# bash support for using Git with a DropBox account
#
# Copyright (C) 2010 Craig Williams
# Distributed under the GNU General Public License, version 2.0.
#
# This file is intended to make using DropBox as a remote Git repository
# very easy.
#
# The following commands are available.
#
# 1) gitshared_help
# Displays a list of available commands with explanations
#
# 2) gitshared_init
# Initialize existing repo in /Dropbox/$DROPBOX_FOLDER/
#
# 3) gitshared_list
# List all available git repos located in /Dropbox/$DROPBOX_FOLDER/
#
# 4) gitshared_clone [repoName]
# Clone one of the repos located in /Dropbox/$DROPBOX_FOLDER/
#
# 5) gitshared_track
# Track an existing remote branch
# .git/config file is updated
#
# 6) gitshared_push
# Push the current working branch to the remote repo
# This will add the tracking information to the .git/config file
# The tracking information will only be added once no matter how
# often you use this command.
#
#
# To use these routines:
#
# 1) Copy this file to somewhere (e.g. ~/.gitshared.sh).
# 2) Add the following line to your .bashrc or .bash_profile
# export DROPBOX_FOLDER="folderName"
# [ -f ~/.gitshared.sh ] && . ~/.gitshared.sh
if [ -z "${DROPBOX_FOLDER}" ]; then
echo "Please set the DROPBOX_FOLDER variable"
echo "Example:"
echo "export DROPBOX_FOLDER=\"Shared\""
return 1
fi
# Ensure the dropbox folder exists
if [ -f ~/Dropbox/$DROPBOX_FOLDER ]; then
echo "Folder does not exist: ~/Dropbox/$DROPBOX_FOLDER"
return 1
fi
parse_git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ \1/'
}
#========================================================================
# Git commands to setup a Dropbox/"$DROPBOX_FOLDER" remote repository
#========================================================================
function gitshared_help {
echo "
gitshared_init
Initialize existing repo in /Dropbox/$DROPBOX_FOLDER/
gitshared_list
List all available git repos located in /Dropbox/$DROPBOX_FOLDER/
gitshared_clone [repoName]
Clone one of the repos located in /Dropbox/$DROPBOX_FOLDER/
gitshared_track
Track an existing remote branch
.git/config file is updated
gitshared_push
Push the current working branch to the remote repo
This will add the tracking information to the .git/config file
The tracking information will only be added once no matter how
often you use this command.
"
};
# Interal function only - DO NOT CALL DIRECTLY
function update_config {
name=`pwd`/.git/config
# check to see if this branch has already
# been added to the config file
exists=$(grep "$1" $name)
if [ -z "$exists" ]; then
echo "[branch \"${1}\"]" >> $name
echo $'\t' "remote = origin" >> $name
echo $'\t' "merge = refs/heads/${branch}" >> $name
git fetch origin
fi
}
function gitshared_init {
name=$(basename `pwd`)
git clone --bare . ~/Dropbox/"$DROPBOX_FOLDER"/"$name".git
git remote add origin ~/Dropbox/"$DROPBOX_FOLDER"/"$name".git
git push origin master
git branch --track $name origin/${name}
};
function gitshared_clone {
if [ "$1" == "" ]; then
echo "You must provide the name of a /Dropbox/"$DROPBOX_FOLDER" repo to clone"
echo "eg. gitCloneShared [repoName]"
else
git clone ~/Dropbox/"$DROPBOX_FOLDER"/"$1".git
cd $1
fi
};
function gitshared_track {
if [ "$1" == "" ]; then
echo $'\n'"You must provide the name of a remote branch to track"
echo $'\t'"eg. gitshared_track [remote_branch_name]"
else
update_config $1
fi
};
function gitshared_push {
branch=`expr "$(parse_git_branch)" : '[[:space:]]*\(.*\)[[:space:]]*$'`
git push origin $branch
update_config $branch
};
function gitshared_list {
DIR=~/Dropbox/"$DROPBOX_FOLDER"
echo ''
echo "Available git repos in /Dropbox/"$DROPBOX_FOLDER""
for i in "$DIR"/*.git
do
echo $'\t' $(basename ${i})
done
echo ''
};

Comments
Any clues as to these errors (cygwin on XP):
./dropbox.sh: line 53: [: too many arguments
Cheers
If you find out, post back and I will make the necessary changes.
RSS feed for comments to this post.