Skip to content

Instantly share code, notes, and snippets.

@Mikulas
Created August 13, 2015 23:37
Show Gist options
  • Save Mikulas/a23f58c5ec30c74f2793 to your computer and use it in GitHub Desktop.
Save Mikulas/a23f58c5ec30c74f2793 to your computer and use it in GitHub Desktop.
git-afix is a smart fixup tool that creates fixup commit with each staged file. Fixed revision is last rev that the file was changed in.
#!/bin/bash
set -euo pipefail
IFS=$'\n\t'
fixups=""
for FILE in $(git --no-pager diff --name-only --cached); do
# TODO limit to last merge commit
REF=$(git log -n 1 --follow --no-merges --pretty="format:%H" -- "$FILE")
fixups+="$REF $FILE"$'\n'
done
# consolidate fixups of same ref to one fixup commit
commits=$(echo "$fixups" | sort | sed 1d | awk 'BEGIN {ORS=""} {if (LAST!=$1) print "\n"$1; print " "$2; LAST=$1}' | sed 1d)
while IFS= read -r LINE; do
REF=$(echo "$LINE" | awk '{print $1}')
FILES=$(echo "$LINE" | awk '{$1=""; print $0}')
echo "$FILES" | xargs git commit --fixup="$REF" --
done <<< "$commits"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment