Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Forcing a directory to be split open #29

Open
anntzer opened this issue Oct 4, 2018 · 11 comments
Open

Forcing a directory to be split open #29

anntzer opened this issue Oct 4, 2018 · 11 comments

Comments

@anntzer
Copy link

anntzer commented Oct 4, 2018

(Initially posted on the help-stow mailing list.)

Like many others, I use stow to manage my dotfiles across multiple machines. In particular, a small exerpt of my stow tree includes

~/dotfiles/vim/plugin
~/dotfiles/vim/syntax
...

so I can just cd ~/dotfiles && stow -S vim after git-cloning dotfiles to a new machine.

But that'll just do a symlink ~/.vim -> ~/dotfiles/.vim, which is not what I want: this will cause e.g. ~/.vim/undo to end up in ~/dotfiles as well. So I always want to split open ~/.vim. In this specific case I could alternatively get away with gitignoring all other directories in ~/.vim, but this is less optimal for dotfiles which reside e.g. in ~/.config, as I really don't want ~/.config to be a symlink but rather ~/.config/foo to be one.
So I need to first create ~/.vim myself and manually add a directory into it (e.g. ~/.vim/undo) before stowing my dotfiles, to force splitting that directory. It would be nice it it was possible to create e.g. ~/dotfiles/vim/.stow-force-split (name is up to bikeshedding) to say, "you are not allowed to symlink this directory directly, but must split it" (and that file shouldn't get symlinked itself).

Looking forward to your thoughts.

@FichteFoll
Copy link

FichteFoll commented Oct 7, 2018

Sounds like you are looking for --no-folding.

You can add that to a .stowrc file in your repository: https://github.com/FichteFoll/dotfiles/blob/master/.stowrc

@anntzer
Copy link
Author

anntzer commented Oct 7, 2018

But (I think?) this completely disables folding, whereas I only want to disable it for specific directories.

@FichteFoll
Copy link

Yes, it completely disables folding (for the package you want to stow). I don't think there's a better way currently than pre-creating a fake directory tree that will cause stow to not fold certain directories.

@aspiers
Copy link
Owner

aspiers commented Jan 20, 2019

That's unfortunately correct. I recently uploaded the repository I use for this, so you can use that as an example for the workaround, but I agree it would be much nicer if Stow could be configured to never fold certain directories. Sadly I don't have time to implement this right now but I'd gladly review pull requests if someone else does.

@paride
Copy link

paride commented Mar 12, 2019

I'll share my solution. Normally I don't want directories to be folded: I want them to be created as actual directories, but I want some of them to be folded, e.g. dotfiles/vim/pack/*. Note that I don't want to apply folding to an entire stowed directory, but to a subdirectory.

I wrote this wrapper script that by default disabled foldins, allowing it for directory trees which begin in a directory containing a file named fold-here.

#!/bin/sh

set -e

stow=stow

if ! "$stow" -V >/dev/null 2>/dev/null; then
	echo "Not found: '$stow'" >&2
	return 1
fi

if [ "$#" -ne 1 ]; then
	echo "Usage: $0 <directory>" >&2
	return 1
fi

dir=$1

if [ ! -d "$dir" ]; then
	echo "Not found: '$dir'." >&2
	return 1
fi

if [ -z "$(find "$dir" -name fold-here)" ]; then
	# No "fold-here" file found, assume no-folding.
	$stow --no-folding "$dir"
else
	# Create the parent folders of the "fold-here" files.
	# The deepest "fold-here" in a tree wins.
	cd "$dir"
	find . -mindepth 2 -name fold-here -exec dirname {} + | xargs -I{} mkdir -vp ../../{}
	cd ..

	$stow --ignore="/fold-here$" "$dir"
fi

I keep this script in my dotfiles directory.

@paride
Copy link

paride commented Mar 12, 2019

The script is buggy. I wrote an improved version of it, but I'm not yet sure it is a good approach.

@paride
Copy link

paride commented Mar 12, 2019

I ended up with a simper (and more flexible) solution. I define the list of directories which should not be folded in a file named <package>/stow-skel.nostow, one per line, and then I stow using this wrapper script:

#!/bin/sh

set -e

for arg; do
    if [ -f "$arg/stow-skel.nostow" ]; then
        xargs -a "$arg/stow-skel.nostow" -I{} mkdir -pv ../{}
    fi
done

stow --ignore="\.nostow" $@

It would be very nice to have this functionality handled directly by stow; hopefully the hooks mentioned in the TODO file will allow to do something similar.

sharrison5 added a commit to sharrison5/dotfiles that referenced this issue Oct 20, 2020
For a good discussion see <aspiers/stow#29>.

The way Stow symlinks non-existent directories means that we often get
generated files being placed back in this repository (e.g. if `.ipython`
points here, then the history etc. appear as untracked files). Rather,
we often want the real directory structure with just symlinked files,
which is what the new `--no-folding` suggestion achieves.
  However, this is still a blunt instrument, and will recurse to
arbitrary depth. For e.g. `vim/` this is unnecessary, as we just want
a `.vim/colors/` symlink (otherwise it propagates through the whole set
of submodules there). For that, we just provide explicit instructions.
@atreyasha
Copy link

atreyasha commented Jul 19, 2021

FWIW, I have a similar but inverted use-case where I want most of my dotfiles to be non-folded; with some exceptions such as ~/.emacs.d which require folding to circumvent upstream bugs that arise when files are copied (i.e. symlinks are not expected there).

To achieve this, I adopted a similar strategy as @paride and added a hidden file called .fold in each dotfile directory which needs to be folded. During (re)stowing, the directories listed in .fold are deleted (with an interactive prompt before) such that they can be effectively folded upon (re)stowing. An example of .fold is shown below:

$ tree -a -L 2

...
├── emacs
│   ├── .emacs.d
│   ├── .fold
│   └── .spacemacs.d
...

The .fold file specifies which directories should be deleted in the target directory to allow for refolding later on.

$ cat emacs/.fold

.emacs.d
.spacemacs.d

The logic of all this is encoded in fold_stow, which is ultimately managed by a Makefile. The relevant portion of fold_stow is shown below, in case it is of help to anyone:

fold_stow() {
  # define local variabless
  local command="$1"
  local source="${2//\~/$HOME}"
  local destination="${3//\~/$HOME}"
  local dotfiles="$4"
  local dotfiles_nofold=()
  local dotfiles_fold=()

  # loop over files and execute logic
  for dotfile in $dotfiles; do
    # check for the existence of .fold
    src_dotfile="$source/$dotfile"
    if [ -f "$src_dotfile/.fold" ]; then
      # loop through all foldable directories
      for fold in $(xargs -a "$src_dotfile/.fold"); do
        src_fold_directory="$src_dotfile/$fold"
        dest_fold_directory="$destination/$fold"
        # prompt to delete directories if they are not symlinks
        if [[ -d "$src_fold_directory" && -d "$dest_fold_directory" && \
                ! -L "$dest_fold_directory" ]]; then
          read -rp "Delete $dest_fold_directory for folding? (y/N): " ans
          # only delete if answer is y or Y
          if [[ "$ans" == [yY] ]]; then
            rm -rf "$dest_fold_directory"
          fi
        fi
      done
      dotfiles_fold+=("$dotfile")
    else
      dotfiles_nofold+=("$dotfile")
    fi
  done

  # execute stow for folding directories
  if ((${#dotfiles_fold[@]})); then
    eval "$command -d $source -t $destination --ignore='^\.fold$' ${dotfiles_fold[*]}"
  fi

  # execute stow for --no-folding directories
  if ((${#dotfiles_nofold[@]})); then
    eval "$command -d $source -t $destination --no-folding ${dotfiles_nofold[*]}"
  fi
}

@aspiers
Copy link
Owner

aspiers commented Jul 19, 2021

In case anyone missed my comment above, my solution is here: https://github.com/aspiers/ANTIFOLD.

@aspiers
Copy link
Owner

aspiers commented Jul 19, 2021

Upon further thought, maybe this is a candidate to be added to the (non-existent) FAQ.

@aspiers aspiers added the faq label Jul 19, 2021
@eggbean
Copy link

eggbean commented Oct 25, 2022

I have started doing this, which seems to work so far, so it seems a lot simpler to do?

# Make some files to prevent folding
[ ! -d ~/.config ] && mkdir ~/.config
touch ~/.config/.stow-no-folding
[ ! -d ~/.local/share ] && mkdir -p ~/.local/share
touch ~/.local/share/.stow-no-folding
[ ! -d ~/.ssh ] && mkdir ~/.ssh
touch ~/.ssh/.stow-no-folding

stow --adopt -Rv -d ~/.dotfiles -t ~ stowpackage

--adopt has never worked for me, whatever I try though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants