ghqでローカルリポジトリの管理をしていると、既存のリモートリポジトリを取ってくるのは楽なのですが、新規リポジトリを作ろうとすると若干面倒です。
そこで、新規リポジトリ作成→ghqの管理下に置く、という操作を簡略化するshell関数ghq-new
を登録しておくと便利です。
https://blog.nishimu.land/entry/2015/03/12/193340 で既にやられている方がいましたが、~/.ghq
以下に自分でgit init
したくない、ghq get
でcloneした状態にしておきたい
、といった理由から別バージョンを作って使っています。
↓を.bashrc
に書いておきます。
function ghq-new() {
local REPONAME=$1
if [ -z "$REPONAME" ]; then
echo 'Repository name must be specified.'
return
fi
local TMPDIR=/tmp/ghq_new
local TMPREPODIR=$TMPDIR/$REPONAME
mkdir -p $TMPREPODIR
cd $TMPREPODIR
hub init
hub create
local REPOURL=$(git remote get-url origin)
local REPOPATH=$(echo $REPOURL | sed -e 's/^https:\/\///' -e 's/^git@//' -e 's/\.git$//' -e 's/github.com:/github.com\//')
local USER_REPO_NAME=$(echo $REPOPATH | sed -e 's/^github\.com\///')
ghq get $USER_REPO_NAME
cd $(ghq root)/$REPOPATH
rm -rf $TMPREPODIR
}
やっているのは↓です。
1. /tmp
以下に新規Repoを作成
2. そのディレクトリに移動してhub create
3. 作成されたgithubのリモートリポジトリをghq get
4. ghq get
でcloneしてきたローカルリポジトリに移動&/tmp
以下のリポジトリは削除
REPOPATH
変数の部分がsed
で色々汚いことになっていますが、hub create
で使われるプロトコルがSSHでもHTTPSでも対応できるようにしたらこうなってしまいました。
(hub create
で利用するプロトコルの固定自体はgit config --global hub.protocol https
でできるようです: https://hub.github.com/hub.1.html )
使用例
$ ghq-new test
Initialized empty Git repository in /private/tmp/ghq_new/test/.git/
Updating origin
https://github.com/tuttieee/test
clone https://github.com/tuttieee/test -> /Users/tuttieee/.ghq/github.com/tuttieee/test
git clone https://github.com/tuttieee/test /Users/tuttieee/.ghq/github.com/tuttieee/test
Cloning into '/Users/tuttieee/.ghq/github.com/tuttieee/test'...
warning: You appear to have cloned an empty repository.
$ pwd
/Users/tuttieee/.ghq/github.com/tuttieee/test