Skip to content

Instantly share code, notes, and snippets.

@turing85
Last active February 12, 2023 04:34
Show Gist options
  • Save turing85/cea0319b5f11dc39a9d19998208a424e to your computer and use it in GitHub Desktop.
Save turing85/cea0319b5f11dc39a9d19998208a424e to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
set -e
function print_git_tree() {
git log \
--graph \
--no-color \
--source \
--all \
--max-count=5 \
--max-parents=2 \
--pretty=oneline \
| cat
}
function get_parent_commits() {
git log --pretty=%P --max-count=1 | tr " " "\n"
}
function die () {
echo >&2 "$1"
exit "${2:-1}"
}
function get_branches_with_commit_and_suffix() {
[ -n "${1}" ] || die "1s argument should be a commit id"
local commit="${1}"
[ -n "${2}" ] || die "2nd argument should be a suffix"
local suffix="${2}"
git branch \
--no-color \
--list "*${suffix}" \
--contains "${commit}" \
| xargs \
| tr " " "\n"
}
function delete_branch() {
[ -n "${1}" ] || die "1st argument should be a branch to delete"
local branch="${1}"
echo " Deleting branch | ${branch}"
# git branch --delete --force "${branch}"
# git push origin --delete --force "${branch}"
}
function delete_branches() {
[ -n "${1}" ] || die "argument should be names of branches to delete"
local branches=( "${@}" )
printf " Branch to delete | %s\n" "${branches[@]}"
echo "--------------------------------------------------------------------------------"
local branch
for branch in "${branches[@]}"
do
delete_branch "${branch}"
done
}
function delete_if_necessary() {
[ -n "${1}" ] || die "1st argument should be a commit id"
local commit="${1}"
[ -n "${2}" ] || die "2nd argument should be a branch suffix"
local suffix="${2}"
if [[ "${commit}" == "${parent}" ]]
then
echo "Skipping commit-id | ${commit} (is parent)"
return 0
fi
echo "Analyzing commit-id | ${commit}"
echo "--------------------------------------------------------------------------------"
local branches
mapfile -t branches < <(get_branches_with_commit_and_suffix "${commit}" "${suffix}")
[ ${#branches} -ge 1 ] && delete_branches "${branches[@]}"
}
function process_parent_commits() {
local commits
mapfile -t commits < <(get_parent_commits)
printf " Commit to analyze | %s\n" "${commits[@]}"
local commit
for commit in "${commits[@]}"
do
echo "================================================================================"
delete_if_necessary "${commit}" "${suffix}"
done
echo "================================================================================="
}
function analyze() {
[ -n "${1}" ] || die "1st argument should be the suffix"
local suffix="${1}"
local parent
parent="$(git rev-parse HEAD~1)"
echo "================================================================================"
echo "Git tree:"
echo
print_git_tree
echo
echo "================================================================================"
echo "My commit-id | $(git rev-parse HEAD)"
echo "Parent commit-id | ${parent}"
echo "--------------------------------------------------------------------------------"
process_parent_commits
}
analyze "deploy"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment