Skip to content

Instantly share code, notes, and snippets.

@mloureiro
Forked from henriquemoody/git-fixup
Created March 28, 2018 11:30
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 mloureiro/6919c63aef636f0c19b9ec44932f2fcc to your computer and use it in GitHub Desktop.
Save mloureiro/6919c63aef636f0c19b9ec44932f2fcc to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
# Usage: {script} PATH
# Construct a commit message for use with rebase --autosquash.
# The commit message will be the subject line from the specified commit with a prefix of "fixup! " and will contain the
# changes on the PATH.
#
# --help, -h Displays this help
#
# Report bugs to Henrique Moody <henriquemoody@gmail.com>
#
set -euo pipefail
declare -a HASH_LIST
declare -i HASH_INDEX=1
declare -i HASH_CHOSEN=0
declare -r PATHNAME=${1}
_help()
{
sed -E 's/^#\s?(.*)/\1/g' "${0}" |
sed -nE '/^Usage/,/^Report/p' |
sed "s/{script}/$(basename "${0}")/g"
}
if [[ "${1}" = "--help" ]] || [[ "${1}" = "-h" ]]; then
_help
exit
fi
if [[ -z "${1}" ]]; then
_help 1>&2
exit 1
fi
if [[ ! -e "${PATHNAME}" ]]; then
echo "\`${PATHNAME}\` is not a valid path" 1>&2
exit 2
fi
while read line; do
echo "${HASH_INDEX}: $(cut -d ' ' -f 2,3- <<< "${line}")"
HASH_LIST[HASH_INDEX]=$(cut -d ' ' -f 1 <<< "${line}")
((++HASH_INDEX))
done < <(git log origin/HEAD...HEAD --format="%H %h %s" "${PATHNAME}")
echo
while [[ -z "${HASH_LIST[HASH_CHOSEN]}" ]]; do
echo -n "Which commit would you like to fixup? "
read HASH_CHOSEN
done
echo
set -x
git commit --fixup="${HASH_LIST[HASH_CHOSEN]}" "${PATHNAME}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment