I recently created a script that would automate the process of using git commands such as clone, commit, branch, pull, merge, blame and stash.
If you have any suggestions on what I should add and/or any improvements for the code, be sure to let me know. It's always very much appreciated.
Let's get into it!
Disclaimer: I didn't add all of the code, just the key functions.
For the whole code, source code.
Importing subprocess module:
import subprocess
Simple run function which we will be using for pretty much all of the code:
def run(*args):
return subprocess.check_call(['git'] + list(args))
Clone:
def clone():
print("\nYou will be asked for the user first and then the repository name\n")
user = input("User: ")
__user__ = f'{user}'
repo = input("Repository: ")
__repo__ = f'{repo}'
print("\nChoose the local path for your clone.")
local = input("Local path: ")
local_path = f'{local}'
subprocess.Popen(['git', 'clone', "https://github.com/" + __user__ + "/" + __repo__ + ".git", local_path])
Commit:
def commit():
message = input("\nType in your commit message: ")
commit_message = f'{message}'
run("commit", "-am", commit_message)
run("push", "-u", "origin", "master")
Creating a branch and pushing it to GitHub:
def branch():
branch = input("\nType in the name of the branch you want to make: ")
br = f'{branch}'
run("checkout", "-b", br)
choice = input("\nDo you want to push the branch right now to GitHub? (y/n): ")
choice = choice.lower()
if choice == "y":
run("push", "-u", "origin", br)
elif choice == "n":
print("\nOkay, goodbye!\n")
else:
print("\nInvalid command! Use y or n.\n")
All of the commands that are currently possible:
def main():
choices = 'clone, commit, branch, pull, merge, blame and stash'
print("Commands to use: " + choices)
choose_command = input("Type in the command you want to use: ")
choose_command = choose_command.lower()
if choose_command == "clone":
clone()
elif choose_command == "commit":
commit()
elif choose_command == "branch":
branch()
elif choose_command == "pull":
pull()
elif choose_command == "merge":
merge()
elif choose_command == "blame":
blame()
elif choose_command == "stash":
stash()
else:
print("\nNot a valid command!")
print("\nUse " + choices)
main()
Thanks for reading.
Discussion (3)
Awesome. Experimenting with building a little CI/CD pipeline for some of my Python code to Github. The 'subprocess' of git commands would do the trick as end points. Will use Django to build the dashboard. Just have to figure on a bridge maybe Python Scheduler so I can kick off scripts on demand or triggered. Thanks for sharing.
It reminds me of the time when I wrote this -- github.com/patarapolw/xgit
Nice!