xaizek / xscripts (License: Apache-2.0) (since 2020-06-28)
Small collection of some scripts I find useful.
<root> / gt-clean-merged-branches (326f915c999a9fb5124a243da06adbe485d70246) (2,263B) (mode 100755) [raw]
#!/bin/bash
# Copyright 2020 xaizek <xaizek@posteo.net>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# This script queries the user to remove branches merged into the current one
# reporting number of commits made in that branch and providing a way to take
# a look at them.  "master" branch is always excluded from the list of branches.

# TODO: display age of the latest commit on branches

current_branch="$(git symbolic-ref --short HEAD)"

bold=$(tput bold)
offbold=$(tput sgr0)

echo "Looking for branches merged into $bold'$current_branch'$offbold..."

merged_branches="$(git branch --merged | grep -Ev '\*|\bmaster\b')"
for branch in $merged_branches; do
    branch_point=$(diff --old-line-format= --new-line-format= <(git rev-list --first-parent HEAD) <(git rev-list --first-parent "$branch") | head -1)
    commit_count="$(git log --pretty=% $branch_point..$branch | wc -l)"

    try_again=1
    while [ -n "$try_again" ]; do
        echo -n "* Do you want to delete $bold'$branch'$offbold branch" \
                "($commit_count commits)? [y/N/v/q/h] "
        read -n1 answer
        echo

        try_again=
        if [ "$answer" = 'y' ]; then
            git branch -d "$branch"
        elif [ "$answer" = 'q' ]; then
            exit 0
        elif [ "$answer" = 'h' ]; then
            echo "    y - delete the branch"
            echo "    v - view commits on the branch"
            echo "    q - exit"
            echo "    h - show this help"
            echo "    anything else - skip current branch"
            try_again=1
        elif [ "$answer" = 'v' ]; then
            echo "Displaying list of commits on $bold'$branch'$offbold branch..."
            git log $branch_point..$branch
            try_again=1
        fi
    done
done
Hints

Before first commit, do not forget to setup your git environment:
git config --global user.name "your_name_here"
git config --global user.email "your@email_here"

Clone this repository using HTTP(S):
git clone https://code.reversed.top/user/xaizek/xscripts

Clone this repository using ssh (do not forget to upload a key first):
git clone ssh://rocketgit@code.reversed.top/user/xaizek/xscripts

You are allowed to anonymously push to this repository.
This means that your pushed commits will automatically be transformed into a pull request:
... clone the repository ...
... make some changes and some commits ...
git push origin master