LoginSignup
31
32

More than 5 years have passed since last update.

ブランチの最終更新日時を一覧にする git branch-activity 書いた

Last updated at Posted at 2015-04-17

概要

どのブランチが新しくてどれが古いのか分からなくなることが多々あるので分かるようにするやつを書いた。

git branch-activity.png

usage: git branch-activity [-r | -a] [-R] [--no-color]

git branch-activity でローカルブランチ名と更新日時を新しい順に表示する。リモートブランチを表示するなら -r オプションをつける。ローカルとリモート両方表示するなら -a。ようするに git branch のオプションと同じ。

現在のブランチは緑、リモートブランチは赤、その他のローカルブランチは白で表示する。これも git branch に合わせてある。

インストール

curl -o /usr/local/bin/git-branch-activity https://gist.githubusercontent.com/uasi/ec9978d793df35184b33/raw/git-branch-activity.sh
chmod +x /usr/local/bin/git-branch-activity

コード

git-branch-activity.sh
#!/bin/bash

USAGE="\
usage: git branch-activity [-r | -a] [-R] [--no-color]

    -r, --remotes  List remote-tracking branches
    -a, --all      List both remote-tracking and local branches
    -R, --reverse  Reverse list order
    --no-color     Don't use colored output"

GIT_BRANCH_OPT=

print_branches() {
    git branch $GIT_BRANCH_OPT | perl -lne 'print $1 if /^[ *] ?(\S+)/'
}

decorate_branches() {
    local use_color=$1
    local format

    if [[ -n "$use_color" ]]; then
        format="format:\e[34m[%ai]\e[m \e[33m%h\e[m" # blue, yellow
    else
        format="format:[%ai] %h"
    fi

    local current_branch=$(git symbolic-ref -q --short HEAD)

    while read -r br; do
        printf "$(git log --date=iso8601 -n 1 --pretty="$format" "$br") "
        if [[ -n "$use_color" ]]; then
            print_colored_branch "$br" "$current_branch"
        else
            echo "$br"
        fi
    done
}

print_colored_branch() {
    local branch=$1
    local current=$2

    local red="\e[31m%s\e[m"
    local green="\e[32m%s\e[m"
    local format="%s"

    case "$GIT_BRANCH_OPT" in
        -a)
            if [[ "$branch" = remotes/* ]]; then
                format=$red
            elif [[ "$branch" = "$current" ]]; then
                format=$green
            fi
            ;;
        -r)
            format=$red
            ;;
        *)
            if [[ "$branch" = "$current" ]]; then
                format=$green
            fi
            ;;
    esac

    printf "$format\n" "$branch"
}

main() {
    local sort_opt=-r
    local use_color

    if [[ -t 1 ]]; then
        use_color=1
    fi

    while [[ $# > 0 ]]; do
        case "$1" in
            -a|--all    ) GIT_BRANCH_OPT=-a ;;
            -r|--remotes) GIT_BRANCH_OPT=-r ;;
            -R|--reverse) sort_opt=         ;;
            --no-color  ) use_color=        ;;
            -h          )
                echo "$USAGE"
                exit
        esac
        shift
    done

   print_branches \
       | decorate_branches "$use_color" \
       | sort $sort_opt
}

main "$@"
31
32
1

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
31
32