Skip to content

Instantly share code, notes, and snippets.

@annibal
Created July 29, 2024 20:08
Show Gist options
  • Save annibal/7261ae341f39b8937889739c3be50721 to your computer and use it in GitHub Desktop.
Save annibal/7261ae341f39b8937889739c3be50721 to your computer and use it in GitHub Desktop.
Usage: cd repositories; rung -c "git branch" -d "./"
run-git-on-all-folders() {
arg_d=$(pwd);
arg_c="git branch --show-current";
arg_a=0;
VERBOSE=0;
verb_echo() {
if [ $VERBOSE -eq 1 ]; then
echo "> $*"
fi
}
while getopts ":d:c:av" o; do
# echo "#$OPTIND: -$o = '$OPTARG'";
case "${o}" in
d)
arg_d=${OPTARG}
verb_echo "received root directory to run from via argument: '$arg_d'";
;;
c)
arg_c=${OPTARG}
verb_echo "received command via argument: '$arg_c'";
;;
a)
arg_a=1;
verb_echo "will list hidden files too";
;;
v)
VERBOSE=1
verb_echo "verbose mode on";
;;
*)
unk_arg=${OPTARG}
verb_echo "unknown argument '$unk_arg', printing usage and exiting";
echo "Usage: run-git-on-all-folders [-d DIR='.'] [-c COMMAND='git branch --show-current'] [-a] [-v]" 1>&2
exit 1
;;
esac
done
# shift $((OPTIND - 1))
if [[ ! -d "$arg_d" ]]; then
echo "Fatal: not a directory: '${arg_d}'"
exit 1
fi
(
verb_echo "changing directory to '$arg_d'";
cd $arg_d;
paths=(*);
if [ $arg_a ]; then paths=(* .*); fi
verb_echo "paths=(${paths[@]})";
for folder in "${paths[@]}"; do
if [ -d "$folder" ]; then
if [ -d "$folder/.git" ]; then
verb_echo "$folder - is a GIT FOLDER, proceeding";
(
cd "$folder";
res=$(eval "$arg_c");
cd - >/dev/null;
str=""
str+="\033[34m"
str+="⯈ $folder "
str+="\033[0m"
str+="\033[36m"
str+="("
str+="\033[0m"
if [ ! -z $res ]; then
if echo "$res" | sed 1d | grep -q .; then
str+="\n$res\n"
else
str+=" $res "
fi
fi
str+="\033[36m"
str+=")"
str+="\033[0m"
echo $str;
)
else
verb_echo "$folder - is a FOLDER, has no GIT";
fi
else
verb_echo "$folder - is NOT a FOLDER"
fi
done
);
verb_echo "completed";
}
alias rung="run-git-on-all-folders"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment