Skip to content

Instantly share code, notes, and snippets.

@mattcree
Last active April 4, 2024 16:17
Show Gist options
  • Save mattcree/c6d8cc95f41e30b5d7467e9d2b01cd3d to your computer and use it in GitHub Desktop.
Save mattcree/c6d8cc95f41e30b5d7467e9d2b01cd3d to your computer and use it in GitHub Desktop.
Simulating different exit code scenarios from a custom merge driver

Usage

The following scenarios can be created

Single merge base

$ ./init-repo.sh && ./run-merge.sh 0    # should successfully merge and return exit 0
$ ./init-repo.sh && ./run-merge.sh 1    # should have conflicts and return exit 1
$ ./init-repo.sh && ./run-merge.sh 129  # should fail to run internal merge and return exit 2

Multiple merge base

$ ./init-repo.sh && ./run-recursive-merge.sh 0    # should successfully merge and return exit 0
$ ./init-repo.sh && ./run-recursive-merge.sh 1    # should have conflicts and return exit 1
$ ./init-repo.sh && ./run-recursive-merge.sh 129  # should fail to run internal merge and return exit 134 for some reason?
rm -rf merge-driver-test
mkdir merge-driver-test
cd merge-driver-test
git init .
git commit --allow-empty -m "Initial"
cd merge-driver-test
echo "Setting merge driver path to $(realpath $(dirname $0))"
git config merge.custom.name "Custom merge driver"
git config merge.custom.driver "$(realpath $(dirname $0))/custom-merge-driver.sh %O %A %B %L %P"
touch .gitattributes
echo "* merge=custom" >> .gitattributes
touch custom-merge-driver.sh
echo "echo 'Hello!'" >> custom-merge-driver.sh
echo "exit $1" >> custom-merge-driver.sh
chmod +x custom-merge-driver.sh
git checkout master
echo "created on: master" > my-file.mrg
git add my-file.mrg
git commit -m"master: added my-file.mrg"
# Update 'my-file.mrg' on branch 1
git checkout -b branch-1
echo "updated on: branch-1" > my-file.mrg
git add my-file.mrg
git commit -m "branch-1: updated my-file.mrg"
# Update 'my-file.mrg' on branch 2
git checkout master
git checkout -b branch-2
echo "updated on: branch-2" > my-file.mrg
git add my-file.mrg
git commit -m "branch-2: updated my-file.mrg"
# Merge the two branches, causing a conflict
git merge -m "Merged in branch-1" branch-1
cd merge-driver-test
current_time=$(date "+%Y%m%d-%H%M%S");
branchName="branch-$current_time";
xmlFileName="sample-metadata.xml";
simpleTextFileName="sample-text.txt";
main="master";
environment="environment";
featureA="$current_time-feature-a";
featureB="$current_time-feature-b";
featureC="$current_time-feature-c";
function writeFiles() {
cat > $xmlFileName << EOM
<?xml version="1.0" encoding="UTF-8"?>
<CustomLabels xmlns="http://soap.sforce.com/2006/04/metadata">
<labels>
<fullName>test</fullName>
<language>en_US</language>
<shortDescription>$1</shortDescription>
<protected>true</protected>
<value>abc</value>
</labels>
</CustomLabels>
EOM
cat > $simpleTextFileName << EOM
A heading
My favourite colour is $1.
A final line.
EOM
}
git init
git merge -abort
git checkout $main
writeFiles "BLACK"
git add --all
git commit -m "Added initial files as BLACK on main"
git branch $environment
git branch $featureA
git branch $featureB
git checkout $featureA
writeFiles "RED"
git add --all
git commit -m "Changed to RED on feature-a"
git checkout $featureB
writeFiles "BLUE"
git add --all
git commit -m "Changed to BLUE on feature-b"
git checkout $environment
git merge $featureA --commit
git checkout $environment
git merge $featureB
writeFiles "RED"
git add --all
git commit -m "Merge feature-b into environment - keep RED"
git checkout $main
git merge $featureA
git checkout $featureB
git merge $main
writeFiles "BLUE"
git add --all
git commit -m "Merge master into feature-b - keep BLUE"
git checkout $main
git merge $featureB
git checkout $environment
cat > $xmlFileName << EOM
<?xml version="1.0" encoding="UTF-8"?>
<CustomLabels xmlns="http://soap.sforce.com/2006/04/metadata">
<labels>
<fullName>test</fullName>
<language>en_US</language>
<protected>true</protected>
<shortDescription>RED</shortDescription>
<value>abc</value>
</labels>
</CustomLabels>
EOM
cat > $simpleTextFileName << EOM
A heading
A final line.
My favourite colour is RED.
EOM
git add --all
git commit -m "Transposed lines on environment"
git checkout $main
git branch $featureC
git checkout $featureC
writeFiles "YELLOW"
git add --all
git commit -m "Changed to YELLOW on feature-c with conflicting change to env"
git checkout $environment
git config merge.custom.name "Custom merge driver"
git config merge.custom.driver "$(realpath $(dirname $0))/custom-merge-driver.sh %O %A %B %L %P"
touch .gitattributes
echo "* merge=custom" >> .gitattributes
echo "echo 'Custom merge driver is running'" >> custom-merge-driver.sh
echo "exit $1" >> custom-merge-driver.sh
chmod +x custom-merge-driver.sh
(set -x; GIT_TRACE=2 GIT_CURL_VERBOSE=2 GIT_TRACE_PERFORMANCE=2 GIT_TRACE_PACK_ACCESS=2 GIT_TRACE_PACKET=2 GIT_TRACE_PACKFILE=2 GIT_TRACE_SETUP=2 GIT_TRACE_SHALLOW=2 git merge $featureC --no-ff --no-commit)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment