Created
July 28, 2020 12:32
-
-
Save b1oki/880ae4029d292003bf5d23873762deca to your computer and use it in GitHub Desktop.
Change git repository email for work
This file contains hidden or 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/python3 | |
# -*- coding: utf-8 -*- | |
import sys | |
import os | |
import logging | |
import git | |
logger = logging.getLogger('scratch') | |
logger.setLevel(logging.INFO) | |
logger.addHandler(logging.StreamHandler()) | |
logger.info('argv: {}'.format(sys.argv)) | |
args = sys.argv[1:] | |
git_path = '~/Documents/git/' | |
email_for_work = '[email protected]' | |
for git_dir in os.listdir(git_path): | |
git_dir_path = os.path.join(git_path, git_dir) | |
if not os.path.isdir(git_dir_path): | |
continue | |
try: | |
repo = git.Repo(git_dir_path) | |
except (git.InvalidGitRepositoryError, ValueError): | |
continue | |
logger.info(f'Repository {git_dir}') | |
with repo.config_reader() as git_config: | |
email = git_config.get_value('user', 'email') | |
logger.info(f'Email {email}') | |
for remote in repo.remotes: | |
logger.info(f'Remote url {remote.url}') | |
if 'git.company.com' in remote.url: | |
if email != email_for_work: | |
logger.info('Change user email for work repository') | |
with repo.config_writer() as git_config: | |
git_config.set_value('user', 'email', email_for_work) | |
with repo.config_reader() as git_config: | |
email = git_config.get_value('user', 'email') | |
logger.info(f'Email {email}') | |
sys.exit(0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment