通过git获取项目的变更记录并打包

Windows上通过git将指定时间到现在变更的文件根据项目文件夹结构打包成zip文件

param (
    [Parameter(Mandatory=$true)]
    [ValidatePattern("^\d{4}-\d{2}-\d{2}$")]
    [string]$SinceDate
)

# 设置路径
$basePath = Get-Location
$zipPath = Join-Path $basePath "changed_files.zip"
$tempDir = Join-Path $env:TEMP "changed_files_temp"

# 获取变更文件列表
$changedFiles = git log --since="$SinceDate" --until="now" --name-only --pretty=format: | Sort-Object | Get-Unique | Where-Object { $_ -and (Test-Path $_) }

if (-not $changedFiles) {
    Write-Host "没有找到自 $SinceDate 起的变更文件。"
    exit
}

# 清理旧的 zip 文件和临时目录
if (Test-Path $zipPath) { Remove-Item $zipPath -Force }
if (Test-Path $tempDir) { Remove-Item $tempDir -Recurse -Force }
New-Item -ItemType Directory -Path $tempDir | Out-Null

# 复制文件保留目录结构
foreach ($file in $changedFiles) {
    $sourcePath = Join-Path $basePath $file
    $destPath = Join-Path $tempDir $file
    $destDir = Split-Path $destPath
    if (!(Test-Path $destDir)) {
        New-Item -ItemType Directory -Path $destDir -Force | Out-Null
    }
    Copy-Item -Path $sourcePath -Destination $destPath
}

# 打包为 zip
Compress-Archive -Path "$tempDir\*" -DestinationPath $zipPath

# 清理临时目录
Remove-Item $tempDir -Recurse -Force

Write-Host "✅ 打包完成:$zipPath"

使用方式示例:

.\Export-ChangedFiles.ps1 -SinceDate "2025-09-02"