Showing posts with label shellscripting. Show all posts
Showing posts with label shellscripting. Show all posts

Sunday, September 15, 2019

Git pre-commit hook to disallow a substring

This took way longer than it should have to figure out, so I wanted to share in case others are trying to do the same thing.

Add this snippet to your Git pre-commit hook to prevent (in this case) the substring FIXME from making it into a committed file. Since the hook includes the substring 'FIXME' you'll need to commit like this:
git add git-hooks/pre-commit
allowfixme=true git commit -m 'Disallow FIXME in committed files.'
Here's the code. Public domain, unless you really need a license, in which case use the MIT license. If you can, I'd appreciate a link, but no worries if not.
# Disallow 'FIXME' from being committed. 'TODO' is for cross-commit notes of
# pending work, but 'FIXME' is for things that need to be updated before
# committing.
# Set allowfixme=true to allow commit anyway, such as if the file contains
# 'FIXME' for a good reason (like this file), or you have some other reason
# to leave it in the repo for now.
if [ "$allowfixme" != "true" ] ; then
  found_fixme=
  for F in $(git diff --cached --name-only) ; do
    C=$(git ls-files -s "$F" | awk '{print $2}')
    if git cat-file blob "$C" | grep -q 'FIXME' ; then
      if [ -z "$found_fixme" ] ; then
        echo "COMMIT REJECTED: Found 'FIXME' in:"
        found_fixme=1
      fi
      echo "  $F"
    fi
  done
  if [ -n "$found_fixme" ] ; then
    echo "Please remove before committing."
    exit 1
  fi
fi

Thursday, October 16, 2008

Shell Scripting

One thing that Linux and its brethren have always done well is shell scripting. Windows users are unfortunately missing out on this wonderful tool.

Oh, sure, Windows has DOS .bat and .cmd files, PowerShell scripts, and VBScript .vbs files. That's three different scripting languages to learn, if you want to work with other people (since in a group, someone else is bound to prefer something different than you). Plus, I have an intense dislike of VBScript, mainly for reasons like this:

Three lines to copy a file? Honestly? Anyway, I digress.

Shell scripting may not always be clear, but it does provide powerful tools for doing tasks that would otherwise take forever. Here are a few examples that may help with your media collection:

Rename with Lowercase Extensions

This will rename all files in the current directory and all subdirectories to have lower-case file extensions. It also demonstrates how you may get around the spaces-in-filenames issue that plagues most script writers:

Convert All Files to AVI

Here is one example that I use regularly at home. It takes all non-AVI files in this and all subdirectories and makes them into 2-pass encoded XviD AVIs using MEncoder. My wife and I own a DVD player that plays DivX/XviD files, so this makes any videos we may acquire playable on it:

I hope these are useful. Check back again for other scripting tips.