From 180152ba4ad8afdd895dd875a778f8cac136dd8a Mon Sep 17 00:00:00 2001 From: buffet Date: Sat, 18 May 2024 15:52:38 +0200 Subject: [PATCH] feat: add fzf functions to interact with git worktrees These have to be functions sadly because `cd` doesn't work otherwise. This just means there is more reason to have subcommand aliases on a shell level. :) Heavily based on nerdypeppers stuff[https://peppe.rs/posts/curing_a_case_of_git-UX/]. --- users/buffet/bash.nix | 48 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/users/buffet/bash.nix b/users/buffet/bash.nix index 0336c31..f0191b0 100644 --- a/users/buffet/bash.nix +++ b/users/buffet/bash.nix @@ -42,6 +42,54 @@ in { ''; initExtra = '' + gwa() { + [[ "$1" ]] || { + echo "required branch name argument" >&2 + return 1 + } + + local repo="''${PWD##*/}" + local uuid="$(< /proc/sys/kernel/random/uuid)" + local basepath="$HOME/worktrees" + local path="$basepath/$repo-$uuid" + + mkdir -p "$basepath" + git worktree add "$path" -b "$1" && cd "$path" + } + + gwd() { + local res="$( + set -o pipefail + git worktree list | \ + ${pkgs.fzf}/bin/fzf --preview 'git log --color --decorate --oneline' \ + --preview-window=up \ + --cycle \ + --query "$1" |\ + cut -d' ' -f1 + )" + + [[ $? -eq 0 ]] && echo "$res" && git worktree remove "$res" + } + + gwg() { + local res="$( + set -o pipefail + + local trees="$(git worktree list | ${pkgs.fzf}/bin/fzf --filter "$1" --no-sort)" + if [[ "$(wc -l <<<"$trees")" -eq 1 ]]; then + printf '%s\n' "$trees" + else + printf '%s\n' "$trees" | \ + ${pkgs.fzf}/bin/fzf --preview='git log --color --decorate --oneline' \ + --preview-window=up \ + --cycle \ + --query "$1" + fi | cut -d' ' -f1 + )" + + [[ $? -eq 0 ]] && echo "$res" && cd "$res" + } + __prompt() { local status="$?" local row