diff --git a/git-drop-changeid b/git-drop-changeid new file mode 100755 index 0000000..f629c55 --- /dev/null +++ b/git-drop-changeid @@ -0,0 +1,56 @@ +#!/bin/bash +# This script is meant to be run as a GIT_SEQUENCE_EDITOR +# when running `git rebase -i`. The idea being that it will +# cross-reference commit logs and history with the target branch and +# drop any changes, squashed or otherwise, that exist on that branch + +FILE=${1} + +# Find the branch we're rebasing onto + +ONTO=$(awk '/^# Rebase/{ print $5 }' ${FILE}) + +# Create tempfile to rewrite the todo list +random=$( (whoami ; hostname ; date; cat $1) | git hash-object --stdin) +dest="$1.tmp.${random}" + +trap 'rm -f "${dest}"' EXIT + +# For each commit... +while read -r line; do + if [[ "$line" =~ ^#.* ]]; then + echo $line >> ${dest} + continue + fi + commit=$(echo -n $line | awk '{ print $2 }') + if [ -z "${commit}" ]; then + continue + fi + # ...see if there's a Change-Id in it.... + changeid=$(git log --format=%B -n 1 ${commit} | awk '/Change-Id/{ print $2 }') + if [ -n "${changeid}" ]; then + # ...and match it against the history of the target branch + gitlog=$(git log --grep "${changeid}" ${ONTO}) + if [ -n "${gitlog}" ] ; then + echo $line "# Has Change-Id ${changeid}" | sed 's/pick/drop/g' >> ${dest} + continue + fi + fi + echo $line >> ${dest} +done < ${FILE} + + +# Apply the changes from the tempfile + +if ! mv "${dest}" "$1" ; then + echo "cannot mv ${dest} to $1" + exit 1 +fi + +# Proceed with the editor as per usual + +REAL_EDITOR="$(git var GIT_EDITOR)" + +if [ -n "$REAL_EDITOR" ]; then + $REAL_EDITOR "$@" +fi