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

请求协助:通过Azure DevOps API生成仓库文件列表并保存至本地

导出Azure DevOps仓库全文件列表到本地

下面提供两种实用方案实现需求,你可以根据自己的技术栈选择:

方案1:使用Azure CLI(快速便捷)

适合不需要复杂逻辑的场景,只需几条命令就能完成:

  1. 登录Azure DevOps账号:
az devops login
  1. 设置默认组织和项目(避免后续命令重复输入):
az devops configure --defaults organization=https://dev.azure.com/你的组织名 project=你的项目名
  1. 递归导出仓库所有文件列表到本地文件:
az repos file list --repository 你的仓库名 --path "/" --recursive --output tsv > azure_repo_files.txt
  • 参数说明:
    • --recursive:递归遍历所有子目录
    • --output tsv:生成制表分隔格式,方便后续对比变更;如果需要JSON格式,替换为--output json
    • 输出文件azure_repo_files.txt会保存在当前脚本执行目录

方案2:使用Python调用Azure DevOps REST API(灵活自定义)

适合需要对文件列表做额外处理的场景,代码示例如下:

import requests
import base64

# 配置你的Azure DevOps信息
ORGANIZATION = "你的组织名"
PROJECT = "你的项目名"
REPO_NAME = "你的仓库名"
PAT = "你的个人访问令牌"  # PAT需具备代码读取权限
OUTPUT_FILE = "azure_repo_files.txt"

# 处理PAT的Base64编码(Azure DevOps API授权要求)
auth_token = base64.b64encode(f":{PAT}".encode()).decode()
headers = {"Authorization": f"Basic {auth_token}"}

# API请求基础地址(递归获取根路径下所有内容)
api_url = f"https://dev.azure.com/{ORGANIZATION}/{PROJECT}/_apis/git/repositories/{REPO_NAME}/items?path=/&recursive=true&api-version=7.1-preview.1"

all_file_paths = []
next_page_url = api_url

# 处理API分页(避免因文件数量过多导致数据截断)
while next_page_url:
    response = requests.get(next_page_url, headers=headers)
    response.raise_for_status()  # 请求失败时抛出异常
    response_data = response.json()
    
    # 过滤出文件(排除文件夹,若需要保留文件夹可去掉判断)
    files = [item["path"] for item in response_data["value"] if item["gitObjectType"] == "blob"]
    all_file_paths.extend(files)
    
    # 获取下一页地址
    next_page_url = response_data.get("nextLink")

# 将文件路径写入本地文件
with open(OUTPUT_FILE, "w", encoding="utf-8") as f:
    for path in sorted(all_file_paths):
        f.write(f"{path}\n")

print(f"文件列表已成功保存到 {OUTPUT_FILE}")
  • 注意事项:
    • 个人访问令牌(PAT)需在Azure DevOps中创建,权限至少勾选"代码(读取)"
    • 如果需要包含文件夹路径,删除代码中if item["gitObjectType"] == "blob"的判断条件

内容的提问来源于stack exchange,提问作者xiao chen

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.01 04:11:20