.. sidebar:: Contents: .. contents:: :local: sdiff.py ======== 2つのテキストファイルまたはディレクトリの差分を取り、人間にとって分かりやすい比較形式で表示します。 差分取得結果は等幅フォントを表示するターミナル(コマンドライン)上で直接表示できます。 Compare two text files or directories; generate the resulting delta. * **GUIが使えない環境での、より良いテキスト比較に** / For better text comparison in the environment without GUI. * **コマンド実行ログに、完全な形式で残るテキスト比較結果として (行が長い場合は折り返してすべて表示)** / As a text comparison result in the command log in a complete form (When a line is long, I turn it down and display it entirely) * **文字単位で比較したい場合に** / for compare of a character ダウンロード(Download) ----------------------- * `Repository(Bitbucket)`_. .. _`Repository(Bitbucket)`: https://bitbucket.org/tanaga/sdiff.py * 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. | +------------------------+---------------------------+--------------------------------------------------------+ .. _`sdiff.py 1.3.2`: http://hungrysnake.net/software/sdiff.py .. _`sdiff.py 1.2.1`: http://hungrysnake.net/software/sdiff_1.2.1.py .. _`sdiff.py 1.1.1`: http://hungrysnake.net/software/sdiff_1.1.0.py インストール(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. 差分を取得して表示する。 .. image:: 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. .. image:: 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 ヴァージョン管理システムと連携する場合 -------------------------------------- .. note:: 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`` ライセンス(License) -------------------- `The MIT License (MIT)`_ .. _`The MIT License (MIT)`: http://www.opensource.org/licenses/mit-license.php DocString ---------- .. automodule:: sdiff :members: :exclude-members: Differ, expandtabs, strwidth .. autoclass:: Differ :members: テキスト差分取得クラス。 内部処理にdifflibのSequenceMatcherクラスを使用している 引数でいくつかのオプションを指定できる。 cutoff SequenceMatcherクラスによって変更が検出されたチェンジセットに対して 前後で異なる行であると判別する最低の割合。 cutoff=0をfuzzy=1と共に指定するとsdiffコマンドと同様の出力が得られる fuzzy SequenceMatcherクラスによって通常変更が検出されたチェンジセットの中で ベストにマッチした行、次にベストにマッチした行・・・と比較とマッチングを行うが その際にベストではないにしても登場順(行番号)が近いことを優先して マッチングを行う場合のベストマッチからの相対割合、 cutoffに0を指定し、すべての変更チェンジセットを必ず最大限にマッチさせ fuzzyに1を指定し、マッチ率に依存しない登場順のみを考慮したマッチングを 行えば、sdiffコマンドと同様の出力が得られる cutoffchar cutoffオプションの行内差分ヴァージョン。 行内差分での変更が検出された箇所で同期を取るかを指定する。 context 差分をコンテキストで取得するか、その場合の前後行の数を指定する。 差分をすべてフルで出力する場合はNoneを指定する。 .. autofunction:: expandtabs タブ文字を展開する。(マルチバイト文字をサポート) .. autofunction:: strwidth 文字列の幅(文字幅)を返す関数。 等幅フォントで表示されるASCII文字の横幅を1とする (この関数は幅広文字を表示する環境のためにある)