LoginSignup
6
6

More than 5 years have passed since last update.

gitでコミットログをブランチ分岐点だけ表示するコマンドを作った(分岐元ブランチを調べるために)

Last updated at Posted at 2015-12-13

この記事はACCESS Advent Calendar 2015 14日目の記事です。

gitを使っていて、レビューなどで他の人が作ったブランチを見るときに、「このブランチはどのブランチから分岐したものなのだろう?」と思うことが時々あります。
こう思うとき、知りたいのは分岐元ブランチ名であって、ブランチを切った後のコミットの内容などはこの時点においてはむしろ邪魔に思ってたりしました。
この欲求に対し、これまで満足のいく方法が見つかっていませんでした。
「方法が無いならコマンドを作ろう」ということで、コマンドを作りました。

上記githubリポジトリから git-show-branching-commits ファイルをダウンロードして、PATHが通ったところに置いてください。
それにより、gitで show-branching-commits コマンドが使えるようになります。
試しに git show-branching-commits -h でこのコマンドのhelpを表示させてみてください。

例として、 http://cgit.freedesktop.org/cairo/ のリポジトリで origin/master とリモートブランチに対して実行した結果は以下の通りです。
origin/master に至るまでの途中から分岐している各リモートブランチに対し、それらの分岐点となるコミットを示しています。


$ git show-branching-commits -r origin/master
showing logs to origin/master
db8a7f1 1.15.2 release
  -> origin/HEAD
     origin/master
e7acf4b pattern: allow for a floating one pixel rounded difference.
  -> origin/color-emoji
f6fd372 pattern: Restore dropped inclusion of cairoint.h
  -> origin/1.14
59e2a93 Post-release version bump
  -> origin/1.12
c84730d version: Post release version bump
  -> origin/1.10
de7270c [cairo] Describe the restrictions upon cairo_set_tolerance()
  -> origin/1.8
44e6cdd Increment version to 1.6.5 after the 1.6.4 release
  -> origin/1.6
107a748 Increment cairo version to 1.4.10 (and libtool versioning to 13:5:11)
  -> origin/1.4
a5f068e [test] Add 128 to any diff component such that differences are visible
  -> origin/1.2
568ce86 Increment CAIRO_VERSION to 1.1.1 after making branch tag BRANCH_1_0.
  -> origin/1.0

もっと直感的に分かりやすい表示にできればと思うのですが(汗

第2引数以降にブランチ名やタグ名などを列挙することにより、第1引数と比較するコミットを指定できるので、grepやpecoなどとxargsとを組み合わせて使いやすいようにしています。
また --table オプションを付けることにより、スクリプト処理しやすい形式で出力します。
例えば 1.1 にマッチするリモートブランチに限定して実行すると、


$ git branch -r | grep -F '1.1' | xargs git show-branching-commits --table origin/master
158 origin/1.14 f6fd372a8b31a0bebbdfe36090d6ffc7bab9a2f8
389 origin/1.12 59e2a93b3c8a7b83d537bb2a4bbdd5f0f36a0cbc
2438 origin/1.10 c84730d88983623dddd6b9980d82648b7297444d

出力の1列目は origin/master との間のコミット数、2列目はブランチ名、3列目は分岐点であるコミットのハッシュです。

以上、自作のgitコマンドの紹介でした。

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