From 85f394fd24fbcd2fef2dfdb269789314be3dcaed Mon Sep 17 00:00:00 2001
From: Luca Bilke <bilke@tralios.de>
Date: Thu, 23 May 2024 18:26:13 +0200
Subject: [PATCH] set up bare repo dotfile management (untested)

---
 .config/lf/lfrc             |  11 ++--
 .config/zsh/configs/widgets |   7 ++-
 .local/bin/dotsync          | 102 +++++++++++++++++++++++++++++-------
 3 files changed, 92 insertions(+), 28 deletions(-)

diff --git a/.config/lf/lfrc b/.config/lf/lfrc
index bbf3972c..51efff33 100644
--- a/.config/lf/lfrc
+++ b/.config/lf/lfrc
@@ -46,14 +46,11 @@ cmd trash-restore ${{
 
 cmd lazygit %{{
     dir=$(dirname "$(realpath "${f:-_}")"})
-    if ! git --noglob-pathspecs -C $dir status >/dev/null 2>&1; then
-        echo "Initialize git repo in '${dir}' [Y/n]:" && read yn &&
-        case $yn in
-            "" | [Yy]*) git -C ${dir} init >/dev/null 2>&1; break ;;
-            [Nn]*) ;;
-        esac
+    if git --noglob-pathspecs -C $dir status >/dev/null 2>&1; then
+        lf -remote "send $id \$cd '$dir' && lazygit"
+    else
+        lazygit --git-dir "$XDG_DATA_HOME/dots" --work-tree "$HOME"
     fi
-    lf -remote "send $id \$cd '$dir' && lazygit"
 }}
 
 cmd link &{{
diff --git a/.config/zsh/configs/widgets b/.config/zsh/configs/widgets
index b2abda34..3e8c844d 100644
--- a/.config/zsh/configs/widgets
+++ b/.config/zsh/configs/widgets
@@ -53,8 +53,11 @@ zle -N edit-command-line
 
 # Lazygit
 function lg() {
-    [ ! -d "$(pwd)/.git" ] && [[ $(read -erk "?Not in a git repository. Create a new git repository? (y/n): ") =~ ^[Yy]$ ]] && git init
-    [ -d "$(pwd)/.git" ] && lazygit -p "$(pwd)"
+    if git --noglob-pathspecs -C $dir status >/dev/null 2>&1; then
+        lf -remote "send $id \$cd '$dir' && lazygit"
+        return
+    fi
+    lazygit --git-dir "$XDG_DATA_HOME/dots" --work-tree "$HOME"
     zle reset-prompt
 }
 zle -N lg
diff --git a/.local/bin/dotsync b/.local/bin/dotsync
index a883b7a2..9955369b 100755
--- a/.local/bin/dotsync
+++ b/.local/bin/dotsync
@@ -6,42 +6,106 @@ GREEN="$(tput setaf 2)"
 BLUE="$(tput setaf 4)"
 NC="$(tput sgr0)"
 
-dgit() {
-	git -C "$STOW_DIR/$DOTS_PACKAGE" "$@"
+target=$HOME
+gitdir=$target/.local/share/dots
+
+mkdir -p "$(dirname "$gitdir")"
+
+baregit() {
+    git --git-dir="$gitdir" --work-tree="$target" "$@"
 }
 
-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"
+[ -e "$gitdir" ] || {
+    printf "%b" "${BOLD}${BLUE}Cloning dotfiles...$NC\n"
+
+    git clone --bare git@git.snaile.de:luca/dotfiles "$gitdir"
+    baregit checkout
+    baregit config status.showUntrackedFiles no
+
+    cd "$target" || exit
+    for f in *; do
+        [ "$f" = ".config" ] || [ "$f" = ".local" ] && {
+            echo "$f" >>"$gitdir/info/exclude"
+            rm -rf "$f"
+        }
+    done
+    cd - || exit
+
+    echo ".local/share/dots" >>"$gitdir/info/exclude"
+}
 
 printf "%b" "${BOLD}${BLUE}Stashing existing changes...$NC\n"
-stash_result=$(dgit stash push)
+stash_result=$(baregit stash push)
 needs_pop=1
 if [ "$stash_result" = "No local changes to save" ]; then
-	needs_pop=0
+    needs_pop=0
 fi
 printf "\n"
 
 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
+old_commit="$(baregit rev-parse HEAD)"
+baregit pull origin main
+baregit submodule update --remote --merge
 printf "\n"
 
 if [ $needs_pop -eq 1 ]; then
-	printf "%b" "${BOLD}${BLUE}Popping stashed changes...$NC\n"
-	dgit stash pop
+    printf "%b" "${BOLD}${BLUE}Popping stashed changes...$NC\n"
+    baregit stash pop
 fi
 printf "\n"
 
-unmerged_files=$(dgit diff --name-only --diff-filter=U)
+unmerged_files=$(baregit 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\n"
-	printf "%s" "$unmerged_files\n\n"
-	printf "%s" "${BOLD}${BLUE}Reverting to previous commit...$NC"
-	dgit checkout "$old_commit"
+    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"
+    baregit checkout "$old_commit"
 fi
 
-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"
+
+# BOLD="$(tput bold)"
+# RED="$(tput setaf 1)"
+# GREEN="$(tput setaf 2)"
+# BLUE="$(tput setaf 4)"
+# NC="$(tput sgr0)"
+#
+# dgit() {
+#     git -C "$STOW_DIR/$DOTS_PACKAGE" "$@"
+# }
+#
+# 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=$(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"
+# 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"
+#     dgit stash pop
+# fi
+# printf "\n"
+#
+# 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\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}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"