R.A. Epigonos et al.

[diff] 文字単位で diff する方法 (git diff --word-diff-regex=. を使う)

ソースコードの場合は行単位や単語単位のdiffが便利だけど、文書の場合は文字単位で比較したくなるケースがある。そんな時は git diff --word-diff-regex=. を使うと便利。

文字単位や単語単位で比較するには、様々な方法 (例えば、DocDiff、wdiffなど) がある。

git の diff サブコマンドはデフォルトで行単位、--word-diff オプションを渡すと単語単位、--word-diff-regex オプションを渡すと単語とみなす単位を指定できる。この --word-diff-regex オプションに "." という正規表現を渡すことで、単語とみなす単位を「文字」にすることができる。日本語でも問題なく使える。しかしながら、単語単位が常に有効とは限らない。ケースバイケース。git の管理下にないファイル同士の比較を行うために、--no-index オプションが必要。

$ git diff --no-index
$ git diff --word-diff --no-index
$ git diff --word-diff-regex=. --no-index

git のコマンドエイリアス登録をしておく。

$ cat ~/.gitconfig
(snip)
[alias]
	cdiff = diff --word-diff-regex=.
(snip)
$ diff cdiff --no-index

git を使う方針ならば、diff-highlight を使うのも一つ。ただし、文字単位ではなくなるし、単位を指定できなくなる。

$ git -c pager.diff='perl /usr/share/doc/git/contrib/diff-highlight/diff-highlight | less' diff

リファレンス

  1. Git - git-diff Documentation
  2. DocDiff
  3. GitHub - hisashim/docdiff: DocDiff primary repository
  4. Debian -- sid の docdiff パッケージに関する詳細
  5. Wdiff - GNU Project - Free Software Foundation
  6. Debian -- sid の wdiff パッケージに関する詳細

ソーシャルブックマーク

  1. はてなブックマーク
  2. Google Bookmarks
  3. del.icio.us

ChangeLog

  1. Posted: 2008-01-16T02:40:34+09:00
  2. Modified: 2008-01-16T02:40:34+09:00
  3. Generated: 2023-08-27T23:09:12+09:00