Skip to content

Instantly share code, notes, and snippets.

@jbielick
Created November 20, 2017 16:49
Show Gist options
  • Save jbielick/3c8d856a3beb5079110457847bd56104 to your computer and use it in GitHub Desktop.
Save jbielick/3c8d856a3beb5079110457847bd56104 to your computer and use it in GitHub Desktop.
If you're using `transcrypt(1)` in your repo and you have merge conflicts, things get a little messy. This short script can help with auto-merging the file with a few user-verified steps.
#!/usr/bin/env bash
set -e
file=$1
# before anyones changes
common=$(git --no-pager show ":1:$file")
# your changes
ours=$(git --no-pager show --textconv ":2:$file")
# the changes someone else made that you're merging in
theirs=$(git --no-pager show ":3:$file")
common_file=$(mktemp)
ours_file=$(mktemp)
theirs_file=$(mktemp)
echo "$commom" > $common_file
echo "$ours" > $ours_file
echo "$theirs" > $theirs_file
read -n1 -p "The first screen is the base file conents. press any key to continue"
echo "$common" | less
echo ''
# the changes you've added
read -n1 -p "The next screen is the diff of our changes to $file. press any key to continue"
git diff \
$(echo "$common" | git hash-object -w --stdin) \
$(echo "$ours" | git hash-object -w --stdin)
echo ''
# the changes someone else added
read -n1 -p "The next screen is the diff of their changes to $file. press any key to continue"
git diff \
$(echo "$common" | git hash-object -w --stdin) \
$(echo "$theirs" | git hash-object -w --stdin)
echo ''
# result (resolved file) should be the combination of all three
merged=$(
git merge-file \
-L "our $file" \
-L "base $file" \
-L "their $file" \
--union \
--stdout \
$ours_file \
$common_file \
$theirs_file
)
rm $common_file || true
rm $ours_file || true
rm $theirs_file || true
key='d'
read -n1 -p "The next screen is diff of the base file with a union of both changes. press any key to continue"
while true; do
case "$key" in
d)
git diff :1:$file $(echo "$merged" | git hash-object -w --stdin)
echo ''
read \
-n1 \
-r \
-p "Does the final diff look correct? Press 'y' to resolve with these contents, 'd' to see the diff again or 'n' to cancel..." \
key
echo ''
;;
y)
echo "writing to $file"
echo "$merged" > $file
echo "$file saved"
exit 0
;;
[nq])
echo 'canceled'
exit 1
;;
*)
echo "key not recognized $key"
key='d'
;;
esac
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment