Skip to content

Instantly share code, notes, and snippets.

@drfloob
Last active November 18, 2021 22:36
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save drfloob/7674c881c9ad6f78a15895dcfd81ab4b to your computer and use it in GitHub Desktop.
Save drfloob/7674c881c9ad6f78a15895dcfd81ab4b to your computer and use it in GitHub Desktop.
A script to check if branch A has been squash-merged to branch B (default: master) by evaluating diffs of branch A vs diff of all commits on branch B
#!/bin/bash
set -e
function cleanup {
rm $DIFF_FILE $TMPFILE
}
trap cleanup EXIT
if [ $# -lt 1 ]; then
>&2 echo "Missing branch name"
exit 1
fi
BRANCH="$1"
echo "Checking for squash-merged branch: ${BRANCH}"
CMP_BRANCH="master"
if [ $# -eq 2 ]; then
CMP_BRANCH="$2"
fi
DIFF_FILE=$(mktemp)
BASE=$(git merge-base ${BRANCH} ${CMP_BRANCH})
git diff ${BASE}..${BRANCH} > ${DIFF_FILE}
if [ "$(stat -c%s ${DIFF_FILE})" -eq 0 ]; then
echo "Branch is empty, and can be deleted with: git branch -d ${BRANCH}"
exit 0
fi
less -S $DIFF_FILE
TMPFILE=$(mktemp)
for commit in $(git log --format='%H' ${BASE}..HEAD | tac -); do
echo -n "$commit ... "
git diff ${commit}^..${commit} > $TMPFILE
LDIFF=$(diff ${TMPFILE} ${DIFF_FILE} | wc -l)
if [ ${LDIFF} -lt 10 ]; then
echo "FOUND MERGE COMMIT?"
echo "-- start of diff --"
diff -wub ${TMPFILE} ${DIFF_FILE}
echo "-- end of diff --"
echo "^c if satisfied. Press any key to continue searching"
read
fi
echo "no match"
done;
echo "NO MATCH FOUND. This branch may not be merged."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment