An open API service indexing awesome lists of open source software.

https://github.com/64j0/simple-bash-cli

A simple bash CLI script
https://github.com/64j0/simple-bash-cli

Last synced: 28 days ago
JSON representation

A simple bash CLI script

Awesome Lists containing this project

README

        

#+title: Simple Bash CLI
#+date: [2023-04-17 seg 15:12]
#+author: 64J0

* Introduction

This repository holds a simple [[https://en.wikipedia.org/wiki/Command-line_interface][CLI]] script.

The goal, is to provide a CLI wrapper for a simple shell script that deletes Git
branches after they're merged to the main branch of some repository.

The main branch name must be fed into the script using parameters.

In the end I present my references for it.

** Deleting branches merged into master

#+BEGIN_SRC bash :tangle no
MAIN_BRANCH="master"
git checkout "${MAIN_BRANCH}"

# fetch the latest from git
git fetch

# see the list of local git branches
git branch

# delete all local branches that have been merged to main branch
git branch --merged "${MAIN_BRANCH}" |\
grep -v "^\* ${MAIN_BRANCH}" |\
xargs -n 1 -r git branch -d
#+END_SRC

** Deleting local branches that no longer exist on the remote

#+BEGIN_SRC bash :tangle no
MAIN_BRANCH="master"
git checkout "${MAIN_BRANCH}"

# fetch the latest from git
git fetch

# see the list of local git branches
git branch

# delete all local branches that have been merged to main branch
git branch -vv |\
grep ': gone]' |\
grep -v '\*' |\
awk '{ print $1; }' |\
xargs -r git branch -d
#+END_SRC

* My script

#+BEGIN_SRC bash :tangle prune-git.sh
#!/usr/bin/env bash

# Script created to study more about CLI tools using shell script (bash).
# Reference:
# https://www.redhat.com/sysadmin/arguments-options-bash-scripts

# https://explainshell.com/explain?cmd=set+-eu
set -eu

help () {
# Display Help
echo "Clean locally your merged branches, leaving only the main branch."
echo
echo "Syntax: script [-h|-b]"
echo "options:"
echo "h Print this help."
echo "b Define the name of the main branch."
echo "v Toggle verbosity."
}

verbose () {
set -x
}

while getopts ":hvb:" option; do
case $option in
h) # display help
help
exit 0;;
v) # toggle verbosity
verbose;;
b) # run the main script
MAIN_BRANCH="${OPTARG}";;
\?) # invalid option
echo "Error: Invalid option."
echo "Use the -h to print the help manual."
exit 1;;
esac
done

git checkout "${MAIN_BRANCH}"
# fetch the latest from git
git fetch
# see the list of local git branches
# git branch
# delete all local branches that have been merged to main branch
git branch --merged "${MAIN_BRANCH}" |\
grep -v "^\* ${MAIN_BRANCH}" |\
xargs -n 1 -r git branch -d
#+END_SRC

** How to use

1. Clone the repository locally.
2. ~chmod +x prune-git.sh~
3. Check the help menu: ~./prune-git.sh -h~
4. Use the script: ~./prune-git.sh -v -b master~

* Org Tangle

+ ~C-c C-v t~

* Reference

+ [[https://digitaldrummerj.me/git-remove-local-merged-branches/][Git - Remove Local Branches That Are Merged or No Longer Exist]]
+ [[https://www.redhat.com/sysadmin/arguments-options-bash-scripts][Adding arguments and options to your Bash scripts]]