Skip to content

Instantly share code, notes, and snippets.

@keif

keif/git_merge_check.sh

Last active Mar 25, 2019
Embed
What would you like to do?
This utilizes an external text file (branches.txt) and runs a test against the branches to see if there is a merge conflict.
#!/bin/bash
function git_merge_test() {
echo "test git branch merges";
RUN_UNIT_TESTS=0
while test $# -gt 0; do
case "$1" in
-h|--help)
echo "test the branches in branches.txt for merge conflicts"
echo " "
echo "git_merge_test [options]"
echo " "
echo "options:"
echo "-h, --help show brief help"
echo "-t, --test execute unit tests on successful merge"
return 0
;;
-t)
shift
RUN_UNIT_TESTS=1
;;
*)
break
;;
esac
done
# prune, pull and update develop
git prune && git checkout develop && git pull;
# create test merge branch
test_branch_name="test-merge$(date '+%d%m%S')";
echo "Creating local testing branch $test_branch_name";
git checkout -b $test_branch_name;
branches="branches.txt";
while IFS='' read -r branch || [[ -n "$branch" ]]; do
# strip out any hash comments
clean_branch=${branch%#*}
# trim whitepsace
trim_branch="$(echo -e "${clean_branch}" | tr -d '[:space:]')"
# only execute if there is a branch
if [ -n "$trim_branch" ]; then
echo "Merging $trim_branch"
(git merge --no-edit refs/remotes/origin/${trim_branch} && echo "$trim_branch: SUCCESS" >> build-result.txt) || (git merge --abort && echo "$trim_branch: FAIL" >> build-result.txt)
fi;
done < "$branches"
cat build-result.txt
if grep -F " FAIL" build-result.txt; then
echo "Stopping because of a merge conflict in one of the branches";
rm build-result.txt;
# switch back to develop and delete the test branch
git checkout develop;
git reset --hard origin/develop;
git branch -D $test_branch_name;
return 1;
fi
rm build-result.txt;
if [ $RUN_UNIT_TESTS == 1 ]; then
echo "Executing build and unit tests...";
grunt;
fi;
# switch back to develop and delete the test branch
git checkout develop;
git reset --hard origin/develop;
git branch -D $test_branch_name;
};
@keif

This comment has been minimized.

Copy link
Owner Author

@keif keif commented Jan 20, 2019

No, I haven't dug into finding a way to grab the messages git responds with to more accurately tell you what "SUCCESS" or "FAILURE" means in a "the branch was already merged" kind of way.

@keif

This comment has been minimized.

Copy link
Owner Author

@keif keif commented Mar 25, 2019

Made the unit test execution a passed in argument for now - there's a few instances where I just want to test the branch merges and not the tests.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment