Skip to content

Instantly share code, notes, and snippets.

@rjocoleman
Created November 22, 2023 12:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rjocoleman/7b549da87e61a6b0be283cb82693df00 to your computer and use it in GitHub Desktop.
Save rjocoleman/7b549da87e61a6b0be283cb82693df00 to your computer and use it in GitHub Desktop.
#!/bin/bash
set -euo pipefail
IFS=$'\n\t'
BRANCH="16.0" # Branch name
REMOTE="origin" # Remote name
CHUNK_SIZE=900
# Get the total number of commits in the branch
TOTAL_COMMITS=$(git rev-list --count $BRANCH)
echo "Total commits in $BRANCH: $TOTAL_COMMITS"
# Calculate how many chunks are needed
NUM_CHUNKS=$((TOTAL_COMMITS / CHUNK_SIZE))
if [ $((TOTAL_COMMITS % CHUNK_SIZE)) -ne 0 ]; then
NUM_CHUNKS=$((NUM_CHUNKS + 1))
fi
echo "Number of chunks to push: $NUM_CHUNKS"
# Get all commits from the branch in reverse order
COMMIT_HASHES=$(git rev-list --reverse $BRANCH)
# Loop for chunked pushing
for ((i=1; i<=$NUM_CHUNKS; i++))
do
# Calculate the commit range for each chunk
CHUNK_START=$(( (i - 1) * CHUNK_SIZE + 1))
CHUNK_END=$(( i * CHUNK_SIZE ))
if [ $CHUNK_END -gt $TOTAL_COMMITS ]; then
CHUNK_END=$TOTAL_COMMITS
fi
# Extract the start and end commit hashes for this chunk
START_COMMIT=$(echo "$COMMIT_HASHES" | sed -n "${CHUNK_START}p")
END_COMMIT=$(echo "$COMMIT_HASHES" | sed -n "${CHUNK_END}p")
# Handle the case where we're pushing the first chunk
if [ $i -eq 1 ]; then
RANGE_REF="$START_COMMIT"
else
RANGE_REF="$START_COMMIT^"
fi
echo "Pushing commits from $START_COMMIT to $END_COMMIT to $REMOTE/$BRANCH"
git push $REMOTE +$RANGE_REF:$BRANCH -f
if [ $? -ne 0 ]; then
echo "Push failed for commits from $START_COMMIT to $END_COMMIT. Exiting."
exit 1
fi
done
# Final push to ensure the latest state is pushed
echo "Pushing the final state of $BRANCH to $REMOTE/$BRANCH"
git push $REMOTE +$BRANCH:$BRANCH -f
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment