Fish Shellでghqを便利にする

shell scriptはちょっとした仕事を楽に処理できるから便利だ。自分はpeco/ghqを日常的に利用し、便利なscriptを幾つか書いてるので記事にしたためておく。


repoはこちら: takeokunn/dotfiles - dotfiles/modules/fish/functions.fish.sample

shell芸初心者なのと、命名が適当なのは御愛嬌。

ghqの任意のrepoに移動する

  1. C-o を押すとpecoがghqで管理しているrepoを取得
  2. isearchで検索できる
  3. 選択されたrepoのディレクトリに移動

といった感じのfunctionだ。一番使用するfunctionといっても過言じゃないほど重宝している。

function peco_ghq
    set -l query (commandline)

    if test -n $query
        set peco_flags --query "$query"
    end

    ghq list --full-path | peco $peco_flags | read recent
    if [ $recent ]
        cd $recent
        commandline -r ''
        commandline -f repaint
    end
end

function fish_user_key_bindings
    bind \co peco_ghq
end

repoをlocalとgithubに作成し、ディレクトリを移動する

  1. ghq createでlocalにrepoを作成し移動する
  2. gh repo createでgithub上にpublic repoを作成する
  3. お馴染のコマンドを叩き、pushする

github cliのお陰で簡単にrepoを作成できるようになった。

function ghq_create
    cd (ghq create (echo $argv)) | gh repo create (echo $argv) --public -y
    echo "# "(echo $argv) >> README.md
    git init
    git add README.md
    git commit -m "first commit"
    git branch -M master
    git remote add origin git@github.com:takeokunn/(echo $argv).git
    git push -u origin master
end

例:

~ (*´ω`*) < ghq_create hoge
       git init
Initialized empty Git repository in /Users/obara_t/.ghq/github.com/takeokunn/hoge/.git/
GraphQL error: Name already exists on this account
Reinitialized existing Git repository in /Users/obara_t/.ghq/github.com/takeokunn/hoge/.git/
[master (root-commit) d67f78a] first commit
 1 file changed, 1 insertion(+)
 create mode 100644 README.md
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Writing objects: 100% (3/3), 216 bytes | 216.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
To github.com:takeokunn/hoge.git
 * [new branch]      master -> master
Branch 'master' set up to track remote branch 'master' from 'origin'.
~/.g/g/t/hoge (*´ω`*) < 

ghqで管理してるユーザ一覧を取得

とりあえず書いてみただけのやつ。

function ghq_users
    ghq list | grep "github.com" | cut -d / -f 2 | sort | uniq
end

例:

~ (*´ω`*) < ghq_users
magnumripper
omniauth
phppg
prabirshrestha
romefrontend
ruby
saml-idp
sjl
takeokunn
....

ユーザのpublic repoを全て取得する

  1. 引数に渡したユーザを元にapiを 叩く
  2. jqでclone_urlを取得
  3. ghq getでcloneする

採用面接必須コマンド。良く使う。

function ghq_get_user_all_repo
    curl "https://api.github.com/users/"(echo $argv)"/repos" | jq -r ".[].clone_url" | xargs -L1 ghq get
end

例:

~ (*´ω`*) < ghq_get_user_all_repo cli
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 15750    0 15750    0     0  31437      0 --:--:-- --:--:-- --:--:-- 31437
     clone https://github.com/cli/cli.git -> /Users/obara_t/.ghq/github.com/cli/cli
       git clone --recursive https://github.com/cli/cli.git /Users/obara_t/.ghq/github.com/cli/cli
Cloning into '/Users/obara_t/.ghq/github.com/cli/cli'...
remote: Enumerating objects: 13758, done.
remote: Total 13758 (delta 0), reused 0 (delta 0), pack-reused 13758
Receiving objects: 100% (13758/13758), 32.50 MiB | 8.28 MiB/s, done.
Resolving deltas: 100% (9177/9177), done.
     clone https://github.com/cli/scoop-gh.git -> /Users/obara_t/.ghq/github.com/cli/scoop-gh
       git clone --recursive https://github.com/cli/scoop-gh.git /Users/obara_t/.ghq/github.com/cli/scoop-gh
Cloning into '/Users/obara_t/.ghq/github.com/cli/scoop-gh'...
remote: Enumerating objects: 77, done.
remote: Counting objects: 100% (77/77), done.
remote: Compressing objects: 100% (52/52), done.
remote: Total 77 (delta 24), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (77/77), 11.73 KiB | 279.00 KiB/s, done.
     clone https://github.com/cli/shurcooL-graphql.git -> /Users/obara_t/.ghq/github.com/cli/shurcooL-graphql
       git clone --recursive https://github.com/cli/shurcooL-graphql.git /Users/obara_t/.ghq/github.com/cli/shurcooL-graphql
Cloning into '/Users/obara_t/.ghq/github.com/cli/shurcooL-graphql'...
remote: Enumerating objects: 171, done.
remote: Total 171 (delta 0), reused 0 (delta 0), pack-reused 171
Receiving objects: 100% (171/171), 51.83 KiB | 253.00 KiB/s, done.
Resolving deltas: 100% (81/81), done.

organizationのrepoを全て取得しcloneする

  1. 引数に渡したorg nameを元にapiを 叩く
  2. jqでclone_urlを取得
  3. ghq getでcloneする

退職時に必要そうなscript。

function ghq_org_all_repo
    curl "takeokunn" "https://api.github.com/orgs/"(echo $argv)"/repos" | jq -r ".[].clone_url" | xargs -L1 ghq get
end

例:

~ (*´ω`*) < ghq_org_all_repo php-java
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0curl: (6) Could not resolve host: takeokunn
100  5629  100  5629    0     0  16653      0 --:--:-- --:--:-- --:--:-- 16653
     clone https://github.com/php-java/php-java.git -> /Users/obara_t/.ghq/github.com/php-java/php-java
       git clone --recursive https://github.com/php-java/php-java.git /Users/obara_t/.ghq/github.com/php-java/php-java
Cloning into '/Users/obara_t/.ghq/github.com/php-java/php-java'...
remote: Enumerating objects: 2175, done.
remote: Counting objects: 100% (2175/2175), done.
remote: Compressing objects: 100% (1095/1095), done.
remote: Total 15233 (delta 1453), reused 1436 (delta 1023), pack-reused 13058
Receiving objects: 100% (15233/15233), 58.02 MiB | 9.82 MiB/s, done.
Resolving deltas: 100% (11706/11706), done.

ghqで管理してるrepoを検索して削除する

ghq getの反対。お掃除する時に使う。

function ghq_remove_repo
    ghq list --full-path | peco | xargs -L1 rm -fr
end

ghqで管理してるユーザのrepoを検索して全て消す

ghq_get_user_all_repo の反対。容量がもったいないので頻度高く使う。

function ghq_remove_user
    ghq list | grep "github.com" | cut -d / -f 2 | sort | uniq | peco | read recent
    ghq list --full-path | grep -E "/"(echo $recent)"/" | xargs -L1 rm -fr
end