Skip to content

Instantly share code, notes, and snippets.

@Mikulas
Last active April 12, 2017 12:30
Show Gist options
  • Save Mikulas/861c7b7d10a7d756a796c8b894c420b3 to your computer and use it in GitHub Desktop.
Save Mikulas/861c7b7d10a7d756a796c8b894c420b3 to your computer and use it in GitHub Desktop.
*.php merge=gumtree
[merge "gumtree"]
name = GumTree merge driver
driver = .git/gumtree-driver %O %A %B "path-to-gumtree"
recursive = binary
#!/usr/bin/env bash
set -euo pipefail
IFS=$'\n\t'
# https://git-scm.com/docs/gitattributes/2.11.1#_defining_a_custom_merge_driver
# The merge.*.driver variable’s value is used to construct
# a command to run to merge ancestor’s version (%O),
# current version (%A) and the other branches' version (%B).
# These three tokens are replaced with the names of temporary
# files that hold the contents of these versions when the
# command line is built. Additionally, %L will be replaced
# with the conflict marker size (see below).
# The merge driver is expected to leave the result of the
# merge in the file named with %A by overwriting it, and exit
# with zero status if it managed to merge them cleanly,
# or non-zero if there were conflicts.
# The merge driver can learn the pathname in which the merged
# result will be stored via placeholder %P.
if [[ "$#" -ne 4 ]]; then
echo "Error: Unexpected number of arguments.
Usage:
## .git/config
[merge \"gumtree\"]
name = GumTree merge driver
driver = $0 %O %A %B \"path-to-gumtree\"
recursive = binary
## .gitattributes
*.php merge=gumtree
"
exit 1
fi
BASE="$1"
OURS="$2"
THEIRS="$3"
GUMTREE="$4"
# GumTree requires valid extension on merged files
cp "$BASE" "$BASE.php"
cp "$OURS" "$OURS.php"
cp "$THEIRS" "$THEIRS.php"
# Clean those temp files regardless of final merge status
function finish {
rm -f "$BASE.php" "$OURS.php" "$THEIRS.php"
}
trap finish EXIT
# Attemp to merge and save result into "%A" as per Git documentation:
"$GUMTREE" merge "$BASE.php" "$OURS.php" "$THEIRS.php" > "$OURS"

GumTree merge driver for Git

Setup

  1. Download GumTree with Merge Client from https://github.com/Mikulas/gumtree/releases/tag/2.1.0-merge
  2. Extract the archive and note the path to gumtree binary
  3. In your Git repository, update your .git/config according to the example above. Replace "path-to-gumtree" with the real path.
  4. Copy the merge driver into .git/gumtree-driver (also in your git repository)
  5. Finally, apply .gitattributes changes to enable the new merge driver.

Usage

After the initial setup, merging is completely transparent. Git will automatically invoke the GumTree merge driver when attempting a merge of two php files.

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