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

Preserve dotfiles inside nested directories #306

Open
pol-rivero opened this issue Dec 21, 2024 · 1 comment
Open

Preserve dotfiles inside nested directories #306

pol-rivero opened this issue Dec 21, 2024 · 1 comment

Comments

@pol-rivero
Copy link

I have some dotfiles that look like this: ~/dir/.some-config, but I haven't been able to figure out how to add those files to RCM.
If I add the file as ~/.dotfiles/dir/.some-config, rcup ignores the file since it starts with a dot, but if I add it as ~/.dotfiles/dir/some-config, it gets installed as ~/dir/some-config instead of ~/dir/.some-config.

I know I could use a hook to rename these files, but this seems like a very basic and common use case, so I must be missing something. How would you accomplish this?

@pol-rivero
Copy link
Author

pol-rivero commented Dec 23, 2024

I thought I would share the post-up hook I used to work around this issue, for future reference in case somebody else has this issue.

In my dotfiles, I add an underscore before the dot so it's not ignored by RCM, like this: ~/.dotfiles/dir/_.some-config. First, rcup creates the symlink ~/dir/_.some-config, and then this hook renames the links to remove the underscore.

It also works with nested directories (~/aa/_.bb/_.cc/dd.txt -> ~/aa/.bb/.cc/dd.txt), so you should make sure your home directory (or its subdirectories, recursively) doesn't have unexpected directories that start with _..

~/.dotfiles/hooks/post-up/01.rename-underscore-dot-files.sh

#!/bin/bash

# Rename symbolic from _.* to .* to work around RCM limitations

find $HOME -maxdepth 15 -type l -name '_.*' | while read link; do
  target=$(readlink "$link")
  [[ "$target" == $HOME/.dotfiles* ]] && mv "$link" "${link/_./.}"
done

# Do the same with nested directories

find $HOME -depth -maxdepth 15 -type d -name '_.*' -not -path "$HOME/.dotfiles/*" | while read dir; do
  new_dir="${dir%/*}/${dir##*/_}"

  if [ -d "$new_dir" ]; then
    # Merge into existing directory https://unix.stackexchange.com/a/127713
    rsync -a --remove-source-files "$dir/" "$new_dir/"
    find "$dir" -depth -type d -exec rmdir {} +
  else
    mv "$dir" "$new_dir"
  fi
done

You may also be interested in this hook to remove broken links (for example, when renaming a file in the dotfiles directory).

~/.dotfiles/hooks/post-up/02.remove-dead-links.sh

#!/bin/bash

find $HOME -depth -maxdepth 15 -type l | while read link; do
  if [ ! -e "$link" ]; then
    target=$(readlink "$link")
    [[ "$target" == $HOME/.dotfiles* ]] && rm "$link"
  fi
done

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

No branches or pull requests

1 participant