Skip to content

Instantly share code, notes, and snippets.

@ssaid
Last active March 8, 2023 16:39
Show Gist options
  • Save ssaid/1b95350441192bffbf4b2d422442c9b4 to your computer and use it in GitHub Desktop.
Save ssaid/1b95350441192bffbf4b2d422442c9b4 to your computer and use it in GitHub Desktop.
After using BFG, check if the source code has not been modified
#!/bin/bash
# PURPOSE: Compare if two directories have the same source code in all branches
# WHY?: Ensure that bgf has not altered the code after running
# HOW?: Easy! dir1 is the original untouched repo, dir2 is what have been altered by BFG.
# 1. From untouched directory, grab all the branches and iterate over them
# 2. Magic happens in compare_branch_and_sha: checkout to that branch and get an md5sum of all files for each directory.
# 3. The output is self-explanatory!
# Thanks to https://rtyley.github.io/bfg-repo-cleaner/ for that awesome and magic project :)
dir1=$1
dir2=$2
compare_branch_and_sha(){
cd $1
git checkout $3 > /dev/null 2>&1
sha_of_all_files=$(find . -type f -not -path "./.git/*" -exec md5sum {} \; | sort -k 2 | md5sum -|cut -d' ' -f1)
actual_branch=$(git rev-parse --abbrev-ref HEAD)
cd ..
src_resume="$actual_branch:$sha_of_all_files"
cd $2
git checkout $3 > /dev/null 2>&1
sha_of_all_files=$(find . -type f -not -path "./.git/*" -exec md5sum {} \; | sort -k 2 | md5sum -|cut -d' ' -f1)
actual_branch=$(git rev-parse --abbrev-ref HEAD)
cd ..
dest_resume="$actual_branch:$sha_of_all_files"
if [[ "$src_resume" == "$dest_resume" ]]; then
echo "Same $1:$2 branch $3"
else
echo "Different $1:$2 branch $3 ($src_resume!=$dest_resume)"
fi
}
cd $1
list_of_branches=$(git branch -a -l --format "%(refname)"|grep -v detached|grep origin|cut -d'/' -f4)
cd ..
while IFS= read -r branch; do
compare_branch_and_sha $1 $2 $branch
done <<< "$list_of_branches"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment