Skip to content

Instantly share code, notes, and snippets.

@abhijitmamarde
Created October 29, 2016 16:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abhijitmamarde/10c4ef75ab33985008a2550d5fa17edb to your computer and use it in GitHub Desktop.
Save abhijitmamarde/10c4ef75ab33985008a2550d5fa17edb to your computer and use it in GitHub Desktop.
git profile changer tool
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
Simple utility to change the active global git profile.
sample profile config file (yaml):
mygit:
user: Abhijit
email: abhijit.XXXX@gmail.com
notes: Personal github account
tags: pwork, github
te:
user: Abhijit
email: Abhijit@example.com
notes: For TE work
tags: pwork, te, gitlab
'''
import baker
from yaml import load as load_yaml
from os import system
from os.path import expanduser, basename
from os.path import join as path_join
# config file path/name: ~/.<script_name>
profile_path = path_join(expanduser("~"), "." + basename(__file__))
profiles = load_yaml(open(profile_path))
@baker.command
def set(profile_name=None):
'''sets the specific git profile'''
if profile_name in profiles:
profile = profiles[profile_name]
user, email = profile['user'], profile['email']
git_user_config_cmd = 'git config --global user.name "%s"' % user
git_email_config_cmd = 'git config --global user.email "%s"' % email
print git_user_config_cmd
print git_email_config_cmd
ans = raw_input("Execute above commands (y/n): ")
if ans.lower() == 'y':
system(git_user_config_cmd + "; " + git_email_config_cmd)
print "\ngit config changed to:\n%(user_name)s <%(user_email)s>" % dict(
user_name=user,
user_email=email
)
else:
print "\nAborted! No changes done!!!"
else:
print "Profile config not found for: %s\nNo changes done!!!" % profile_name
@baker.command
def show():
'''lists all the profile found from config file'''
print
profiles_count = len(profiles)
for i, profile_name in enumerate(profiles):
profile = profiles[profile_name]
user, email, notes, tags = (
profile['user'],
profile['email'],
profile['notes'],
profile['tags']
)
print "%(index)d. %(alias)s: %(notes)s \n%(user_name)s <%(user_email)s>\ntags: %(tags)s" % dict(
user_name=user,
user_email=email,
notes=notes,
tags=tags,
index=i+1,
alias=profile_name
)
if i != profiles_count-1:
# print "-" * 30
print
print "\nFound %d profile(s)" % profiles_count
baker.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment