LoginSignup
3
5

More than 5 years have passed since last update.

ghq利用時の新規gitレポジトリ作成

Posted at

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

参考

3
5
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
3
5