Skip to content

Instantly share code, notes, and snippets.

@smoofra
Created July 25, 2015 01:53
Show Gist options
  • Save smoofra/418ca82daf0ea2bfb418 to your computer and use it in GitHub Desktop.
Save smoofra/418ca82daf0ea2bfb418 to your computer and use it in GitHub Desktop.
performs a git-merge and prints a tree-ish without modifying any of your state
#!/bin/sh
set -e
. "$(git --exec-path)"/git-sh-setup
cleanup() {
if [ ! -z "$tmp" ]; then
rm -rf "$tmp"
fi
}
tmp=""
trap cleanup EXIT
f() {
tmp=`mktemp -d`
export GIT_INDEX_FILE="$tmp/i"
if [ "$#" -eq 2 ]; then
base=`git merge-base $1 $2`
git read-tree -m $base $1 $2
out=`git write-tree 2>/dev/null` && echo $out && return 0
else
base=`git rev-parse $1`
fi
mkdir "$tmp/d"
export GIT_WORK_TREE="$tmp/d"
rm -f "$GIT_INDEX_FILE"
old_git_dir="$GIT_DIR"
mkdir "$tmp/git"
export GIT_DIR="$tmp/git"
for d in objects refs info hooks branches config; do
ln -s "$old_git_dir/$d" "$GIT_DIR/$d"
done
echo "$base" >"$GIT_DIR/HEAD"
git read-tree $base
git merge --quiet --no-commit $@ >/dev/null 2>&1 && git write-tree && return 0
echo "merge failed!" >&2
}
f "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment