SVN Hook Sub-explanation (e.g. must write message on commit)

Source: Internet
Author: User
Tags commit svn

"1. Business Requirements" For the Department ofSVNThe server adds a commitControl: That is, the user commits the changemust be written whenNote, and must not be less than 10 characters.

"2. Basic ideas" SVNIn itself does not provide this force write log function, but through a series of hooks (we call the hook script), before the submission (Pre-commit), the submission process (START-COMMIT), after submission (Post-commit), Call the scheduled hook program to complete some additional functions.

This time we are going to do is to check whether the user has written a comment before committing to the repository, and of course use the Pre-commit hook. We openSVNRepository under the hook directory, you can find several files, one of which is "Pre-commit.tmpl". This file is a template file that tells us how to implement the pre-commitControl。 To open the template file, we see a description of the following paragraph:

# The Pre-commit hook is invoked before a Subversion txn are
# committed. Subversion runs this hook by invoking a program
# (script, executable, binary, etc) named ' Pre-commit ' (for which
# This file was a template), with the following ordered arguments:
#
# [1] Repos-path (the PATH to this repository)
# [2] Txn-name (the NAME of the TXN about to be committed)
#
# The default working directory for the invocation are undefined, so
# The program should set one explicitly if it cares.
#
# If The hook program exits with success, the TXN is committed; But
# if it exits with failure (Non-zero), the TXN is aborted, no commit
# takes place, and STDERR are returned to the client. The hook
# program can with the ' svnlook ' utility to help it examine the TXN.
We see that the hook script is called before a COMMIT transaction executes. Then pass two parameters to the script: Repos-path and Txn-name, one is the URL that the user submits, and the other is a transaction number for this transaction. Returns 0 if the commit succeeds, otherwise returns other non-0 results. Then our hook procedure is to intercept these requests before the transaction is committed, and then use the Svnlook command to check if the log has been written.

"3. Sample Code"The following code is a piece of code that is directly copied online:

@echo off
Setlocal
Set repos=%1
Set txn=%2
REM Check that logmessage contains at least characters
Svnlook Log "%repos%"-T "%txn%" | Findstr "." > Nul
If%errorlevel% GTR 0 goto ERR
Exit 0
: Err
echo Empty LogmessageNot allowed. Commit aborted! 1>&2
Exit 1
The following is a look at the role of the green highlight code:

①set repos=%1
Set txn=%2
Remember what we mentioned earlier, but the businessupon submission, two parameters are passed. This is where the URL and the transaction number are to be received separately.

②svnlook Log "%repos%"-T "%txn%" | findstr "..." > nul
This sentence is the core procedure. First, Svnlook log is used to view the log of a certain repository commit, so how do we know if the two
Parameters. The answer is the repos and TXN parameters that we have previously saved.
Its role is to see%repos% this URL%txn% commit log information, then | Findstr "....."? Careful
Readers will find that there are 10 dot numbers, representing 10 characters.
The function of the whole sentence is to obtain the current commit log content, and then determine whether there are more than 10 characters

③echo Empty Log message not allowed. Commit aborted! 1>&2
The function of this sentence is that when the submission check fails, the output of the prompt message

"4. Run test and error Resolution"We should be able to work properly here. Below we save the above program as Pre-commit.bat, save to the hook directory. StartSVN, do not write comment submission after modifying a file arbitrarily. Take a look at the following results: It's a hint we can't findSVNFormat file under the Repository directory:

Svnlook: Cannot open the file "C:\peng\Other\newRepo\fom...\" The system cannot find the path specified.
It's depressing. What's the reason. Check the file carefully, no syntax errors found. FromSVNOn the official web site a copy of the same content of the bat file, test pass. Could that be the reason for the file format? I tried to compare the characters of these two documents and found that they were different. Will it be someControlThe reason for the character.

So open the old file, rearrange the format, and find that each line is followed by one moreline break in Chinese。 Remove these newline characters before testing and pass the test.

Lesson: Copying code directly from a Web page can sometimes have special characters, such as a Chinese space for control characters, which can sometimes cause unnecessary errors.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.