获取 Git 仓库的 commit 信息
(repo_path: Path)
| 70 | |
| 71 | |
| 72 | def get_commit_info(repo_path: Path) -> Dict[str, str]: |
| 73 | """获取 Git 仓库的 commit 信息""" |
| 74 | try: |
| 75 | repo = git.Repo(repo_path, search_parent_directories=True) |
| 76 | commit = repo.head.commit |
| 77 | short_hash = commit.hexsha[:7] |
| 78 | |
| 79 | remote_url, commit_link = "", "" |
| 80 | try: |
| 81 | remote_url = repo.remotes.origin.url |
| 82 | if remote_url.endswith(".git"): |
| 83 | remote_url = remote_url[:-4] |
| 84 | remote_url = re.sub(r"git@([^:]+):", r"https://\1/", remote_url) |
| 85 | commit_link = f"{remote_url}/commit/{commit.hexsha}" |
| 86 | except Exception: |
| 87 | pass |
| 88 | |
| 89 | return { |
| 90 | "hash": commit.hexsha, |
| 91 | "short_hash": short_hash, |
| 92 | "message": commit.message.strip().split("\n")[0], |
| 93 | "author": commit.author.name, |
| 94 | "date": commit.committed_datetime.strftime("%Y-%m-%d %H:%M:%S"), |
| 95 | "link": commit_link, |
| 96 | } |
| 97 | except git.InvalidGitRepositoryError: |
| 98 | return {"error": "不是一个有效的 Git 仓库"} |
| 99 | except Exception as e: |
| 100 | return {"error": f"获取 Git 信息时出错:{e}"} |
| 101 | |
| 102 | |
| 103 | # --- 主分析类 --- |