Skip to content

Instantly share code, notes, and snippets.

@felipealfonsog
Last active March 27, 2024 03:48
Show Gist options
  • Save felipealfonsog/c42b8ace896e82d5220f88e6ac781ce5 to your computer and use it in GitHub Desktop.
Save felipealfonsog/c42b8ace896e82d5220f88e6ac781ce5 to your computer and use it in GitHub Desktop.
"Gitcheck," checks for repositories within a directory that require actions, such as adding files or committing changes.
#!/bin/bash
################################################################################
# Script: gitcheck #
# Description: Script to check for repositories needing actions. #
# Created by: Felipe Alfonso González - github.com/felipealfonsog - #
# [email protected] #
# License: BSD 3-Clause #
################################################################################
# Function to check if there are any repositories requiring actions
check_repos() {
# Change directory to the specified path
cd "$1" || exit
# Array to hold directories requiring actions
declare -a repos_needing_action
# Loop through directories
for dir in */; do
# Check if the directory is a git repository
if [ -d "$dir/.git" ]; then
# Change directory to the git repository
cd "$dir" || exit
# Check if there are any changes
if [[ -n $(git status --porcelain) ]]; then
# Add the directory to the array
repos_needing_action+=("$dir")
fi
# Change directory back to the parent directory
cd .. || exit
fi
done
# If there are repositories requiring actions
if [ ${#repos_needing_action[@]} -gt 0 ]; then
# Print the directories requiring actions
echo "The following repositories require actions:"
printf '%s\n' "${repos_needing_action[@]}"
else
echo "No repositories require actions."
fi
}
# Main function
main() {
# Ask user if they want to proceed
read -rp "Do you want to proceed with the action? (Y/n): " choice
# Check user's choice
case ${choice,,} in
[y]|"") check_repos "$PWD";;
[n]) echo "Action aborted.";;
*) echo "Invalid choice. Action aborted.";;
esac
}
# Call the main function
main
#!/bin/bash
################################################################################
# Script: gitcheck_macos #
# Description: Script to check for repositories needing actions. #
# Created by: Felipe Alfonso González - github.com/felipealfonsog - #
# [email protected] #
# License: BSD 3-Clause #
################################################################################
# Function to check if there are any repositories requiring actions
check_repos() {
# Change directory to the specified path
cd "$1" || exit
# Array to hold directories requiring actions
declare -a repos_needing_action
# Loop through directories
for dir in */; do
# Check if the directory is a git repository
if [ -d "$dir/.git" ]; then
# Change directory to the git repository
cd "$dir" || exit
# Check if there are any changes
if [[ -n $(git status --porcelain) ]]; then
# Add the directory to the array
repos_needing_action+=("$dir")
fi
# Change directory back to the parent directory
cd .. || exit
fi
done
# If there are repositories requiring actions
if [ ${#repos_needing_action[@]} -gt 0 ]; then
# Print the directories requiring actions
echo "The following repositories require actions:"
printf '%s\n' "${repos_needing_action[@]}"
else
echo "No repositories require actions."
fi
}
# Main function
main() {
# Ask user if they want to proceed
read -rp "Do you want to proceed with the action? (Y/n): " choice
# Check user's choice
case ${choice,,} in
[y]|"") check_repos "$PWD";;
[n]) echo "Action aborted.";;
*) echo "Invalid choice. Action aborted.";;
esac
}
# Call the main function
main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment