Skip to content

Instantly share code, notes, and snippets.

@mahi424
Created August 23, 2023 11:06
Show Gist options
  • Save mahi424/2bcb8542288c41ff13dd303e468031fe to your computer and use it in GitHub Desktop.
Save mahi424/2bcb8542288c41ff13dd303e468031fe to your computer and use it in GitHub Desktop.
#!/bin/bash
checkout_latest_branch() {
# Fetch all remote branches
git fetch --all
# Get the remote branch with the latest commit
latest_branch=$(git for-each-ref --sort=-committerdate --format='%(refname:short)' refs/remotes/origin/ | head -n 1)
if [ -n "$latest_branch" ]; then
# Remove 'origin/' prefix to get the branch name
branch_name=${latest_branch#origin/}
# Checkout the branch with the latest commit
git checkout -b "$branch_name" "$latest_branch"
else
echo "No remote branches found in repo $dir. Skipping..."
fi
}
yarn_install() {
yarn install
}
yarn_build() {
yarn build
}
# Get the root directory from the command-line argument or default to the current directory
ROOT_DIR="${1:-$(pwd)}"
# Loop through all subdirectories of the specified root directory
for dir in "$ROOT_DIR"/*/; do
# Check if the directory is a git repository
if [ -d "$dir/.git" ]; then
# Navigate into the directory
cd "$dir" || continue
echo "Processing repository $dir ..."
checkout_latest_branch
yarn_install
yarn_build
# Navigate back to the root directory for the next iteration
cd "$ROOT_DIR" || continue
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment