diff options
| author | Hsieh Chin Fan <pham@topo.tw> | 2022-11-18 10:32:53 +0800 |
|---|---|---|
| committer | Hsieh Chin Fan <pham@topo.tw> | 2022-11-18 10:32:53 +0800 |
| commit | 65d008feebb024b21b75f1901d55605d8448cd66 (patch) | |
| tree | 2dd0ba85a403eead37f48d88ce45e70363f1a530 /tools | |
| parent | 9134283c801da59c3236f90a70bd82d1a9dbffe3 (diff) | |
Refactor alias 'ch'
Diffstat (limited to 'tools')
| -rwxr-xr-x | tools/git/check-repos.sh | 46 |
1 files changed, 46 insertions, 0 deletions
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 @@ | |||
| 1 | #! /bin/bash | ||
| 2 | |||
| 3 | LIST=~/.repos | ||
| 4 | [[ $1 == -n ]] && { | ||
| 5 | COUNT_ONLY=true | ||
| 6 | count=0 | ||
| 7 | } | ||
| 8 | |||
| 9 | |||
| 10 | # Only works when file ~/.repos exists and readable | ||
| 11 | if [ ! -r $LIST ]; then | ||
| 12 | echo File ~/.repos not found/readable | ||
| 13 | exit 1 | ||
| 14 | fi | ||
| 15 | |||
| 16 | |||
| 17 | while read repo remote; do | ||
| 18 | [[ "$repo" =~ ^[[:space:]]*#.* ]] && continue | ||
| 19 | |||
| 20 | # In case repo is consists of variable like $HOME | ||
| 21 | # Use eval to get git information | ||
| 22 | eval cd $repo 2>/dev/null || { | ||
| 23 | echo Repo $repo is inaccessible | ||
| 24 | exit 1 | ||
| 25 | } | ||
| 26 | |||
| 27 | # Changes in working dir, not yet to be a commit | ||
| 28 | changes="$(git -c color.status=always status --short)" | ||
| 29 | |||
| 30 | # Diff between from local repo and remote | ||
| 31 | cherry="$(git cherry)" | ||
| 32 | |||
| 33 | if [[ $COUNT_ONLY == true ]]; then | ||
| 34 | # If '-n' is specified, only count repo with changes/local-diff | ||
| 35 | [[ -n "$changes" || -n "$cherry" ]] && (( count++ )) | ||
| 36 | else | ||
| 37 | # Or, just print their status | ||
| 38 | echo Check $repo | ||
| 39 | [[ -n "$changes" ]] && echo "$changes" | ||
| 40 | [[ -n "$cherry" ]] && echo -e "\e[31m[ahead]\e[0m" | ||
| 41 | fi | ||
| 42 | done <$LIST | ||
| 43 | |||
| 44 | |||
| 45 | # If '-n' is specified, print number of repos with changes/local-diff | ||
| 46 | [[ $COUNT_ONLY == true ]] && echo $count | ||