C # implements the SVN server-side hook

Source: Internet
Author: User
Tags collabnet subversion svn client tortoisesvn collabnet

Goal

The thing to do, is when the file is submitted to SVN, you can re-read the server to all the content of the commit file, and according to some rules to verify the legitimacy of the file, if the validation fails, the submission is terminated, and the SVN client displays an error message.

Preparatory work

1, install SVN server, this example uses COLLABNETSUBVERSION-SERVER-1.6.3-3.WIN32, the installation using the default repository directory, that is, c:/svn_repository. 2, create an SVN repository. In the command line, execute "svnadmin create c:/svn_repository/test" command, the system will be created in the Svn_repository Directory Test folder, there are conf,db,hooks,locks four folders, This hooks folder is our home. Note: Before executing the svnadmin command, it may be necessary to restart the machine for the environment variable to take effect, otherwise the system could not find the svnadmin command. 3, install SVN client, here is the use of TortoiseSVN, for update,commit files, testing the availability of server-side hooks.

SVN Hook Introduction

In the Hooks folder, you can see that the system has generated a lot of tmpl files, they are the template files of hooks. The hook method is simple, as long as the executable file (such as. exe/.bat) is placed in the Hooks folder and changed to an SVN-recognizable name, it will be executed. If you put a pre-commit.exe into the Hooks folder, it will run before the code is actually commit.

Note: TORTOISESVN also provides hook function, can be set in setting, there are many options such as Precommit,postcommit, and can be specified to run the executable file, but this hook is on the client, The server-side hooks described in this article are not the same and do not seem to be able to execute WinForm form of the program. This hook is not a hook!

Start C #

To create a console application, the. exe file generated by this project can be placed in the Hooks folder and renamed to Pre-commit.exe.

Changes the default Main method return value to int if return 0 succeeds, and if return 1 causes the commit to fail. Use Console.Error.WriteLing ("...") to send the error message to the client. Main function parameter Args[0]: repository path, for example "C:\svn_repository\test" args[1]: Pre-commit when Transactionid,post-commit is Revisi On number. If you want to get the file list or the file contents of this commit, you need to use the Svnlook command, which will use both parameters.

Code ————————————————————————

private static int Main (string[] args) {if (...)      return 0;      Console.Error.WriteLing (errormessage); return 1; }

————————————————————————

Svnlook

Svnlook.exe is placed in the SVN installation directory, default to "C:\Program files\collabnet Subversion Server\svnlook.exe"

command line: List of changed files: Svnlook changed-t transactionid repository_path svnlook changed-r revisionnumber Repository_pat H Output Example: U folder1/1.txt D 2.txt A 3.txt

Get the file content Svnlook cat-t transactionid repository_path filePath svnlook cat-r revisionnumber repository_path filePath output The contents of the file.

Because in Pre-commit, the second parameter of the main function is TransactionID, so to use the-t,post-commit parameter is Revisionnmuber, so use-R. The answer can be found in the corresponding Tmpl file in the Hooks folder for the two parameters in the other hooks. Using C # to run the command line, take Svnlook changed as an example ————————————————————————

using (var process = new Process ()) {process.         Startinfo.useshellexecute = false; Process.         Startinfo.redirectstandardoutput = true; Process.         Startinfo.filename = @ "C:\Program files\collabnet Subversion Server\svnlook.exe"; Process. Startinfo.arguments = string. Format ("changed-t {0} \" {1}\ "", TransactionID, Repository_path); TransactionID and Repository_path are the two parameters that are passed in the main function.

Process.          Start (); Content = Process.  Standardoutput.readtoend (); Gets the return content of the Svnlook command process.       WaitForExit (); }

————————————————————————

The above is the required content, assembly, plus their own verification logic and so on, compile, exe file copy to the Hooks folder, rename, you can run it ~ ~ ~

Originally from: http://zaocanhebaodan.blogbus.com/logs/41743640.html

C # implements the SVN server-side hook

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.