LoginSignup
16
16

More than 5 years have passed since last update.

Zsh + peco + ghq で一工夫したらリポジトリへの移動とインポートがとても楽になった

Last updated at Posted at 2014-09-12

ghq便利なんですが、サブコマンドいらないなと思って、ちょっとしたラッパーを書きました。
$ rでリポジトリ管理に必要なことがだいたい出来るのでよいです。

  • $ r (repository name) : そのリポジトリに移動
  • $ r : ghqでインポートしたリポジトリをpecoで選択
  • $ r [tab] : ghqでインポートしたリポジトリ一覧が補完される
  • $ r git@github.com:peco/peco.git : インポートしてなかったらインポートする

動く様子

de2.gif

下記の関数と補完を.zshrcに追加すれば動きます。

Zshの関数:

function repo(){
  local result=""
  local choice=""
  local dest=""

  if [ "$1" =~ "^(git|http)" ] ; then
    ghq get $1
    return
  fi

  if [ -z $1 ]; then
    choice=$(ghq list --unique | peco)
    if [ -z $choice ]; then
      echo "No repo was chosen"
    else
      result=$choice
    fi
  else
    result=$1
  fi

  dest=`ghq list --exact --full-path $result`

  if [ "$dest" == "" ] ; then
    echo "Not found"
  else
    cd $dest
  fi
}

補完:

function _repo {
  _values $(ghq list --unique)
}

compdef _repo repo

alias r='repo'

本当は$ rでインポートした後にそこに移動したいんだけど、URIのパースが面倒なのでやっていない。

16
16
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
16
16