sdiff.py

2つのテキストファイルまたはディレクトリの差分を取り、人間にとって分かりやすい比較形式で表示します。 差分取得結果は等幅フォントを表示するターミナル(コマンドライン)上で直接表示できます。

Compare two text files or directories; generate the resulting delta.

ダウンロード(Download)

  • Current

sdiff.py 1.3.2

current "stable" version.

Show colored diff and automatic width detection.

  • History

sdiff.py 1.2.1

old "stable" version.

able to parse Unified diff formated text from stdin. by include "unidiff" library. Thanks a lot !! Visit here https://github.com/matiasb/python-unidiff

sdiff.py 1.1.1

old "stable" version.

able to compare directories.

インストール(Installation)

特に無し。 ヴァージョン 2.7, 3.5 のPythonが実行可能な環境であること。

Do nothing. need Python ver 2.7 or 3.5.

実行例(Example)

以下のような2つのテキストファイルtext1.txtとtext2.txtを用意して

text1.txt:

1. Beautiful is better than ugly.
2. Explicit is better than implicit.
3. Simple is better than complex.
4. Complex is better than complicated.

text2.txt:

1. Beautiful is better than ugly.
3.   Simple is better than complex.
4. Complicated is better than complex.
5. Flat is better than nested.

差分を取得して表示する。

_images/sdiff.py_ascii.png

色付けをしない場合はプレーンテキストの内容で差分が完結する。 The difference is complete with the contents of plain text if you do not want the colored

sdiff.py text1.txt text2.txt -w 93 --color never

--- text1.txt (utf-8)
+++ text2.txt (utf-8)
     1|  1. Beautiful is better than ugly.           1|  1. Beautiful is better than ugly.
     2|  2. Explicit is better than implicit. <       |
     3|  3. Simple is better than complex.    |      2|  3.   Simple is better than complex.
     4|  4. Complex is better than complicate |      3|  4. Complicated is better than comple
     ^|d.                                     ^      ^|x.
      |                                       >      4|  5. Flat is better than nested.

[     ]      |    ++
[ <-  ]     3|  3.   Simple is better than complex.
[  -> ]     2|  3.   Simple is better than complex.

[     ]      |          ++++ !                     ---- !
[ <-  ]     4|  4. Compl    ex is better than complicated.
[  -> ]     3|  4. Complicated is better than compl    ex.

日本語などのマルチバイト文字にも対応しています。 各種エンコーディングは引数で指定してください。

supported multi-byte string. set the encoding with an argument.

_images/sdiff.py_mbytes.png

使い方(Usage)

コマンドラインで以下のように指定する。 2つのテキストファイルまたはディレクトリの差分を表示することができる。 引数に比較対象を指定しなかった場合は標準入力からのUnified diff formatのテキストを待つ。

It is possible to show the differences between two text files or directories. and waits for the text of the Unified diff format from standard input if there is not an argument.

usage: sdiff.py [-h] [--version] [-f] [-c NUM] [-w WIDTH] [-r]
                [--linejunk LINEJUNK] [--charjunk CHARJUNK] [--cutoff RATIO]
                [--fuzzy RATIO] [--cutoffchar] [--enc-file1 ENCODING]
                [--enc-file2 ENCODING] [--enc-stdin ENCODING]
                [--enc-stdout ENCODING] [--enc-filepath ENCODING]
                [--ignore-crlf] [--color [WHEN]] [--no-color] [--withbg] [-u]
                [-L L] [--test]
                [file_or_dir_1] [file_or_dir_2]

positional arguments:
 file_or_dir_1         file or dir 1
 file_or_dir_2         file or dir 2

optional arguments:
 -h, --help            show this help message and exit
 --version             show program's version number and exit
 -f, --full            Fulltext diff (default False) (disable context option)
 -c NUM, --context NUM
                       Set number of context lines (default 5)
 -w WIDTH, --width WIDTH
                       Set number of width (default auto(or 130))
 -r, --recursive       Recursively compare any subdirectories found. (default
                       False) (enable only compare directories)
 --linejunk LINEJUNK   linejunk
 --charjunk CHARJUNK   charjunk
 --cutoff RATIO        Set number of cutoff ratio (default 0.75)
                       (0.0<=ratio<=1.0)
 --fuzzy RATIO         Set number of fuzzy matching ratio (default 0.0)
                       (0.0<=ratio<=1.0)
 --cutoffchar          Cutoff character in line diffs (default False)
 --enc-file1 ENCODING  Set encoding of leftside inputfile1 (default utf-8)
 --enc-file2 ENCODING  Set encoding of rightside inputfile2 (default utf-8)
 --enc-stdin ENCODING  Set encoding of standard input (default
                       `defaultencoding`)
 --enc-stdout ENCODING
                       Set encoding of standard output (default
                       `defaultencoding`)
 --enc-filepath ENCODING
                       Set encoding of filepath (default `defaultencoding`)
 --ignore-crlf         Ignore carriage return ('\r') and line feed ('\n')
                       (default False)
 --color [WHEN]        Show colored diff. --color is the same as
                       --color=always. WHEN can be one of always, never, or
                       auto. (default auto)
 --no-color            Turn off colored diff. override color option if both.
                       (default False)
 --withbg              Colored diff with background color. It will be ignored
                       if no-color option. (default False)
 -u                    Do nothing (It is accepted only for compatibility with
                       "svn diff --diff-cmd" interface)
 -L L                  label of file1 and file2(can set 2 times)
 --test                Test self

ヴァージョン管理システムと連携する場合

注釈

sdiff.pyに対してパス(環境変数PATH)を通しておくこと。

Subversionと連携する場合

  • 単純に実行 svn diff --diff-cmd sdiff.py

  • オプションを渡す場合 svn diff --diff-cmd sdiff.py -x "-w 180"

  • もっと簡単に svn diff | sdiff.py

Mercurialと連携する場合

extdiffを使う場合はホームディレクトリ直下の~/.hgrc (or mercurial.ini for Windows)に以下の内容を追加しておく

~/.hgrc:

[extensions]

extdiff =
  • 単純に実行 hg extdiff -p sdiff.py

  • オプションを渡す場合 hg extdiff -p sdiff.py -o "-w 180"

  • もっと簡単に hg diff | sdiff.py