Created
March 10, 2023 16:16
-
-
Save markatom/b3c6bb2e3fae800f6cb073cfbe9ffd79 to your computer and use it in GitHub Desktop.
Git Auto Fixup script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
BASE_COMMIT=$1 | |
if [[ -z $BASE_COMMIT ]]; then | |
BASE_COMMIT=$(git rev-list --simplify-by-decoration --max-count 1 --skip 1 HEAD) | |
fi | |
git reset | |
MODIFIED_FILES=$(git status --porcelain | cut -c 4-) | |
for MODIFIED_FILE in $MODIFIED_FILES; do | |
FILE_COMMITS=$(git --no-pager log --format='%H' $BASE_COMMIT..HEAD -- $MODIFIED_FILE) | |
if [[ $(echo "$FILE_COMMITS" | wc -l) -gt 1 ]]; then | |
echo "Skipping file $MODIFIED_FILE which is modified in more than one commit." | |
elif [[ -z $FILE_COMMITS ]]; then | |
echo "Skipping file $MODIFIED_FILE which is not modified in any commit." | |
else | |
FILES_TO_FIXUP=$(echo $FILES_TO_FIXUP; echo $MODIFIED_FILE) | |
fi | |
done | |
COMMITS=$(git --no-pager log --format='%H' $BASE_COMMIT..HEAD) | |
for COMMIT in $COMMITS; do | |
MODIFIED_FILES_IN_COMMIT=$(git show $COMMIT --name-only --pretty=format:) | |
FILES_TO_FIXUP_IN_COMMIT=$(comm -12 <(echo "$FILES_TO_FIXUP" | sort) <(echo "$MODIFIED_FILES_IN_COMMIT" | sort)) | |
if [[ -n $FILES_TO_FIXUP_IN_COMMIT ]]; then | |
git add $FILES_TO_FIXUP_IN_COMMIT | |
git commit --fixup $COMMIT | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment