Repository navigation
Expand file tree
/
Copy pathgitupdate.py
More file actions
79 lines (63 loc) · 2.44 KB
/
Copy pathgitupdate.py
File metadata and controls
79 lines (63 loc) · 2.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import os
import subprocess
import sys
def run_git_command(command):
"""Helper function to run git commands and handle errors."""
try:
result = subprocess.run(
command, check=True, text=True, capture_output=True
)
return result.stdout
except subprocess.CalledProcessError as e:
print(f"\n❌ Error executing command: {' '.join(command)}")
print(f"Details: {e.stderr.strip()}")
sys.exit(1)
def auto_commit():
print("🚀 Git Auto-Commit Assistant 🚀")
print("-" * 35)
# 1. Check if the current directory is a git repository
if not os.path.exists(".git"):
print(
"❌ Error: This directory is not a Git repository. Run 'git init' first."
)
return
# 2. Check if there are any changes to commit
status = run_git_command(["git", "status", "--porcelain"])
if not status.strip():
print("💡 No changes detected. Nothing to commit!")
return
# Show current changes to the user
print("Current changes detected:")
print(status)
# 3. Prompt user for the commit message (name of changes)
commit_message = input("📝 Enter the name of your changes (commit message): ").strip()
if not commit_message:
print("❌ Commit message cannot be empty. Operation cancelled.")
return
print("\nProcessing your commit...")
# 4. Stage all changes (git add .)
print("📁 Staging changes...")
run_git_command(["git", "add", "."])
# 5. Commit changes (git commit -m "message")
print("🔒 Committing changes...")
commit_output = run_git_command(["git", "commit", "-m", commit_message])
print(commit_output.strip())
# 6. Ask user if they want to push to the remote repository
push_choice = (
input("\n🌐 Do you want to push these changes to GitHub? (y/n): ")
.strip()
.lower()
)
if push_choice in ["y", "yes"]:
print("☁️ Pushing to GitHub remote master/main branch...")
# Automatically detects current branch and pushes to origin
current_branch = run_git_command(
["git", "branch", "--show-current"]
).strip()
push_output = run_git_command(["git", "push", "origin", current_branch])
print(push_output.strip())
print("✅ Pushed successfully!")
else:
print("✅ Changes committed locally! Remember to push later.")
if __name__ == "__main__":
auto_commit()