From ec5b18d5034f156b7c404e35eb3850c064c72ff8 Mon Sep 17 00:00:00 2001 From: Kenichi Kamiya Date: Thu, 15 Jun 2023 13:52:21 +0900 Subject: [PATCH] Import zsh aliases for git branch handling into nushell --- .config/nushell/config.nu | 21 +++++++++++++++++++++ home/.aliases.sh | 4 ++-- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/.config/nushell/config.nu b/.config/nushell/config.nu index 2e43f808..e509fba3 100644 --- a/.config/nushell/config.nu +++ b/.config/nushell/config.nu @@ -650,5 +650,26 @@ let-env config = { ] } +def git-main-branch [] { + git branch | lines | str replace '^ *(?:\*\s+(.+))?' '$1' | where ($it == "main" or $it == "master") | take 1 +} + +def git-upstream [] { + git remote | lines | str trim | where ($it == "upstream" or $it == "origin") | reverse | take 1 +} + +# https://www.nushell.sh/cookbook/git.html#delete-git-merged-branches +def git-delete-merged-branches [] { + git branch --merged | lines | where ($it != "* master" and $it != "* main") | each {|br| git branch -D ($br | str trim) } | str trim +} + +def git-switch-default [] { + git switch (git-main-branch) +} + +def git-cleanup-branches [] { + git-switch-default; git pull (git-upstream) (git current-branch); git fetch (git-upstream) --tags --prune; git-delete-merged-branches +} + # https://github.com/starship/starship/tree/0cffd59b72adbc4c2c33d6bb14dbca170c775fc4#step-2-setup-your-shell-to-use-starship source ~/.cache/starship/init.nu diff --git a/home/.aliases.sh b/home/.aliases.sh index 928b75e2..ef3a60a2 100644 --- a/home/.aliases.sh +++ b/home/.aliases.sh @@ -1,10 +1,10 @@ #!/usr/bin/env bash alias git-switch-default='git checkout main 2>/dev/null || git checkout master 2>/dev/null' -alias git-remote-upsteram="git remote | grep -E '^upstream$'|| git remote | grep -E '^origin$'" +alias git-upsteram="git remote | grep -E '^upstream$'|| git remote | grep -E '^origin$'" # https://github.com/kyanny/git-delete-merged-branches/pull/6 alias git-delete-merged-branches="git branch --merged | grep -vE '((^\*)|^ *(main|master)$)' | xargs -I % git branch -d %" -alias git-cleanup-branches='git-switch-default && git pull $(git-remote-upsteram) $(git current-branch) && git fetch $(git-remote-upsteram) --tags --prune && git-delete-merged-branches' +alias git-cleanup-branches='git-switch-default && git pull $(git-upsteram) $(git current-branch) && git fetch $(git-upsteram) --tags --prune && git-delete-merged-branches' alias la='exa --long --all --group-directories-first'