SVN adds mandatory annotations, pre-commit with Python

Source: Internet
Author: User

Since some people in the group do not write comments when they commit the code, and there is no fixed format, they are ready to add mandatory comments to SVN commits.

First find the hooks directory in the code base, which is available when the SVN repository is built properly. Enter the hooks directory, find the Pre-commit.tmpl, remove the Tmpl, and rename it to Pre-commit.

This is a shell script, if it is simple to judge the content of the comment cannot be less than a few characters, directly write shell script can, but now I want to let development in a fixed format to submit code,

For example:

Reason: e-commerce

Developer:du

Reviewer:du

The shell should also be able to do, but I do not use the shell very skilled, and python is easier to write, so a shell in conjunction with Python to do the hook.

First, the Python script

check.py

#Coding=utf-8ImportSYS inputstr= Sys.argv[1]ifinputstr=="": Exit (1); Lstr=inputstr.split ("\ n")ifLen (LSTR) ==3:    if('Reason:' inchLSTR[0] andLen (lstr[0]) >len ('Reason:')) and('Developer:' inchLSTR[1] andLen (lstr[1]) >len ('Developer:')) and('Reviewer:' inchLSTR[2] andLen (lstr[2]) >len ('Reviewer:'): Exit (0); Else: Exit (1); Else: Exit (1);


And then the Pre-commit.

#!/bin/SHSvn_bindir=/opt/subversion/bin/Svnlookrepos=" $"TXN=" $"logmsg= ' $SVN _bindir log-t"$TXN" "$REPOS"' var= ' python/opt/svndata/repos/hooks/check.py"${logmsg}"' Result=$?if[$result-GT0 ] ThenEcho-E"You must input comment like this:\r\nreason:\r\ndeveloper:\r\nreviewer:" 1>&2Exit1fiExit0

This pre-commit best use vim to write, do not write in Windows and then upload to the Linux server, will report the format is wrong, in fact, 1>&2,-E also add, only these two add, the client submits the time to see the error message.
References to variables in the shell $result and "$result" are OK, but do not use single quotes. and if [$result-gt 0 ] this piece, the space must be left out, otherwise it will error .

Verify that the following

No comment submission

Will prompt

The above error is the system cause, no major hindrance. Mainly in the middle red box inside the hint. We can see the increase in mandatory annotation success.

----------------

As can be seen from this example, we can completely meet other needs on this basis, such as the submission of code to send e-mail, log storage and other functions.

SVN adds mandatory annotations, pre-commit with Python

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.