Sunday, April 21, 2013

Little helpers

Do you sometimes prepare a commit series, look through it again, and find some small changes like doc improvements or name changes that you need to squash into the last commit to that file?  This little snippet creates a fixup commit for each dirty file tracked by Git (it predates the --fixup option somewhat :).


err() {
  echo "Error: $@" >&2

git rev-parse --is-inside-work-tree &>/dev/null || err "Not inside a work tree"
[ -z "$(git diff --staged --name-only)" ] || err "Stage is not empty"

for dirtyfile in $(git diff --name-only) ; do
  commit_message=$(git log --oneline -- "$dirtyfile" | head -1 | sed 's/^\w* //g')
  git commit -m "fixup! ${commit_message}" -- "${dirtyfile}"

These are the sort of tools that are unlikely to be shipped with a VCS, but would fit nicely into a workbench tool-shed.  Of course, there can be much more sophisticated tools as well as whole toolsets describing workflows.