Repository navigation
Expand file tree
/
Copy pathgit_sync.py
More file actions
108 lines (89 loc) · 3.84 KB
/
Copy pathgit_sync.py
File metadata and controls
108 lines (89 loc) · 3.84 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
"""
git_sync.py — Automated GitHub Sync Utility
Commits all changes and pushes to the configured GitHub remote.
Uses the Windows Credential Manager for authentication (pre-configured).
Usage:
python git_sync.py # Auto-commit with timestamp message
python git_sync.py "Custom message" # Custom commit message
This script is safe to run repeatedly — it only commits if there are changes.
"""
import os
import sys
import subprocess
from datetime import datetime
# ── Configuration ───────────────────────────────────────────────
REPO_DIR = os.path.dirname(os.path.abspath(__file__))
DEFAULT_BRANCH = "master"
def run_git_command(args, capture_output=True):
"""
Execute a git command in the repository directory.
Returns (success: bool, output: str).
"""
try:
result = subprocess.run(
["git"] + args,
cwd=REPO_DIR,
capture_output=capture_output,
text=True,
timeout=60
)
output = (result.stdout or "") + (result.stderr or "")
return result.returncode == 0, output.strip()
except subprocess.TimeoutExpired:
return False, "Command timed out after 60 seconds"
except FileNotFoundError:
return False, "Git is not installed or not in PATH"
def has_changes():
"""Check if there are any uncommitted changes in the repository."""
success, output = run_git_command(["status", "--porcelain"])
return success and len(output) > 0
def sync_to_github(commit_message=None):
"""
Main sync workflow:
1. Stage all changes (respects .gitignore)
2. Commit with a timestamped message
3. Push to origin
"""
print("=" * 50)
print("TaskMaster — GitHub Sync")
print("=" * 50)
print(f"Repository: {REPO_DIR}")
print()
# ── Check for changes ────────────────────────────────────
if not has_changes():
print("No changes to commit. Repository is clean.")
return True
# ── Stage all changes ────────────────────────────────────
print("[1/3] Staging changes...")
success, output = run_git_command(["add", "."])
if not success:
print(f" ERROR: Failed to stage changes: {output}")
return False
print(" Done.")
# ── Commit ───────────────────────────────────────────────
if commit_message is None:
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
commit_message = f"Auto-sync: {timestamp}"
print(f"[2/3] Committing: {commit_message}")
success, output = run_git_command(["commit", "-m", commit_message])
if not success:
print(f" ERROR: Commit failed: {output}")
return False
print(" Done.")
# ── Push to origin ───────────────────────────────────────
print(f"[3/3] Pushing to origin/{DEFAULT_BRANCH}...")
success, output = run_git_command(["push", "origin", DEFAULT_BRANCH])
if not success:
print(f" ERROR: Push failed: {output}")
print(" Tip: Make sure your Git credentials are configured.")
print(" Run: git config credential.helper manager")
return False
print(" Done.")
print()
print("Sync complete! Changes pushed to GitHub.")
return True
if __name__ == "__main__":
# Accept an optional custom commit message from the command line
custom_message = " ".join(sys.argv[1:]) if len(sys.argv) > 1 else None
success = sync_to_github(custom_message)
sys.exit(0 if success else 1)