diff --git a/Python/Automate_Commiting_using_Git/Automate_Commiting_using_Git.png b/Python/Automate_Commiting_using_Git/Automate_Commiting_using_Git.png new file mode 100644 index 000000000..e8ffd2525 Binary files /dev/null and b/Python/Automate_Commiting_using_Git/Automate_Commiting_using_Git.png differ diff --git a/Python/Automate_Commiting_using_Git/README.md b/Python/Automate_Commiting_using_Git/README.md index 5efa0baa3..e104d12e7 100644 --- a/Python/Automate_Commiting_using_Git/README.md +++ b/Python/Automate_Commiting_using_Git/README.md @@ -1,47 +1,60 @@ # Automate commit using Git -![image](https://steadylearner.com/static/images/post/Python/python-github-by-Steadylearner.png) -Are you tired of adding, committing and pushing you code everytime you change it? If so, you can use this Python script to automate this boring stuff. +Are you tired of adding, committing and pushing your code everytime you change it? If so, you can use this Python script to automate this boring stuff. This code is the simplest one to automate the task. ## Understanding the code -![image](https://snipboard.io/iqvAFy.jpg) +![image](https://snipboard.io/JaSpdt.jpg) ``` import subprocess +import sys ``` -The subprocess module allows us to spawn processes, connect to their input/output/error pipes, and obtain their return codes. +The subprocess module allows us to spawn processes, connect to their input/output/error pipes, and obtain their return codes. sys module provides access to some variables used or maintained by the interpreter and to functions that interact strongly with the interpreter. ``` -subprocess.getoutput('git add .') +result = subprocess.run([GIT_PATH, "add", ":/"], check=True) + ``` -_subprocess.getoutput:_ Return output (stdout and stderr) of executing cmd in a shell. That means, it will execute the command `git add .` +This line of code stages all changes made to files in the root directory of the repository, no matter what subdirectory the user is in, using the git add command. ``` -message = input("Enter commit message") +message = input("Enter commit message (or press Enter to use default 'Auto commit'): ") + ``` -Now, you can simply understand that we are taking an input message to give it to the commit message in the next command. +This line of code prompts the user to enter a commit message. If the user enters nothing and presses Enter, the commit message will default to "Auto commit". ``` -subprocess.getoutput('git commit -m ' + message) +remote = input("Enter remote name (or press Enter to use default 'origin'): ") + ``` -In this line of code, we can see that the commit message will be appended to the command `git commit -m `. +This line of code prompts the user to enter a remote name for the repository. If the user enters nothing and presses Enter, the remote name will default to "origin". ``` -branchname = input("Enter branch name") +branchname = input("Enter branch name (or press Enter to use default 'HEAD'): ") +``` +This line of code prompts the user to enter a branch name to which the changes should be pushed. If the user enters nothing and presses Enter, the changes will be pushed to the current branch (HEAD) by default. + ``` +result = subprocess.run([GIT_PATH, "commit", "-m", message],check=True) -Then, give the branch name to which you want to push your code. +``` +This line of code commits the staged changes using the commit message provided by the user (or the default message, if none was provided). ``` -subprocess.getoutput('git push origin ' + branchname) +result = subprocess.run([GIT_PATH, "push", remote, branchname],check=True) + ``` +Finally, this line of code pushes the committed changes to the specified branch and remote. + +## Usage +To use this script, simply run it in the directory where your git repository is located. Follow the prompts to enter a commit message, remote name, and branch name, or press Enter to accept the defaults. -Finally, to push our code we are using, `git push origin `. -You can also add a variable to the remote url for defining the origin, but by default it is origin. +## Note +> This script assumes that you have already initialized a git repository in the directory where it is being run. If you have not done so, you will need to initialize a git repository using git init before using this script. diff --git a/Python/Automate_Commiting_using_Git/automate_commiting_using_git.png b/Python/Automate_Commiting_using_Git/automate_commiting_using_git.png deleted file mode 100644 index 56e41097e..000000000 Binary files a/Python/Automate_Commiting_using_Git/automate_commiting_using_git.png and /dev/null differ diff --git a/Python/Automate_Commiting_using_Git/automate_commiting_using_git.py b/Python/Automate_Commiting_using_Git/automate_commiting_using_git.py index 8cc4c01a2..a5489cd5a 100644 --- a/Python/Automate_Commiting_using_Git/automate_commiting_using_git.py +++ b/Python/Automate_Commiting_using_Git/automate_commiting_using_git.py @@ -1,7 +1,42 @@ import subprocess +import sys + +# Define the full path to Git +GIT_PATH = "/usr/bin/git" + +# Stage all changes from the root of the git project +result = subprocess.run([GIT_PATH, "add", ":/"], check=True) +if result.returncode != 0: + print("Error: Failed to stage changes.") + sys.exit(1) + +# Get commit message from user input or use default value +message = input("Enter commit message (or press Enter to use default): ") +if not message: + message = "Auto commit" + +# Get remote name from user input or use default value +remote = input("Enter remote name (or press Enter to use default 'origin'): ") +if not remote: + remote = "origin" + +# Get current branch name +# Get current branch name +branchname = input("Enter branch name (or press Enter to use default 'HEAD'): ") +if not branchname: + branchname = "HEAD" + +# Commit changes +result = subprocess.run([GIT_PATH, "commit", "-m", message],check=True) +if result.returncode != 0: + print("Error: Failed to commit changes.") + sys.exit(1) + +# Push changes to remote +result = subprocess.run([GIT_PATH, "push", remote, branchname],check=True) +if result.returncode != 0: + print("Error: Failed to push changes.") + sys.exit(1) + +print("Changes successfully committed and pushed to remote.") -subprocess.getoutput("git add .") -message = input("Enter commit message") -subprocess.getoutput("git commit -m " + message) -branchname = input("Enter branch name") -subprocess.getoutput("git push origin " + branchname)