Skip to content

Instantly share code, notes, and snippets.

@phoolish
Created September 24, 2012 20:14
Show Gist options
  • Save phoolish/3778082 to your computer and use it in GitHub Desktop.
Save phoolish/3778082 to your computer and use it in GitHub Desktop.
Set git config.email/user based on path
[DEFAULT]
name=My Name
email=email@address.com
[work]
path=/media/drive/work
name=My Name
email=email@address.com
[play]
path=/media/drive/play
name=My Other Name
email=email@address.com
#!/usr/bin/env python
# TODO:
# allow for clone action
from optparse import OptionParser
import ConfigParser, os
from subprocess import call
def main():
usage = "usage: %prog [options] SOURCE"
parser = OptionParser(usage=usage)
parser.add_option("-v", "--verbose",
action="store_true", dest="verbose")
parser.add_option("-q", "--quiet",
action="store_false", dest="verbose", default=False)
parser.add_option("-u", "--update", dest="update",
action="store_true", default=False,
help="Update the current git config info.")
(options, args) = parser.parse_args()
if len(args) != 1 and not options.update:
parser.error("This only takes 1 argument")
path = '.'
if not options.update:
path = args[0]
source = os.path.abspath(path)
gitme_conf = os.path.expanduser('~/.gitme.conf')
config = "DEFAULT"
if not os.path.exists(gitme_conf):
parser.error("%s seems to be missing." % gitme_conf)
if options.verbose:
print "Parsing configuration %s..." % gitme_conf
conf = ConfigParser.RawConfigParser()
conf.read(gitme_conf)
for section in conf.sections():
if options.verbose:
print "Checking %s for path..." % section
path = conf.get(section, 'path')
if source.find(path) != -1:
if options.verbose:
print "Found path %s..." % path
config = section
if not options.update:
if options.verbose:
print "Running git init on %s..." % source
call("git init %s" % source, shell=True)
if options.verbose:
print "Running git config username..."
call("git config --file %s user.name '%s' " % ("%s/.git/config" % source, conf.get(config, 'name')), shell=True)
if options.verbose:
print "Running git config email..."
call("git config --file %s user.email %s" % ("%s/.git/config" % source, conf.get(config, 'email')), shell=True)
if __name__ == "__main__":
main()
@phoolish
Copy link
Author

Edit .gitme.conf and place it in your home directory.

Make gitme.py executable and add it to path. I use ~/bin.

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