This Bash script performs recursive checks on all subdirectories of...
This Bash script performs recursive checks on all subdirectories of a specified base directory to identify Git repositories and pull the latest changes from their remote repositories if no local changes exist. Here's a step-by-step explanation of what it does:
-
Set the Base Directory:
TheBASE_DIR
variable is set to$HOME/Repos
(default directory to begin scanning for Git repositories). -
Verify Base Directory:
The script checks if the specifiedBASE_DIR
exists. If it does not, it exits with an error message. -
Define Recursive Function
check_git_repos
:- This function takes a directory path as an argument and iterates over all items in that directory.
- For each item:
- If the item is a directory and contains a
.git
subdirectory, it's identified as a Git repository.- The script then:
- Outputs the repository name and path.
- Changes the working directory to the repository.
- Uses
git diff
to check for local unstaged or staged changes:- If no changes are detected, it runs
git pull
to fetch and merge the latest changes from the remote repository. - If changes are detected, it skips the
git pull
and avoids disrupting local work.
- If no changes are detected, it runs
- Runs
git status
to display the current state of the repository.
- The script then:
- If the item is not a Git repository but still a directory, the function recurses into that directory to repeat the checks.
- If the item is a directory and contains a
-
Run the Function on
BASE_DIR
:
After defining the function, the script invokes it onBASE_DIR
, recursively checking its subdirectories for Git repositories. -
Output Status:
The script prints messages to indicate progress or issues (e.g., identifying repositories, detecting local changes, skipping pulls, etc.) and notifies the user upon completion.
Use Case:
This script is useful for managing multiple Git repositories under a parent directory, ensuring they are up-to-date with their remote changes without disrupting ongoing work if local changes are present. It's especially handy for developers working with many projects simultaneously.