Skip to content

Instantly share code, notes, and snippets.

@imaami
Created August 31, 2019 06:07
Show Gist options
  • Save imaami/0b5c385cd958f41e7f4b524c339606eb to your computer and use it in GitHub Desktop.
Save imaami/0b5c385cd958f41e7f4b524c339606eb to your computer and use it in GitHub Desktop.
Helper scripts for cherry-picking patches with modifications and a "(ported ...)" message
#!/bin/bash
usage()
{
cat << EOF
usage: git port [<options>] <commit>
or: git port --continue
EOF
exit $((1))
}
invargs()
{
echo 'Invalid arguments' >&2
exit 1
}
(( $# > 0 )) || usage 1
port_from=
port_to=
full_commit_hash=
if [[ "$1" == '--continue' ]]; then
(( $# == 1 )) || invargs
do_commit=1
else
while (( $# )); do
case "$1" in
'--help'|'-h') usage ;;
'--from'|'-f') shift; port_from="$1" ;;
'--from='*) port_from="${1:7}" ;;
'--to'|'-t') shift; port_to="$1" ;;
'--to='*) port_to="${1:5}" ;;
*)
[[ -z "$full_commit_hash" ]] || invargs
[[ "$1" =~ ^[[:xdigit:]]{1,40}$ ]] || invargs
full_commit_hash=$(git log -n 1 --pretty=tformat:%H "$1") || exit $?
[[ "$full_commit_hash" =~ ^[[:xdigit:]]{40}$ ]] || {
echo "Not a commit hash: $full_commit_hash" >&2
exit 1
}
;;
esac
shift
done
[[ "$full_commit_hash" ]] || usage 1
do_commit=0
fi
current_head=$(git rev-parse --verify HEAD) || exit $?
[[ "$current_head" ]] || {
echo "Can't find HEAD" >&2
exit 1
}
tmp_file="/tmp/.git-port-$(realpath .|sha1sum|cut -c1-8)-$current_head"
if (( do_commit )); then
[[ -f "$tmp_file" ]] || {
echo "Can't find git-port tempfile" >&2
exit 1
}
z="$(head -1 "$tmp_file")"
[[ "$z" =~ ^[[:xdigit:]]{40} ]] || {
echo "Invalid content in git-port tempfile" >&2
exit 1
}
full_commit_hash="${z:0:40}"
GIT_EDITOR='git-port-editor' git commit -s -c "$full_commit_hash"
ret=$?; (( ret == 0 )) || exit $ret
else
user_name=$(git config --get user.name) || exit $?
user_email=$(git config --get user.email) || exit $?
[[ "$user_name" && "$user_email" ]] || {
echo "No user.name and/or user.email" >&2
exit 1
}
echo "$full_commit_hash (ported${port_from:+ from }$port_from${port_to:+ to }$port_to by $user_name <$user_email>)" > "$tmp_file" &&
git cherry-pick -n "$full_commit_hash"
ret=$?; (( ret != 0 )) || exit 0
fi
rm -f "$tmp_file"
exit $ret
#!/bin/bash
[[ "$1" ]] || exit 0
current_head=$(git rev-parse --verify HEAD) || exit $?
[[ "$current_head" ]] || {
echo "Can't find HEAD" >&2
exit 1
}
tmp_file="/tmp/.git-port-$(realpath .|sha1sum|cut -c1-8)-$current_head"
[[ -f "$tmp_file" ]] || {
echo "Can't find git-port tempfile" >&2
exit 1
}
head -1 "$tmp_file" | grep -Eq '^[[:xdigit:]]{40}' || {
echo "Invalid content in git-port tempfile" >&2
exit 1
}
echo >> "$1"
cut -c42- "$tmp_file" >> "$1"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment