Below is a Fish script that recursively checks all .git folders starting from the default folder /home/wovri/ and warns when there are unstaged files in any of the Git repositories. The script uses find to locate .git directories and git status to check for unstaged changes.
Fish Script: check_unstaged.fish
fish
#!/usr/bin/env fish
# Default folder to start the search
set DEFAULT_FOLDER "/home/wovri/"
# Function to check for unstaged files in a Git repository
function check_unstaged --argument repo_path
# Navigate to the repository directory
cd $repo_path
# Check if there are unstaged changes using `git status`
set unstaged (git status --porcelain 2>/dev/null | grep -v '^??' | wc -l)
# If there are unstaged changes, print a warning
if test $unstaged -gt 0
echo "Warning: Unstaged files found in repository at $repo_path"
end
end
# Main function to find all .git folders and check for unstaged files
function main --argument folder
# Find all .git directories recursively
set git_folders (find $folder -type d -name ".git" 2>/dev/null)
# Iterate over each .git folder
for git_folder in $git_folders
# Extract the parent directory of the .git folder (repository root)
set repo_path (dirname $git_folder)
# Check for unstaged files in the repository
check_unstaged $repo_path
end
end
# Run the main function with the default folder or user-provided folder
if test (count $argv) -eq 0
main $DEFAULT_FOLDER
else
main $argv[1]
fi
Explanation of the Script
Default Folder:
The script defaults to /home/wovri/ as the starting point for searching .git folders. This can be overridden by passing a different folder as an argument when running the script.
Finding .git Folders:
The find command is used to locate all .git directories recursively within the specified folder. The -type d -name ".git" ensures that only directories named .git are matched.
Checking for Unstaged Files:
For each .git folder found, the script navigates to its parent directory (the repository root).
The git status --porcelain command is used to check the status of the repository. The output is filtered to exclude untracked files (grep -v '^??') and count the number of lines representing unstaged changes.
If there are any unstaged changes (wc -l > 0), a warning message is printed.
Handling Arguments:
If no argument is provided, the script uses the default folder /home/wovri/.
If an argument is provided, it is treated as the starting folder for the search.
Error Handling:
The script suppresses errors from find and git commands using 2>/dev/null to avoid cluttering the output with irrelevant messages.
How to Use the Script
Save the script to a file, e.g., check_unstaged.fish.
Make the script executable:
fish
chmod +x check_unstaged.fish
Run the script:
To use the default folder:
fish
./check_unstaged.fish
To specify a custom folder:
fish
./check_unstaged.fish /path/to/custom/folder
Example Output
If there are unstaged files in any of the repositories, the script will output warnings like this:
code
Warning: Unstaged files found in repository at /home/wovri/project1
Warning: Unstaged files found in repository at /home/wovri/project2/submodule
If no unstaged files are found, the script will produce no output.
This script is efficient and handles multiple repositories gracefully, making it suitable for managing large codebases.