Last active
August 7, 2020 06:39
-
-
Save hikarin522/e50011818e4399e86b6f19b7f7fc6769 to your computer and use it in GitHub Desktop.
プルリクのLFSチェック
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env -S pwsh -nop | |
# プルリクにLFS化してないバイナリファイルが存在するかチェックする | |
# 引っかかったらLFS化した後 git rebase -i で fixup して歴史から消すこと | |
# 文字コード問題用 | |
# $OutputEncoding = [Text.Encoding]::UTF8 | |
# [Console]::OutputEncoding = $OutputEncoding | |
# [Console]::InputEncoding = $OutputEncoding | |
# とりあえず master へのプルリクとする | |
$logs = git log --stat --pretty=format:'%H %s' origin/master..HEAD | |
$commits = @() | |
foreach ($log in $logs) { | |
if ($log -match '^([\da-f]+) (.+)$') { | |
$commits += @{ | |
hash = $Matches[1] | |
message = $Matches[2] | |
files = @() | |
} | |
} elseif ($log -match '^ (.+) +\| +(.+)$') { | |
$commits[-1].files += @{ | |
line = $Matches[0] | |
path = $Matches[1] | |
details = $Matches[2] | |
} | |
} | |
} | |
$bin_commits = @() | |
foreach ($commit in $commits) { | |
$new_bin_files = @() | |
$change_bin_files = @() | |
foreach ($file in $commit.files) { | |
if ($file.details -match 'Bin +(\d+) +->') { | |
if ([long]($Matches[1]) -eq 0) { | |
$new_bin_files += $file | |
} else { | |
$change_bin_files += $file | |
} | |
} | |
} | |
if ($new_bin_files.Count -eq 0 -and $change_bin_files.Count -eq 0) { | |
continue | |
} | |
$bin_commits += @{ | |
hash = $commit.hash | |
message = $commit.message | |
new_bin_files = $new_bin_files | |
change_bin_files = $change_bin_files | |
} | |
} | |
if ($bin_commits.Count -ne 0) { | |
Write-Host '過去と未来、全ての未LFS化バイナリを生まれる前に消し去りたい' | |
Write-Host '※ LFS化した後 rebase -i で fixup して歴史から削除してください' | |
Write-Host '以下のコミットからバイナリファイルを検出しました' | |
foreach ($commit in $bin_commits) { | |
Write-Host '--------------------------------' | |
Write-Host "$($commit.hash): $($commit.message)" | |
if ($commit.new_bin_files.Count -ne 0) { | |
Write-Host '新たに追加されたバイナリファイル' | |
foreach ($file in $commit.new_bin_files) { | |
Write-Host $file.line | |
} | |
} | |
if ($commit.change_bin_files.Count -ne 0) { | |
Write-Host '変更されたバイナリファイル' | |
foreach ($file in $commit.change_bin_files) { | |
Write-Host $file.line | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment