Error during the Java installation process: git installができないどころかbrew updateすらもできない

夏にjavaの授業を取る予定なので先取りしようと[クラスのHP](http://cs61b.ug/sp16/materials/lab/lab1b/lab1b.html)参考に`brew install git`を動かそうとしするもこんなエラーを吐く。。

```
/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:55:in `require': cannot load such file -- mach (LoadError)
	from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:55:in `require'
	from /usr/local/Library/Homebrew/extend/pathname.rb:2:in `<top (required)>'
	from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:55:in `require'
	from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:55:in `require'
	from /usr/local/Library/Homebrew/global.rb:3:in `<top (required)>'
	from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:55:in `require'
	from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:55:in `require'
	from /usr/local/Library/brew.rb:15:in `<main>'
airbears2-10-142-122-61:~ hironorisama$ brew install python3
/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:55:in `require': cannot load such file -- mach (LoadError)
	from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:55:in `require'
	from /usr/local/Library/Homebrew/extend/pathname.rb:2:in `<top (required)>'
	from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:55:in `require'
	from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:55:in `require'
	from /usr/local/Library/Homebrew/global.rb:3:in `<top (required)>'
	from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:55:in `require'
	from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:55:in `require'
	from /usr/local/Library/brew.rb:15:in `<main>'
```

stackoverflowでざっとスキミングしながらback to backしても全く改善の余地なし。以下、直接的な問題の解決という点ではあまり参考にならなかったリンク：

- [Unable to install git](http://stackoverflow.com/questions/17973074/unable-to-install-git)
- [Unable to install Git using Homebrew](http://stackoverflow.com/questions/7389712/unable-to-install-git-using-homebrew)
- [Unable to install Git on my mac](http://stackoverflow.com/questions/32674064/unable-to-install-git-on-my-mac)
- [How to install git on Linux [closed]](http://stackoverflow.com/questions/11550029/how-to-install-git-on-linux)

*とは言えhomebrew周辺の構造理解に少しでも役に立ったということは言うまでもないので興味のある方は自分もエラーを吐いた気に鳴って読んでみると面白いかもしれない。

そこで包括的に<a href="https://www.google.com/search?q=System%2FLibrary%2FFrameworks%2FRuby.framework%2FVersions%2F2.0%2Fusr%2Flib%2Fruby%2F2.0.0%2Frubygems%2Fcore_ext%2Fkernel_require.rb%3A55%3Ain+%60require%27%3A+cannot+load+such+file+--+mach+(LoadError)&oq=System%2FLibrary%2FFrameworks%2FRuby.framework%2FVersions%2F2.0%2Fusr%2Flib%2Fruby%2F2.0.0%2Frubygems%2Fcore_ext%2Fkernel_require.rb%3A55%3Ain+%60require%27%3A+cannot+load+such+file+--+mach+(LoadError)&aqs=chrome..69i57j69i58.387j0j7&sourceid=chrome&ie=UTF-8#safe=off&q=System%2FLibrary%2FFrameworks%2FRuby.framework%2FVersions%2F2.0%2Fusr%2Flib%2Fruby%2F2.0.0%2Frubygems%2Fcore_ext%2Fkernel_require.rb:55:in+%60require%27:+cannot+load+such+file+--+mach+(LoadError)+git" target="_blank">ググってみる</a>ことに。なんでこれを最初にしなかったのか。。

すると最初のトップページで以下のようなリンクを発見:

- [Homebrewを久々にupdateしたら出来なかった時の対処法](http://qiita.com/takezoux2@github/items/aa2a5f0bef19cd0d8508)
- [homebrew not working on OSX](http://stackoverflow.com/questions/24652996/homebrew-not-working-on-osx)
- [Upgrading brew in El Capitan](http://apple.stackexchange.com/questions/208944/upgrading-brew-in-el-capitan)

[homebrew not working on OSX](http://stackoverflow.com/questions/24652996/homebrew-not-working-on-osx)に書いてあるとおりにやってみる:

>_First, open terminal and `cd /usr/local/`, and git status to see if Homebrew is clean._
>_if dirty, `git reset --hard && git clean -df`_
>_then `brew doctor`, `brew update`_

```
On branch master
Changes not staged for commit:
  (use "git add/rm <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

	deleted:    Library/Aliases/caf
	deleted:    Library/Aliases/docker-compose
	deleted:    Library/Aliases/gperftools
	deleted:    Library/Aliases/htop
	deleted:    Library/Aliases/kotlin
	deleted:    Library/Aliases/mbedtls
	deleted:    Library/Aliases/mpich
	deleted:    Library/Aliases/nim
	deleted:    Library/Aliases/ocaml
	deleted:    Library/Aliases/offlineimap
	deleted:    Library/Aliases/shell-fm
	deleted:    Library/Aliases/wolfssl
	deleted:    Library/Contributions/example-formula.rb
	deleted:    Library/ENV/include/6.3/__debug
	deleted:    Library/Formula/app-engine-java-sdk.rb
	deleted:    Library/Formula/cdimgtools.rb
	deleted:    Library/Formula/clay.rb
	deleted:    Library/Formula/cppdom.rb
	deleted:    Library/Formula/cracklib-words.rb
	deleted:    Library/Formula/cyassl.rb
	deleted:    Library/Formula/echoping.rb
	deleted:    Library/Formula/ffts.rb
	deleted:    Library/Formula/fig.rb
	deleted:    Library/Formula/fsharp.rb
	deleted:    Library/Formula/gant.rb
	deleted:    Library/Formula/gdrive.rb
	deleted:    Library/Formula/git-encrypt.rb
	deleted:    Library/Formula/go-app-engine-32.rb
	deleted:    Library/Formula/go-app-engine-64.rb
	deleted:    Library/Formula/google-app-engine.rb
	deleted:    Library/Formula/google-perftools.rb
	deleted:    Library/Formula/googlecl.rb
	deleted:    Library/Formula/gptfdisk.rb
	deleted:    Library/Formula/groonga-normalizer-mysql.rb
	deleted:    Library/Formula/gtkglarea.rb
	deleted:    Library/Formula/hamsterdb.rb
	deleted:    Library/Formula/honeyd.rb
	deleted:    Library/Formula/iojs.rb
	deleted:    Library/Formula/ipe.rb
	deleted:    Library/Formula/kbtin.rb
	deleted:    Library/Formula/kotlin-compiler.rb
	deleted:    Library/Formula/kumofs.rb
	deleted:    Library/Formula/libcppa.rb
	deleted:    Library/Formula/magit.rb
	deleted:    Library/Formula/mpich2.rb
	deleted:    Library/Formula/mysql-connector-odbc.rb
	deleted:    Library/Formula/mysql-proxy.rb
	deleted:    Library/Formula/net6.rb
	deleted:    Library/Formula/nimrod.rb
	deleted:    Library/Formula/objective-caml.rb
	deleted:    Library/Formula/offline-imap.rb
	deleted:    Library/Formula/p11-kit.rb
	deleted:    Library/Formula/paq8px.rb
	deleted:    Library/Formula/par2tbb.rb
	deleted:    Library/Formula/pdf-tools.rb
	deleted:    Library/Formula/perlmagick.rb
	deleted:    Library/Formula/phash.rb
	deleted:    Library/Formula/plt-racket.rb
	deleted:    Library/Formula/polarssl.rb
	deleted:    Library/Formula/qi.rb
	deleted:    Library/Formula/qiv.rb
	deleted:    Library/Formula/rbenv-gem-rehash.rb
	deleted:    Library/Formula/rocket.rb
	deleted:    Library/Formula/rubber.rb
	deleted:    Library/Formula/sedna.rb
	deleted:    Library/Formula/synfigstudio.rb
	deleted:    Library/Formula/telepathy-gabble.rb
	deleted:    Library/Formula/telepathy-glib.rb
	deleted:    Library/Formula/telepathy-idle.rb
	deleted:    Library/Formula/telepathy-mission-control.rb
	deleted:    Library/Formula/uim.rb
	deleted:    Library/Formula/vobcopy.rb
	deleted:    Library/Formula/whereami.rb
	deleted:    Library/Formula/why3.rb
	deleted:    Library/Formula/wps2odt.rb
	deleted:    Library/Formula/xastir.rb
	deleted:    Library/Formula/ydict.rb
	deleted:    Library/Homebrew/cmd/aspell-dictionaries.rb
	deleted:    Library/Homebrew/cmd/update.rb
	deleted:    Library/Homebrew/extend/symbol.rb
	deleted:    Library/Homebrew/keg_fix_install_names.rb
	deleted:    Library/Homebrew/mach.rb
	deleted:    Library/Homebrew/requirements/apr_dependency.rb
	deleted:    Library/Homebrew/requirements/fortran_dependency.rb
	deleted:    Library/Homebrew/requirements/java_dependency.rb
	deleted:    Library/Homebrew/requirements/language_module_dependency.rb
	deleted:    Library/Homebrew/requirements/ld64_dependency.rb
	deleted:    Library/Homebrew/requirements/mpi_dependency.rb
	deleted:    Library/Homebrew/requirements/osxfuse_dependency.rb
	deleted:    Library/Homebrew/requirements/python_dependency.rb
	deleted:    Library/Homebrew/requirements/tuntap_dependency.rb
	deleted:    Library/Homebrew/requirements/x11_dependency.rb
	deleted:    Library/Homebrew/test/.gitignore
	deleted:    Library/Homebrew/test/test_cmd_cleanup.rb
	deleted:    Library/Homebrew/test/test_language_module_dependency.rb
	deleted:    Library/Homebrew/test/test_mpi_dependency.rb
	deleted:    Library/Homebrew/test/test_updater.rb
	deleted:    Library/Homebrew/test/test_x11_dependency.rb
	deleted:    share/doc/homebrew/Homebrew-0.8.md
	deleted:    share/doc/homebrew/Homebrew-0.9.3.md
	deleted:    share/doc/homebrew/Homebrew-0.9.md
	deleted:    share/doc/homebrew/Homebrew-2.md
	deleted:    share/doc/homebrew/Press.md
	deleted:    share/doc/homebrew/The-brew-command.md
	deleted:    share/doc/homebrew/brew-test-bot-failed-jenkins.png
	deleted:    share/doc/homebrew/brew-test-bot-failed-pr.png
	deleted:    share/doc/homebrew/brew-test-bot-failed-test.png
	deleted:    share/doc/homebrew/brew-test-bot-for-core-contributors-block.png
	deleted:    share/doc/homebrew/brew-test-bot-for-core-contributors-console.png
	deleted:    share/doc/homebrew/brew-test-bot-for-core-contributors-tabs.png
	deleted:    share/doc/homebrew/brew-test-bot-for-core-contributors-trigger.png
	deleted:    share/doc/homebrew/brew-test-bot-passed-jenkins.png
	deleted:    share/doc/homebrew/brew-test-bot-passed-pr.png
	deleted:    share/doc/homebrew/brew-test-bot-triggered-pr.png

no changes added to commit (use "git add" and/or "git commit -a")
```

これは汚いと考えてよろしいのでしょうか。。とりあえず`git reset --hard && git clean -df`を実行してみることに。

```
HEAD is now at ce8f101 Update minitest
```

と返ってきたので`brew doctor`と`brew update`をもう一度実行。まだ同じエラーを吐く。

```
warning: unable to unlink CONTRIBUTING.md: Permission denied
warning: unable to unlink SUPPORTERS.md: Permission denied
fatal: cannot create directory at '.github': Permission denied
```

どうやら`/usr/local`内の権限に問題があるのでは？と判断したので上記のリンク周辺を参考に`sudo chown -R $(whoami):admin /usr/local`を実行し、もう一度`brew doctor`と`brew update`をするも状況は変わらず。そこでpermissionを変更した後に`git reset --hard && git clean -df`をやったら何か変わるのでは？と考え試しに行ってみる。その後`brew doctor`と`brew update`を実行するとさっきとは少しだけ違うメッセージを受け取る:

```
Error: undefined method `new' for Patch:Module
Please report this bug:
    https://git.io/brew-troubleshooting
/usr/local/Library/Homebrew/patch.rb:115:in `initialize'
/usr/local/Library/Homebrew/patch.rb:18:in `new'
/usr/local/Library/Homebrew/patch.rb:18:in `create'
/usr/local/Library/Homebrew/software_spec.rb:168:in `patch'
/usr/local/Library/Homebrew/formula.rb:1920:in `block in patch'
/usr/local/Library/Homebrew/formula.rb:1920:in `each'
/usr/local/Library/Homebrew/formula.rb:1920:in `patch'
/usr/local/Library/Formula/a2ps.rb:19:in `<class:A2ps>'
/usr/local/Library/Formula/a2ps.rb:1:in `load_formula'
/usr/local/Library/Homebrew/formulary.rb:22:in `module_eval'
/usr/local/Library/Homebrew/formulary.rb:22:in `load_formula'
/usr/local/Library/Homebrew/formulary.rb:35:in `load_formula_from_path'
/usr/local/Library/Homebrew/formulary.rb:84:in `load_file'
/usr/local/Library/Homebrew/formulary.rb:75:in `klass'
/usr/local/Library/Homebrew/formulary.rb:71:in `get_formula'
/usr/local/Library/Homebrew/formulary.rb:208:in `factory'
/usr/local/Library/Homebrew/cmd/update.rb:173:in `block in report'
/usr/local/Library/Homebrew/cmd/update.rb:159:in `each_line'
/usr/local/Library/Homebrew/cmd/update.rb:159:in `report'
/usr/local/Library/Homebrew/cmd/update.rb:24:in `update'
/usr/local/Library/brew.rb:140:in `<main>'
```

いちいち報告している暇がなかったので警告をシカトしてもう一度`brew update`:

```
Already up-to-date.
```

この一言までたどり着くのに長かった。。念のためもう一度`brew doctor`を行い最後にもう一度`brew install git`を実行。

```
==> Downloading https://homebrew.bintray.com/bottles/git-2.7.2.el_capitan.bottle.tar.gz
######################################################################## 100.0%
==> Pouring git-2.7.2.el_capitan.bottle.tar.gz
==> Caveats
The OS X keychain credential helper has been installed to:
  /usr/local/bin/git-credential-osxkeychain

The "contrib" directory has been installed to:
  /usr/local/share/git-core/contrib

Bash completion has been installed to:
  /usr/local/etc/bash_completion.d

zsh completion has been installed to:
  /usr/local/share/zsh/site-functions

Emacs Lisp files have been installed to:
  /usr/local/share/emacs/site-lisp/git
==> Summary
🍺  /usr/local/Cellar/git/2.7.2: 1,409 files, 236.4M
```

上のリンクにも書いてありますが、振り返ってみるとどうやらパソコン自体のバージョンアップデートした際の互換性にも原因があったみたい。