fix(chezmoi): chezmoi-update.service fails when dotfiles have changed (#222)

This commit is contained in:
fiftydinar 2024-05-09 17:27:59 +02:00 committed by GitHub
commit c3084ad86f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 31 additions and 2 deletions

View file

@ -6,6 +6,15 @@ Each feature can be enabled or disabled individually.
Installation of the `chezmoi` binary happens at build time and is done by downloading the `amd64` binary from the latest release to `/usr/bin/chezmoi`.
This can be disabled by setting `install` to false. (defaults: true)
Choose how `chezmoi` handles changed files with `changed-file-policy`.
The following values are valid:
`"skip"` Will not take any action if the file has changed from what is in your dotfiles repository.
This executes `chezmoi update --no-tty --keep-going` under the hood.
`"replace"` Will overwrite the file if it has changed from what is in your dotfiles repository.
This executes `chezmoi update --no-tty --force` under the hood.
See `chezmoi`s documentation for [`--no-tty`](https://www.chezmoi.io/reference/command-line-flags/global/#-no-tty), [`--keep-going`](https://www.chezmoi.io/reference/command-line-flags/global/#-k-keep-going) and [`--force`](https://www.chezmoi.io/reference/command-line-flags/global/#-force) for details.
A systemd user service is installed that will initialize a `chezmoi` repository on chezmoi's default path (`~/.local/share/chezmoi`) for any user when it logs in, or at boot if it has lingering enabled.
The service will only run if `~/.local/share/chezmoi` does not exist.
Set `repository` to the URL of your dotfiles repository. (eg. `repository: https://example.org/user/dotfiles`)

View file

@ -10,7 +10,7 @@ set -euo pipefail
# -u = Treat unset variables as errors. Useful for spotting typos.
# -x = Show expanded input for conditional statements.
DEBUG=false
if [[ $DEBUG ]]; then
if [[ $DEBUG == true ]]; then
echo "Running in debug mode. If you didn't enable this yourself, this is a bug."
set -vux
fi
@ -66,6 +66,24 @@ if [[ -z $DISABLE_UPDATE || $DISABLE_UPDATE == "null" ]]; then
DISABLE_UPDATE=false
fi
# Determines how chezmoi handles duplicate files. (default: "skip")
# "skip" will not replace files that have changed.
# "replace" will overwrite all files that have changed.
CHANGED_FILE_POLICY=$(echo "$1" | yq -I=0 ".changed-file-policy") # (string)
if [[ -z $CHANGED_FILE_POLICY || $CHANGED_FILE_POLICY == "null" ]]; then
CHANGED_FILE_POLICY="skip"
fi
if [[ $CHANGED_FILE_POLICY == "skip" ]]; then
CHANGED_FILE_POLICY_FLAGS="--no-tty --keep-going"
elif [[ $CHANGED_FILE_POLICY == "replace" ]]; then
CHANGED_FILE_POLICY_FLAGS="--no-tty --force"
else
echo "ERROR: 'duplicate-file-policy' has an invalid value."
echo "Only \"skip\" or \"replace\" are acceptable values"
exit 1
fi
echo "Checking for conflicting arguments"
if [[ (-z $DOTFILE_REPOSITORY || $DOTFILE_REPOSITORY == "null") && $DISABLE_INIT == false ]]; then
echo "ERROR: Invalid Config: 'repository' is not set, but initialization is not disabled."
@ -117,7 +135,7 @@ if [[ $DISABLE_UPDATE == false ]]; then
Description=Chezmoi Update
[Service]
ExecStart=/usr/bin/chezmoi update
ExecStart=/usr/bin/chezmoi update ${CHANGED_FILE_POLICY_FLAGS}
Type=oneshot
EOF

View file

@ -17,3 +17,5 @@ example: |
disable_init: false # Optional - Default: false - Expects type: boolean
# Disable the timer that updates chezmoi with the interval set above
disable_update: false # Optional - Default: false - Expects type: boolean
# Policy for handling file that has changed on disk compared to your repo. Accepts "skip" or "replace"
changed-file-policy: "skip" # Optional - Default: "skip" - Expects type: string