Skip to content

Instantly share code, notes, and snippets.

@nicolas-oliveira
Last active September 24, 2024 19:24
Show Gist options
  • Save nicolas-oliveira/00e6d8ffa000df2582121ece55ed489f to your computer and use it in GitHub Desktop.
Save nicolas-oliveira/00e6d8ffa000df2582121ece55ed489f to your computer and use it in GitHub Desktop.
Get .git subfolders from any folder and more metadata
#!/bin/bash
PWD="$(pwd)"
stat_mod=""
stat_created=""
stat_perm=""
stat_size=""
echo -e "\n\e[1m SHOW COMMAND\n"
function start_loading() {
local pid=$1
local loading_animation=( '-' "\\" '|' '/' )
tput civis
trap "tput cnorm" EXIT
# Loop until the process finishes
while kill -0 "$pid" 2>/dev/null; do
for frame in "${loading_animation[@]}" ; do
printf " %s\b\b\b" "${frame}"
sleep 0.15
done
done
printf " \b"
tput cnorm
}
function run() {
{
e=$(eval $1)
echo "$e" > /tmp/e
} & pid=$!
start_loading $pid
}
# Run commands in the background and capture their outputs
run "stat -c %y $PWD"
stat_mod=$(cat /tmp/e)
run "stat -c %w $PWD"
stat_created=$(cat /tmp/e)
run "stat -c %a $PWD"
stat_perm=$(cat /tmp/e)
run "du -sh"
stat_size=$(cat /tmp/e)
rm /tmp/e
echo -e "\e[34m last modified in: \e[39m$stat_mod"
echo -e "\e[34m created in: \e[39m$stat_created"
echo -e "\e[34m folder permissions: \e[39m$stat_perm"
echo -e "\e[34m folder size: \e[39m$stat_size"
scan_current_folder() {
git_info="$(git config --get remote.origin.url)"
git_last_commit="$(git log -1 --format='%cr')"
git_last_author="$(git log -1 --format='%an')"
echo -e "\n\n Git folder found!\n"
echo -e "\e[32m remote.origin.url: \e[39m$git_info"
echo -e "\e[32m last commit: \e[39m$git_last_commit ($git_last_author)"
}
scan_subfolders() {
for subfolder in $@
do
echo " -----------------------------"
git_subfolder_info="$(git -C $subfolder config --get remote.origin.url)"
git_subfolder_last_commit="$(git -C $subfolder log -1 --format='%cr')"
git_subfolder_author="$(git -C $subfolder log -1 --format='%an')"
echo -e "\n\n\e[32m Git folder found: \e[39m$(dirname $subfolder)\n"
echo -e "\e[32m remote.origin.url: \e[39m$git_subfolder_info"
echo -e "\e[32m last commit: \e[39m$git_subfolder_last_commit ($git_subfolder_author)"
done
}
#VERIFIES IF GIT EXISTS IN CURRENT FOLDER AND SHOW RELEVANTS INFORMATIONS
if [[ -d "$PWD/.git/" ]]
then
scan_current_folder
elif [[ -n $(find . -type d -name '.git' -maxdepth 5 2>/dev/null) ]]; then
readarray -t gitdir < <(find . -type d -name '.git' -maxdepth 5 2>/dev/null)
echo -e "\n\n Found ${#gitdir[@]} repositories that contains '.git' subfolders\n"
while true; do
read -p " Show all? [y/n] " yn
case $yn in
[Yy]* ) scan_subfolders ${gitdir[@]}; break;;
[Nn]* ) exit;;
* ) echo "Please answer yes or no.";;
esac
done
else
echo -e "\n\n \e[31mGit folder doesn't exists in current folder\e[39m"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment