Modify files under svn in hook script
What is the best way (if any) to modify a particular file under our repository within a post开发者_运维技巧_commit hook?
eg: I want to append a checksum line to a somefile.conf file
You might want to read this chapter of the SVN book. At the end of it, there is the following warning in a nice red box:
While hook scripts can do almost anything, there is one dimension in which hook script authors should show restraint: do not modify a commit transaction using hook scripts. While it might be tempting to use hook scripts to automatically correct errors, shortcomings, or policy violations present in the files being committed, doing so can cause problems. Subversion keeps client-side caches of certain bits of repository data, and if you change a commit transaction in this way, those caches become indetectably stale. This inconsistency can lead to surprising and unexpected behavior. Instead of modifying the transaction, you should simply validate the transaction in the pre-commit hook and reject the commit if it does not meet the desired requirements. As a bonus, your users will learn the value of careful, compliance-minded work habits.
Don't do this in a hook -- do it in a separate change, that other users of that branch can then pull down into their working copy. It doesn't need to be more complicated than this (in pseudocode):
- script gets a working copy from the repository into local directory
- script modifies file
- script commits file with a good commit message "e.g. "script Foo: adding checksum"
It's not uncommon for build scripts (running under cron or triggered by a Makefile) to make periodic revisions to a repository, for example regenerating files based on other files. Sometimes these are checked into the repository and sometimes they aren't, depending on who consumes these files and how.
There's no problem with doing what you suggest in a post commit hook, but do be aware that it will add a new revision to the repository and that the original committer will need to do an update before they can see the changes the script made. It will also slow down the commit as the post commit runs before returning from the commit operation.
精彩评论