19
19

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

ghq で管理している各リポジトリでコマンドを実行する ghq-foreach 書いた

Posted at

ghq で取得したリポジトリ全部で git pull --ff-only したり git gc したりできたら嬉しいな、ということで嬉しくなるやつを書いた。

ghq-foreach gc --aggressive

ghq-foreach <git-subcmd> で任意のサブコマンドを各リポジトリに対して実行できる。 -e オプションをつけて ghq-foreach -e <shell-cmd> するとシェルコマンドも使える。

複数行のシェルコマンドなら ghq-foreach -e sh -c 'cmd1; cmd2' となる。

ghq-foreach.sh
#!/bin/bash

#
# ghq-foreach - executes git subcmd or shell cmd for each repo managed by ghq
#

EXEC=
QUIET=

say() {
    [[ -n "$QUIET" ]] && return
    if [[ -t 1 ]]; then
        printf '\e[1;34m%s\e[m\n' "$1"
    else
        printf '%s\n' "$1"
    fi
}

main() {
    if [[ "$1" = "-q" ]]; then
        QUIET=1
        shift
    fi
    if [[ "$1" = "-e" ]]; then
        EXEC=1
        shift
    fi

    if [[ "$#" -eq 0 ]]; then
        echo "usage: ghq-foreach [-q] (<git-cmd> | -e <shell-cmd>) [args...]" >&2
        echo "    -e  Execute shell command" >&2
        echo "    -q  Don't print each repository path" >&2
        return 1
    fi

    ghq list -p | while read -r repo; do
        (
            cd "$repo"
            say "> $repo"
            if [[ -n "$EXEC" ]]; then
                "$@"
            else
                git "$@"
            fi
        )
    done
}

main "$@"
19
19
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
19
19

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?