The SVN library in PHP allows to work with SVN through a working copy or directly to the SVN repository files: http://us.php.net/manual/en/ref.svn.php
The problem is there is really few documentation for the direct access functions to repository (svn_repos_*
and svn_fs_*
) the code is almost the same to working in C
the libsvn
, but also there are few documentation, most of the svn command.
The good is that is really easy to work with SVN, basically you only need to:
- call to
svn_repos_open($path)
for getting a$repo
handler - call to
svn_repos_fs($repo)
to get a$repo_fs
handler - get the latest revision through:
svn_fs_youngest_rev($repo_fs)
; - Get a $transaction through
svn_repos_fs_begin_txn_for_commit($repo, $latest_revision, $commiter, $log_message);
- Get a
$repo_root
resource withsvn_fs_txn_root($repo_txn);
With$repo_root
will be the most of operations and the$repo_txn
will just work for do a commit or abort - do all the operations (create a file/dir, remove, set contents)
- Call to
svn_repos_fs_commit_txn($repo_txn);
if you want to commit, orsvn_fs_abort_txn ( $repo_txn );
if you want to abort the transaction
If you read the part of API for SVN: http://svnbook.red-bean.com/en/1.5/svn.developer.usingapi.html you will see an example for creating a new dir; this code can be easily changed to PHP since the calls are almost the same (replace svn_repos_fs_begin_txn_for_commit2
for svn_repos_fs_begin_txn_for_commit
) I will show you a script for init the repository creating the common paths (branches, tags, trunk) into an empty repository:
<?php //the path to your repository $svn_path = "/var/lib/svn/repos/testrepo"; //open the repo $svn_repo = svn_repos_open($svn_path); //get a fs handler $repo_fs = svn_repos_fs($svn_repo); //get latest revision $latest_revision = svn_fs_youngest_rev($repo_fs); //generate a transaction, with user and log message $repo_txn = svn_repos_fs_begin_txn_for_commit($svn_repo, $latest_revision, 'user-repo-label', 'Creating paths'); //get the root respource to this transaction $repo_root = svn_fs_txn_root($repo_txn); //create the dirs svn_fs_make_dir($repo_root, "trunk"); svn_fs_make_dir($repo_root, "tags"); svn_fs_make_dir($repo_root, "branches"); //finally commit the transaction svn_repos_fs_commit_txn($repo_txn);
This script is called: create-repository-paths.php
You will see at end the list of scripts, the order to use is: (with svn commands in the console to check if worked):
#create SVN repository danguer@rukia:~$ svnadmin create /var/lib/svn/repos/testrepo #checking is empty danguer@rukia:~$ svn ls file:///var/lib/svn/repos/testrepo #create paths danguer@rukia:~$ php create-repository-paths.php danguer@rukia:~$ svn ls file:///var/lib/svn/repos/testrepo branches/ tags/ trunk/ #creating dirs danguer@rukia:~$ php create-dirs-recursive.php danguer@rukia:~$ svn ls file:///var/lib/svn/repos/testrepo/test subdir1/ danguer@rukia:~$ svn ls file:///var/lib/svn/repos/testrepo/test/subdir1 subdir2/ danguer@rukia:~$ svn ls file:///var/lib/svn/repos/testrepo/test/subdir1/subdir2 subdir3/ danguer@rukia:~$ svn ls file:///var/lib/svn/repos/testrepo/test/subdir1/subdir2/subdir3 #create a new file danguer@rukia:~$ php write-contents.php danguer@rukia:~$ svn ls file:///var/lib/svn/repos/testrepo/test/myfile.txt myfile.txt danguer@rukia:~$ svn cat file:///var/lib/svn/repos/testrepo/test/myfile.txt Lorem Ipsum, file generated with svn #remove file danguer@rukia:~$ php remove-object.php danguer@rukia:~$ svn cat file:///var/lib/svn/repos/testrepo/test/myfile.txt svn: File not found: revision 4, path '/test/myfile.txt'
Scripts:
- https://github.com/danguer/blog-examples/blob/master/php/svn/recipes/create-repository-paths.php
- https://github.com/danguer/blog-examples/blob/master/php/svn/recipes/create-dirs-recursive.php
- https://github.com/danguer/blog-examples/blob/master/php/svn/recipes/write-contents.php
- https://github.com/danguer/blog-examples/blob/master/php/svn/recipes/remove-object.php