cfg/bin/git-ps

44 lines
1.2 KiB
Text
Raw Permalink Normal View History

2016-07-21 13:48:49 +00:00
#!/bin/bash
set -euo pipefail
source "${HOME}/bin/script_framework.sh"
2016-07-21 13:48:49 +00:00
jump_branch="${1:-}"
step "Loading required keys to pull"
git loadkey
2016-07-21 13:48:49 +00:00
step "Fetching data from remote..."
for remote in $(git remote -v | awk '{print $1}' | sort | uniq); do
step "+++ Remote: '${remote}'"
git fetch -p ${remote} --tags
2016-07-21 13:48:49 +00:00
done
[[ -z ${jump_branch} ]] || {
[[ ${jump_branch} != "-d" ]] || {
step "Detecting base branch to jump to..."
known_bases=($(git branch | grep -Eo '(develop|main|master)$'))
[ ${#known_bases[@]} -eq 1 ] || fail "Expected exactly one potential base branch, got ${#known_bases[@]}: ${known_bases[@]}"
jump_branch=${known_bases[0]}
}
step "Switching to branch ${jump_branch}"
git switch "${jump_branch}"
}
2016-07-21 13:48:49 +00:00
step "Rebasing branch / updating submodules..."
git pull --rebase && git submodule update --init --recursive
step "Cleaning local branches..."
for branch in $(git branch --merged | grep -v '^*'); do
# Do not delete master as the main branch
if ! [[ $branch =~ (develop|main|master) ]]; then
git branch -d ${branch}
fi
2016-07-21 13:48:49 +00:00
done
step "Removing local branches where remote branch is gone..."
if (git branch -vv | grep -q ': gone]'); then
git branch -vv | awk '/: gone]/{print $1}' | xargs git branch -D
2016-10-24 06:20:44 +00:00
fi