Permalink
Browse files

added p4merge script

  • Loading branch information...
1 parent e9f67a2 commit 3063b9135f6d27873b5351ffe2292664bf42b71e @olifante committed Apr 13, 2012
Showing with 84 additions and 0 deletions.
  1. +84 −0 bin/p4merge
View
@@ -0,0 +1,84 @@
+#!/bin/sh
+realpath() {
+ [[ $1 = /* ]] && echo "$1" || echo "$PWD/${1#./}"
+}
+
+## begin code modified from http://stackoverflow.com/a/1403489
+## By Doctor J: http://stackoverflow.com/users/171236/doctor-j
+last_extension() {
+ local fullpath
+ local filename
+ local dir
+ local base
+ local ext
+ for fullpath in "$@"; do
+ fullpath="$1"
+ filename="${fullpath##*/}" # Strip longest match of */ from start
+ dir="${fullpath:0:${#fullpath} - ${#filename}}" # Substring from 0 thru pos of filename
+ base="${filename%.[^.]*}" # Strip shortest match of . plus at least one non-dot char from end
+ ext="${filename:${#base} + 1}" # Substring from len of base thru end
+ if [[ -z "$base" && -n "$ext" ]]; then # If we have an extension and no base, it's really the base
+ base=".$ext"
+ ext=""
+ fi
+ echo "$ext"
+ done
+}
+## end code modified from http://stackoverflow.com/a/1403489
+
+lazy_basename() {
+ local FILE
+ for FILE in "$@"; do
+ echo "${FILE%%.*}"
+ done
+}
+
+greedy_basename() {
+ local FILE
+ for FILE in "$@"; do
+ echo "${FILE%.*}"
+ done
+}
+
+greedy_extension() {
+ local FILE
+ for FILE in "$@"; do
+ echo "${FILE#*.}"
+ done
+}
+
+lazy_extension() {
+ local FILE
+ for FILE in "$@"; do
+ echo "${FILE##*.}"
+ done
+}
+
+if [ $# -eq 2 ]; then
+ BASE=$(realpath "$1")
+ LOCAL=$(realpath "$1")
+ REMOTE=$(realpath "$2")
+ EXTENSION=$(greedy_extension "$1")
+ MERGED="$PWD/$(lazy_basename $1)_$(lazy_basename $2).$EXTENSION"
+ echo "$MERGED"
+ touch "$MERGED"
+ /Applications/p4merge.app/Contents/MacOS/p4merge "$BASE" "$LOCAL" "$REMOTE" "$MERGED"
+elif [ $# -eq 3 ]; then
+ BASE=$(realpath "$1")
+ LOCAL=$(realpath "$1")
+ REMOTE=$(realpath "$2")
+ MERGED=$(realpath "$3")
+ echo "$MERGED"
+ touch "$MERGED"
+ /Applications/p4merge.app/Contents/MacOS/p4merge "$BASE" "$LOCAL" "$REMOTE" "$MERGED"
+elif [ $# -eq 4 ]; then
+ BASE=$(realpath "$1")
+ LOCAL=$(realpath "$2")
+ REMOTE=$(realpath "$3")
+ MERGED=$(realpath "$4")
+ echo "$MERGED"
+ touch "$MERGED"
+ /Applications/p4merge.app/Contents/MacOS/p4merge "$BASE" "$LOCAL" "$REMOTE" "$MERGED"
+else
+ /Applications/p4merge.app/Contents/MacOS/p4merge $*
+fi

0 comments on commit 3063b91

Please sign in to comment.