-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgitcred.py
More file actions
54 lines (40 loc) · 1.69 KB
/
gitcred.py
File metadata and controls
54 lines (40 loc) · 1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import argparse
import subprocess as sub
from pathlib import Path
from logger import Logger
class Git:
def __init__(self, logger, username = None):
self.logger = logger
self.username = username
if self.username == None:
self.username = self.find_username()
@staticmethod
def username_from_gitconfig():
gitconfig = Path(Path.home() / ".gitconfig")
if gitconfig.exists():
# This is a fix for python versions prior to Python 3.6 (str(gitconfig.resolve))
with open(str(gitconfig.resolve())) as config_file:
for line in config_file:
line = line.rstrip().strip()
if line.startswith("name"):
return line.replace("name", "").replace("=", "").strip()
return None
def find_username(self):
username_gitconfig = Git.username_from_gitconfig()
username = username_gitconfig
if username == None:
self.logger.log("We failed to find a git username in your ~/.gitconfig", important=True)
username = self.logger.ask("Username:")
self.logger.log("Using git username: " + username)
if not self.logger.silent and username_gitconfig == None:
choice = self.logger.ask_yn("Do you want to save '" + username + "' in your ~/.gitconfig?")
if choice:
sub.Popen(['git', 'config', '--global', 'user.name', username])
return username
def main():
parser = argparse.ArgumentParser()
parser.add_argument('--silent', '-s', action="store_true")
args = parser.parse_args()
Git(Logger(args.silent))
if __name__ == '__main__':
main()