Skip to content

Instantly share code, notes, and snippets.

@tangoabcdelta
Last active May 22, 2023 12:14
Show Gist options
  • Save tangoabcdelta/7fb603755e19e72ac05ff68fa56eb57d to your computer and use it in GitHub Desktop.
Save tangoabcdelta/7fb603755e19e72ac05ff68fa56eb57d to your computer and use it in GitHub Desktop.
recursively checkout all folders
#!/bin/bash
# this file recursively traverse through each sub-folder inside the parent folder
# it performs the git actions e.g. stash, rebase from main and move on as specified
# it also waits for manual conflict resolution if necessary
# set permission for this file: chmod +x checkout-all-repos.sh
# execute the script: `./checkout-all-repos.sh`
# Function to check if there are any uncommitted changes
function has_uncommitted_changes() {
git diff-index --quiet HEAD -- || return 0
git diff-files --quiet || return 0
git ls-files --other --exclude-standard --directory --no-empty-directory --error-unmatch . >/dev/null 2>&1 || return 0
return 1
}
# Function to perform the required actions in each directory
function process_directory() {
cd "$1" || return
echo "Processing directory: $PWD"
# Check if the current directory is a git repository
if [[ -d ".git" ]]; then
# Check if there are any uncommitted changes
# Comment these if you get these errors everytime
if has_uncommitted_changes; then
echo "Error: There are uncommitted changes in $PWD. Resolve the conflicts manually."
return
fi
# Perform git actions
git status
git add .
git stash
git fetch -a
git checkout master || git checkout main
git pull --rebase
git stash pop
git stash clear
# Check if the pull succeeded
if [[ $? -ne 0 ]]; then
echo "Error: There was a problem pulling changes in $PWD. Resolve the conflicts manually."
return
fi
else
echo "Error: $PWD is not a git repository."
fi
echo "Completed processing directory: $PWD"
echo
}
# Main script execution
# Make sure to replace "/path/to/parent/folder" in this script
# with the actual path to your parent folder
parent_directory="/path/to/parent/folder"
# Check if the parent directory exists
if [[ ! -d "$parent_directory" ]]; then
echo "Error: The parent directory does not exist."
exit 1
fi
# Loop through each sub-directory in the parent directory
for directory in "$parent_directory"/*; do
if [[ -d "$directory" ]]; then
process_directory "$directory"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment