Skip to content

Instantly share code, notes, and snippets.

@Magnus167
Last active February 21, 2025 15:19
Show Gist options
  • Save Magnus167/d5a721840993d31f54a8a5ec442bba63 to your computer and use it in GitHub Desktop.
Save Magnus167/d5a721840993d31f54a8a5ec442bba63 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Usage: myscript --base branchA --head branchB
# This script iterates through directories starting with "MYREPO" in the current folder,
# checks if they are Git repositories, updates the specified branches, and saves the commit
# diff (with a custom format) into a release info file for each repo.
# Define your repository prefix
PREFIX="MYREPO"
# Parse command-line arguments
while [[ "$#" -gt 0 ]]; do
case $1 in
--base)
BASE="$2"
shift
;;
--head)
HEAD="$2"
shift
;;
*)
echo "Unknown parameter passed: $1"
exit 1
;;
esac
shift
done
if [[ -z "$BASE" || -z "$HEAD" ]]; then
echo "Usage: $0 --base branchA --head branchB"
exit 1
fi
# Save the top-level directory (where the script was invoked)
TOP_DIR=$(pwd)
# Iterate over each directory starting with the defined prefix
for dir in ${PREFIX}*/; do
if [ -d "$dir" ]; then
repo_name=$(basename "$dir")
# Check if the directory contains a .git folder
if [ -d "${dir}/.git" ]; then
echo "Processing repository: $repo_name"
cd "$dir" || continue
# Save current branch so we can return to it later
current=$(git rev-parse --abbrev-ref HEAD)
# Update the base branch
echo "Checking out and pulling latest changes for $BASE in $repo_name..."
git checkout "$BASE" || { echo "Failed to checkout $BASE in $repo_name"; cd "$TOP_DIR"; continue; }
git pull || { echo "Failed to pull on $BASE in $repo_name"; cd "$TOP_DIR"; continue; }
# Update the head branch
echo "Checking out and pulling latest changes for $HEAD in $repo_name..."
git checkout "$HEAD" || { echo "Failed to checkout $HEAD in $repo_name"; cd "$TOP_DIR"; continue; }
git pull || { echo "Failed to pull on $HEAD in $repo_name"; cd "$TOP_DIR"; continue; }
# Save the commit list directly to the output file with the desired formatting.
# The format is "|||commit-hash|||commit-message|||;" with a newline after each commit.
output_file="${TOP_DIR}/${repo_name}_release-info.txt"
git log "$BASE..$HEAD" --pretty=format:"|||%H|||%s|||;\n" > "$output_file"
echo "Saved release info for $repo_name to $output_file"
# Return to the original branch and then to the top-level directory
git checkout "$current" || { echo "Failed to return to $current in $repo_name"; }
cd "$TOP_DIR" || exit
else
echo "Skipping $repo_name: no .git folder found."
fi
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment