SVN Hook
Click here to load reader
-
Upload
thomas-weinert -
Category
Technology
-
view
2.973 -
download
2
description
Transcript of SVN Hook
![Page 1: SVN Hook](https://reader038.fdocuments.us/reader038/viewer/2022100517/558154b2d8b42ab9548b50e0/html5/thumbnails/1.jpg)
PHPUG
SVN Commit Hookcoding standard and syntax check
![Page 2: SVN Hook](https://reader038.fdocuments.us/reader038/viewer/2022100517/558154b2d8b42ab9548b50e0/html5/thumbnails/2.jpg)
Pre-Commit Hook
● Check data before commit● Can not change files● Return error messages● Allow/disallow commit
![Page 3: SVN Hook](https://reader038.fdocuments.us/reader038/viewer/2022100517/558154b2d8b42ab9548b50e0/html5/thumbnails/3.jpg)
pre-commit
● Shellscript– Parameters
● --transaction● --revision
– Attributes● 1 = repository● 2 = transaction
– Return error code● = 0 – no error, finish commit● <> 0 – error, abort
![Page 4: SVN Hook](https://reader038.fdocuments.us/reader038/viewer/2022100517/558154b2d8b42ab9548b50e0/html5/thumbnails/4.jpg)
Get Changes
● svnlook changed /svn/repository– Returns file list prefixed with action (no xml)
● A = added, U = updated
● Create a list of changes– Filter for action and files extension– Filter for external code files
![Page 5: SVN Hook](https://reader038.fdocuments.us/reader038/viewer/2022100517/558154b2d8b42ab9548b50e0/html5/thumbnails/5.jpg)
Get File Content● svnlook cat /svn/repository/trunk/project/file.php --transaction 123
● No local file, just the content
![Page 6: SVN Hook](https://reader038.fdocuments.us/reader038/viewer/2022100517/558154b2d8b42ab9548b50e0/html5/thumbnails/6.jpg)
PHPCodeSniffer
● PEAR package● Check files, directories or a string● Parses php source to tokens
– ext/tokenizer– JS support
● Runs checks on token tree● Default checks for several coding standards● Outputs text or xml
![Page 7: SVN Hook](https://reader038.fdocuments.us/reader038/viewer/2022100517/558154b2d8b42ab9548b50e0/html5/thumbnails/7.jpg)
Use PHPCodeSniffer: Source
//init code snifferinclude('PHP/CodeSniffer.php');$phpcs = new PHP_CodeSniffer(1, 1, array(), TRUE); //run the codesnifferob_start();$phpcs->process($phpFiles, 'PAPAYA');ob_end_clean(); ob_start();$phpcs->printXMLErrorReport(FALSE);$xml = ob_get_clean();
![Page 8: SVN Hook](https://reader038.fdocuments.us/reader038/viewer/2022100517/558154b2d8b42ab9548b50e0/html5/thumbnails/8.jpg)
Use PHPCodeSniffer
● Block output during process()● Catch output from printXMLErrorReport()● Parse XML messages● Extract/map file name● Output error messages to php://stderr
![Page 9: SVN Hook](https://reader038.fdocuments.us/reader038/viewer/2022100517/558154b2d8b42ab9548b50e0/html5/thumbnails/9.jpg)
Problem: No local files
● Call PHPCodesniffer for each file providing its content
– Performance● Write temporary files
– Performance– Different file names
● PHP stream wrapper– Different file names
![Page 10: SVN Hook](https://reader038.fdocuments.us/reader038/viewer/2022100517/558154b2d8b42ab9548b50e0/html5/thumbnails/10.jpg)
PHP Stream Wrapper
● Define a location syntax– svnlook:///svn/repository/.../file.php|transaction|123
● Register your own stream wrapper
include(dirname(__FILE__).'/svnlookstreamwrapper.php');stream_wrapper_register( 'svnlook', 'SVNLookStreamWrapper');
![Page 11: SVN Hook](https://reader038.fdocuments.us/reader038/viewer/2022100517/558154b2d8b42ab9548b50e0/html5/thumbnails/11.jpg)
SVNLookStreamWrapper
● PHP class● Read only● Public methods
– url_stat()● Used by file_exists()
– stream_open() / stream_read() / stream_seek()– stream_tell() / stream_eof()
![Page 12: SVN Hook](https://reader038.fdocuments.us/reader038/viewer/2022100517/558154b2d8b42ab9548b50e0/html5/thumbnails/12.jpg)
Error Sample
![Page 13: SVN Hook](https://reader038.fdocuments.us/reader038/viewer/2022100517/558154b2d8b42ab9548b50e0/html5/thumbnails/13.jpg)
PHP Syntax Check
● PAPAYA_Sniffs_Debug_PHPLintSniff– .../PEAR/PHP/CodeSniffer/Standards/PAPAYA/
Sniffs/Debug/PHPLintSniff.php● Use the PHP binary: php -l● No local files
– Temporary file– Pipes
![Page 14: SVN Hook](https://reader038.fdocuments.us/reader038/viewer/2022100517/558154b2d8b42ab9548b50e0/html5/thumbnails/14.jpg)
PHP Lint$descriptorSpec = array( 0 => array('pipe', 'r'), 1 => array('pipe', 'w'),);$pipes = array(); $lint = proc_open( $lintPath.' -l', $descriptorSpec, $pipes);if (is_resource($lint)) { fwrite($pipes[0], file_get_contents($fileName)); fclose($pipes[0]); $output = preg_split( '(\r\n|\n\r|[\r\n])', stream_get_contents($pipes[1]) ); fclose($pipes[1]); $exitCode = proc_close($lint);
![Page 15: SVN Hook](https://reader038.fdocuments.us/reader038/viewer/2022100517/558154b2d8b42ab9548b50e0/html5/thumbnails/15.jpg)
Syntax Error Sample