LoginSignup
6
5

More than 1 year has passed since last update.

レジストリファイル (UTF-16) をdiffやGitで扱う

Last updated at Posted at 2015-08-09

はじめに

$ file foo.reg
foo.reg: Little-endian UTF-16 Unicode text, with CRLF line terminators

レジストリファイル (.reg) はテキストファイルであるが,
UTF-16は, 以下のようにバイナリとして認識されてしまうため, 適切に処理できない.

$ less foo.reg
"foo.reg" may be a binary file.  See it anyway?

$ grep HKEY foo.reg # 何も出力されない

$ sed 's/HKEY/hkey/g' foo.reg # 変換されない

$ diff foo.reg bar.reg
Binary files foo.reg and bar.reg differ

$ git grep HKEY # 何も出力されない

$ git diff
Binary files a/foo.reg and b/foo.reg differ

テキスト処理したり, Gitで管理できるようにするためには, nkfでUTF-8に変換すればいい.

UTF-16をdiff

プロセス置換でUTF-8に変換してから, diffに渡す.

$ diff <( nkf -w foo.reg ) <( nkf -w bar.reg )

なお, vimはUTF-16のファイルを見ることができるので, vimdiffを使ってもいい.

UTF-16でGit

まず, 以下のように, UTF-8に変換するフィルタを設定する.

$ git config --global diff.to_utf8.textconv 'nkf -w'

そうすると, ~/.gitconfigに以下のような行が追加されている.

~/.gitconfig
[diff "to_utf8"]
    textconv = nkf -w

このフィルタを*.regに適用したい場合は, .gitattributesに以下の行を追加する.

.gitattributes
*.reg diff=to_utf8

以上により, git diffする前に, *.regのファイルはnkfでUTF-8に変換されるため,
レジストリファイルもgit diffで見ることができるようになる.

git grep--textconvオプションで見ることができる.

$ git grep --textconv HKEY # 出力される

参考

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