diff --git a/nixos/fish.nix b/nixos/fish.nix index 7e34f22..10294ca 100644 --- a/nixos/fish.nix +++ b/nixos/fish.nix @@ -5,6 +5,7 @@ enable = true; shellAliases = { cdtmp = "cd $(mktemp -d)"; + gclean = "git cleanup-branches"; theme = "bash ~/dotfiles/scripts/theme.sh"; music = "mpv --no-video --log-file=~/music_history.log \"$(find $HOME/Music/ -type f \\( -name '*.mp3' -o -name '*.wav' -o -name '*.flac' -o -name '*.m4a' -o -name '*.ogg' \\) | fzf)\""; weather = "curl wttr.in/?T"; diff --git a/nixos/home/danny/home.nix b/nixos/home/danny/home.nix index d14b454..b1d8345 100644 --- a/nixos/home/danny/home.nix +++ b/nixos/home/danny/home.nix @@ -74,6 +74,9 @@ core = { editor = "nvim"; }; + alias = { + "cleanup-branches" = "!bash ~/dotfiles/scripts/git-cleanup-branches.sh"; + }; }; }; diff --git a/scripts/git-cleanup-branches.sh b/scripts/git-cleanup-branches.sh new file mode 100755 index 0000000..ef25f8c --- /dev/null +++ b/scripts/git-cleanup-branches.sh @@ -0,0 +1,21 @@ +#!/usr/bin/env bash +set -e + +git rev-parse --git-dir >/dev/null 2>&1 || { echo "Not a git repository." >&2; exit 1; } + +default_branch="main" +git rev-parse --verify refs/heads/main &>/dev/null || default_branch="master" + +git branch --merged "$default_branch" \ + | grep -Fv "$default_branch" \ + | grep -vF '*' \ + | grep -vF '+' \ + | xargs git branch -d \ + && git fetch \ + && git remote prune origin \ + && git branch -v \ + | grep -F '[gone]' \ + | grep -vF '*' \ + | grep -vF '+' \ + | awk '{print $1}' \ + | xargs git branch -D