diff options
author | Jan-Michael Brummer <jan.brummer@tabos.org> | 2023-05-02 10:53:36 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-05-02 10:53:36 +0200 |
commit | d159c5f40ff7e82354e45116d9b66b1f596d9320 (patch) | |
tree | 34506d9ee6c699444a170c6bba8c925c45407d3e /data/canonicalize_filename.sh | |
parent | 8fec01ed4b95afc71bf7710bf5b736a5de03b343 (diff) | |
parent | 5272fb3d114f0d012871380bd429546c73f0226d (diff) | |
download | libproxy-git-d159c5f40ff7e82354e45116d9b66b1f596d9320.tar.gz |
Merge pull request #201 from janbrummer/rewrite
Complete rewrite
Diffstat (limited to 'data/canonicalize_filename.sh')
-rw-r--r-- | data/canonicalize_filename.sh | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/data/canonicalize_filename.sh b/data/canonicalize_filename.sh new file mode 100644 index 0000000..49c4dec --- /dev/null +++ b/data/canonicalize_filename.sh @@ -0,0 +1,48 @@ +#!/bin/sh + +# Provide the canonicalize filename (physical filename with out any symlinks) +# like the GNU version readlink with the -f option regardless of the version of +# readlink (GNU or BSD). + +# This file is part of a set of unofficial pre-commit hooks available +# at github. +# Link: https://github.com/ddddavidmartin/Pre-commit-hooks +# Contact: David Martin, ddddavidmartin@fastmail.com + +########################################################### +# There should be no need to change anything below this line. + +# Canonicalize by recursively following every symlink in every component of the +# specified filename. This should reproduce the results of the GNU version of +# readlink with the -f option. +# +# Reference: http://stackoverflow.com/questions/1055671/how-can-i-get-the-behavior-of-gnus-readlink-f-on-a-mac +canonicalize_filename () { + local target_file="$1" + local physical_directory="" + local result="" + + # Need to restore the working directory after work. + local working_dir="`pwd`" + + cd -- "$(dirname -- "$target_file")" + target_file="$(basename -- "$target_file")" + + # Iterate down a (possible) chain of symlinks + while [ -L "$target_file" ] + do + target_file="$(readlink -- "$target_file")" + cd -- "$(dirname -- "$target_file")" + target_file="$(basename -- "$target_file")" + done + + # Compute the canonicalized name by finding the physical path + # for the directory we're in and appending the target file. + physical_directory="`pwd -P`" + result="$physical_directory/$target_file" + + # restore the working directory after work. + cd -- "$working_dir" + + echo "$result" +} |