ghqを使っているとリモートリポジトリもコンソールから簡単に作りたくなった(bitBucketとgithub用)


はじめに

ghqでローカルリポジトリを管理していまして、
結構快適なんですが、新規プロジェクトを作る際に、
リモートリポジトリの作成をWEB側でいちいち作るのが面倒くさくなってきました。

ということでコマンド一発でリモートリポジトリの新規作成とremote add、pushまでを行います。

尚、githubではオープンリポジトリ、bitbucketではプライベートリポジトリとして登録を行います、

インストールとか

bitbucketとgithubのcliツール&必要コマンドを導入します。

bb
$ pip install bitbucket-cli
$ touch ~/.bitbucket
$ chmod 600 ~/.bitbucket
$ vim ~/.bitbucket 
~/.bitbucket

[auth]
username = <username>
password = <password>

[options]
scm = git
protocol = ssh
hub
$ brew install hub
wget
$ brew install wget

以下を~/.zshrcに追記

~/.zshrc
function crrr() {
    if [ ! -e .git ]; then
      echo "please git init."
      return
    fi

    local arr
    arr=( `pwd | tr -s '/' ' '`)
    local REPO=$arr[-1]
    local OWNER=$arr[-2]
    local SERVICE=$arr[-3]
    case $SERVICE in
        "bitbucket.org")
            bb list | grep  $OWNER/$REPO
            if [[ $? == 0 ]]; then
                echo "this repo is exist."
                return
            fi
            echo "create repo in $SERVICE"
            #crete_from_local代替
            bb create -w $OWNER -c $REPO
            git remote add origin [email protected]:${OWNER:l}/${REPO:l}.git
            git push -u origin master
            ;;
        "github.com")
            wget -q --spider "https://github.com/$OWNER/$REPO"
            if [[ $? == 0 ]]; then
                echo "this repo is exist."
                return
            fi
            echo "create repo in $SERVICE"
            gh create $OWNER/$REPO
            git push -u origin master
            ;;
        *)
            echo "no hit.";;
    esac
}

source ~/.zshrcで再設定しておきます。

実際の手順

手順
$ cd ~/repo/github.com/m0a
$ #新規プロジェクトを作成します
$ mkdir sample00
$ cd sample00
$ git init
$ echo "test create" > README.md
$ git add -A
$ git commit -m "init."
$ crrr

という感じで使います。