Columbus, Ohio

Subversion Repository Creation Script

I have been using Subversion (SVN) pretty heavily the past couple of weeks. I wanted a quick way to create a repository that included the standard trunk, branches and tags directories and set the correct owner and file permissions for the new repo. I came up with the following script. Please feel free to comment out sections you do not need.

#!/usr/bin/php -q
< ?php
define('REPO_PARENT_PATH', '/path/to/repos');
define('REPO_OWNER', 'devel_user:devel_group');
define('REPO_MOD', '770');

if( $argc < 2 )
        die("No repo name specified.\n\nUsage: addrepo.php repo_name\n\n");
$repo_name = $argv[1];
echo "Adding Repository: $repo_name\n\n";

// Create repositoryi
$repo_path = REPO_PARENT_PATH;
if( substr( $repo_path, strlen($repo_path)-1, 1) != '/' )
        $repo_path .= '/';
exec("svnadmin create $repo_path$repo_name");

// Add trunk
exec("svn mkdir file://$repo_path$repo_name/trunk -m "Add trunk"");

// Add branches
exec("svn mkdir file://$repo_path$repo_name/branches -m "Add branches"");

// Add tags
exec("svn mkdir file://$repo_path$repo_name/tags -m "Add tags"");

// Change file permissions
exec("chmod -R ".REPO_MOD." $repo_path$repo_name");

// Change file ownership
exec("chown -R ".REPO_OWNER." $repo_path$repo_name");
?>

To use, execute the script from the shell one parameter being the name of your repository.

Example: ./addrepo.php repo_name

Join My FREE Newsletter

Get the latest news and episodes of the Cloud Entrepreneur Podcast and Angelo’s development blog directly in your inbox!