From 801446a5ebf86ec5b61ec940a632b1d6c34b9182 Mon Sep 17 00:00:00 2001 From: Adam Spiers Date: Wed, 16 Nov 2011 15:57:17 +0000 Subject: [PATCH] Use File::Spec->abs2rel() instead of home-grown relative_path which actually gets some inputs wrong (e.g. "/" relative to "/") --- Makefile.am | 1 - stow.in | 36 ++---------------------------------- t/relative_path.t | 41 ----------------------------------------- 3 files changed, 2 insertions(+), 76 deletions(-) delete mode 100755 t/relative_path.t diff --git a/Makefile.am b/Makefile.am index 83bebcb..5d5217c 100644 --- a/Makefile.am +++ b/Makefile.am @@ -15,7 +15,6 @@ TESTS = \ t/foldable.t \ t/join_paths.t \ t/parent.t \ - t/relative_path.t \ t/stow_contents.t \ t/stow.t \ t/unstow_contents_orig.t \ diff --git a/stow.in b/stow.in index 5c6c6bc..5ddd4c1 100755 --- a/stow.in +++ b/stow.in @@ -24,6 +24,7 @@ use warnings; require 5.6.1; +use File::Spec; use POSIX qw(getcwd); use Getopt::Long; @@ -380,7 +381,7 @@ sub set_stow_path { } # set our one global - $Stow_Path = relative_path(getcwd(),$stow_dir); + $Stow_Path = File::Spec->abs2rel($stow_dir); if ($Option{'verbose'} > 1) { warn "current dir is ".getcwd()."\n"; @@ -1665,39 +1666,6 @@ sub strip_quotes { return $string; } -#===== SUBROUTINE =========================================================== -# Name : relative_path() -# Purpose : find the relative path between two given paths -# Parameters: path1 => a directory path -# : path2 => a directory path -# Returns : path2 relative to path1 -# Throws : n/a -# Comments : only used once by main interactive routine -# : factored out for testing -#============================================================================ -sub relative_path { - - my ($path1, $path2) = @_; - - my (@path1) = split m{/+}, $path1; - my (@path2) = split m{/+}, $path2; - - # drop common prefixes until we find a difference - PREFIX: - while ( @path1 && @path2 ) { - last PREFIX if $path1[0] ne $path2[0]; - shift @path1; - shift @path2; - } - - # prepend one '..' to $path2 for each component of $path1 - while ( shift @path1 ) { - unshift @path2, '..'; - } - - return join_paths(@path2); -} - #===== SUBROUTINE =========================================================== # Name : join_paths() # Purpose : concatenates given paths diff --git a/t/relative_path.t b/t/relative_path.t deleted file mode 100755 index 7f60da2..0000000 --- a/t/relative_path.t +++ /dev/null @@ -1,41 +0,0 @@ -#!/usr/local/bin/perl - -# -# Testing relative_path(); -# - -# load as a library -BEGIN { use lib qw(. ..); require "stow"; } - -use Test::More tests => 5; - -is( - relative_path('a/b/c', 'a/b/d'), - '../d', - => 'different branches' -); - -is( - relative_path('/a/b/c', '/a/b/c/d'), - 'd', - => 'lower same branch' -); - -is( - relative_path('a/b/c', 'a/b'), - '..', - => 'higher, same branch' -); - -is( - relative_path('/a/b/c', '/d/e/f'), - '../../../d/e/f', - => 'common parent is /' -); - -is( - relative_path('///a//b//c////', '/a////b/c/d////'), - 'd', - => 'extra /\'s ' -); -