SVN Pre-commit duty: Lint your PHP
September 21st, 2008 by Aaron
We’ve all been there before, committing code - and then realizing that it was broken (hrm - our unit test didn’t catch it? or… “what unit test?” if you’re in another environment). Well, there is a solution.
Batch File Fun!
Since I dev primarily on windows, I wrote a batch file. You can also write a linux script using awk in bash (or insert your fav shell here) that will do basically the same thing. From now on, especially at #superdev, I’m going to run “lint.bat” before I commit:
lint.bat
1 2 3 4 5 6 7 8 9 10 | @echo off echo. echo Starting SVN Stat + PHP Lint echo ============================ svn stat |findstr /I /R "\.php$ \.phtml$" >lint.txt for /F "tokens=2 delims= " %%i in (lint.txt) do php -l %%i |findstr /I /B /V "No syntax errors" del lint.txt echo. echo ============================ echo Finished SVN Stat + PHP Lint |
So what is happening?
Basically, when you run “lint.bat” in the root of your repository, you’ll see a start and end message. If you’re lucky, nothing will be between the two double bars. If you’re not lucky, then you know what you need to fix before you commit. But, here’s what is really happening:
Basic Batch File Stuff
Turn off the command echo (@echo off), and print some context (starting the test).
SVN stat of PHP files
Run an SVN stat and grab files that end with .php or .phtml. If you have other files that need to be ran through the php interpreter, you can add them, space delimited, in this “almost” regular expression format. Note the “$” which signifies that the pattern match should be at the end of the string. Output this to a file called ‘lint.txt’ - hopefully this not part of your code repository!
Loop through and look for syntax errors
Loop through each line of that stat file and run the php interpreter against it. If the message does not begin with “No syntax errors”, display the error message to the console.
Clean up and end
Delete the lint.txt file, and then finish up by printing some nice message and double bar.
Things to remember
Both ’svn’ and ‘php’ should be in your system path. Also, the user who is running the lint.bat file should have permission to write files into the current working directory. Finally, if you have more than .php and .phtml files, modify the regular expression to find all of the files that need linting!
Automatic Backup with SVN on Windows
September 12th, 2008 by Aaron
A while ago, I decided that I needed to have a better backup solution for my file server. After doing some research on various systems, I let my inner programmer take over - in addition to my desire to NEVER LOSE ANYTHING - and I defaulted to use SVN.
I was using a Windows machine as my file server - so I wrote some batch files. I also had SVN installed on the machine. The final touch was adding scheduled tasks.
The setup includes a computer that is always on with windows, svn command line, and 5 directories to monitor for backups.
First thing’s first, do an SVN Checkout
The very first thing I did was make an SVN checkout in all of the five parent directories. This way I can continue to use SVN add, svn commit without any other interaction. Don’t worry, we’ll use recursion!
Create the full list of backups
So, first thing’s first: Create the list of directories that need to be monitored. I made them in this txt file named ’svndirectories.txt’:
1 | D:\pictures D:\storage\videos\misc D:\storage\files\art D:\storage\files\NeverAgain D:\storage\files\Therapee |
Note, all of them are separated by a space. This becomes important in our next batch script.
Schedule the SVN Add
I added an SVN Add batch script at Midnight on sundays. Actually, there are two batch files. I made them separately so that I could invoke a scheduled task - but also run the “add” by hand if need be.
The first file, addsvn.bat:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | @echo off REM ------------------------------------------------------------------ REM - forces adds on all svn files REM ------------------------------------------------------------------ :START REM - Get file to process set direct=%1 echo %direct% echo. :SVNADD svn add --force %direct%\* :NEXTFILE shift if "%1"=="" goto END goto START :END |
That will force an add of each file passed in on the command line. Then, the batch file that I made to be ran from the scheduler will read in the folders from the text file, and run this script. Here is ’scheduled_addsvn.bat’:
1 2 3 4 5 6 7 8 | @echo off REM - This is what should be scheduled to add files to svn repos REM - read in svndirectories.txt for /f "tokens=*" %%a in ('type svndirectories.txt 2^>NUL') do set value=%%a REM - call the addsvn program with all the directories addsvn %value% |
Theoretically, I could have called it with the entire line of files after it, but I wanted to call them separately to handle errors better.
After all of these have been added, lets move on…
Schedule SVN Commit
Just in case I made a huge addition of files, I let an hour pass between scheduled add and scheduled commits. Additionally, I ran the commit every day instead of every week. I figured I’d make more changes than I would make additions.
So first, read in all of the directories again and run the commit. Then, the file to schedule. These are pretty much similar, just different commands:
commitsvn.bat:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | @echo off REM ------------------------------------------------------------------ REM - commits all SVN changes REM ------------------------------------------------------------------ :START REM - Get file to process set direct=%1 echo %direct% echo. :SVNCOMMIT svn commit --message="Auto Backup" %direct%\* :NEXTFILE shift if "%1"=="" goto END goto START :END |
scheduled_commitsvn.bat:
1 2 3 4 5 6 7 8 | @echo off REM - This is what should be scheduled to commit files to svn repos REM - read in svndirectories.txt for /f "tokens=*" %%a in ('type svndirectories.txt 2^>NUL') do set value=%%a REM - call the addsvn program with all the directories commitsvn %value% |
This has worked out pretty well for me. If you see anything I could do better, please let me know!
Force Log Messages using Tortoise SVN
July 25th, 2007 by Aaron
Everyone knows that standard SVN has its list of 6 or 8 standard hooks - but what if you’re 1) lazy, 2) busy, 3) don’t have access to the SVN server? Using one of the popular win32 shell integrated svn clients, TortoiseSVN, we can still force commit log messages easily:
Continue reading Force Log Messages using Tortoise SVN
Eclipse Integration with Tortoise SVN
July 14th, 2007 by Aaron
I generally don’t like to be tied to a specific IDE when developing. Additionally, I like to have my choice in using tools to manage my source control (tortoisesvn, svn command line, etc). I just think this is the open-source way - it seems to be just a more free-spirited way of developing and managing projects. With this in mind, I’ve been looking for ways to integrate my SVN into my current IDE (Eclipse PDT) but not limit myself from accessing my SVN repositories from the file system. I’ve found a great plugin to help with this - so lets go over the specifics:
Continue reading Eclipse Integration with Tortoise SVN
Security Issue with Subversion Deployment?
July 4th, 2007 by Aaron
I use Subversion (SVN) for source control and deployment both for JEMDiary and at (”the triangle”). While working on my local copy of one of the websites, I got to thinking about the .svn folder and all of its files. The .svn folder is a local cache/db of the file changes in order to support diffs, reverts, and to give cues about file changes and the need to commit. I started poking around inside of the folder - and discovered the text-base folder. Inside of there, every one of my recently changed files were in there with an extension of .svn-base. Could this be a security issue - was I showing my code to the whole world? Lets figure this out:
Continue reading Security Issue with Subversion Deployment?
