Created
September 24, 2012 20:14
-
-
Save phoolish/3778082 to your computer and use it in GitHub Desktop.
Set git config.email/user based on path
This file contains 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
[DEFAULT] | |
name=My Name | |
[email protected] | |
[work] | |
path=/media/drive/work | |
name=My Name | |
[email protected] | |
[play] | |
path=/media/drive/play | |
name=My Other Name | |
[email protected] |
This file contains 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
#!/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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Edit .gitme.conf and place it in your home directory.
Make gitme.py executable and add it to path. I use ~/bin.