Skip to content
This repository has been archived by the owner on Jun 15, 2018. It is now read-only.

Commit

Permalink
Get the deepest common path of any number of directories.
Browse files Browse the repository at this point in the history
Currently doesn't normalize input paths, so the following fails (although it will match if the repeating slashes are equal):
test "$(path_common /foo/bar //foo//bar//baz; echo x)" = /foo/barx

Successful tests:
test "$(path_common /a/b/c/d /a/b/e/f; echo x)" = /a/bx
test "$(path_common /long/names/foo /long/names/bar; echo x)" = /long/namesx
test "$(path_common / /a/b/c; echo x)" = /x
test "$(path_common a/b/c/d a/b/e/f ; echo x)" = a/bx
test "$(path_common ./a/b/c/d ./a/b/e/f; echo x)" = ./a/bx
test "$(path_common $'\n/\n/\n' $'\n/\n'; echo x)" = $'\n/\n'x
test "$(path_common --/-- --; echo x)" = '--x'
test "$(path_common '' ''; echo x)" = x
test "$(path_common /foo/bar ''; echo x)" = x
test "$(path_common /foo /fo; echo x)" = x
test "$(path_common $'--$`\! *@ \a\b\e\E\f\r\t\v\\\"\' \n' $'--$`\! *@ \a\b\e\E\f\r\t\v\\\"\' \n'; echo x)" = $'--$`\! *@ \a\b\e\E\f\r\t\v\\\"\' \n'x
  • Loading branch information
l0b0 committed Apr 15, 2011
1 parent 1c483d4 commit ca080f7
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions .bash_aliases
Expand Up @@ -114,6 +114,29 @@ find_grouped()
done
}

path_common()
{
# Get the deepest common path.
local common_path="${1:-}"
shift # $1 is obviously part of $1

while true
do
for path in "$@"
do
if ! [[ "$path" = "$common_path"* ]]
then
new_common_path="${common_path%/*}"
[ "$new_common_path" = "$common_path" ] && return 1
common_path="${new_common_path}"
continue 2
fi
done
break
done
printf %s "$common_path"
}

# Bash
bash_timeout()
{
Expand Down

0 comments on commit ca080f7

Please sign in to comment.