Files
dotfiles/README.adoc
Felix Nehrke ff01db14f4 Improve git hook to remove invalid links
In case a file is moved to another location this change will most likely
end in a dangling link in the home-dir. This change fixes this by
removing links automatically which are off because the target-file has
been moved to another location.
2025-09-09 00:57:17 +02:00

55 lines
1.7 KiB
Plaintext

= Dotfiles
Felix Nehrke <felix@nehrke.info>
:icons: font
:source-highlighter: rouge
[abstract]
I use https://www.gnu.org/software/stow/[`stow`] to sync my dotfiles between the OS and this repository.
== Requirements
Install `git` and `stow` if not already present on the system.
.Install via apt on Debian/Ubuntu
[source,bash]
sudo apt install git stow
.Install via brew on macos
[source,bash]
brew install git stow
== Usage
Checkout this repository into `~/dotfiles` and apply run `stow` to sync the dotfiles.
[IMPORTANT]
The directory should be loacted directly in the HOME directory.
Otherwise stow needs an additional flat `-t $HOME` everytime, which is a bit annoying!
[source,bash]
git clone git@gitea.nehrke.info:nemoinho/dotfiles.git ~/dotfiles # <1>
cd $_
stow --adopt . # <2>
[ -n "$(git status --short)" ] && git restore . # <3>
<1> Clone this repository
<2> Create symlinks for all files managed by this repository, so that the file in the $HOME points to the file in this repository
<3> In case of differences between existing files and the ones from this repository keep the version of the repository
=== Removing files
If a file is going to be removed make sure to remove the linked file as well.
Otherwise we end up with a bunch of dangling files in `$HOME`!
To automatically delete these dangling links we can use a git-hook as the following.
That will remove the links pre-commit.
.Setup git-hook to automatically remove links to deleted files
[source,bash]
cat <<EOF > .git/hooks/pre-commit
#!/usr/bin/env bash
set -e
git status --short | grep -E '^D|^R' | sed 's/...//;s/ -> .*//' | while read f
do
test -L "\$HOME/\$f" && rm "\$HOME/\$f"
done
EOF
chmod +x .git/hooks/pre-commit