Last active
December 13, 2022 06:27
-
-
Save ryochin/176265bfe632974e877dfc8c083aac0e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# https://qiita.com/uasi/items/430f9403ac9b437c0499 | |
# * -R を削除し固定した | |
# * 同名のファイルが存在する場合にエラーになる問題を修正した | |
# * 日付フォーマットを変更した | |
# * 現在のブランチにマークを付けた | |
# * いくつかのブランチを特別な色で表示するよう変更した | |
USAGE="\ | |
usage: git branch-activity [-r | -a] [--no-color] | |
-r, --remotes List remote-tracking branches | |
-a, --all List both remote-tracking and local branches | |
--no-color Don't use colored output" | |
GIT_BRANCH_OPT= | |
print_branches() { | |
git branch --sort=authordate $GIT_BRANCH_OPT | perl -lne 'print $1 if /^[ *] ?(\S+)/' | |
} | |
decorate_branches() { | |
local use_color=$1 | |
local format | |
local current_branch | |
if [[ -n "$use_color" ]]; then | |
format="format:\e[34m[%cd]\e[m \e[33m%h\e[m" # blue, yellow | |
else | |
format="format:[%cd] %h" | |
fi | |
current_branch=$(git symbolic-ref -q --short HEAD) | |
while read -r br; do | |
printf "$(git log --date=iso8601 -n 1 --pretty="$format" "$br" --date=format:'%Y.%m.%d %H:%M' --) " | |
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 magenta="\e[35m%s\e[m" | |
local format="%s" | |
case "$GIT_BRANCH_OPT" in | |
-a) | |
if [[ "$branch" = remotes/* ]]; then | |
format=$red | |
elif [[ "$branch" = "$current" ]]; then | |
format=$green | |
elif [[ "$branch" =~ master|main|develop|alpha|beta|test|demo|tmp|release ]]; then | |
format=$magenta | |
fi | |
;; | |
-r) | |
format=$red | |
;; | |
*) | |
if [[ "$branch" = "$current" ]]; then | |
format=$green | |
elif [[ "$branch" =~ master|main|develop|alpha|beta|test|demo|tmp|release ]]; then | |
format=$magenta | |
fi | |
;; | |
esac | |
if [[ "$branch" = "$current" ]]; then | |
printf "$format\n" "* $branch" | |
else | |
printf "$format\n" " $branch" | |
fi | |
} | |
main() { | |
local use_color | |
if [[ -t 1 ]]; then | |
use_color=1 | |
fi | |
while [[ $# -gt 0 ]]; do | |
case "$1" in | |
-a|--all ) GIT_BRANCH_OPT=-a ;; | |
-r|--remotes) GIT_BRANCH_OPT=-r ;; | |
--no-color ) use_color= ;; | |
-h ) | |
echo "$USAGE" | |
exit | |
esac | |
shift | |
done | |
print_branches \ | |
| decorate_branches "$use_color" | |
} | |
main "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment