1. Hook introduction
The specific method of Hook script is to write the shell script program in the operating system, which is actually a shell script. Write the code according to the operating system and shell program of your SVN.
The hook is a program triggered by some version library events, such as the creation of a new version or the modification of unversionized attributes. Every hook will be told enough information, including what the event is, the object to be operated, and the user name to trigger the event. Through the output or return status of the hook, the hook program can continue, stop, or suspend work in some way. A hook is triggered when data changes.
By default, the subdirectory of the hook contains various version Library hook templates. First, go to the hooks hook directory of a project version library and check the template:
[Root @ niaoyun43442 svndata] # cd 21 yunweinew/hooks/
[Root @ niaoyun43442 hooks] # ll
Total 36
-Rw-r -- 1 root 1977 Aug 29 post-commit.tmpl
-Rw-r -- 1 root 1638 Aug 29 post-lock.tmpl
-Rw-r -- 1 root 2289 Aug 29 post-revprop-change.tmpl
-Rw-r -- 1 root 1567 Aug 29 post-unlock.tmpl
-Rw-r -- 1 root 3426 Aug 29 pre-commit.tmpl
-Rw-r -- 1 root 2410 Aug 29 pre-lock.tmpl
-Rw-r -- 1 root 2786 Aug 29 pre-revprop-change.tmpl
-Rw-r -- 1 root 2100 Aug 29 pre-unlock.tmpl
-Rw-r -- 1 root 2780 Aug 29 start-commit.tmpl
By viewing these hook templates, we can see what events trigger the script and how to pass data to the script. When using the hook, you only need to copy the corresponding template to the corresponding file name (remove the suffix. tmpl) and then edit it to use it.
Tip:
For security reasons, the Subversion Library executes the hook script in an empty environment-that is, there is no environment variable, or even no $ PATH or % PATH %. For this reason, many administrators may be confused that their hook scripts are normal during manual running, but they cannot run in Subversion. Note that you must set the environment variables in your hooks or specify the absolute path for your program.
2. Common hooks.
Currently, Subversion has five hooks:
1, start-commit
It runs before the transaction is committed. It is usually used to determine whether a user has the right to commit. The version library sends two parameters to the program: the path to the version Library and the user name to be submitted. If the program returns a non-zero value, the commit operation is stopped before the transaction is generated. If the hook program writes data to stderr, It queues the data to the client.
2, pre-commit
Run before the transaction is committed. This hook is usually used to protect the content or location (for example, you require that all commits to a specific branch must include a bug tracking ticket number, or, the log information must not be empty) and cannot be submitted. The version library passes two parameters to the program: the path of the version Library and the name of the transaction being committed. If the program returns a non-zero value, the commit will fail and the transaction will be deleted. If the hook program writes data to stderr, it will also be passed to the client.
Purpose: restrict the extension and size of the uploaded file, and control the information to be submitted.
The distribution version of Subversion includes some access control scripts (in the tools/hook-scripts directory of the Subversion source file directory tree), which can be called by pre-commit to implement precise write access control. Another option is to use Apache's httpd module mod_authz_svn to control read/write access to a single directory (see "access control per directory ). In future versions of Subversion, we plan to implement the access control list (ACL) directly in the file system ).
3, post-commit
It runs after the transaction is complete and creates a new revision. Most people use this hook to send descriptive emails about the submission, or as a backup of the version Library. The version library sends two parameters to the program: the path to the version Library and the new version number to be created. Exit the program is ignored.
Purpose: automatically renew svn updates, send email or SMS notifications, update the departure checkout program, and push rsync to the server in real time.
The Subversion distribution version includes mailer. py and commit-email.pl scripts (stored in the tools/hook-scripts/directory in the Subversion source code tree) can be used to send an email describing a given commit (or simply append it to a log file ), this mail contains a list of changed paths, submitted log information, date and author, and output of the GNU differential style for modifying files.
,
Another useful tool provided by Subversion is the hot-backup.py script (in the tools/backup/directory in the Subversion source code tree ). This script can perform hot backup for the Subversion database (a feature supported by the backend of the Berkeley DB database), and create snapshots submitted each time by the version database as backups for archiving and emergencies.
4, pre-revprop-change
Because the attributes of the Subversion revision are not versionized, modifications to these attributes (for example, submitting the log attribute svn: log) will permanently overwrite the previous attribute values. Because data may be lost here, Subversion provides this hook (and the corresponding post-revprop-change), so the version database administrator can use some external methods to record changes. The Subversion client cannot remotely modify the attributes of a version, unless you implement this hook for your version Library.
This hook runs only when you modify the version Library. The version library sends four parameters to the hook: to the path of the version library, you need to modify the attribute revision, the name of the authenticated user name and attributes.
5, post-revprop-change
As we mentioned earlier, this hook corresponds to pre-revprop-change. In fact, this script is executed only when pre-revprop-change exists due to the suspect. When both hooks exist, post-revprop-change runs after the attributes of the revision are changed. It is usually used to send emails containing the new attributes. The version library passes four parameters to the Hook: The path to the version Library, the revision of the attribute, the verified username that changes, and the name of the attribute itself.
The Subversion distribution version contains the propchange-email.pl script (in the tools/hook-scripts/directory in the Subversion source code tree ), it can be used to send an email (or only append to a log file) that modifies the attributes of a revision ). This email contains the revised version and changed attribute names, modified users, and new attribute values.
Note:
Do not use hook scripts to modify transactions. A common example is to automatically set attributes such as svn: eol-style or svn: mime-type during submission. This seems like a good idea, but it can cause problems. The main problem is that the customer does not know how to modify the hook script and cannot notify the customer of its data. Such conflicts will lead to unexpected and unpredictable behaviors.
As an alternative to trying to modify the transaction, we reject the commit when the pre-commit hook transaction does not meet the requirements.
Subversion tries to execute the hook as the user currently accessing the version Library. Generally, access to the version Library is always performed through the Apache HTTP Server and mod_dav_svn. Therefore, the user who executes the hook is the user who runs Apache. The hook itself requires an access permission at the operating system level. You can run it. In addition, other files or programs directly or indirectly used by hooks (including the Subversion version Library itself) will also be accessed by the same user. In other words, pay attention to potential access control problems, which may make your hooks fail to be executed smoothly for your purposes.