Skip to content

Commit 28b587a

Browse files
committed
feat(_comp_abspath): new utility function, use in _comp_realcommand
1 parent 03bdac4 commit 28b587a

File tree

1 file changed

+20
-10
lines changed

1 file changed

+20
-10
lines changed

bash_completion

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1659,6 +1659,25 @@ _fstypes()
16591659
[[ $fss ]] && COMPREPLY+=($(compgen -W "$fss" -- "$cur"))
16601660
}
16611661

1662+
# Get absolute path to a file, with rudimentary canonicalization.
1663+
# No symlink resolution or existence checks are done;
1664+
# see `_comp_realcommand` for those.
1665+
# @param $1 The file
1666+
# @var[out] ret The path
1667+
_comp_abspath()
1668+
{
1669+
ret=$1
1670+
case $ret in
1671+
/*) ;;
1672+
../*) ret=$PWD/${ret:3} ;;
1673+
*) ret=$PWD/$ret ;;
1674+
esac
1675+
while [[ $ret == */./* ]]; do
1676+
ret=${ret//\/.\//\/}
1677+
done
1678+
ret=${ret//+(\/)/\/}
1679+
}
1680+
16621681
# Get real command.
16631682
# Command is the filename of command in PATH with possible symlinks resolved
16641683
# (if resolve tooling available), empty string if command not found.
@@ -1677,16 +1696,7 @@ _comp_realcommand()
16771696
elif type -p readlink >/dev/null; then
16781697
ret=$(readlink -f "$file")
16791698
else
1680-
ret=$file
1681-
if [[ $ret == */* ]]; then
1682-
if [[ $ret == ./* ]]; then
1683-
ret=$PWD/${file:2}
1684-
elif [[ $ret == ../* ]]; then
1685-
ret=$PWD/${file:3}
1686-
elif [[ $ret != /* ]]; then
1687-
ret=$PWD/$file
1688-
fi
1689-
fi
1699+
_comp_abspath "$file"
16901700
fi
16911701
}
16921702

0 commit comments

Comments
 (0)