Browse Source

functions: introduced relative_path() returning a relative way to reach one path from another

user/chris/test/patchcksum
Alejandro Mery 15 years ago
parent
commit
3a2953f6b6
1 changed files with 48 additions and 0 deletions
  1. +48
    -0
      lib/functions.in

+ 48
- 0
lib/functions.in

@ -142,6 +142,54 @@ pseudo_realpath() {
echo "$newpath"
}
# relative_path <target> <source>
# returns a relative patch from source to target (for symlinking)
#
relative_path() {
local orig_to="$1" orig_from="$2"
local to=$(pseudo_realpath "$orig_to")
local from=$(pseudo_realpath "$orig_from")
local rel= x= y=
[ -n "$to" -a -n "$from" ] || return
# remove common part
while [ -n "$from" ]; do
x="${to%%/*}"
y="${from%%/*}"
[ "$x" = "$y" ] || break
if [ "$x" = "$to" ]; then
to=
else
to="${to#*/}"
fi
if [ "$y" = "$from" ]; then
from=
else
from="${from#*/}"
fi
done
if [ -z "$from" ]; then
echo "$orig_to: invalid target for $orig_from" >&2
return
fi
case "$from" in
*/*)
rel=$(echo "${from%/*}/" | sed -e 's,[^/]*/,../,g' -e 's,/$,,')
;;
*)
rel=.
;;
esac
echo "$rel${to:+/$to}" | sed -e 's,^\./,,'
}
# Hook variables
#
unset hook_functions hook_fcounter

Loading…
Cancel
Save