更换HomeBrew源

本文为作者原创,转载请注明出处(http://www.cnblogs.com/mar-q/)by 负赑屃
 
  比较少用brew,只有之前安装Opencv的时候用过一次,后面有人问我怎么装,于是帮他研究了一下。MacOS的brew其实就是通过两个git仓库(brew和homebrew-core)来实现的源更新机制,如果程序员没有科学的Internet方法,就只好换国内源了,国内brew源不是很多,首选中科大或者清华的源:
一、更换
  下面这个更换步骤应该是大部分更换brew源的文章里都会介绍的,已经做了还存在bug的可以跳过。
###首选中科大源
cd "$(brew --repo)"
git remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git
cd "$(brew --repo)/Library/Taps/homebrew/homebrew-core"
git remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git
#bottles是Homebrew预编译二进制软件包
echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.ustc.edu.cn/homebrew-bottles' >> ~/.bash_profile 
source ~/.bash_profile
 
###清华测试git源无法访问,可能已经失效
cd "$(brew --repo)"
git remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git
cd "$(brew --repo)/Library/Taps/homebrew/homebrew-core"
git remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git
echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles' >> ~/.bash_profile 
source ~/.bash_profile
 
###处理完成后升级
brew update
 
###重置
cd "$(brew --repo)”
git remote set-url origin https://github.com/Homebrew/brew.git 
cd "$(brew --repo)/Library/Taps/homebrew/homebrew-core”
git remote set-url origin https://github.com/Homebrew/homebrew-core.git
#最后删掉~/.bash_profile的那句话 
 
二、bug
  无法执行brew update,或者更新后报错:
error: Unable to get pack file http://mirrors.ustc.edu.cn/homebrew-core.git/objects/pack/pack-88812c2ec49654184974c8d70848953ff76433ae.pack
The requested URL returned error: 416 Requested Range Not Satisfiable
error: Unable to find 6bcbfc51973a7d4891676c15b06ec24e54b74e30 under http://mirrors.ustc.edu.cn/homebrew-core.git
Cannot obtain needed object 6bcbfc51973a7d4891676c15b06ec24e54b74e30
error: fetch failed.
  这是因为git更换remote url后仓库更新状况不一致导致的
#诊断问题
brew doctor 
#reset仓库的Head cd
"$(brew --repo)” git fetch git reset --hard origin/master cd "$(brew --repo)/Library/Taps/homebrew/homebrew-core” git fetch git reset --hard origin/master
#最后别忘了update brew update
  如果依然报错,或者执行git fetch就报错,只好进到仓库里删东西啦:
  在这个pack中会出现一个未完成的.tmp文件,这时候需要把它mv掉
cd "$(brew --repo)/Library/Taps/homebrew/homebrew-core
mv .git/objects/pack/pack-88812c2ec49654184974c8d70848953ff76433ae.idx /tmp
mv .git/objects/pack/pack-88812c2ec49654184974c8d70848953ff76433ae.pack.temp /tmp
   或者在github的issue里据说还有一个方案:
git fsck
git gc
  再次执行git fetch
 
三、终极解决方案
  很多同学这么配置了依旧没有生效,那就介绍两个大招吧:
  1. 搭梯子
  2. 重新装一波home-brew,参见官网https://brew.sh/
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
  而且重装后,前面的配置依旧生效,可以使用国内的源,因此推测,更新了源,而且reset了git head也不能执行brew update,那么很有可能是你的brew版本不兼容了。
  不过这样做有一个问题:会吃掉一部分磁盘空间(我的大概1g) 
posted @ 2018-01-22 00:51  负赑屃  阅读(4291)  评论(0编辑  收藏  举报