LoginSignup
2
3

More than 5 years have passed since last update.

git archive で日本語の差分ファイルを抽出するPowerShell

Last updated at Posted at 2016-05-22

先日gitで変更のあったファイルを抜き出そうと思い、まず以下の記事を読んだのですが、

gitで差分ファイルを抽出する

職場はWindows環境でshellが動かせなかったため、さらに以下の記事からPowerShellを拝借しました。

git archiveで日本語を含むファイルの差分抽出できない場合の別案

そのまま動かしたら、対象となるファイルの数が多かったために、
コマンドが長くなりすぎてうまく動きませんでしたが、
1ファイルずつzipに追加するように変更したところ、うまくいきました。
以下、変更したソースになります。

Param($commit1 = "HEAD~", $commit2 = "HEAD")

$7zip = "C:\Program Files\7-Zip\7z.exe"
$archive = "update_files.zip"
$enc = [console]::OutputEncoding


#すでにアーカイブファイルができていたら削除
Remove-Item $archive -Force 2>&1 | Out-Null

#日本語ファイル名の文字化けを防ぐため一時的にコンソールをutf-8に変更
[console]::OutputEncoding = [text.encoding]::utf8

git diff --name-only $commit1 $commit2 | % {
  & "$7zip" a $archive $_
}

#コンソールを元に戻す
[console]::OutputEncoding = $enc
2
3
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3