In Windows, wsh/JS implements the svn server hook script to prevent empty log information and junk files from being submitted.

Source: Internet
Author: User
Tags svn client tortoisesvn

How to force users to fill in log information when submitting SVN?

If you are using the tortoisesvn client, you can add the "tsvn: logminsize" attribute to the folder. When you use tsvn to submit changes in this folder, if the length of the log information is insufficient, the submit OK button will remain unavailable. However, this method is ineffective for non-tsvn clients.

As a more restrictive restriction, You can forcibly enter the log information on the Subversion server. In this case, you must use the pre-commit hook script.

The Code is as follows:

File: pre-commit.bat

svnlook log %1 -t %2 | CScript "%~dp0pre-commit.js"EXIT %ERRORLEVEL%

File: pre-commit.js

VaR log = wscript. stdin. readall (); log = log. replace (/^ \ s + | \ s + $/g, ''); If (log. length <1) {wscript. stderr. writeline ('[because of the version Database Administrator setting, you must enter non-empty log information to describe the content of this submission.] '); Wscript. Quit (1 );}

Script description:

All the above script files must be ANSI encoded and saved in the hooks folder in the repository.

Before committing a transaction, subversion executes the hook script "pre-commit" and determines whether to submit the transaction based on the returned results.

On a Windows platform, the hook script should be a windowsexecutable program, such as your pre-commit.exe or pre-commit.bat ".

Of course, you can compile a program in some programming languages, but the implementation is too complicated.

Because of its own shortcomings, batch processing is not easy to judge non-empty logs without using other tools.

Some examples on the Internet use batch processing in combination with third-party programs, provided that third-party programs need to be installed.

But in windows, there is actually a more "green" approach: Using wsh (Windows Script Host) scripts-more powerful "batch processing" in the Windows era, supporting JS, vbs and other scripting languages.

Although in Windows, you can configure pathext environment variables to treat certain extensions as executable file extensions and directly tap into the main file name for execution-for example, You need to execute the wsh script "pre-commit.js ", simply type "pre-commit" at the command prompt, but unfortunately the test found that the Subversion does not directly trigger the "pre-commit.js" when you need to trigger the pre-commit hook script ".

But we can still indirectly call the wsh script through the batch script pre-commit.bat or pre-commit.cmd.

Batch Processing Parameters: % 0 indicates the batch processing script command itself (file name), % 1, % 2, % 3 ...... Parameters of batch processing are represented in sequence.

Batch parameter Extension: "% ~ Dp0 is used to obtain the absolute path of the folder where the batch processing script is located. You can enter "Call/?" In the command prompt /?" Learn more about related syntaxes.

Parameters passed in the pre-commit hook Script: 1st is the version library location, and 2nd is the ID of the transaction to be committed.

The cscript Script Host supports standard input output and error streams, but the wscript host does not. The default Script Host for Windows is wscript. Therefore, you must call cscript explicitly.

(This method is also feasible when the default host is changed to cscript, "svnlook log % 1-T % 2 |" % ~ Dp0pre-commit.js "").

The called wsh script name must be enclosed in double quotation marks (% ~ Batch parameter extension removes the double quotation marks enclosed by the original parameters.

Hook Script Execution Process:
The batch processing script executes the svnlook command to output the log information, and outputs the log stream to the wsh script through the pipeline;
After the wsh script reads the stream, it determines whether the log content contains only blank strings;
After the wsh script is executed, the exit code is written to the errorlevel environment variable. The final batch processing script also uses this error code to exit.
If the exit code of the hook script is not 0, the transaction commit will be blocked, and the message output from the script to the standard error stream will be sent to the svn client for a prompt.

The above method uses a pipeline stream. The following is another way to directly transfer the hook Script Parameters received by the batch processing to the wsh script.

In this example, log messages with only blank characters are blocked, and some common junk files are also blocked.

File: pre-commit.bat

CScript "%~dp0pre-commit.js" %1 %2EXIT %ERRORLEVEL%

File: pre-commit.js

VaR wshshell = new activexobject ("wscript. shell "); var ARGs = wscript. arguments; var error = 0; // Replace the parameters in the Command template with the wsh Script Parameters, and wrap the function replaceargs ($0, $1) that is compatible with the double quotation marks containing space paths) {return '"' + ARGs ($1) + '"';} // Detection Log information function checklog () {var cmd = 'svnlook log % 0-T % 1'; cmd = cmd. replace (/% (\ D)/g, replaceargs); var log = wshshell. exec (CMD ). stdout. readall (); log = log. replace (/^ \ s + | \ s + $/g, ''); If (log. length <1) {WS Summary. stderr. writeline ('[because of the version Database Administrator setting, you must enter non-empty log information to describe the content of this submission .] '); Return 1;} return 0;} // checks the junk file function filefilter () {var cmd = 'svnlook changed % 0-T % 1'; cmd = cmd. replace (/% (\ D)/g, replaceargs); var oexec = wshshell. exec (CMD); var stdout = oexec. stdout; // The output format of the svnlook changed command can be found in the svn manual. Each line represents a file change, and the first letter of the line, A, represents the new file in the version difference. // This regular expression matches "thumbs. DB" and "desktop. ini" in the new file and some temporary office files var r =/^ A. \ s ((?: [^ \/] + \/)*(?: Thumbs \. DB | Desktop \. ini | ~ \ $ .*\.(?: Docx? | XLSX? | Pptx ?))) $/I; var illegals = []; var m; while (! Stdout. atendofstream) {var line = stdout. readline (); If (M = line. match (R) {illegals [illegals. length] = m [1] ;}} if (illegals. length) {wscript. stderr. writeline ('[the following files are determined as spam files and cannot be submitted. Please exclude them and resubmit them.] '); Wscript. stderr. writeline (' [if an error is identified or there are other problems, contact the version database administrator .] '); Wscript. stderr. writeline (illegals. join ('\ n'); return 1;} return 0;} error + = checklog (); error + = filefilter (); wscript. quit (error );

Wsh is powerful enough to deal with these problems.

The disadvantage of the pre-commit hook script is that it is called only after all the changes are transmitted to the server.

When there were a lot of changes, the data was transferred for half a day, and it was not depressing that a commit was blocked.

You can consider using the server hook script as the final level. In normal times, you should prioritize the client configuration for Operation filtering.

Test environment: Windows XP/2003, wsh 5.7, TortoiseSVN-1.7.10 (subversion 1.7.7)

Related Article

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.