Skip to content

Instantly share code, notes, and snippets.

@jhuttner
Created June 5, 2012 22:49
Show Gist options
  • Save jhuttner/2878634 to your computer and use it in GitHub Desktop.
Save jhuttner/2878634 to your computer and use it in GitHub Desktop.
Easily create Dropbox remotes for your Git repositories.
Background:
I don't have a paid GitHub account, so I use Dropbox to backup my repositories privately.
What's annoying is that every time I create a "dropbox" remote I go through the same time-consuming steps.
Well I'm done with that, homeboy. Now it's as easy as:
>>>>>>>>>>>>>
$ pwd
/usr/local/apps/db-cache.git
$ git dropbox add-remote
Initialized empty Git repository in /home/jhuttner/Dropbox/git-repos/db-cache.git/
$ git push dropbox master
<<<<<<<<<<<<<
Installation instructions:
1. Install Dropbox for Linux, or whatever you are using. I recommend the command line install: https://www.dropbox.com/install?os=lnx
2. Start the Dropbox daemon: $ ~/.dropbox-dist/dropboxd
3. Paste the commands from run.sh (below) into your Terminal. In a nutshell, they:
- Clone this gist
- Make the Python file executable
- Setup a symlink in ~/bin
- Create a "git dropbox" alias to call the Python file.
NOTE that ~/bin must be in your PATH for this to all work.
#!/usr/bin/env python
import getpass
import os
import subprocess
import sys
###############################################################################
#
# Author: Joseph Huttner, [email protected]
#
# License: Dual-licensed under the GPL and MIT licenses.
#
# Changelog:
# 2012-06-05: Initial commit
# 2012-06-13: Fix bug where add_remote was not returning correctly.
#
###############################################################################
def get_current_folder_name():
return os.path.basename(os.getcwd())
def setup_config():
"""Write required key-value pairs to the global git config."""
sys.stdout.write("Searching for your Dropbox folder... ")
dropbox_path = os.path.join(os.getenv('HOME'), 'Dropbox')
if os.path.exists(dropbox_path):
sys.stdout.write("found " + dropbox_path + "\n")
else:
sys.stdout.write("not found.\n")
dropbox_path = raw_input("Enter the full path to your Dropbox folder: ")
git_folder = raw_input("Enter the folder name to use for your repos (default=git-repos): ")
dropbox_path = dropbox_path.strip()
git_folder = git_folder.strip() or "git-repos"
subprocess.call(['git', 'config', '--global', 'dropbox.dropboxpath', dropbox_path, '--path'])
subprocess.call(['git', 'config', '--global', 'dropbox.gitfolder', git_folder, '--path'])
print '.gitconfig modified successfully.'
def check_remote_exists(remote_name):
cmd = 'git remote show dropbox'
exit_code = subprocess.call(cmd.split(), shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
if exit_code == 0:
return True
else:
return False
def add_remote(remote_name, allow_config_setup=True):
"""Create a new bare git folder."""
cmd = 'git config --global dropbox.dropboxpath'
p = subprocess.Popen(cmd.split(), shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
dropbox_path, stderr = p.communicate()
cmd = 'git config --global dropbox.gitfolder'
p = subprocess.Popen(cmd.split(), shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
git_folder, stderr = p.communicate()
dropbox_path = dropbox_path.strip()
git_folder = git_folder.strip()
if not (dropbox_path and git_folder):
if allow_config_setup:
setup_config()
return add_remote(remote_name, False)
else:
sys.exit('Config setup failed.')
repo_location = os.path.join(dropbox_path, git_folder, get_current_folder_name())
if not os.path.exists(repo_location):
try:
os.makedirs(repo_location)
except:
sys.exit("The specified folder could not be created: " + repo_location)
else:
sys.exit("Fatal: A folder already exists at the specified location: " + repo_location)
subprocess.call(['git', 'init', '--bare', repo_location])
subprocess.call(['git', 'remote', 'add', remote_name, repo_location])
def main(args):
if len(args) == 1 and args[0] == 'add-remote':
if not check_remote_exists("dropbox"):
add_remote("dropbox")
else:
sys.exit("Fatal: remote 'dropbox' already exists.");
if __name__ == "__main__":
args = sys.argv[1:]
main(args)
cd ~
mkdir -p bin
git clone [email protected]:2878634.git ; cd `ls -t | head -n1`
chmod +x git-dropbox.py
cd ../bin
ln -s `echo ~/``ls -t ../ | head -n1`/git-dropbox.py git-dropbox.py
git config --global alias.dropbox '!git-dropbox.py'
cd ~
@jhuttner
Copy link
Author

Bug verified and fixed in 854e72. Please "git pull" to get this latest code. Thank you.

@davidcrawford
Copy link

davidcrawford commented Jun 13, 2012 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment