Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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, joseph.huttner@gmail.com
#
# 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 git@gist.github.com: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 ~
@davidcrawford

This comment has been minimized.

Copy link

davidcrawford commented Jun 13, 2012

I like it! However, I have a bug report. It seems that when I run it, it does some things twice. First, it adds a new directory under my current directory and puts a whole bunch of files in it. Second, it tries to add the 'dropbox' remote twice and fails the second time with an error message. Here's my full output.

09:14:50 dcrawford ~/fun/citely: git dropbox add-remote
Searching for your Dropbox folder... found /Users/dcrawford/Dropbox
Enter the folder name to use for your repos (default=git-repos): 
.gitconfig modified successfully.
Initialized empty Git repository in /Users/dcrawford/Dropbox/git-repos/citely/
Initialized empty Git repository in /Users/dcrawford/fun/citely/citely/
fatal: remote dropbox already exists.
09:15:17 dcrawford ~/fun/citely: git remote show dropbox
* remote dropbox
  Fetch URL: /Users/dcrawford/Dropbox/git-repos/citely
  Push  URL: /Users/dcrawford/Dropbox/git-repos/citely
  HEAD branch: (unknown)
09:15:31 dcrawford ~/fun/citely: ls citely/
HEAD        branches    config      description hooks       info        objects     refs
09:15:41 dcrawford ~/fun/citely: rm -rf citely/
09:16:07 dcrawford ~/fun/citely: git push dropbox master
Counting objects: 713, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (650/650), done.
Writing objects: 100% (713/713), 887.77 KiB, done.
Total 713 (delta 78), reused 0 (delta 0)
To /Users/dcrawford/Dropbox/git-repos/citely
 * [new branch]      master -> master
@jhuttner

This comment has been minimized.

Copy link
Owner Author

jhuttner commented Jun 13, 2012

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

@davidcrawford

This comment has been minimized.

Copy link

davidcrawford commented Jun 13, 2012

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.