LoginSignup
4
5

More than 5 years have passed since last update.

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

Posted at

windowsのpowershell上でgit archiveによる差分ファイルの抽出をしたけど
日本語を含むファイルがzipに含まれてなかったので別案を考えてみた。

実行したのは以下のコマンド
ps1:
git archive --format=zip head $(git diff head head~1 --name-only) -o update_files.zip

7zを使用すると目的が達成できる

#一つ前のコミットから差分ファイルの一覧を抽出する場合
$diff = "HEAD~1"
#日本語ファイル名の文字化けを防ぐため一時的にコンソールをutf-8に変更
$enc = [console]::OutputEncoding; 
[console]::OutputEncoding = [text.encoding]::utf8
$targets = invoke-expression "git diff --name-only $diff  | % -begin {`$script:a = `"`";} -proc {`$script:a += `"'`" + `$_ + `"' `" } -end {`$script:a}";
[console]::OutputEncoding = $enc;
#ファイルの圧縮を実行
invoke-expression "``7z a update_files.zip $targets"

注意点としては、現在のワークツリーが圧縮されるので任意のコミット間の差分抽出にはならない。
面倒だけどgit checkoutするなりしなければならないが目的は達成できる。

4
5
0

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
4
5