Skip to content

Instantly share code, notes, and snippets.

@jmatsu
Created December 10, 2021 03:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jmatsu/b151ef2340f1d8194358fe238dbc7337 to your computer and use it in GitHub Desktop.
Save jmatsu/b151ef2340f1d8194358fe238dbc7337 to your computer and use it in GitHub Desktop.
Force me to set advanced messages to git stash
proxy-git() {
if ! type command > /dev/null 2>&1; then
git "$@"
elif let "$# > 0"; then
# git git git git... :)
while [[ "$1" == "git" ]]; do
shift 1
done
if [[ "$1" = "stash" ]]; then
shift 1
IFS=" " set -- "stash-proxy.bash" "$@"
fi
command git "$@"
else
command git
fi
}
alias git=proxy-git
#!/usr/bin/env bash
set -Eeuo pipefail
exec::git::stash() {
exec command git stash "$@"
}
setup_colors() {
if [[ -t 2 ]] && [[ -z "${NO_COLOR-}" ]] && [[ "${TERM-}" != "dumb" ]]; then
NOFORMAT='\033[0m' RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m'
fi
}
msg() {
echo >&2 -e "${1-}"
}
info() {
msg "${GREEN}$1${NOFORMAT}"
}
warn() {
msg "${YELLOW}$1${NOFORMAT}"
}
err() {
msg "${RED}$1${NOFORMAT}"
}
die() {
err "${1-}"
exit "${2-1}"
}
NOFORMAT='' RED='' GREEN='' YELLOW=''
first_argument="${1-}"
if [[ "$first_argument" = "save" ]]; then
die "$0 $first_argument is deprecated so the execution is prohibided."
fi
if [[ "$first_argument" =~ -.* ]]; then
subcommand="push"
elif (("$# > 0")); then
subcommand="$first_argument"
shift 1
else
subcommand="push"
fi
if [[ ! "$subcommand" = "push" ]]; then
exec::git::stash "$subcommand" "$@"
fi
_args=()
message=''
while :; do
if (("$# == 0")); then
break
fi
case "${1-}" in
-m | --message)
message="${2-}"
shift
;;
*)
_args+=("${1-}")
;;
esac
shift
done
if [[ -z "$message" ]]; then
die "-m <a reason why you stach changes> is required to avoid confusion."
fi
branch_name="$(git rev-parse --abbrev-ref HEAD)"
head_log="$(git log --pretty="format:%h %s" -1)"
# git stash's message can't handle branch names that contain '/' properly
IFS=" " set -- "-m" "$branch_name ($(git rev-parse --short HEAD)): because of $message. ref: $head_log" "${_args[@]}"
exec::git::stash push "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment