You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Python免克隆仓库并在Bitbucket Server执行git log获取活动详情?

无需克隆仓库,用Python获取Bitbucket Server仓库活动记录的方法

当然有办法啦!不用克隆整个仓库,咱们可以借助Bitbucket Server自带的REST API来实现需求——直接通过Python发送请求就能拿到类似git log的提交活动记录,完全不用在本地拉取仓库内容。下面给你两种实用的实现方式:

方法一:用requests库直接调用REST API

这是最通用的方式,不需要额外的专用库,只需要用Python的requests发送HTTP请求即可。

步骤说明

  1. 安装依赖:先确保安装了requests库:
pip install requests
  1. 配置认证与参数:Bitbucket Server是私有仓库,需要用用户名+密码(或个人访问令牌,更安全)做基本认证,同时替换你的服务器地址、项目键和仓库slug。
  2. 编写代码获取记录:
import requests
from requests.auth import HTTPBasicAuth

# 替换成你的实际配置
BITBUCKET_SERVER = "https://your-bitbucket-server-domain"
PROJECT_KEY = "YOUR_PROJECT_SHORT_KEY"  # 比如项目URL里的PROJ
REPO_SLUG = "your-repository-slug"  # 仓库的URL标识名
USERNAME = "your-login-username"
ACCESS_TOKEN = "your-personal-access-token"  # 推荐用令牌,比密码安全

# 构建API请求地址
api_endpoint = f"{BITBUCKET_SERVER}/rest/api/latest/projects/{PROJECT_KEY}/repos/{REPO_SLUG}/commits"

# 分页获取所有提交记录
all_commits = []
start_offset = 0
page_size = 100  # 每次请求的记录数,Bitbucket有默认上限

while True:
    response = requests.get(
        api_endpoint,
        auth=HTTPBasicAuth(USERNAME, ACCESS_TOKEN),
        params={"start": start_offset, "limit": page_size}
    )
    
    if response.status_code != 200:
        print(f"请求出错:状态码 {response.status_code}")
        print(response.text)
        break
    
    commit_data = response.json()
    current_page_commits = commit_data["values"]
    
    if not current_page_commits:
        break  # 没有更多记录了
    
    all_commits.extend(current_page_commits)
    
    # 检查是否有下一页
    if "nextPageStart" not in commit_data:
        break
    start_offset = commit_data["nextPageStart"]

# 输出结果示例
print(f"总共获取到 {len(all_commits)} 条活动记录:")
for idx, commit in enumerate(all_commits[:5], 1):  # 只打印前5条示例
    print(f"\n第{idx}条提交:")
    print(f"哈希值:{commit['id']}")
    print(f"作者:{commit['author']['name']} <{commit['author']['emailAddress']}>")
    print(f"提交时间:{commit['authorTimestamp']}")
    print(f"提交信息:{commit['message'].strip()}")

这个API返回的是结构化的JSON数据,你可以根据需求提取更多字段,比如关联的分支、变更文件列表等,都能在响应中找到对应的键。

方法二:用专用Python客户端库(atlassian-python-api)

如果你经常和Atlassian产品打交道,用atlassian-python-api库会更省心——它封装了Bitbucket Server的所有API,不用自己拼接URL和处理分页逻辑。

步骤说明

  1. 安装库:
pip install atlassian-python-api
  1. 编写代码:
from atlassian import Bitbucket

# 初始化客户端
bitbucket_client = Bitbucket(
    url="https://your-bitbucket-server-domain",
    username="your-login-username",
    password="your-personal-access-token"
)

# 获取所有提交记录(自动处理分页)
all_commits = []
page_commits = bitbucket_client.get_commits(
    project_key=PROJECT_KEY,
    repository_slug=REPO_SLUG,
    limit=100
)

while page_commits:
    all_commits.extend(page_commits)
    page_commits = bitbucket_client.get_next_page(page_commits)

# 输出示例
for commit in all_commits[:3]:
    print(f"\n提交哈希:{commit['id']}")
    print(f"作者:{commit['author']['name']}")
    print(f"提交摘要:{commit['message'].split('\n')[0]}")

注意事项

  • 权限问题:确保你的账号有目标仓库的读取权限,否则会返回401或403错误。
  • 令牌安全:推荐使用Bitbucket Server的个人访问令牌代替密码,令牌可以设置过期时间和权限范围,更安全。
  • API版本:上面用的是latest版本的API,你也可以指定具体版本(比如/rest/api/1.0/),根据你的Bitbucket Server版本选择合适的API版本。

内容的提问来源于stack exchange,提问作者Mayank Srivastava

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 08:32:42