惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

N
News and Events Feed by Topic
T
The Exploit Database - CXSecurity.com
P
Palo Alto Networks Blog
T
Threat Research - Cisco Blogs
Cloudbric
Cloudbric
Recent Commits to openclaw:main
Recent Commits to openclaw:main
I
Intezer
Attack and Defense Labs
Attack and Defense Labs
P
Privacy International News Feed
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
L
Lohrmann on Cybersecurity
C
Cybersecurity and Infrastructure Security Agency CISA
V2EX - 技术
V2EX - 技术
AWS News Blog
AWS News Blog
O
OpenAI News
L
LINUX DO - 最新话题
N
News | PayPal Newsroom
PCI Perspectives
PCI Perspectives
www.infosecurity-magazine.com
www.infosecurity-magazine.com
T
Troy Hunt's Blog
Latest news
Latest news
D
Darknet – Hacking Tools, Hacker News & Cyber Security
A
Arctic Wolf
Spread Privacy
Spread Privacy
G
GRAHAM CLULEY
T
Tor Project blog
博客园_首页
Know Your Adversary
Know Your Adversary
有赞技术团队
有赞技术团队
S
Secure Thoughts
美团技术团队
Apple Machine Learning Research
Apple Machine Learning Research
爱范儿
爱范儿
T
Tailwind CSS Blog
Application and Cybersecurity Blog
Application and Cybersecurity Blog
V
Visual Studio Blog
J
Java Code Geeks
Cisco Talos Blog
Cisco Talos Blog
Schneier on Security
Schneier on Security
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
S
Security Affairs
Jina AI
Jina AI
人人都是产品经理
人人都是产品经理
雷峰网
雷峰网
宝玉的分享
宝玉的分享
量子位
Last Week in AI
Last Week in AI
月光博客
月光博客
罗磊的独立博客
S
SegmentFault 最新的问题

运维开发绿皮书

OBS虚拟摄像头重命名 Ubuntu备份为LiveOS 使用Powershell卸载windows默认程序 Cisco路由器OSPF配置 汽车的分类和特点 Windows11跳过TPM2.0 HTTPS 双向认证与 USB 加密锁配置实战 SSL 证书工具 字数统计 BMI 计算 颜色转换 Hash 生成器 JSON 格式化 JWT 解码 房贷计算 时间戳转换 URL 编码 UUID 生成 牛马时钟 二维码批量生成器 CKS Simulator Kubernetes 1.25 FRP TOTP验证码生成器 direct-ssh-passthrough-nat 脚本使用说明 Python软件授权 Linux命令行百度网盘 为 Containerd 配置 Harbor 无证书镜像站 Docker一键部署Meta和MetacubexD面板 必应搜索屏蔽垃圾网站 VMware的ubuntu完整安装vm-tools支持粘贴板 Docker Desktop 安装到其他位置 Dell EMC PowerEdge R740服务器内存插槽使用说明及正确安装方法 tc常用命令总结 OpenStack排错 稳定币监管与投资指南 VMware ESXi 密码恢复指南 VMware 加密 state.tgz 文件解密教程 六百万数据 MySQL count(*) 优化 使用iptables禁止特定子网访问指定端口 解决 iptables DNAT 无法转发到 127.0.0.1 的问题及安全风险分析 Linux 一键测试脚本 Gitlab配置详解 Gitlab批量创建用户 Ubuntu安装VNC教程 Windows11跳过微软账号登录 vue配置Nginx伪静态 Linux下Ollama开放端口 模型详解配置 修改Git提交历史中的作者信息 在PowerShell中启动Git Bash的方法 一键安装OpenVPN 大语言模型 模型架构-Transformer模型 训练相关资料 GPT&DeepSeek模型 大模型技术基础 Git Bash 中一键安装 pacman Gitlab-Runner的一些问题 Docker运行kwaivgi-liveportrait Firefox密码提取 在x86-64上构建和运行多种架构的Docker容器 flutter配置镜像站 VMware最新下载地址(纪念碑) 一键安装Conda Docker老版本runc报错无法启动 清理Rancher节点 aapanel 7.x 中文语言配置指南 Ubuntu 22 安装 Kubernetes 1.29 集群指南 Ubuntu修复CNVD-2024-4920726 GRUB锁定密码 Grub2手动引导Linux Cron在线表达式生成器 GitLab一键设置镜像源 文本字符串倒序 Windows禁用任务管理器 UOS(1070a)服务器版本部署Kubernetes1.28 Windows11恢复Windows10右键菜单 Ubuntu一键设置镜像源 CentOS 7 一键设置Vault镜像 Base64在线编码解码 代码差异对比 为Docker分配物理网卡 解密CFSSL生成的CRL数据 已经在谷底了 怎么走都是向上 Windows11的24H2出现扩展错误 Windows绕过MicroSoft Store直接下载应用 Fossy平台离线部署分析 在WSL中移除Windows环境变量 重装MicroSoft Store Sony Xperia 10 IV(pdx225)AOSP14编译和刷机方法 pnpm在DevContainer中存储出错解决方法 从SVN迁移仓库到Git Ubuntu中移除Snap软件包 使用Netcat检测UDP端口连通性 rke2集群命令行调试方法 Ubuntu隐私优化-关闭公共门户连接检查 Cisco路由器配置ipv4和ipv6的VTY账户密码 Python实现九九乘法表 Windows10中用多网卡链路聚合来解决网卡网速瓶颈 CentOS7 安装 OpenResty
GitLab统计提交代码行数Python代码
Paper-Dragon · 2024-11-01 · via 运维开发绿皮书

GitLab统计提交代码行数Python代码

要求统计GitLab所有项目的任何人提交代码行数

代码逻辑

  • 获取仓库列表
  • 克隆仓库
  • 在本地统计

代码如下

# http://gitlabr.cidana.com/
import os
from git import Repo
import requests
import subprocess

GITLAB_URL = 'http://gitlabr.c4a.com/'
GITLAB_TOKEN = 'hboA_yaibr3b2WwFahaQ'
GITLAB_CLONE_METHOD = 'ssh_url_to_repo'  # http_url_to_repo ssh_url_to_repo
ssh_private_key = "./id_rsa"
merge_dict = {}


def get_gitlab_repositories():
    url = f'{GITLAB_URL}/api/v4/projects?per_page=5'
    headers = {
        'Authorization': f'Bearer {GITLAB_TOKEN}',
    }
    response = requests.get(url, headers=headers)
    response.raise_for_status()
    repositories = []
    for i in response.json():
        repositories.append(i[GITLAB_CLONE_METHOD])
    return repositories


def clone_repository(clone_url):
    env = os.environ.copy()
    env['PKEY'] = GITLAB_CLONE_METHOD
    subprocess.run(['git', 'clone', '--no-checkout', clone_url], env=env)


def walk_dir():
    # 获取当前目录下的所有Git仓库
    repo_dirs = []
    for d in os.listdir('.'):
        if os.path.isdir(d) and '.git' in os.listdir(d):
            repo_dirs.append(d)
    return repo_dirs


def get_commit_lines(repo_path):
    repo = Repo(repo_path)
    commits = list(repo.iter_commits())
    last_commit = commits[-1]
    total_lines_added = last_commit.stats.total['lines']
    return total_lines_added


def get_all_committers(repo_path):
    repo = Repo(repo_path)
    committers = set()
    for commit in repo.iter_commits():
        committers.add(commit.author.name)
    return committers


def count_lines_per_committer(repo_path):
    repo = Repo(repo_path)
    committers = get_all_committers(repo_path)
    lines_per_committer = {}
    for committer in committers:
        total_lines = 0
        for commit in repo.iter_commits():
            if commit.author.name == committer:
                total_lines += commit.stats.total['lines']
        lines_per_committer[committer] = total_lines
        if committer in merge_dict:
            merge_dict[committer] = merge_dict[committer] + total_lines
        else:
            merge_dict[committer] = total_lines
    return lines_per_committer


def calc(repo_path):
    lines_per_committer = count_lines_per_committer(repo_path)
    return lines_per_committer


def main():
    for i in walk_dir():
        print(i)
        print(calc(i))
    print(dict(sorted(merge_dict.items(), key=lambda item: item[1], reverse=True)))
    # repositories = get_gitlab_repositories()
    # for repository in repositories:
    #     clone_repository(repository)


if __name__ == '__main__':
    main()

更新日志

  • 33ada-optimize
  • 75477-Gitlab统计提交代码行数Python代码