如何在Python脚本中获取或计算Bitbucket Server提交哈希?
一、直接在Python中计算Git提交哈希
你之前计算blob哈希的思路是对的,但Git提交对象的结构和blob不一样,咱们先理清楚提交对象的格式:
Git的提交对象是一段结构化文本,格式大致如下:
tree <对应的树对象哈希值>
parent <父提交的哈希值(首次提交无此行)>
author <作者名> <作者邮箱> <Unix时间戳> <时区偏移>
committer <提交者名> <提交者邮箱> <Unix时间戳> <时区偏移><提交信息>
计算提交哈希的逻辑和blob类似,步骤是:
- 把上述提交内容拼接成完整字符串
- 计算该字符串的字节长度
- 构造前缀:
commit <字节数>\0 - 将前缀与提交内容拼接后,用SHA-1计算哈希,得到的就是提交哈希值
下面是具体的Python实现代码:
from hashlib import sha1 def calculate_commit_hash(tree_hash, parent_hashes=None, author_name="", author_email="", author_timestamp=0, author_tz="+0000", committer_name="", committer_email="", committer_timestamp=0, committer_tz="+0000", commit_message=""): # 初始化提交内容的行列表 commit_lines = [f"tree {tree_hash}"] # 添加父提交(支持多个父提交,比如合并提交场景) if parent_hashes: for parent in parent_hashes: commit_lines.append(f"parent {parent}") # 追加作者与提交者信息 commit_lines.append(f"author {author_name} <{author_email}> {author_timestamp} {author_tz}") commit_lines.append(f"committer {committer_name} <{committer_email}> {committer_timestamp} {committer_tz}") # 空行分隔提交信息 commit_lines.append("") commit_lines.append(commit_message) # 拼接成完整的字节流提交内容 commit_content = "\n".join(commit_lines).encode("utf-8") # 构造Git提交对象的前缀 prefix = f"commit {len(commit_content)}\0".encode("utf-8") # 计算SHA-1哈希 h = sha1() h.update(prefix + commit_content) return h.hexdigest() # 示例使用:需提前获取tree哈希等信息 if __name__ == "__main__": commit_hash = calculate_commit_hash( tree_hash="a1b2c3d4e5f67890abcdef1234567890abcdef12", parent_hashes=["9876543210fedcba09876543210fedcba0987654"], author_name="Mathieu", author_email="mathieu@example.com", author_timestamp=1690000000, author_tz="+0200", committer_name="Mathieu", committer_email="mathieu@example.com", committer_timestamp=1690000100, committer_tz="+0200", commit_message="Fix bug in blob hash calculation" ) print(f"计算得到的提交哈希:{commit_hash}")
需要注意的是,你得先获取对应的tree_hash(可以通过计算目录树对象哈希,或从Git仓库中查询),以及作者、提交者的时间戳等信息——如果是参考现有仓库的提交格式,可以用git cat-file -p <commit_hash>查看提交对象的原始内容。
二、从Bitbucket Server直接获取提交哈希
如果不想自己计算,直接从Bitbucket Server拉取是更省心的方案,你提到的Stashy确实是官方推荐的Python客户端,用它操作Bitbucket Server的API非常便捷:
步骤1:安装Stashy
pip install stashy
步骤2:使用Stashy获取提交哈希
import stashy # 连接到你的Bitbucket Server # 推荐用个人访问令牌(Personal Access Token)替代密码,安全性更高 bitbucket_url = "https://your-bitbucket-server-domain" username = "your-username" password_or_token = "your-password-or-token" client = stashy.connect(bitbucket_url, username, password_or_token) # 指定项目Key和仓库Slug(可从Bitbucket仓库页面的URL中找到) project_key = "YOUR_PROJECT_KEY" repo_slug = "YOUR_REPO_SLUG" # 获取仓库对象 repo = client.projects[project_key].repos[repo_slug] # 获取最新提交的哈希 latest_commit = repo.commits.list(limit=1)[0] print(f"最新提交哈希:{latest_commit['id']}") # 获取指定分支的最新提交哈希 target_branch = "main" branch_latest_commit = repo.commits.list(branch=target_branch, limit=1)[0] print(f"{target_branch}分支最新提交哈希:{branch_latest_commit['id']}") # 获取指定部分哈希对应的完整提交信息 specific_commit = repo.commits.get("a1b2c3d") print(f"指定提交的完整哈希:{specific_commit['id']}")
如果不想依赖Stashy,也可以直接调用Bitbucket Server的REST API:发送GET请求到https://your-bitbucket-server-domain/rest/api/1.0/projects/{project_key}/repos/{repo_slug}/commits?limit=1,然后解析返回的JSON数据,提取values[0].id即可得到最新提交哈希。
内容的提问来源于stack exchange,提问作者Mathieu

