Initial upload: TermDiagram v0.1.0
This commit is contained in:
42
src/termdiagram/git/history_tracker.py
Normal file
42
src/termdiagram/git/history_tracker.py
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
from typing import List, Optional
|
||||||
|
from dataclasses import dataclass
|
||||||
|
from git import Repo
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class CommitInfo:
|
||||||
|
hash: str
|
||||||
|
message: str
|
||||||
|
author: str
|
||||||
|
date: Optional[str] = None
|
||||||
|
|
||||||
|
|
||||||
|
class GitHistoryTracker:
|
||||||
|
def __init__(self, repo_path: str):
|
||||||
|
self.repo_path = repo_path
|
||||||
|
self.repo: Optional[Repo] = None
|
||||||
|
|
||||||
|
def is_git_repo(self) -> bool:
|
||||||
|
try:
|
||||||
|
self.repo = Repo(self.repo_path)
|
||||||
|
return not self.repo.bare
|
||||||
|
except:
|
||||||
|
return False
|
||||||
|
|
||||||
|
def get_commit_history(self, limit: int = 20) -> List[CommitInfo]:
|
||||||
|
if not self.repo:
|
||||||
|
if not self.is_git_repo():
|
||||||
|
return []
|
||||||
|
|
||||||
|
commits = []
|
||||||
|
for commit in self.repo.iter_commits("HEAD", max_count=limit):
|
||||||
|
commits.append(
|
||||||
|
CommitInfo(
|
||||||
|
hash=commit.hexsha[:7],
|
||||||
|
message=commit.message.strip(),
|
||||||
|
author=f"{commit.author.name} <{commit.author.email}>",
|
||||||
|
date=commit.committed_datetime.strftime("%Y-%m-%d %H:%M"),
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
return commits
|
||||||
Reference in New Issue
Block a user