git-deps で 依存コミット の 一挙 洗い出し (gitだけでやるやり方も)

# 普通の手法

```
$ git show b5e4722891e7bbf2fffcd995af02838667a3abab
commit b5e4722891e7bbf2fffcd995af02838667a3abab
Author: Adrian Roos <roosa@google.com>
Date:   Fri Aug 14 15:53:06 2015 -0700

    Add StrongAuthTracker
    
    Bug: 22846469
    Bug: 22115393
    Change-Id: I6ef5322d02e540fc043e7f20d3aabf595ce7c224

diff --git a/Android.mk b/Android.mk
index 18d9c69..2a87fd2 100644
--- a/Android.mk
+++ b/Android.mk
@@ -90,6 +90,7 @@ LOCAL_SRC_FILES += \
        core/java/android/app/IWallpaperManager.aidl \
        core/java/android/app/IWallpaperManagerCallback.aidl \
        core/java/android/app/admin/IDevicePolicyManager.aidl \
+       core/java/android/app/trust/IStrongAuthTracker.aidl \
        core/java/android/app/trust/ITrustManager.aidl \
        core/java/android/app/trust/ITrustListener.aidl \
        core/java/android/app/backup/IBackupManager.aidl \
..snip..
```

たとえば、これの依存を見たい場合は、

```
$ git log --pretty=oneline android-6.0.0_r1...b5e4722891e7bbf2fffcd995af02838667a3abab Android.mk
b5e4722891e7bbf2fffcd995af02838667a3abab Add StrongAuthTracker
3d52040359721934231e0e89d45b3bd4179db475 am 30c8245a: Merge "Add data binding to built javadoc." into mnc-dev
4cfdcf5b0551e5656ea379c428e78b812c2e5cbe Touch, wake and unlock
```

Android.mkに関する変更はこのようにある。

```
$ git log --pretty=oneline Android.mk
b5e4722891e7bbf2fffcd995af02838667a3abab Add StrongAuthTracker
39e5e947447bc611205404ae6a4690656f1aa0f9 Add TabletModeChangedListener for SystemUI.
1827e3230dfc3610780f0ffdb704aa9c142aa22b docs: Update developer docs make file to include API 23
740452e8756cb7f51ed185c3906a5ca61b8fcdd4 Touch, wake and unlock
a340f07670c300c40ffebad547e34abdacab5184 Add data binding to built javadoc.
49e3edff5156f471819e4ea2a88994bca70bd870 Add android.net.CaptivePortal class for captive-portal-handling-app callbacks
cdfd230a392d0f0557a3a5bada221b7a05113392 Grant default permissons to the default SMS, Phone, Browser app.
2a880312086147577e1e814bda6985fa97fb343b Default permissions for carrier apps.
f7c06eb03ab4479b9d0656a23a4733d17e995183 Add system API to watch for permission changes
7c74f78a85283912d7239214024ccca702622f21 Zen: New user flow for requesting DND access.
e0a6ca64fac5bd4f10139321604031816e90adb4 MidiManager: proxy all requests to open devices through MidiService
5c728994311c40821604c6711171e860776659a6 Activity info and stats from modem
69c6adc96eecfde74ceb83cf9177428dc08b6067 More API changes.
42ecc9eb902ef90876cd345a906c24e0d58720a3 Drop the "Config". Just "CarrierService". It's cleaner.
4cbe359abe26d749b1dbc9cf489a825af0cad005 Add 'System' group to DAC sample browser
85dea528866217d4b3d40a1162c00c4e836504aa Doc change: updated images, landings, and ndk updates.
be67542182e41a6e31cf002db72ec42ee717bd1c Move from native FingerprintService implementation to fingerprintd
6664aee15ae2f1183d5c6820bb47c6d7d55806c0 camera: Add AIDL interface for CameraServiceProxy.
d23e0d6901935588f9472bd7073fea0009581e9b Update power manager to track uid state like netstats.
b3b9eb3cfc5b3b3609a5d01258315798b38a5cf9 DO NOT MERGE - Display mode switches.
d86d58cd010b087d6d481062f84c894e0ced7bbc Tweak SystemUpdatePolicy according to API review.
00f59774ed7c3f42d655ed9688bc83bfecfa2fe7 Removed unused Face Unlock aidl files
a8cc0584c60cbe7e880895353d95e9b9cc5c1b62 Revert "Remove incomplete media router API."
..snip..
```

```
$ git blame -s -b --abbrev=40 -L 90,100 b5e4722891e7bbf2fffcd995af02838667a3abab Android.mk
a8cc0584c60cbe7e880895353d95e9b9cc5c1b62  90)   core/java/android/app/IWallpaperManager.aidl \
a8cc0584c60cbe7e880895353d95e9b9cc5c1b62  91)   core/java/android/app/IWallpaperManagerCallback.aidl \
a8cc0584c60cbe7e880895353d95e9b9cc5c1b62  92)   core/java/android/app/admin/IDevicePolicyManager.aidl \
b5e4722891e7bbf2fffcd995af02838667a3abab  93)   core/java/android/app/trust/IStrongAuthTracker.aidl \
a8cc0584c60cbe7e880895353d95e9b9cc5c1b62  94)   core/java/android/app/trust/ITrustManager.aidl \
a8cc0584c60cbe7e880895353d95e9b9cc5c1b62  95)   core/java/android/app/trust/ITrustListener.aidl \
a8cc0584c60cbe7e880895353d95e9b9cc5c1b62  96)   core/java/android/app/backup/IBackupManager.aidl \
a8cc0584c60cbe7e880895353d95e9b9cc5c1b62  97)   core/java/android/app/backup/IFullBackupRestoreObserver.aidl \
a8cc0584c60cbe7e880895353d95e9b9cc5c1b62  98)   core/java/android/app/backup/IRestoreObserver.aidl \
a8cc0584c60cbe7e880895353d95e9b9cc5c1b62  99)   core/java/android/app/backup/IRestoreSession.aidl \
a8cc0584c60cbe7e880895353d95e9b9cc5c1b62 100)   core/java/android/app/usage/IUsageStatsManager.aidl \
```

付近は、a8cc0584c60cbe7e880895353d95e9b9cc5c1b62 とあるので、依存コミットはこれになる。


# toolを使う

先の方法は面倒。。。
なので、git-depsを使ってみる。

しかし、setupが面倒なので、そのsetupを・・・

# git-depsのsetup

Ubuntu 14.04LTS / 16.04LTS での手順です.

## setup pip (すでにあるならば不要)

```
$ sudo apt-get install python-pip
$ sudo pip install --upgrade pip
```

## setup ez_setup (optional. 少なくとも16.04では不要)

```
$ wget https://bootstrap.pypa.io/ez_setup.py -O - | /usr/local/lib/python2.7.9/bin/python
```

## setup pip via easy_install (optional. 少なくとも16.04では不要)

```
$ /usr/local/lib/python2.7.9/bin/easy_install pip
$ sudo ln -s /usr/local/lib/python2.7.9/bin/easy_install /usr/bin/easy_install
```

## setup cffi

```
$ sudo apt-get install libffi-dev (これ必須. 16.04LTSでは)
$ sudo apt-get install python-cffi (これ必須)
$ pip install cffi
```

## setup zyper

```
$ sudo apt-get install zypper
```

## setup pygit2

```
$ sudo apt-get install python-dev

$ sudo pip install pygit2
$ python -c 'import pygit2'
```
>で、おそらく、 ```src/blob.h:33:18: fatal error: git2.h``` みたいerrorのが出るのではないかと思います。
>下のようにして、libgit2を入れてください。pip install pygit2の出力を見ると、どのversionのlibgit2を使おうと思っているのかが表示されていますので、そのversionを指定してsyncして、buildして、installした後は、pygit2自体はpip経由でinstallしても大丈夫です。

> 以下のlibgit2とpygit2は、Ubuntu 14.04LTSではv0.21.0で実績あり. Ubuntu 16.04LTSではv0.24.1で実績あり. -b masterは使わない方がよい。同じversion tagを必ず指定すること.

```
$ cd ~/work
$ git clone https://github.com/libgit2/libgit2.git -b v0.24.1
$ cd libgit2
$ mkdir -p build && cd build
$ cmake ..
$ cmake --build .
$ sudo cmake --build . --target install
$ sudo ldconfig
$ cd ~/work
$ git clone git://github.com/libgit2/pygit2.git -b v0.24.1
$ cd pygit2
$ python setup.py build
$ sudo python setup.py install
```

おかしくなったら

```
$ sudo rm /tmp/pip_build_root/pygit2
```
とかも

## setup git-deps

```
$ git clone https://github.com/aspiers/git-deps.git
$ cd git-deps
$ chmod +x git-deps.py
$ ln -s /home/hidenorly/work/git-deps/git-deps.py /home/hidenorly/bin/git-deps.py
```

# git-depsを使ってみる

```
~/work/android-6.0.1_r3/frameworks/base$ git-deps.py b5e4722891e7bbf2fffcd995af02838667a3abab
a8cc0584c60cbe7e880895353d95e9b9cc5c1b62
82142c21dd333307682d5f4bb09de3ab3ccfa06c
2c12cfa1d53b586ae8a8d6aca64a4de771dc85b0
82893681ad6530eec8f2a9b4ecd2e039addf48b7
237b061182d36fd3bf2238092ccf3d529ec8877b
481a6df99fea124bc4354da34ff668750cdc9041
52c489cd63cca0361f374f7cb392018fabfa8bcc
2397427cb1a0bad8a42e6a342dcf29b31e40a234
9066cfe9886ac131c34d59ed0e2d287b0e3c0087
78c48f6c1700ab8d507dd7bf7ebef404913648bd
```

こんな感じで一発で出せる！


# Appendix

libgit2とpygit2は同じバージョンを使うこと.
git-deps.pyは、v0.21.0は動きましたが、v0.23.0(v0.25.1でも同じ)では

```
--- ./git-deps.py	2016-06-25 07:59:55.056621735 +0900
+++ /home/hidenory/bin/git-deps.py
@@ -420,7 +420,7 @@
         diff = self.repo.diff(parent, dependent,
                               context_lines=self.options.context_lines)
         for patch in diff:
-            path = patch.old_file_path
+            path = patch.delta.old_file.path #patch.old_file_path
             self.logger.debug("    Examining hunks in %s" % path)
             for hunk in patch.hunks:
                 self.blame_hunk(dependent, parent, path, hunk)
@@ -522,14 +522,14 @@
         hunk_header = '@@ %s %s @@' % (line_range_before, line_range_after)
         self.logger.debug(diff_format % ('--------', '-----', '', hunk_header))
         line_num = hunk.old_start
-        for mode, line in hunk.lines:
+        for mode, line in enumerate(hunk.lines):
             if mode == '+':
                 rev = ln = ''
             else:
-                rev = line_to_culprit[line_num]
+                rev = line_to_culprit.get(line_num) #line_to_culprit[line_num]
                 ln = line_num
                 line_num += 1
-            self.logger.debug(diff_format % (rev, ln, mode, line.rstrip()))
+            # self.logger.debug(diff_format % (rev, ln, mode, line.rstrip()))
 
     def oneline(self, commit):
         return commit.message.split('\n', 1)[0]

```

のように、git-deps.py自体を改変しないと動かなかった記憶があります.
(もう1つのUbuntu環境でv0.23.0を使ってしまってハマったので・・・)