git和svn接口调用小试

gitlab

python 复制代码
import requests
import pandas as pd

# GitLab API设置
GITLAB_API_URL = "https://gitlab.com/api/v4"
PROJECT_ID = "your_project_id" 
ACCESS_TOKEN = "your_access_token"


def get_gitlab_commits(project_id, token, per_page=100):
    """获取 GitLab 仓库的所有提交记录"""
    commits = []
    page = 1

    while True:
        # 构造 API 请求 URL
        url = f"{GITLAB_API_URL}/projects/{project_id}/repository/commits"
        params = {
            "per_page": per_page,
            "page": page,
            "private_token": token
        }

        # 发送请求获取提交记录
        response = requests.get(url, params=params)
        if response.status_code != 200:
            raise Exception(f"Failed to fetch commits: {response.text}")

        data = response.json()
        if not data:
            break  # 如果没有更多数据则退出

        commits.extend(data)
        page += 1

    return commits


def get_changed_files_in_commit(project_id, token, commit_sha):
    """获取指定提交的所有变更文件"""
    url = f"{GITLAB_API_URL}/projects/{project_id}/repository/commits/{commit_sha}/diff"
    params = {
        "private_token": token
    }
    response = requests.get(url, params=params)

    if response.status_code != 200:
        raise Exception(f"Failed to fetch commit diff: {response.text}")

    data = response.json()
    changed_files = [file['new_path'] for file in data if
                     file['new_path'].endswith('.c') or file['new_path'].endswith('.h')]

    return changed_files


def count_file_changes(project_id, token):
    """统计 .c 和 .h 文件的历史变更次数"""
    commits = get_gitlab_commits(project_id, token)
    file_changes = {}

    # 遍历每个提交记录,获取变更的文件
    for commit in commits:
        commit_sha = commit['id']
        changed_files = get_changed_files_in_commit(project_id, token, commit_sha)

        for file_path in changed_files:
            if file_path not in file_changes:
                file_changes[file_path] = 0
            file_changes[file_path] += 1  # 每次出现,变更次数加1

    # 对于没有变更的文件,记录为 0
    all_files = set(file_changes.keys())
    return file_changes


def save_to_excel(file_changes, output_file='file_changes.xlsx'):
    """将文件变更次数保存到 Excel 文件"""
    # 将文件变更次数转换为 DataFrame
    df = pd.DataFrame(list(file_changes.items()), columns=['File', 'Change Count'])

    # 保存为 Excel 文件
    df.to_excel(output_file, index=False, engine='openpyxl')
    print(f"File changes have been saved to {output_file}")


def main():
    # 获取文件变更统计
    file_changes = count_file_changes(PROJECT_ID, ACCESS_TOKEN)

    # 将结果保存为 Excel 文件
    save_to_excel(file_changes)


if __name__ == '__main__':
    main()

svn

python 复制代码
import subprocess
import xml.etree.ElementTree as ET
import pandas as pd


def run_svn_command(cmd, repo_url=None, working_copy=None):
    """运行 SVN 命令并返回输出"""
    if working_copy:
        cmd.insert(0, working_copy)  
    elif repo_url:
        cmd.insert(0, repo_url) 

    result = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
    if result.returncode != 0:
        raise Exception(f"SVN command failed: {result.stderr}")
    return result.stdout


def get_svn_log(repo_url, start_rev=0, limit=100):
    """获取 SVN 仓库的提交日志(分页)"""
    cmd = ['svn', 'log', '--xml', '--verbose', '--start', str(start_rev), '--limit', str(limit)]
    log_output = run_svn_command(cmd, repo_url=repo_url)
    return log_output


def get_changed_files_in_commit(log_xml):
    """从 SVN 日志 XML 输出中提取每个提交的变化文件"""
    changed_files = []
    root = ET.fromstring(log_xml)

    for logentry in root.findall('logentry'):
        revision = logentry.get('revision')
        for path in logentry.findall('paths/path'):
            file_path = path.text
            if file_path.endswith('.c') or file_path.endswith('.h'):
                changed_files.append((revision, file_path))

    return changed_files


def get_all_commits(repo_url):
    """获取 SVN 仓库所有提交的 .c 和 .h 文件的变更历史"""
    start_rev = 0
    limit = 100  # 每次请求获取 100 条日志
    all_changed_files = []
    while True:
        log_xml = get_svn_log(repo_url, start_rev, limit)
        changed_files = get_changed_files_in_commit(log_xml)

        if not changed_files:
            break  # 没有更多提交记录,停止循环

        all_changed_files.extend(changed_files)

        # 获取下一次请求的起始版本号
        root = ET.fromstring(log_xml)
        last_revision = root.find('logentry').get('revision')
        start_rev = int(last_revision) + 1

    return all_changed_files


def count_file_changes(repo_url):
    """统计 SVN 仓库中每个 .c 和 .h 文件的变更次数"""
    all_changed_files = get_all_commits(repo_url)
    file_changes = {}

    for revision, file_path in all_changed_files:
        if file_path not in file_changes:
            file_changes[file_path] = 0
        file_changes[file_path] += 1  # 每次出现,变更次数加1

    return file_changes


def save_to_excel(file_changes, output_file='file_changes.xlsx'):
    """将文件变更次数保存到 Excel 文件"""
    # 将文件变更次数转换为 DataFrame
    df = pd.DataFrame(list(file_changes.items()), columns=['File', 'Change Count'])

    # 保存为 Excel 文件
    df.to_excel(output_file, index=False, engine='openpyxl')
    print(f"File changes have been saved to {output_file}")


def main():
    # 远程 SVN 仓库的 URL
    repo_url = 'https://your-svn-repository-url/path/to/repository'

    # 获取文件变更统计
    file_changes = count_file_changes(repo_url)

    # 将结果保存为 Excel 文件
    save_to_excel(file_changes)


if __name__ == '__main__':
    main()
相关推荐
无足鸟ICT4 小时前
【RHCA+】安装Tuned
linux·服务器·centos
Decay__8 小时前
VMware 安装 SQL Server 双节点 AlwaysOn 可用性组教程 —— WSFC、AG 与侦听器完整部署(系列核心篇)
运维·数据库·sql·mysql·架构
测试界的小码农8 小时前
【Linux 命令实战(06):Linux 文件权限管理,掌握 chmod、chown、chgrp】
linux·运维·服务器
艺杯羹9 小时前
1Panel 装好后 ufw 仍是 inactive:一次服务器安全盲区复盘
服务器·数据库·安全·网络安全·系统架构·实战·1panel
陈希1239 小时前
负载均衡与高可用:LVS、HAProxy、Keepalived 实战
运维·负载均衡·lvs
一条破秋裤10 小时前
【1114】【并发控制】【笔记】
linux
2035去旅行12 小时前
ADB的底层原理
linux·adb·嵌入式·arm·soc
武子康12 小时前
单卡 A6000 跑 Qwen-Drive:怎样留下可比较的第一条基线?
linux·人工智能·深度学习
女神下凡12 小时前
芯参谋(107):杰理AC_中科蓝讯_开发指南
linux·服务器·网络·单片机·嵌入式硬件
落羽的落羽12 小时前
【C++】C++17的主要特性
linux·服务器·开发语言·c++·算法·机器学习