-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit_sync.py
More file actions
62 lines (51 loc) · 1.72 KB
/
git_sync.py
File metadata and controls
62 lines (51 loc) · 1.72 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
"""
git_sync.py — Git integration for LevelUp CLI v2.1
Scans multiple local Git repositories for today's commits and awards XP.
Multi-repo paths configured via config.json.
"""
import subprocess
from datetime import datetime
from config import load_config
def scan_today_commits(repo_path: str = ".") -> list[str]:
"""Scan a Git repo for commits made today. Returns list of commit messages."""
try:
result = subprocess.run(
["git", "log", "--since=midnight", "--oneline", "--no-merges"],
cwd=repo_path,
capture_output=True,
text=True,
timeout=10,
)
if result.returncode != 0:
return []
lines = [l.strip() for l in result.stdout.strip().split("\n") if l.strip()]
return lines
except (subprocess.TimeoutExpired, FileNotFoundError, OSError):
return []
def scan_all_repos() -> dict[str, list[str]]:
"""
Scan all configured repositories for today's commits.
Returns {repo_path: [commit_messages]}.
"""
config = load_config()
repos = config.get("git_repos", ["."])
results = {}
for repo in repos:
commits = scan_today_commits(repo)
if commits:
results[repo] = commits
return results
def get_git_user(repo_path: str = ".") -> str | None:
"""Get the configured Git username."""
try:
result = subprocess.run(
["git", "config", "user.name"],
cwd=repo_path,
capture_output=True,
text=True,
timeout=5,
)
return result.stdout.strip() if result.returncode == 0 else None
except (subprocess.TimeoutExpired, FileNotFoundError, OSError):
return None
XP_PER_COMMIT = 10