Tutorial :Subversion whitespace-removal commit hook



Question:

Jeff Atwood wrote about whitespace and suggested creating commit hook which removes trailing whitespace at the ends of lines and files.

I've searched, but I haven't found a clear example of using a pre-commit hook to modify files like this. Do you have a script which you'd like to share?


Solution:1

I'm pretty sure that pre-commit hooks can't be used to modify the transaction in progress. It can only allow or deny the commit action. In your case, you'd want to examine the files to see if they meet your whitespace requirements and fail the commit with a useful error message if they do not. You could use a post-commit hook to do what you propose, but it would involve making a second commit from the hook script to fix the bad whitespace in the first commit. I think the pre-commit hook approach is better.


Solution:2

You can't really use commit hooks to do this in subversion, because if the file is modified by the server, it will not match the version on the client. Commit hooks are for read-only events such as generating notification messages, or by checking the files to see if the commit should be blocked.

See the documentation on commit hooks:

A hook is a program triggered by some repository event, such as the creation of a new revision or the modification of an unversioned property. Some hooks (the so-called “pre hooks”) run in advance of a repository operation and provide a means by which to both report what is about to happen and prevent it from happening at all. Other hooks (the “post hooks”) run after the completion of a repository event and are useful for performing tasks that examineâ€"but don't modifyâ€"the repository. Each hook is handed enough information to tell what that event is (or was), the specific repository changes proposed (or completed), and the username of the person who triggered the event.


Solution:3

Not sure what platform you are on, but you can do this with TortoiseSVN, using its client-side hooks.

Essentially you would write a script that would modify the files using a pre-commit hook. I think this is more correctly done in a build script though: you don't really want to commit code you have not even compiled or run! Your script that removes spaces could potentially break something, depending upon the context/language.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »