From d575a32696fa9dde6fe12656bc74ccb7cb7a6f70 Mon Sep 17 00:00:00 2001 From: exponentactivity <18619090+exponentactivity@users.noreply.github.com> Date: Thu, 9 May 2024 15:26:12 +0200 Subject: [PATCH 1/2] fix: chezmoi-update.service fails when dotfiles have changed Fixes #219 --- modules/chezmoi/README.md | 9 +++++++++ modules/chezmoi/chezmoi.sh | 20 +++++++++++++++++++- modules/chezmoi/module.yml | 2 ++ 3 files changed, 30 insertions(+), 1 deletion(-) diff --git a/modules/chezmoi/README.md b/modules/chezmoi/README.md index 57d142e..dccb62f 100644 --- a/modules/chezmoi/README.md +++ b/modules/chezmoi/README.md @@ -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`) diff --git a/modules/chezmoi/chezmoi.sh b/modules/chezmoi/chezmoi.sh index 33c8d20..5fce19b 100644 --- a/modules/chezmoi/chezmoi.sh +++ b/modules/chezmoi/chezmoi.sh @@ -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 diff --git a/modules/chezmoi/module.yml b/modules/chezmoi/module.yml index 437c64a..e96e82e 100644 --- a/modules/chezmoi/module.yml +++ b/modules/chezmoi/module.yml @@ -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 From d1ff4ad5f5c56c6f94cd2e4fbb394bdda64a18da Mon Sep 17 00:00:00 2001 From: exponentactivity <18619090+exponentactivity@users.noreply.github.com> Date: Thu, 9 May 2024 17:14:18 +0200 Subject: [PATCH 2/2] fix: Debug output is enabled despite DEBUG=false Fixes #221 --- modules/chezmoi/chezmoi.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/chezmoi/chezmoi.sh b/modules/chezmoi/chezmoi.sh index 5fce19b..39cc57c 100644 --- a/modules/chezmoi/chezmoi.sh +++ b/modules/chezmoi/chezmoi.sh @@ -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