diff options
author | Benedikt Peetz <benedikt.peetz@b-peetz.de> | 2024-05-25 18:02:15 +0200 |
---|---|---|
committer | Benedikt Peetz <benedikt.peetz@b-peetz.de> | 2024-05-25 18:02:15 +0200 |
commit | c6e29c9ebef761f27829ed991700550b1fd1e811 (patch) | |
tree | 77a1a786068070df8894c924dbb64e04927dda68 /pkgs/by-name | |
parent | refactor(git/scripts): Upgrade `git-cm` to a 'real' package (diff) | |
download | nixos-config-c6e29c9ebef761f27829ed991700550b1fd1e811.tar.gz nixos-config-c6e29c9ebef761f27829ed991700550b1fd1e811.zip |
feat(pkgs): Add a `git-cleanup` script
Diffstat (limited to 'pkgs/by-name')
-rwxr-xr-x | pkgs/by-name/gi/git-cleanup/git-cleanup.sh | 77 | ||||
-rw-r--r-- | pkgs/by-name/gi/git-cleanup/package.nix | 15 |
2 files changed, 92 insertions, 0 deletions
diff --git a/pkgs/by-name/gi/git-cleanup/git-cleanup.sh b/pkgs/by-name/gi/git-cleanup/git-cleanup.sh new file mode 100755 index 00000000..1ab7bbd1 --- /dev/null +++ b/pkgs/by-name/gi/git-cleanup/git-cleanup.sh @@ -0,0 +1,77 @@ +#!/usr/bin/env dash + +# shellcheck source=/dev/null +SHELL_LIBRARY_VERSION="2.1.2" . %SHELL_LIBRARY_PATH + +help() { + cat << EOF +Automatically remove merged branches (remote and local) + +USAGE: + git-cleanup [OPTIONS] + +OPTIONS: + --remote | -r + Act on remote branches + + --help | -h + Display this help and exit. + + --version | -v + Display version and copyright information and exit. +EOF +} + +# This should always be the correct answer. +get_default_branch() { + # source: https://stackoverflow.com/a/50056710 + # We assume, that 'origin' is the remote in use + git remote show origin | sed -n '/HEAD branch/s|.*: ||p' +} + +cleanup() { + default_branch="$(get_default_branch)"; + + merged_branches="$(git branch --merged "$default_branch" --no-contains "$default_branch" --format='%(refname:short)')"; + + [ "$merged_branches" ] && git branch --delete "$merged_branches" +} +cleanup_remote() { + default_branch="$(get_default_branch)"; + + merged_branches="$(git branch --remotes --merged "$default_branch" --no-contains "$default_branch" --format='%(refname:short)' | sed 's|origin/||')"; + + [ "$merged_branches" ] && git push --delete origin "$merged_branches" +} + + + +remote=false; +for arg in "$@"; do + case "$arg" in + "--help" | "-h") + help; + exit 0; + ;; + "--version" | "-v") + version; + exit 0; + ;; + "--remote" | "-r") + remote=true; + ;; + esac +done + + + + +if [ "$remote" = "true" ]; then + cleanup_remote; +elif [ "$remote" = "false" ]; then + cleanup; +else + die "BUG: 'remote' is not true or false but: '$remote'"; +fi + +# vim: ft=sh diff --git a/pkgs/by-name/gi/git-cleanup/package.nix b/pkgs/by-name/gi/git-cleanup/package.nix new file mode 100644 index 00000000..e9aab772 --- /dev/null +++ b/pkgs/by-name/gi/git-cleanup/package.nix @@ -0,0 +1,15 @@ +{ + sysLib, + git, + gnused, +}: +sysLib.writeShellScript { + name = "git-cleanup"; + src = ./git-cleanup.sh; + keepPath = false; + generateCompletions = true; + dependencies = [ + git + gnused + ]; +} |