From 65d008feebb024b21b75f1901d55605d8448cd66 Mon Sep 17 00:00:00 2001 From: Hsieh Chin Fan Date: Fri, 18 Nov 2022 10:32:53 +0800 Subject: Refactor alias 'ch' --- alias | 19 ++----------------- tools/git/check-repos.sh | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 17 deletions(-) create mode 100755 tools/git/check-repos.sh diff --git a/alias b/alias index 2956eb1..c264154 100644 --- a/alias +++ b/alias @@ -72,6 +72,7 @@ alias gg='gist grep' # unix alias chx='chmod +x' +alias chr='chmod +r' alias s='sudo systemctl' alias j='sudo journalctl -xe' alias ju='sudo journalctl -u' @@ -222,23 +223,7 @@ alias gls='git log -S' alias cdgg='cd $(git rev-parse --show-toplevel)' alias cdgw='cdgg && cd .github/workflows' alias cdgs='cd $(git submodule status | sed "s/^.//" | cut -d" " -f2)' # cd to first submodule -check_repo() { - cd $1 2>/dev/null || return 0 - echo check $1 - git status -s - [[ -n $(git cherry ${2:-origin}) ]] 2>/dev/null && print "\e[31m[ahead]\e[0m" -} -check() { - DIR=`pwd` - check_repo $SETTING_DIR - check_repo $HOME/.password-store - check_repo $HOME/log - check_repo $HOME/.task - check_repo $HOME/bean - check_repo $HOME/.vim/vim-init vps - cd $DIR -} -alias ch='check' +alias ch="/home/pham/helper/tools/git/check-repos.sh" # github export GITHUB_API='https://api.github.com' diff --git a/tools/git/check-repos.sh b/tools/git/check-repos.sh new file mode 100755 index 0000000..308bea6 --- /dev/null +++ b/tools/git/check-repos.sh @@ -0,0 +1,46 @@ +#! /bin/bash + +LIST=~/.repos +[[ $1 == -n ]] && { + COUNT_ONLY=true + count=0 +} + + +# Only works when file ~/.repos exists and readable +if [ ! -r $LIST ]; then + echo File ~/.repos not found/readable + exit 1 +fi + + +while read repo remote; do + [[ "$repo" =~ ^[[:space:]]*#.* ]] && continue + + # In case repo is consists of variable like $HOME + # Use eval to get git information + eval cd $repo 2>/dev/null || { + echo Repo $repo is inaccessible + exit 1 + } + + # Changes in working dir, not yet to be a commit + changes="$(git -c color.status=always status --short)" + + # Diff between from local repo and remote + cherry="$(git cherry)" + + if [[ $COUNT_ONLY == true ]]; then + # If '-n' is specified, only count repo with changes/local-diff + [[ -n "$changes" || -n "$cherry" ]] && (( count++ )) + else + # Or, just print their status + echo Check $repo + [[ -n "$changes" ]] && echo "$changes" + [[ -n "$cherry" ]] && echo -e "\e[31m[ahead]\e[0m" + fi +done <$LIST + + +# If '-n' is specified, print number of repos with changes/local-diff +[[ $COUNT_ONLY == true ]] && echo $count -- cgit v1.2.3-70-g09d2