Permalink
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
executable file 60 lines (43 sloc) 1.12 KB
#!/usr/bin/env bash
###
### git-merge: Add convenience options to git-merge(1).
###
ERR_NOTREPO="Not a Git repository."
ERR_NO_PREV_BRANCH="Unable to merge, no previous branch could be found."
ARGV=()
while [[ $# -gt 0 ]]; do
case "$1" in
-h|--help)
exec $GIT merge --help
exit 0
;;
-T|--trim)
shift
if [[ ! "$($GIT rev-parse --is-inside-work-tree)" ]] 2>/dev/null; then
printf '%s: %s\n' "$0" "$ERR_NOTREPO"
exit 1
fi
LASTBRANCH="$($GIT check-ref-format --branch @{-1} 2>/dev/null)"
if [[ $? -gt 0 ]]; then
printf '%s: %s\n' "$0" "$ERR_NO_PREV_BRANCH"
exit 1
fi
;;
*)
ARGV+=("$1")
shift
;;
esac
done
if [[ ! "$($GIT rev-parse --is-inside-work-tree)" ]] 2>/dev/null; then
printf '%s: %s\n' "$0" "$ERR_NOTREPO"
exit 1
fi
$GIT merge "${ARGV[@]}"
# On a successful merge where --trim was specified and a previous branch (ref) exists.
if [[ $? -eq 0 ]] && \
[[ -n "$LASTBRANCH" ]]; then
$(echo $($GIT branch --list) | grep "$LASTBRANCH" >/dev/null 2>&1)
# If $LASTBRANCH is a valid, local branch, attempt soft delete.
[[ $? -eq 0 ]] && exec $GIT branch -d $LASTBRANCH
fi