From 6fae25b305d714b3ab7608fa003f1af9bf024545 Mon Sep 17 00:00:00 2001 From: Hsieh Chin Fan Date: Tue, 14 Feb 2023 13:33:23 +0800 Subject: Rename tools into bin --- bin/git/check-repos.sh | 48 ++++++++++++++++++++++++++++++++++++++++++++++ bin/git/swap-protocol.bash | 25 ++++++++++++++++++++++++ 2 files changed, 73 insertions(+) create mode 100755 bin/git/check-repos.sh create mode 100755 bin/git/swap-protocol.bash (limited to 'bin/git') diff --git a/bin/git/check-repos.sh b/bin/git/check-repos.sh new file mode 100755 index 0000000..5180fc9 --- /dev/null +++ b/bin/git/check-repos.sh @@ -0,0 +1,48 @@ +#! /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="$([ -n "`git remote`" ] && 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 + +exit 0 diff --git a/bin/git/swap-protocol.bash b/bin/git/swap-protocol.bash new file mode 100755 index 0000000..b7f4db2 --- /dev/null +++ b/bin/git/swap-protocol.bash @@ -0,0 +1,25 @@ +#! /usr/bin/env bash +# Get the first remote URL within git/https protocol on github.com +# Swap the protocol, and apply new protocol to every remaining remotes + +target='' +extra='' + +# For each remote +git remote -v \ +| while read remote url etc; do + # Set fetch/push URL seperately + [[ $etc =~ push ]] && extra='--push' || extra='' + + if [[ $url =~ git@.*github.com ]]; then + target=${target:-https} + # git@ -> https:// + [[ $target == https ]] && sed -E 's#^git@(.+):(.+)$#https://\1/\2#' <<<$url | xargs git remote set-url $extra $remote + elif [[ $url =~ https://.*github.com ]]; then + target=${target:-git} + # https:// -> git@ + [[ $target == git ]] && sed -E 's#^https://([^/]+)/(.+)$#git@\1:\2#' <<<$url | xargs git remote set-url $extra $remote + fi +done + +git remote -v -- cgit v1.2.3-70-g09d2