Skip to content

Instantly share code, notes, and snippets.

@thetemplateblog
Forked from crbyxwpzfl/git.py
Created April 13, 2023 14:42
Show Gist options
  • Save thetemplateblog/c47b801e26de61762e5d8243f01d9aca to your computer and use it in GitHub Desktop.
Save thetemplateblog/c47b801e26de61762e5d8243f01d9aca to your computer and use it in GitHub Desktop.
python script for git
import subprocess
#import privates variable
import sys
import os
sys.path.append(os.path.join(os.getenv('privates')))
import privates
def test():
global branch
output = subprocess.run([f"{privates.sshprogramm}", '-i', f"{privates.pathtosshkey}", 'git@github.com'])
output = subprocess.run([f"{privates.gitprogramm}", 'status'])
def pull():
global branch
output = subprocess.run([f"{privates.gitprogramm}", '-c', 'user.name="crbyxwpzfl"', '-c', 'user.email=socials.fl@icloud.com', '-c' , f"core.sshCommand=\"\"{privates.sshprogramm} -i {privates.pathtosshkey}\"\"", 'pull', 'origin', branch])
pullsubs = input('want to p(u)ll all submodlues aswell or (n)othing ')
if pullsubs == "u":
output = subprocess.run([f"{privates.gitprogramm}", '-c', f"core.sshCommand=\"\"{privates.sshprogramm} -i {privates.pathtosshkey}\"\"", 'submodule', 'update', '--init', '--recursive', '--remote'])
def forcepull():
global branch
savety = input('all local data will be overwritten to continue enter continue ')
if savety == "continue":
output = subprocess.run([f"{privates.gitprogramm}", 'fetch', '--all'])
output = subprocess.run([f"{privates.gitprogramm}", 'reset', '--hard', f"origin/{branch}"])
branch = input('put in branch name ')
output = subprocess.run([f"{privates.gitprogramm}", 'branch', '-m', branch])
output = subprocess.run([f"{privates.gitprogramm}", '-c', 'user.name="crbyxwpzfl"', '-c', 'user.email=socials.fl@icloud.com', '-c' , f"core.sshCommand=\"\"{privates.sshprogramm} -i {privates.pathtosshkey}\"\"", 'pull', 'origin', branch])
else:
print(f"recived \"{savety}\" aborted force pull")
def init():
global branch
branch = input('put in branch name ')
output = subprocess.run([f"{privates.gitprogramm}", 'init'])
output = subprocess.run([f"{privates.gitprogramm}", 'branch', '-m', branch])
repo = input('enter reposetory name ')
output = subprocess.run([f"{privates.gitprogramm}", 'remote', 'add', 'origin', f"git@github.com:crbyxwpzfl/{repo}.git"])
def add():
global branch
output = subprocess.run([f"{privates.gitprogramm}", 'add', '.'])
def commit():
global branch
comitmess = input('enter commit message ')
output = subprocess.run([f"{privates.gitprogramm}", '-c', 'user.name="crbyxwpzfl"', '-c', 'user.email=60987359+crbyxwpzfl@users.noreply.github.com', 'commit', '-m', comitmess])
def push():
global branch
output = subprocess.run([f"{privates.gitprogramm}", '-c', f"core.sshCommand=\"\"{privates.sshprogramm} -i {privates.pathtosshkey}\"\"", 'push', 'origin', branch])
def forcepush():
global branch
output = subprocess.run([f"{privates.gitprogramm}", '-c', f"core.sshCommand=\"\"{privates.sshprogramm} -i {privates.pathtosshkey}\"\"", 'push', 'origin', branch, '--force'])
def addsubs():
global branch
subremote = input('input the submodule ssh remote ')
subpath = input('input the submodule path rel to current dir ./')
output = subprocess.run([f"{privates.gitprogramm}", '-c', f"core.sshCommand=\"\"{privates.sshprogramm} -i {privates.pathtosshkey}\"\"", 'submodule', 'add', subremote, subpath])
def status():
#print git status and ask what to do
print()
print("---------------------------------------------------------------------------")
output = subprocess.run(['git', 'status'])
print()
global branch
#only git version 2.22 +
#output = subprocess.Popen([f"{privates.gitprogramm}", 'branch', '--show-current'], stdout=subprocess.PIPE)
#print(str(output.stdout.read()).replace("\\n", " ").strip("b''"))
output = subprocess.Popen([f"{privates.gitprogramm}", 'rev-parse', '--abbrev-ref', 'HEAD'], stdout=subprocess.PIPE)
branch = (str(output.stdout.read()).replace("\\n", "").strip("b''")) #this strips b from branch name aswell e.g. blabla will be labla
print(f"branch used for next action -{branch}-")
global frage
frage = input('p(u)ll or (i)nit or (a)dd change or (c)ommit or (p)ush or add (s)ubmodule or (n)othing ')
if str(frage) == "test":
test()
status()
if str(frage) == "u":
pull()
status()
if str(frage) == "forcepull":
forcepull()
status()
if str(frage) == "i":
init()
status()
if str(frage) == "a":
add()
status()
if str(frage) == "c":
commit()
status()
if str(frage) == "p":
push()
status()
if str(frage) == "forcepush":
forcepush()
status()
if str(frage) == "s":
addsubs()
status()
sys.exit()
print("end of script fehler")
status()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment