push_in_batches/pib
2024-05-25 10:28:23 +00:00

57 lines
1.3 KiB
Bash

#!/bin/sh
# Check if the repository URL is provided
if [ -z "$1" ]; then
echo "Usage: $0 <remote_repo_url>"
exit 1
fi
# Set the remote repository URL from the first positional argument
REMOTE_REPO="$1"
# Get the list of all commit hashes
commits=$(git rev-list --reverse main)
# Set the batch size
BATCH_SIZE=10000
# Initialize counter and range variables
start=0
end=$BATCH_SIZE
# Convert commits to an array
commit_array=($commits)
# Get the total number of commits
total_commits=${#commit_array[@]}
# Push commits in batches
while [ $start -lt $total_commits ]
do
# Determine the end of the current batch
if [ $end -gt $total_commits ]; then
end=$total_commits
fi
# Get the range of commits for the current batch
range="${commit_array[$end-1]}"
# Progress display before each push
percent_complete=$((start * 100 / total_commits))
echo -ne "Pushing commits $start to $((end-1)) of $total_commits ($percent_complete%)\r"
# Redirect git push output
git push $REMOTE_REPO $range:refs/heads/main -f > /dev/null 2>&1
# Check if push succeeded
if [ $? -ne 0 ]; then
echo "Error pushing commits. Exiting."
exit 1
fi
# Update the start and end for the next batch
start=$end
end=$(($end + $BATCH_SIZE))
done