From 692b6e9e4d3a67db04cbfaf48561701dd0f9d98f Mon Sep 17 00:00:00 2001
From: Luca Bilke <bilke@tralios.de>
Date: Thu, 14 Dec 2023 13:48:10 +0100
Subject: [PATCH] cleanup dotsync script

---
 .config/zsh/.zprofile |  2 +-
 .local/bin/dotsync    | 44 +++++++++++++++++++++----------------------
 2 files changed, 23 insertions(+), 23 deletions(-)

diff --git a/.config/zsh/.zprofile b/.config/zsh/.zprofile
index 7c24d4a1..3c0061ed 100644
--- a/.config/zsh/.zprofile
+++ b/.config/zsh/.zprofile
@@ -21,6 +21,7 @@ export LESS_TERMCAP_us="$(printf '%b' '')"
 export LESS_TERMCAP_ue="$(printf '%b' '')"
 export LESSOPEN="| /usr/bin/highlight -O ansi %s 2>/dev/null"
 export ANSIBLE_NOCOWS=1			# Stop those fucking cows in Ansible
+export DOTS_PACKAGE="dots"
 
 # Color palette for console dialogs in tools such as nmtui and whiptail
 export NEWT_COLORS='
@@ -64,7 +65,6 @@ export XDG_CACHE_HOME="$HOME/.cache"
 export XDG_STATE_HOME="$HOME/.local/state"
 export XINITRC="$XDG_CONFIG_HOME/X11/xinitrc"
 export STOW_DIR="$XDG_DATA_HOME/stow"
-export DOTS_DIR="$STOW_DIR/dots"
 
 export WORKON_HOME="$XDG_DATA_HOME/virtualenv"
 export ZSH_COMPDUMP="$XDG_CACHE_HOME/zcompdump"
diff --git a/.local/bin/dotsync b/.local/bin/dotsync
index 6864f81a..baedebe2 100755
--- a/.local/bin/dotsync
+++ b/.local/bin/dotsync
@@ -6,42 +6,42 @@ BLUE='\033[34m'
 RED='\033[31m'
 NC='\033[0m'
 
-printf "%b" "$BOLD${BLUE}Changing directory to $DOTS_DIR$NC\n"
-if ! cd "$DOTS_DIR"; then
-    printf "%b" "${RED}Could not CD into $DOTS_DIR$NC\n"
-    exit
-fi
-printf "\n"
+dgit(){
+    git -C "$STOW_DIR/$DOTS_PACKAGE" $@
+}
 
-stow -D -t "$HOME" dots || printf "%b" "${RED}Failed to run stow!$NC\n"
+printf "%b" "${BOLD}${BLUE}Removing dotfile symlinks...$NC\n\n"
+stow -D -d "$STOW_DIR" -t "$HOME" "$DOTS_PACKAGE" || printf "%b" "${RED}Failed to run stow!$NC\n"
 
-printf "%b" "$BOLD${BLUE}Stashing existing changes...$NC\n"
-stash_result=$(git stash push -m "sync-dotfiles: Before syncing dotfiles")
+printf "%b" "${BOLD}${BLUE}Stashing existing changes...$NC\n"
+stash_result=$(dgit stash push)
 needs_pop=1
 if [ "$stash_result" = "No local changes to save" ]; then
     needs_pop=0
 fi
 printf "\n"
 
-printf "%b" "$BOLD${BLUE}Pulling updates from dotfiles repo...$NC\n"
-git pull origin main
-git submodule update --remote --merge
+printf "%b" "${BOLD}${BLUE}Pulling updates from dotfiles repo...$NC\n"
+old_commit="$(dgit rev-parse HEAD)"
+dgit pull origin main
+dgit submodule update --remote --merge
 printf "\n"
 
 if [ $needs_pop -eq 1 ]; then
-    printf "%b" "$BOLD${BLUE}Popping stashed changes...$NC\n"
-    git stash pop
+    printf "%b" "${BOLD}${BLUE}Popping stashed changes...$NC\n"
+    dgit stash pop
 fi
 printf "\n"
 
-unmerged_files=$(git diff --name-only --diff-filter=U)
+unmerged_files=$(dgit diff --name-only --diff-filter=U)
 if [ -n "$unmerged_files" ]; then
-    printf "%b" "${RED}The following files have merge conflicts after popping the stash:$NC\n"
-    printf "\n"
-    printf "%s" "$unmerged_files\n"
-else
-    printf "%b" "$BOLD${BLUE}Symlinking dotfiles...$NC\n\n"
-    stow -t "$HOME" dots || printf "%b" "${RED}Failed to run stow!$NC\n"
+    printf "%b" "${RED}The following files have merge conflicts after popping the stash:$NC\n\n"
+    printf "%s" "$unmerged_files\n\n"
+    printf "%s" "${BOLD}${BLUE}Reverting to previous commit...$NC"
+    dgit checkout "$old_commit"
 fi
 
-printf "%b" "$BOLD${BLUE}Dotfiles Synced Successfully!$NC\n"
+printf "%b" "${BOLD}${BLUE}Symlinking dotfiles...$NC\n\n"
+stow -d "$STOW_DIR" -t "$HOME" "$DOTS_PACKAGE" || printf "%b" "${RED}Failed to run stow!$NC\n"
+
+printf "%b" "${BOLD}${GREEN}Dotfiles synced successfully!$NC\n"