34 GIT configuration helper interface
38 __settings_check = [os.getenv(
"GIT_USER",
""), os.getenv(
"GIT_PASSWORD",
"")]
40 if os.path.exists(
".git"):
42 if os.path.exists(os.path.expanduser(
"~/.gitconfig_old")):
return
44 with open(os.path.expanduser(
"~/.gitconfig"), mode=
'a'):
pass
45 os.replace(os.path.expanduser(
"~/.gitconfig"),os.path.expanduser(
"~/.gitconfig_old"))
47 __commands = [
"git config --global pull.ff only",
48 "git config --global http.sslverify false",
49 "git config --global core.ignorecase false",
50 "git config --global credential.helper %s" % (
"wincred" if os.name.lower()
in [
"nt"]
else "cache",)]
52 if all(__settings_check):
53 __commands.extend([
'git config --global user.name "%s"' % os.getenv(
"GIT_USER"),
54 'git config --global user.password "%s"' % os.getenv(
"GIT_PASSWORD")])
56 __settings_check.extend([os.getenv(
"CI_SERVER_URL",
"")])
58 if len(__settings_check) > 2
and all(__settings_check):
59 __commands.extend([
'git config --global url."%s://%s:%s@%s/".insteadOf "%s/"' % (
60 str(urlparse(os.getenv(
"CI_SERVER_URL")).scheme),
61 os.getenv(
"GIT_USER"), os.getenv(
"GIT_PASSWORD"),
62 str(urlparse(os.getenv(
"CI_SERVER_URL")).netloc),
63 os.getenv(
"CI_SERVER_URL"), )])
65 if not os.name.lower()
in [
"nt"]:
67 for x
in [
"apt-get update",
"apt-get install -y git-lfs"][::-1]: __commands.insert(0,x)
69 for command
in __commands:
70 subprocess.call(shlex.split(command,posix=
not os.name.lower()
in [
"nt"]),stderr=sys.stderr, stdout=sys.stdout)
75 GIT pull command with recursive submodule and LFS support
79 __settings_check = [ os.getenv(
"GIT_USER",
""), os.getenv(
"GIT_PASSWORD",
""), os.getenv(
"CI_SERVER_URL",
"") ]
81 if not os.path.exists(
"~/.gitconfig_old"):
setup()
83 if os.path.exists(
".git"):
84 __commands = [
"git lfs install",
"git submodule sync --recursive"]
86 if all(__settings_check):
87 __commands.extend([
"""git submodule foreach --recursive 'git config --local url."%s://%s:%s@%s/".insteadOf "%s/"'""" % (
88 str(urlparse(os.getenv(
"CI_SERVER_URL")).scheme),
89 os.getenv(
"GIT_USER"), os.getenv(
"GIT_PASSWORD"),
90 str(urlparse(os.getenv(
"CI_SERVER_URL")).netloc),
91 os.getenv(
"CI_SERVER_URL"), )])
93 for command
in __commands:
94 subprocess.call(shlex.split(command,posix=
not os.name.lower()
in [
"nt"]),stderr=sys.stderr, stdout=sys.stdout)
96 g = git.Repo(os.getcwd())
98 try: branch = str(g.active_branch.name)
99 except: branch =
"HEAD"
101 g.git.pull(
"origin",branch)
103 g.git.pull(
"origin",branch)
105 try: g.git.submodule(
'update',
'--init',
"--recursive")
109 try: g.git.submodule(
'update',
'--init',
"--recursive",
"--remote")
111 finally: g.git.submodule(
'foreach',
'--recursive',
"git pull origin $(git config -f $toplevel/.gitmodules submodule.$name.branch || echo master) || git pull origin main")
113 try: os.replace(os.path.expanduser(
"~/.gitconfig"),os.path.expanduser(
"~/.gitlfs"))
116 if all(__settings_check)
and any(__commands):
117 cache =
"""-c credential.helper='!f() { sleep 1; echo "username=%s"; echo "password=%s"; }; f'""" % (os.getenv(
"GIT_USER"),os.getenv(
"GIT_PASSWORD"),)
119 try: g.git.submodule(
'foreach',
'--recursive',
"git %s lfs pull" % cache)
123 os.replace(os.path.expanduser(
"~/.gitlfs"),os.path.expanduser(
"~/.gitconfig"))
124 os.replace(os.path.expanduser(
"~/.gitconfig_old"),os.path.expanduser(
"~/.gitconfig"))
126 if os.stat( os.path.expanduser(
"~/.gitconfig") ).st_size == 0: os.remove(os.path.expanduser(
"~/.gitconfig"))
132 Main command line parser.
134 if not kwargs.get(
"method",
""):
135 parser = argparse.ArgumentParser(description=
'CLI wrapper options for GIT.')
136 parser.add_argument(
'method', metavar=
'option', type=str, nargs=1,
137 help=
'An option identifier. Either <setup> or <update>. All other CLI arguments are directly parsed to GIT.')
139 args, _ = parser.parse_known_args()
140 method = str(args.method[0])
141 else: method = kwargs.get(
"method")
143 command =
" ".join([
"git"] + sys.argv[1:])
145 if not method
in globals()
or ( len(sys.argv[1:]) > 1
and not method
in globals() ):
146 subprocess.call(shlex.split(command,posix=
not os.name.lower()
in [
"nt"]),stderr=sys.stderr, stdout=sys.stdout)
147 else: globals()[method]()