Back to Blog
Subversion pre commit hook6/4/2023 ![]() ![]() Command line substitution actually strips newlines, which is why we have to check against an empty string instead of n. The following will check if the last character of a file is a newline character and add one if necessary. Next we examine known text files for the whitespace errors discussed above. “ pngcrush.sh” is a short shell script that wraps up some standard pngcrush configuration options. png extension we run pngcrush if the executable is present and echo a warning if it is not. The end result is a list of files in the index that are being added or have been modified:įor each file ending with a. We grep out lines that start with D since these are deleted files, and cut to just the file path. We add the -name-status flag to get the output in a format that’s easier to work with. Git diff-index -cached HEAD will provide you with a list of files currently in the index (staged for commit) that differ from HEAD. # Remove trailing whitespace if it exists # Add a linebreak to the file if it doesn't have one If (conf|css|erb|html|js|json|log|properties|rb|ru|txt|xml|yml)$ ]] #Files (not deleted) in the indexįiles=$(git diff-index -name-status -cached HEAD | grep -v ^D | cut -c3-)Įcho "* Warning: Can't find pngcrush.sh, is it installed and on your path?"Įcho "* Remember, all PNGs must be crushed before committing!" We’ll examine a few key lines from the script below. Our hook script looks for any new or modified pngs, runs pngcrush against them, and re-adds them to the index before commit. As a guideline we require all PNG images to be pngcrushed before they are committed. You’ll also see we do more than correct whitespace. It removes trailing whitespace and appends newline characters when necessary, correcting these errors silently and not troubling us with pesky warnings. Tired of failed commits, we adapted our SVN pre-commit hook to fix errors instead of of just reporting them and installed it as a local git pre-commit hook.īelow you’ll find the script in its entirety. Many of us here at Wealthfront use git locally and rely on its SVN integration to interact with Subversion. They can actually take corrective action on files in your index, modifying them before the final commit. In contrast git’s pre-commit hooks run locally. As a result they are powerless to correct any errors they find. SVN hooks run on the server and are forbidden from altering commit transactions. Unfortunately, when a SVN pre-commit hook fails, the commit fails. When we commit to our front-end repository, for example, the hook verifies that lines don’t have trailing whitespace and that all text files end with a newline character. Like many teams we use a Subversion pre-commit hook to perform simple validation on code we’re about to commit. ![]()
0 Comments
Read More
Leave a Reply. |