From a7af23bfbb532a0271c4905f2d2e8f95f34bc570 Mon Sep 17 00:00:00 2001 From: Joe Groocock Date: Mon, 24 Oct 2022 14:51:42 +0000 Subject: [PATCH] git: Allow git cleanmerged/defbranch to take args These aliases now make zero assumptions about either branch or origin names. They default to origin, and the primary branch is determined from the refs/remotes/$remote/HEAD symbolic-ref, which will be pulled from the remote if it's not known locally. With these it is now possible to act on any remote/branch combination: # Get default branch for 'upstream' remote $ git defbranch upstream # Delete branches merged into the default branch of the 'upstream' # remote $ git cleanmerged upstream # Delete branches merged into the upstream/foo branch $ git cleanmerged upstream foo Signed-off-by: Joe Groocock --- git/config | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/git/config b/git/config index 589d7ea..5016bd7 100644 --- a/git/config +++ b/git/config @@ -37,8 +37,23 @@ count = !echo $(pwd) && git rev-list HEAD --count unpushed = !git log --oneline @{u}..HEAD leaders = !git shortlog -s -n --all --no-merges - defbranch = !git symbolic-ref refs/remotes/origin/HEAD | sed \"s@^refs/remotes/origin/@@\" - cleanmerged = !git branch --merged \"origin/$(git defbranch)\" | grep -ve \"^\\\\*\" -e \"^. $(git defbranch)$\" | xargs -r git branch -d + defbranch = "!defbranch() { \ + r=\"${1:-origin}\"; \ + { \ + git symbolic-ref \"refs/remotes/$r/HEAD\" || { \ + git remote set-head \"$r\" -a >&2 && \ + git symbolic-ref \"refs/remotes/$r/HEAD\"; \ + }; \ + } 2>/dev/null \ + | sed \"s@^refs/remotes/$r/@@\"; \ + }; defbranch" + cleanmerged = "!cleanmerged() { \ + r=\"${1:-origin}\"; \ + b=\"${2:-$(git defbranch \"$r\")}\"; \ + git branch --merged \"$r/$b\" | \ + grep -ve \"^\\\\*\" -e \"^. $b$\" | \ + xargs -r git branch -d; \ + }; cleanmerged" [advice] statusHints = false pushUpdateRejected = false