Git はソースコードなどのバージョン管理を行うためのシステムです。Git でソースコードを管理していると何かの際にファイルを抜き出したくなります。例えば git diff で差分のあるファイルのみを抽出するなどといった具合です。これは次のように使えます。
# オプションと引数でコミットハッシュで示された二つの間の差があるファイルをパスのみ表示します # 例で使っているのは laravel/framework リポジトリです。 $ git diff --name-only 2e98072c8b7cb2206d3c8a2f64a1f64446b5f434 7f84e6eef149e47af686e85d243eb7de748da452 src/Illuminate/Config/Repository.php src/Illuminate/Foundation/Console/ListenerMakeCommand.php src/Illuminate/Foundation/Console/StubPublishCommand.php src/Illuminate/Foundation/Console/stubs/api-routes.stub src/Illuminate/Foundation/Console/stubs/listener.queued.stub src/Illuminate/Foundation/Console/stubs/listener.stub src/Illuminate/Foundation/Console/stubs/listener.typed.queued.stub src/Illuminate/Foundation/Console/stubs/listener.typed.stub src/Illuminate/Support/Facades/Config.php
tree はディレクトリ構造をツリー形式で視覚的に表示するためのコマンドラインツールです。次のように一目でどこになにがあるかわかる様にディレクトリ構造を表示してくれます。
$ tree /etc/apt
/etc/apt
├── apt.conf.d
│ ├── 01-vendor-ubuntu
│ ├── 01autoremove
│ ├── 02autoremove-postgresql
│ ├── 20apt-esm-hook.conf
│ ├── 20auto-upgrades
│ ├── 20packagekit
│ ├── 20snapd.conf
│ ├── 50command-not-found
│ ├── 50unattended-upgrades
│ └── 70debconf
├── auth.conf.d
├── keyrings
├── preferences.d
├── sources.list
├── sources.list.d
│ ├── cuda-debian10-11-3-local.list
│ ├── ondrej-ubuntu-php-jammy.list
│ └── ubuntu-toolchain-r-ubuntu-test-jammy.list
├── trusted.gpg
└── trusted.gpg.d
├── ondrej-ubuntu-php.gpg
├── ondrej-ubuntu-php.gpg~
├── ubuntu-keyring-2012-cdimage.gpg
├── ubuntu-keyring-2018-archive.gpg
├── ubuntu-toolchain-r-ubuntu-test.gpg
└── ubuntu-toolchain-r-ubuntu-test.gpg~
git diff と tree を組み合わせることで変化のあったソースコードがどこにある何なのかが分かりやすくなります。これは次のコマンドでできます。
$ tree --fromfile <(git diff --name-only 2e98072c8b7cb2206d3c8a2f64a1f64446b5f434 7f84e6eef149e47af686e85d243eb7de748da452)
/dev/fd/63
└── src
└── Illuminate
├── Config
│ └── Repository.php
├── Foundation
│ └── Console
│ ├── ListenerMakeCommand.php
│ ├── StubPublishCommand.php
│ └── stubs
│ ├── api-routes.stub
│ ├── listener.queued.stub
│ ├── listener.stub
│ ├── listener.typed.queued.stub
│ └── listener.typed.stub
└── Support
└── Facades
└── Config.php
プロセス置換(<()の括弧の中にあるコマンドの実行結果をファイルとして扱うような仕組み)を用いて git diff の結果を tree コマンドに渡しています。tree にはファイルに羅列されたパスを元にしてツリー構造を表示する機能があるので、プロセス置換を用いてそこにファイルとして出力結果を渡すと出力結果がいい感じにツリー構造に整えられて表示されます。
この記事の主題は git diff ですが、ファイルパスのリストならば何でも同様にツリー構造で表示できます。例えば次のように find や grep を利用することができます。
# 1M以上のファイルを探して、ツリー表示 tree --fromfile <(find . -type f -size +1M) # 特定の文字列を含むファイルを検索し、ツリー表示 tree --fromfile <(grep -rl "TODO" .)
ずらーっとフルパスが並ぶのはプログラムで処理する分にはありがたいです(実際、無加工で tree に流し込めてます)が、視覚的に見る分には微妙です。人間の目で確認する段階では tree 等を使って例のような見た目の整理をすると扱いやすくなります。