I am honored if this article has helped you, and sorry if I have wasted your time. I hope to use the simplest plain words to help people like me. If there is any mistake, please do point out, so as not to mislead you, also mislead me. This article from: www.jianshu.com/users/320f9… Thank you for your attention.

This is the use of a SVN function – hook.

About SVN hooks

What is a HOOK for?

Subversion provides a hook mechanism for administrators to control the commit process. ** When a particular event occurs, the corresponding hook will be called. The hook is actually the handler of the particular event. ** Each hook gets parameters related to the event it is handling, and based on the value returned by the hook, Subversion decides whether to proceed with the current commit process.

Subversion currently provides five hooks that can be installed:


Requirements:

If you’re working on a WEB project with multiple people and you’re using SVN, there’s a situation where I not only have to submit code to SVN, but I also need to reflect code to the server. What to do? Do I have to upload to the server every time I change the code to see the results immediately?

Therefore, we will solve the problem. The SVN uses the hook hook mechanism.

I use Windows, PHP and the Laravel framework.

First steps:

  1. Create a new Laravel project to perform uploads, adding custom upload commands;
  2. Create a hook. Bat to execute the command.
  3. Set SVN hooks.

Now, follow the steps and start the implementation.


1. Create a new Laravel project for uploading and add custom upload commands

Mine is laravel 4.2 framework, 5.0 above see here: laravel-china.org/docs/5.0/co… It’s all similar.

The items in 4.2 are as follows:

First, do some configuration in the app\config\remote. PHP file to set up the necessary information for the server to be uploaded.

'connections' = > array (' dev' = > array (' host '= >' 172.0.0.1 ', 'username' = > 'dev - user' and 'password' = > ', 'key' => 'E:\PHPProject\test\app\config\pk\generate_dev-key.pem', 'keyphrase' => '123456', 'root' => '/home/dev-user', ), the 'stage' = > array (' host '= >' 172.0.0.2 ', 'username' = > 'stage - the user', 'password' = > ', 'key' => 'E:\PHPProject\test\app\config\pk\generate_dev-key.pem', 'keyphrase' => '123456', 'root' = > '/ home/stage - the user'), 'prod' = > array (' host '= >' 172.0.0.3 ', 'username' = > 'root' and 'password' = > '123456', 'key' => '', 'keyphrase' => '', 'root' => '/var/www/html/project_prod', ), ),Copy the code

Then, to customize the uploaded command, create a file synchronize.php in the app\ Commands directory.

<? php use Illuminate\Console\Command; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Input\InputArgument; Class Synchronize extends Command {/** * first path: our local path, not the full path, matches automatically. * * Second path: */ private $app_path=['dev'=>["Trunk/Project_dev","/home/ Project_dev"], 'stage'=>["Trunk/Project_stage","/home/project_stage"], 'prod'=>["trunk/Project_prod","/var/www/html/project_prod"] ]; /** * The console command name. * * @var string */ protected $name = 'synchronize'; /** * The console command description. * * @var string */ protected $description = 'synchronize dev source with svn'; /** * Create a new command instance. * * @return void */ public function __construct() { parent::__construct(); } /** * Execute the console command. * * @return mixed */ public function fire() { // set_time_limit(0); $path=$this->argument("path"); $fp=fopen($path,"r"); while(! Feof ($fp) && $line = the fgets ($fp)) {/ / remove the newline $line = str_replace (PHP_EOL, ' ', $line); Array_walk ($this->app_path,function($v,$k) use ($line){$path_array=explode($v[0],$line,2); if(count($path_array)==2) { \Log::info($line); \Log::info($k); $remote_path=$v[1].$path_array[1]; If (is_dir ($line)) {/ / folder \ Log: : info (' CD '. $v [1]). \Log::info('mkdir -p '.trim($path_array[1],'/')); \SSH::into($k)->run( [ 'cd '.$v[1], 'mkdir -p '.trim($path_array[1],'/'), ] ); $re_path=explode('/',$path_array[1]); $re_path=explode('/',$path_array[1]); $re_path_length=count($re_path); $work_path=$v[1]; array_walk($re_path,function($v,$k) use ($re_path_length,&$work_path){ if($k! =$re_path_length-1) $work_path.=$v.'/'; }); \Log::info(is_file($line)); If (is_file ($line)) {\ Log: create the folder / / / / folder: info (' mkdir -p '. $work_path); \SSH::into($k)->run( [ 'mkdir -p '.$work_path, ] ); / / upload files \ SSH: : into ($k) - > put ($line, $temp_remote_path); }else {// Delete operation \Log::info(' CD '.$work_path); \Log::info('rm -rf '.$re_path[$re_path_length-1]); \SSH::into($k)->run( [ 'cd '.$work_path, 'rm -rf '.$re_path[$re_path_length-1], ] ); }}}}); } fclose($fp); } /** * Get the console command arguments. * * @return array */ protected function getArguments() { return array( array('path', InputArgument::REQUIRED, 'An example argument.'), ); } /** * Get the console command options. * * @return array */ protected function getOptions() { return array( array('example', null, InputOption::VALUE_OPTIONAL, 'An example option.', null), ); }}Copy the code

Once this is done, the project can upload files to the server we specified, but how do we get it to execute? Let’s move on.


2. Create a hook. Bat to execute the command

Since I’m using Windows, create a.bat executable file.

First, create a.txt text file in a directory and type as follows:

CD E:\PHPProject\test D:\wamp\bin\ PHP \php5.5.12\php.exe artisan Synchronize %1Copy the code

The path in the first line is the path of the project we created in the previous step; The second line is the PHP installation path.

Then, rename the.txt file to.bat.

So, we have built a hook, and then we configure it to SVN.


3. Set the hooks of the SVN

The picture is clear.







If you have any questions, please do point them out.