diff --git a/lib/Stow.pm.in b/lib/Stow.pm.in index 77f67b3..7a78a1b 100755 --- a/lib/Stow.pm.in +++ b/lib/Stow.pm.in @@ -367,9 +367,16 @@ sub within_target_do { #============================================================================ sub stow_contents { my $self = shift; - my ($stow_path, $package, $target, $source) = @_; + my ($stow_path, $package, $package_target, $source) = @_; - my $path = join_paths($stow_path, $package, $target); + my $target = $package_target; + + if ($self->{dotfiles}) { + debug(4, " Adjusting: $package_target => $target"); + $target = adjust_dotfile($package_target); + } + + my $path = join_paths($stow_path, $package, $package_target); return if $self->should_skip_target_which_is_stow_dir($target); @@ -393,15 +400,9 @@ sub stow_contents { for my $node (@listing) { next NODE if $node eq '.'; next NODE if $node eq '..'; - my $node_target = join_paths($target, $node); + my $node_target = join_paths($package_target, $node); next NODE if $self->ignore($stow_path, $package, $node_target); - if ($self->{dotfiles}) { - my $adj_node_target = adjust_dotfile($node_target); - debug(4, " Adjusting: $node_target => $adj_node_target"); - $node_target = $adj_node_target; - } - $self->stow_node( $stow_path, $package, @@ -429,11 +430,18 @@ sub stow_contents { #============================================================================ sub stow_node { my $self = shift; - my ($stow_path, $package, $target, $source) = @_; + my ($stow_path, $package, $package_target, $source) = @_; - my $path = join_paths($stow_path, $package, $target); + my $target = $package_target; + + if ($self->{dotfiles}) { + debug(4, " Adjusting: $package_target => $target"); + $target = adjust_dotfile($package_target); + } - debug(3, "Stowing $stow_path / $package / $target"); + my $path = join_paths($stow_path, $package, $package_target); + + debug(3, "Stowing $stow_path / $package / $package_target"); debug(4, " => $source"); # Don't try to stow absolute symlinks (they can't be unstowed) @@ -497,13 +505,13 @@ sub stow_node { $self->stow_contents( $existing_stow_path, $existing_package, - $target, + $package_target, join_paths('..', $existing_source), ); $self->stow_contents( $self->{stow_path}, $package, - $target, + $package_target, join_paths('..', $source), ); } @@ -529,7 +537,7 @@ sub stow_node { $self->stow_contents( $self->{stow_path}, $package, - $target, + $package_target, join_paths('..', $source), ); } @@ -552,7 +560,7 @@ sub stow_node { $self->stow_contents( $self->{stow_path}, $package, - $target, + $package_target, join_paths('..', $source), ); } @@ -740,9 +748,15 @@ sub unstow_node_orig { #============================================================================ sub unstow_contents { my $self = shift; - my ($stow_path, $package, $target) = @_; + my ($stow_path, $package, $package_target) = @_; - my $path = join_paths($stow_path, $package, $target); + my $target = $package_target; + + if ($self->{dotfiles}) { + $target = adjust_dotfile($package_target); + } + + my $path = join_paths($stow_path, $package, $package_target); return if $self->should_skip_target_which_is_stow_dir($target); @@ -769,15 +783,9 @@ sub unstow_contents { for my $node (@listing) { next NODE if $node eq '.'; next NODE if $node eq '..'; - my $node_target = join_paths($target, $node); + my $node_target = join_paths($package_target, $node); next NODE if $self->ignore($stow_path, $package, $node_target); - if ($self->{dotfiles}) { - my $adj_node_target = adjust_dotfile($node_target); - debug(4, " Adjusting: $node_target => $adj_node_target"); - $node_target = $adj_node_target; - } - $self->unstow_node($stow_path, $package, $node_target); } if (-d $target) { @@ -798,9 +806,15 @@ sub unstow_contents { #============================================================================ sub unstow_node { my $self = shift; - my ($stow_path, $package, $target) = @_; + my ($stow_path, $package, $package_target) = @_; - my $path = join_paths($stow_path, $package, $target); + my $target = $package_target; + + if ($self->{dotfiles}) { + $target = adjust_dotfile($package_target); + } + + my $path = join_paths($stow_path, $package, $package_target); debug(3, "Unstowing $path"); debug(4, " target is $target"); @@ -836,11 +850,6 @@ sub unstow_node { if (-e $existing_path) { # Does link points to the right place? - # Adjust for dotfile if necessary. - if ($self->{dotfiles}) { - $existing_path = adjust_dotfile($existing_path); - } - if ($existing_path eq $path) { $self->do_unlink($target); } @@ -872,11 +881,11 @@ sub unstow_node { elsif (-e $target) { debug(4, " Evaluate existing node: $target"); if (-d $target) { - $self->unstow_contents($stow_path, $package, $target); + $self->unstow_contents($stow_path, $package, $package_target); # This action may have made the parent directory foldable if (my $parent = $self->foldable($target)) { - $self->fold_tree($target, $parent); + $self->fold_tree($package_target, $parent); } } else { diff --git a/t/dotfiles.t b/t/dotfiles.t index a4a45c8..5699a75 100755 --- a/t/dotfiles.t +++ b/t/dotfiles.t @@ -24,7 +24,7 @@ use warnings; use testutil; -use Test::More tests => 6; +use Test::More tests => 9; use English qw(-no_match_vars); use testutil; @@ -129,3 +129,53 @@ ok( -f '../stow/dotfiles/dot-bar' && ! -e '.bar' => 'unstow a simple dotfile' ); + +# +# stow into existing directory +# + +$stow = new_Stow(dir => '../stow', dotfiles => 1); + +make_path('.config'); +make_path('../stow/emacs/dot-config/emacs'); +make_file('../stow/emacs/dot-config/emacs/init.el'); + +$stow->plan_stow('emacs'); +$stow->process_tasks(); +is( + readlink('.config/emacs'), + '../../stow/emacs/dot-config/emacs', + => 'processed dotfile folder into existing folder' +); + +# +# unstow from existing directory +# + +$stow = new_Stow(dir => '../stow', dotfiles => 1); + +$stow->plan_unstow('emacs'); +$stow->process_tasks(); +ok( + $stow->get_conflict_count == 0 && + -d '.config' && ! -e '.config/stow' && + -f '../stow/emacs/dot-config/emacs/init.el' + => 'unstow from a directory' +); + +# +# package containing a dot dir, no folding +# + +$stow = new_Stow(dir => '../stow', dotfiles => 1, 'no-folding' => 1); + +make_path('../stow/emacs/dot-config/emacs'); +make_file('../stow/emacs/dot-config/emacs/init.el'); + +$stow->plan_stow('emacs'); +$stow->process_tasks(); +is( + readlink('.config/emacs/init.el'), + '../../../stow/emacs/dot-config/emacs/init.el', + => 'processed dotfile folder without folding' +);