使用 Python 获取今天 svn / git 提交的 log

479 阅读1分钟

svn log 输出神器

使用 Python 获取今天 svn 提交的 log,有的公司是要求每天提交工作内容的,有了这个神器,你就可以方便整理工作内容了

import os
import subprocess

from datetime import datetime

# 你的 svn 提交目录
svn_log_dir = "D:/app"

#获取当前操作系统
current_os = os.name

# 根据当前操作系统选择换行符
if current_os == 'nt':  # Windows系统
    line_break = '\r\n'
elif current_os == 'posix':  # Linux、Unix-like系统
    line_break = '\n'
else:  # 其他操作系统,默认使用换行符'\n'
    line_break = '\n'

now = datetime.now()

# time =  "-r{2023-12-01T00:00:00}:{2023-12-28T13:00:00}"

# 时间段只看今天的提交信息
time =  f'-r{{{now.year}-{now.month}-{now.day}T00:00:00}}:{{{now.year}-{now.month}-{now.day}T23:00:00}}'
command = ['svn', 'log', time]

os.chdir(svn_log_dir) # 

result = subprocess.run(command, capture_output=True)

# 解析输出以获取常见信息
output = result.stdout.decode('gbk', 'ignore')
lines = output.split(line_break)
for line in lines:
    if "ljf" in line or "-------" in line or line == "":
        continue
    print(line.replace("\n", "").replace("\r", ""))

git log 的写法

import os
import subprocess

from datetime import datetime, timedelta


def diplay_log(tag, git_log_dir):
    # 作者的邮箱
    author_email = "lujianfei@myatoto.cn"

    # 获取昨天的日期
    yesterday = datetime.now() - timedelta(days=1)

    # 计算今天的日期
    today = datetime.now()

    # 构造 git log 命令
    command = ['git', 
            'log',
            '--oneline',
            '--pretty=format:%s',
            '--author={}'.format(author_email),
            '--since={}'.format(yesterday.strftime('%Y-%m-%d')),
            '--until={}'.format(today.strftime('%Y-%m-%d'))]

    os.chdir(git_log_dir) # 

    # 运行 git log 命令,设置 encoding 为 'utf-8'
    result = subprocess.run(
        command,
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE,
        check=True,
        text=True,
        encoding='utf-8'
    )

    # 解析输出以获取常见信息
    output = result.stdout

    print(tag)
    print(output)
    print()

diplay_log(tag="--------------------------------标记--------------------------------", git_log_dir="你的项目目录")