From 910242c92b661505fbe0e72d1086006b25f8f616 Mon Sep 17 00:00:00 2001 From: Vladimir Jimenez Date: Mon, 24 May 2021 00:13:57 -0700 Subject: [PATCH 1/3] Add delete-squashed-branches command --- Commands.md | 11 +++ bin/git-delete-squashed-branches | 17 ++++ etc/bash_completion.sh | 4 + etc/git-extras-completion.zsh | 6 ++ etc/git-extras.fish | 3 + man/git-delete-squashed-branches.1 | 54 +++++++++++ man/git-delete-squashed-branches.html | 124 ++++++++++++++++++++++++++ man/git-delete-squashed-branches.md | 38 ++++++++ man/git-extras.md | 1 + man/index.txt | 1 + 10 files changed, 259 insertions(+) create mode 100755 bin/git-delete-squashed-branches create mode 100644 man/git-delete-squashed-branches.1 create mode 100644 man/git-delete-squashed-branches.html create mode 100644 man/git-delete-squashed-branches.md diff --git a/Commands.md b/Commands.md index b03cf65cb..b2f417f1f 100644 --- a/Commands.md +++ b/Commands.md @@ -16,6 +16,7 @@ - [`git create-branch`](#git-create-branch) - [`git delete-branch`](#git-delete-branch) - [`git delete-merged-branches`](#git-delete-merged-branches) + - [`git delete-squashed-branches`](#git-delete-squashed-branches) - [`git delete-submodule`](#git-delete-submodule) - [`git delete-tag`](#git-delete-tag) - [`git delta`](#git-delta) @@ -851,6 +852,16 @@ Deleted feature/dashboard (was 923befa). ... ``` +## git delete-squashed-branches + +Deletes branches that have been "squashed-merged" into a specified branch + +```bash +$ git delete-squashed-branches main +Deleted branch dependabot/bundler/kramdown-2.3.1 (was 1d3fb00). +Deleted branch dependabot/bundler/rexml-3.2.5 (was a7e4052). +``` + ## git fresh-branch Create empty local branch `name`: diff --git a/bin/git-delete-squashed-branches b/bin/git-delete-squashed-branches new file mode 100755 index 000000000..e6a4fa85d --- /dev/null +++ b/bin/git-delete-squashed-branches @@ -0,0 +1,17 @@ +#!/usr/bin/env bash + +targetBranch=$1 + +if [[ -z $targetBranch ]]; then + targetBranch=$(git rev-parse --abbrev-ref HEAD) +else + git checkout -q $targetBranch +fi + +git for-each-ref refs/heads/ "--format=%(refname:short)" | while read branch; do + mergeBase=$(git merge-base $targetBranch $branch) + + if [[ $(git cherry $targetBranch $(git commit-tree $(git rev-parse $branch\^{tree}) -p $mergeBase -m _)) == "-"* ]]; then + git branch -D $branch + fi +done diff --git a/etc/bash_completion.sh b/etc/bash_completion.sh index 0c6cb0078..1493fdda4 100644 --- a/etc/bash_completion.sh +++ b/etc/bash_completion.sh @@ -49,6 +49,10 @@ _git_delete_branch(){ __gitcomp "$(__git_heads)" } +_git_delete_squashed_branches(){ + __gitcomp "$(__git_heads)" +} + _git_delete_submodule(){ __gitcomp "$(git submodule status | awk '{print $2}')" } diff --git a/etc/git-extras-completion.zsh b/etc/git-extras-completion.zsh index f21817bb2..ea7cba9d4 100644 --- a/etc/git-extras-completion.zsh +++ b/etc/git-extras-completion.zsh @@ -158,6 +158,11 @@ _git-delete-branch() { ':branch-name:__gitex_branch_names' } +_git-delete-squashed-branches() { + _arguments \ + ':branch-name:__gitex_branch_names' +} + _git-delete-submodule() { _arguments \ @@ -347,6 +352,7 @@ zstyle ':completion:*:*:git:*' user-commands $existing_user_commands \ create-branch:'create branches' \ delete-branch:'delete branches' \ delete-merged-branches:'delete merged branches' \ + delete-squashed-branches:'delete squashed branches' \ delete-submodule:'delete submodules' \ delete-tag:'delete tags' \ delta:'lists changed files' \ diff --git a/etc/git-extras.fish b/etc/git-extras.fish index 58e2f19f3..319bdd65c 100644 --- a/etc/git-extras.fish +++ b/etc/git-extras.fish @@ -17,6 +17,7 @@ set __fish_git_extras_commands \ "create-branch:Create branches" \ "delete-branch:Delete branches" \ "delete-merged-branches:Delete merged branches" \ + "delete-squashed-branches:Delete squashed branches" \ "delete-submodule:Delete submodules" \ "delete-tag:Delete tags" \ "delta:Lists changed files" \ @@ -106,6 +107,8 @@ complete -c git -f -n '__fish_git_using_command count' -l all -d 'detailed commi complete -c git -x -n '__fish_git_using_command create-branch' -s r -l remote -a '(__fish_git_unique_remote_branches)' -d 'setup remote tracking branch' # delete-branch complete -c git -x -n '__fish_git_using_command delete-branch' -a '(__fish_git_branches)' -d 'branch to delete' +# delete-squashed-branches +complete -c git -x -n '__fish_git_using_command delete-squashed-branches' -a '(__fish_git_branches)' -d 'branch to target for squashed merges' # delete-submodule complete -c git -x -n "__fish_git_using_command delete-submodule" -a "(__fish_git submodule status 2>/dev/null | string trim | cut -d ' ' -f 2)" -d 'submodule to delete' # delete-tag diff --git a/man/git-delete-squashed-branches.1 b/man/git-delete-squashed-branches.1 new file mode 100644 index 000000000..7e5296ea5 --- /dev/null +++ b/man/git-delete-squashed-branches.1 @@ -0,0 +1,54 @@ +.\" generated with Ronn/v0.7.3 +.\" http://github.com/rtomayko/ronn/tree/0.7.3 +. +.TH "GIT\-DELETE\-SQUASHED\-BRANCHES" "1" "May 2021" "" "Git Extras" +. +.SH "NAME" +\fBgit\-delete\-squashed\-branches\fR \- Delete branches that were squashed +. +.SH "SYNOPSIS" +\fBgit\-delete\-squashed\-branches\fR [] +. +.SH "DESCRIPTION" +Deletes all git branches that have been "squash\-merged" into \fBtarget\-branch\fR\. +. +.SH "OPTIONS" + +. +.P +The target branch were the "squashed\-merged" branches were committed to\. +. +.SH "EXAMPLES" +Delete all branches that were "squash\-merged" into the current checked out branch\. +. +.IP "" 4 +. +.nf + +$ git delete\-squashed\-branches +. +.fi +. +.IP "" 0 +. +.P +Delete all branches that were "squash\-merged" into the \fBmain\fR branch\. This will checkout the target branch and leave you on said branch after the command has completed\. +. +.IP "" 4 +. +.nf + +$ git delete\-squashed\-branches main +. +.fi +. +.IP "" 0 +. +.SH "AUTHOR" +Written by Teddy Katz <\fIteddy\.katz@gmail\.com\fR> and Vladimir Jimenez <\fIme@allejo\.io\fR> +. +.SH "REPORTING BUGS" +<\fIhttps://github\.com/tj/git\-extras/issues\fR> +. +.SH "SEE ALSO" +<\fIhttps://github\.com/tj/git\-extras\fR> diff --git a/man/git-delete-squashed-branches.html b/man/git-delete-squashed-branches.html new file mode 100644 index 000000000..a3065e6a1 --- /dev/null +++ b/man/git-delete-squashed-branches.html @@ -0,0 +1,124 @@ + + + + + + git-delete-squashed-branches(1) - Delete branches that were squashed + + + + +
+ + + +
    +
  1. git-delete-squashed-branches(1)
  2. +
  3. Git Extras
  4. +
  5. git-delete-squashed-branches(1)
  6. +
+ +

NAME

+

+ git-delete-squashed-branches - Delete branches that were squashed +

+ +

SYNOPSIS

+ +

git-delete-squashed-branches [<target-branch>]

+ +

DESCRIPTION

+ +

Deletes all git branches that have been "squash-merged" into target-branch.

+ +

OPTIONS

+ +

<target-branch>

+ +

The target branch were the "squashed-merged" branches were committed to.

+ +

EXAMPLES

+ +

Delete all branches that were "squash-merged" into the current checked out branch.

+ +
$ git delete-squashed-branches
+
+ +

Delete all branches that were "squash-merged" into the main branch. This will checkout the target branch and leave you on said branch after the command has completed.

+ +
$ git delete-squashed-branches main
+
+ +

AUTHOR

+ +

Written by Teddy Katz <teddy.katz@gmail.com> and Vladimir Jimenez <me@allejo.io>

+ +

REPORTING BUGS

+ +

<https://github.com/tj/git-extras/issues>

+ +

SEE ALSO

+ +

<https://github.com/tj/git-extras>

+ + +
    +
  1. +
  2. May 2021
  3. +
  4. git-delete-squashed-branches(1)
  5. +
+ +
+ + diff --git a/man/git-delete-squashed-branches.md b/man/git-delete-squashed-branches.md new file mode 100644 index 000000000..d98959007 --- /dev/null +++ b/man/git-delete-squashed-branches.md @@ -0,0 +1,38 @@ +git-delete-squashed-branches(1) -- Delete branches that were squashed +===================================================================== + +## SYNOPSIS + +`git-delete-squashed-branches` [<branch-name>] + +## DESCRIPTION + +Deletes all git branches that have been "squash-merged" into `branch-name`. + +## OPTIONS + + <branch-name> + + The target branch were the "squashed-merged" branches were committed to. + +## EXAMPLES + + Delete all branches that were "squash-merged" into the current checked out branch. + + $ git delete-squashed-branches + + Delete all branches that were "squash-merged" into the `main` branch. This will checkout the target branch and leave you on said branch after the command has completed. + + $ git delete-squashed-branches main + +## AUTHOR + +Written by Teddy Katz <> and Vladimir Jimenez <> + +## REPORTING BUGS + +<> + +## SEE ALSO + +<> diff --git a/man/git-extras.md b/man/git-extras.md index 4a8625a3b..d9e921b40 100644 --- a/man/git-extras.md +++ b/man/git-extras.md @@ -44,6 +44,7 @@ git-extras(1) -- Awesome GIT utilities - **git-create-branch(1)** Create branches - **git-delete-branch(1)** Delete branches - **git-delete-merged-branches(1)** Delete merged branches + - **git-delete-squashed-branches(1)** Delete branches that were squashed - **git-delete-submodule(1)** Delete submodules - **git-delete-tag(1)** Delete tags - **git-delta(1)** Lists changed files diff --git a/man/index.txt b/man/index.txt index 0850a6997..fc6f88c90 100644 --- a/man/index.txt +++ b/man/index.txt @@ -16,6 +16,7 @@ git-cp(1) git-cp git-create-branch(1) git-create-branch git-delete-branch(1) git-delete-branch git-delete-merged-branches(1) git-delete-merged-branches +git-delete-squashed-branches(1) git-delete-squashed-branches git-delete-submodule(1) git-delete-submodule git-delete-tag(1) git-delete-tag git-delta(1) git-delta From fb1e5db6d7e32c39bb1576554eb0b092623ee515 Mon Sep 17 00:00:00 2001 From: Vladimir Jimenez Date: Mon, 24 May 2021 13:55:32 -0700 Subject: [PATCH 2/3] delete-squashed-branches: Make branch checkout side effect clear --- bin/git-delete-squashed-branches | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/git-delete-squashed-branches b/bin/git-delete-squashed-branches index e6a4fa85d..6707da0a2 100755 --- a/bin/git-delete-squashed-branches +++ b/bin/git-delete-squashed-branches @@ -5,7 +5,7 @@ targetBranch=$1 if [[ -z $targetBranch ]]; then targetBranch=$(git rev-parse --abbrev-ref HEAD) else - git checkout -q $targetBranch + git checkout $targetBranch fi git for-each-ref refs/heads/ "--format=%(refname:short)" | while read branch; do From bebcbd11cae38ab1ee3427dda590b96b4579970b Mon Sep 17 00:00:00 2001 From: Vladimir Jimenez Date: Tue, 25 May 2021 01:22:51 -0700 Subject: [PATCH 3/3] delete-squashed-branches: Make side-effect clear in docs --- Commands.md | 5 +++-- man/git-delete-squashed-branches.1 | 8 ++++---- man/git-delete-squashed-branches.html | 10 +++++----- man/git-delete-squashed-branches.md | 2 +- 4 files changed, 13 insertions(+), 12 deletions(-) diff --git a/Commands.md b/Commands.md index b2f417f1f..8e4f52a89 100644 --- a/Commands.md +++ b/Commands.md @@ -854,12 +854,13 @@ Deleted feature/dashboard (was 923befa). ## git delete-squashed-branches -Deletes branches that have been "squashed-merged" into a specified branch +Deletes branches that have been "squashed-merged" into a specified branch; this branch will be checked out as a side-effect. If no branch is specified, then it will default to the current checked out branch. ```bash -$ git delete-squashed-branches main +$ (feature-branch) git delete-squashed-branches main Deleted branch dependabot/bundler/kramdown-2.3.1 (was 1d3fb00). Deleted branch dependabot/bundler/rexml-3.2.5 (was a7e4052). +$ (main) git ... ``` ## git fresh-branch diff --git a/man/git-delete-squashed-branches.1 b/man/git-delete-squashed-branches.1 index 7e5296ea5..45831b19a 100644 --- a/man/git-delete-squashed-branches.1 +++ b/man/git-delete-squashed-branches.1 @@ -7,16 +7,16 @@ \fBgit\-delete\-squashed\-branches\fR \- Delete branches that were squashed . .SH "SYNOPSIS" -\fBgit\-delete\-squashed\-branches\fR [] +\fBgit\-delete\-squashed\-branches\fR [] . .SH "DESCRIPTION" -Deletes all git branches that have been "squash\-merged" into \fBtarget\-branch\fR\. +Deletes all git branches that have been "squash\-merged" into \fBbranch\-name\fR\. . .SH "OPTIONS" - + . .P -The target branch were the "squashed\-merged" branches were committed to\. +The target branch were the "squashed\-merged" branches were committed to\. If no value is given, then the current checked out branch will be used\. . .SH "EXAMPLES" Delete all branches that were "squash\-merged" into the current checked out branch\. diff --git a/man/git-delete-squashed-branches.html b/man/git-delete-squashed-branches.html index a3065e6a1..a1ab81b69 100644 --- a/man/git-delete-squashed-branches.html +++ b/man/git-delete-squashed-branches.html @@ -76,17 +76,17 @@

NAME

SYNOPSIS

-

git-delete-squashed-branches [<target-branch>]

+

git-delete-squashed-branches [<branch-name>]

DESCRIPTION

-

Deletes all git branches that have been "squash-merged" into target-branch.

+

Deletes all git branches that have been "squash-merged" into branch-name.

OPTIONS

-

<target-branch>

+

<branch-name>

-

The target branch were the "squashed-merged" branches were committed to.

+

The target branch were the "squashed-merged" branches were committed to. If no value is given, then the current checked out branch will be used.

EXAMPLES

@@ -102,7 +102,7 @@

EXAMPLES

AUTHOR

-

Written by Teddy Katz <teddy.katz@gmail.com> and Vladimir Jimenez <me@allejo.io>

+

Written by Teddy Katz <teddy.katz@gmail.com> and Vladimir Jimenez <me@allejo.io>

REPORTING BUGS

diff --git a/man/git-delete-squashed-branches.md b/man/git-delete-squashed-branches.md index d98959007..e46cd2831 100644 --- a/man/git-delete-squashed-branches.md +++ b/man/git-delete-squashed-branches.md @@ -13,7 +13,7 @@ Deletes all git branches that have been "squash-merged" into `branch-name`. <branch-name> - The target branch were the "squashed-merged" branches were committed to. + The target branch were the "squashed-merged" branches were committed to. If no value is given, then the current checked out branch will be used. ## EXAMPLES