LoginSignup
216
171

More than 1 year has passed since last update.

Commit Hash から、該当 Pull Request を見つける方法

Last updated at Posted at 2015-12-18

git blameなどを使用して、変更を加えたcommit sha hashだけわかった時、git show daced1d3のようにすれば、そのコミットの変更内容を見れます。ですが、本当は内容よりその変更を加えたPull Requestを知りたいことありますよね?

そんなコミットからプルリクエストを探したい時に使えるgit aliasコマンドを紹介します。

git showpr

Pull Requestをマージしているコミットログを見つけます。
show pull request => showpr としてますが、名前は好きにつけてください。

.gitconfigのalias設定

.gitconfig
[alias]
  showpr = !"f() { git log --merges --oneline --reverse --ancestry-path $1...master | grep 'Merge pull request #' | head -n 1; }; f"

シェルから設定するなら以下

git config --global alias.showpr \!"f() { git log --merges --oneline --reverse --ancestry-path \$1...master | grep 'Merge pull request #' | head -n 1; }; f"

使用例

$ git showpr daced1d3
5bf98dd Merge pull request #12856 from duglin/ConfigLocation

git openpr (hubユーザー用)

Githubをコマンドでいろいろ操作できるようになるghコマンドを入れている場合使えます。こちらの方が便利なはずです。

.gitconfigのalias設定

.gitconfig
[alias]
	openpr = "!f() { gh browse -- `git log --merges --oneline --reverse --ancestry-path $1...master | grep 'Merge pull request #' | head -n 1 | cut -f5 -d' ' | sed -e 's%#%%'`; }; f"

シェルから設定するなら以下

$ git config --global alias.openpr \!"f() { gh browse -- \`git log --merges --oneline --reverse --ancestry-path \$1...master | grep 'Merge pull request #' | head -n 1 | cut -f5 -d' ' | sed -e 's%#%%'\`; }; f"

使用例

$ git openpr daced1d3

=> 該当のPull Request https://github.com/docker/docker/pull/12856 がブラウザで開く

使い方の実例

例えば、docker/dockerレポジトリのcliconfig/config.goの39行目の最後の変更がど入ったか知りたい場合、

Kobito.FwA6FR.png

git blameを使うと

$ git blame cliconfig/config.go 
...
dea49b74 (Morgan Bauer      2015-07-20 22:39:07 +0000  38) // ConfigDir returns the directory the configuration file is stored in
daced1d3 (Doug Davis        2015-04-28 08:00:18 -0700  39) func ConfigDir() string {
daced1d3 (Doug Davis        2015-04-28 08:00:18 -0700  40)      return configDir
...

と、39行目を変更した最後のコミットがdaced1d3だということがわかります。

Githubで見る場合は、https://github.com/docker/docker/blame/b0a18d57a73bf6b6bd2b7ea71de6065a0053e048/cliconfig/config.go#L39 で見れます。

Kobito.OzZxmV.png

ここで、おもむろに

$ git openpr daced1d3

と打つと、該当のPull Request https://github.com/docker/docker/pull/12856 がブラウザで開きます。

Kobito.WNTi2R.png

コマンドの解説

2つとも、大本のコマンドはgit log

$ git log --merges --oneline --reverse --ancestry-path daced1d3..master

がベースになっています。

各オプションの意味は、以下のようになってます。

  • --merges: mergeコミットのみ表示
  • --online: ログを1行で表示
  • --reverse: 古いものを上に表示
  • --ancestry-path: from..toのpath上にあるコミットだけ表示

あとは、headでそれの1番上を取り出して、表示したり、ブラウザで開いたりすればよいのですが、この時grep

Merge pull request #

というコトバが入っている部分を抜き出しています。

この理由は、Pull Requestのマージ以外にも、普通に開発の途中にmergeのコミット

4815fdc Merge branch 'master' of github.com:docker/docker into error

なども混ざってしまうからです。

216
171
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
216
171