Commits

Ryan Wilcox committed 1198aa1

hate relative paths

Comments (0)

Files changed (2)

bin/relative_path_helper.sh

+#!/bin/zsh
+
+~/bin/relpath $(dirname $1) $2
+~/bin/relpath $(dirname $2) $1
+#!/opt/local/bin/zsh
+
+# From: <http://stackoverflow.com/a/14914070/224334>
+
+[[ $# -ge 1 ]] && [[ $# -le 2 ]] || return 1
+local target=${${2:-$1}:a} # replace `:a' by `:A` to resolve symlinks
+local current=${${${2:+$1}:-$PWD}:a} # replace `:a' by `:A` to resolve symlinks
+local appendix=${target#/}
+local relative=''
+while appendix=${target#$current/}
+	[[ $current != '/' ]] && [[ $appendix = $target ]]; do
+	if [[ $current = $appendix ]]; then
+		relative=${relative:-.}
+		print ${relative#/}
+		return 0
+	fi
+	current=${current%/*}
+	relative="$relative${relative:+/}.."
+done
+relative+=${relative:+${appendix:+/}}${appendix#/}
+print $relative