commit 1c96435c84120a5159a747ec199e35c47bc76832
parent 5187970abcf394a75aaea4f155241e29db51c4b9
Author: Amit Dutta <amitdutta4255@gmail.com>
Date: Sun, 4 Jan 2026 18:27:54 +0530
[2026-01-04] : .\{root} : Updated folder path decider.
Diffstat:
1 file changed, 1 insertion(+), 21 deletions(-)
diff --git a/git_automation.py b/git_automation.py
@@ -2,22 +2,17 @@ import os
import subprocess
from datetime import datetime
-# --- CONFIGURATION ---
-# The root of your git repository (where the .git folder is)
REPO_ROOT_DIRECTORY = r"G:\bsc"
def main():
print("--- Git Automation ---")
- # 1. Change directory to the root of the repo
if not os.path.exists(REPO_ROOT_DIRECTORY):
print(f"[Error] Repository root not found: {REPO_ROOT_DIRECTORY}")
return
- # Switch to repo root so git commands work correctly
os.chdir(REPO_ROOT_DIRECTORY)
- # 2. Git Add
print(f"\n> git add .")
try:
subprocess.run(["git", "add", "."], check=True)
@@ -25,10 +20,7 @@ def main():
print("[Error] 'git add' failed.")
return
- # 3. Detect Changed Files & Determine Path
try:
- # Run git diff to see what is staged (what was just added)
- # --name-only lists the filenames, --cached looks at the staging area
result = subprocess.run(
["git", "diff", "--name-only", "--cached"],
capture_output=True,
@@ -44,46 +36,35 @@ def main():
print("\n[Info] No changes detected to commit.")
return
- # Calculate the common directory of all changed files
- # We strip filenames to get just directories, and normalize slashes for Windows
changed_dirs = []
for f in changed_files:
- # Git uses forward slashes; convert to OS separator (backslash on Windows)
normalized_path = f.replace('/', os.sep)
dirname = os.path.dirname(normalized_path)
changed_dirs.append(dirname)
- # Find the most specific common folder
if not changed_dirs:
common_path = ""
else:
try:
- # os.path.commonpath finds the deepest common folder
- # e.g., if files changed in "Semester_1\A" and "Semester_1\B", result is "Semester_1"
common_path = os.path.commonpath(changed_dirs)
except ValueError:
common_path = ""
- # Format path for message: .\Semester_1\subfolder
if not common_path:
- path_str = "."
+ path_str = ".\\{root}"
else:
path_str = f".\\{common_path}"
print(f"\n[Detected Changes in]: {path_str}")
- # 4. Get User Message
user_message = input(f"Enter commit message for '{path_str}': ")
if not user_message:
print("Commit message cannot be empty. Aborting.")
return
- # 5. Construct the Commit Message
today = datetime.now().strftime("%Y-%m-%d")
- # Final Format: [YYYY-MM-DD] : .\path : message
full_commit_msg = f"[{today}] : {path_str} : {user_message}"
- # 6. Git Commit
print(f"\n> git commit -m \"{full_commit_msg}\"")
try:
subprocess.run(["git", "commit", "-m", full_commit_msg], check=True)
@@ -91,7 +72,6 @@ def main():
print("[Error] 'git commit' failed.")
return
- # 7. Git Push
print("\n> git push")
try:
subprocess.run(["git", "push"], check=True)