Skip to content

Instantly share code, notes, and snippets.

@armcknight
Last active May 25, 2021 21:53
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save armcknight/9b3e80863375f745bba5 to your computer and use it in GitHub Desktop.
Save armcknight/9b3e80863375f745bba5 to your computer and use it in GitHub Desktop.
UGH - uncrustify git history
#!/bin/sh
# ugh.sh
#
#
# Created by Andrew McKnight on 11/27/14.
#
# UGH: uncrustify git history.
# Creates a new branch and cherry picks a range of commits, uncrustifying any modified .h/.m files.
# Requires an uncrustify config file path to be defined in UNCRUSTIFY_CONFIG.
# if no arguments provided, print usage
if [[ $# -lt 1 ]];
then
echo "Usage: ugh <start hash> [<end hash>]"
else
# if only the start hash is provided, uncrustify all commits from it up to HEAD
endCommit="HEAD"
# get the end commit hash if provided
if [[ $# -gt 1 ]];
then
endCommit=$2
fi
# get list of all hashes in range (including the start hash)
hashes=$(git log --pretty=format:"%H" $1..$endCommit --reverse)
hashes=("$1" "${hashes[@]}")
# branch off the parent of the first commit
git checkout -b "`git rev-parse --abbrev-ref HEAD`-uncrustified" $1~1
# for each commit hash in the range
for hash in ${hashes[@]}; do
# cherry pick changes in commit onto current working index
git cherry-pick $hash --no-commit
# get list of changed files in current working index
files=$(git diff HEAD --name-only)
# for each listed file
for file in $files; do
# check if extension is .h/.m
extension="${file##*.}"
if [[ "$extension" == 'h' || "$extension" == 'm' ]];
then
# uncrustify file
uncrustify $file --no-backup
fi
done
# apply commit with same commit message
git commit -a -m "`git log --format=%B -n 1 $hash`"
done
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment