QRdvark.com

Introduction To Subversion SVN PHP Tutorial

Introduction To Subversion SVN PHP Tutorial

  1. Abstract
  2. Creating a Repository
  3. Create an SVN User
  4. Initial File Import
  5. Running the Daemon
  6. Checkout Working Copy
  7. Commit Changes
  8. Creating A Branch

Abstract

This document covers the creation, usage and management of the Subversion (SVN) versioning system. The installation of Subversion is beyond the scope of this tutorial, however, to make sure you have SVN installed, use this command.

which svn

You should see something like this

/usr/bin/svn

If this is good, then Subversion is installed and we can continue. If you need to install Subversion, consult your distro's docs.

Creating a Repository

To create a repository for your project, select a place where you have write privileges. For our purposes here, we will create a repository for a project name ACL. So lets begin with creating the repository(repo).

mkdir ~/svn_repos
svnadmin create ~/svn_repos/acl

The above commands will firstly create a directory to keep your svn repositories, as you may have many projects. Secondly, the svn repository is created. A quick look in the newly created ~/svn_repos/acl directory will show you much has been done by svn already.

# ls ~/svn_repos/acl
conf db format hooks locks README.txt

Create an SVN User

Creating an SVN user is quite simple. Using your favourite editor, open the file ~/svn_repos/acl/conf/svnserve.conf

In this file you will need to add, or uncomment if already existing, the following lines in the [general] section of the file.

anon-access = none
auth-access = write
password-db = passwd

As you will note, we have created the password-db as passwd. So now, we need to create the password file. Once again, with your favourite editor, open the file ~/svn_repos/acl/conf/passwd. There may be several example users commented out. Add yourself to the file. In this instance, the username kevin is added with the password my_password.

kevin = my_password

Importing Files

Now that SVN is ready for use, lets import your project files into SVN.

svn import /home/kevin/acl file:///home/kevin/svn_repos/acl -m "Initial File Load"

With this done, you should see something like this. In this example, two files are imported. A PHP class and a database schema file. These two files will form the trunk of the SVN repository. You will also create two other directories named "branches" and "tags". No need to know why we do this just yet, more on this later, however, the names might be a give away.

Adding /home/kevin/acl/branches Adding /home/kevin/acl/tags Adding /home/kevin/acl/trunk Adding /home/kevin/acl/trunk/acl.class.php
Adding /home/kevin/acl/trunk/schema.sql

Committed revision 1.

In the above command, svn has imported the files in /home/kevin/trunk/acl, and all the sub directories and files, into the repo file:///home/kevin/svn_repos/acl. The -m flag denotes a commit message saying this was the initial file upload.

Running the Daemon

svnserve -d

Thats IT!?. Yup, thats it

Checkout Working Copy

Go to your workspace, here we will use ~/workspace, and issue this command

cd ~/workspace
svn co svn://kevin@localhost/acl/trunk

Commit Changes

So now your acl repository is checked out, you can begin work. Edit one of the files in your repository, write and quit. Here we make a change to the schema.sql file. Then commit your changes as follows.

svn commit schema.sql
svn commit schema.sql
svn: Commit failed (details follow):
svn: Could not use external editor to fetch log message; consider setting the $SVN_EDITOR environment variable or using the --message (-m) or --file (-F) options
svn: None of the environment variables SVN_EDITOR, VISUAL or EDITOR are set, and no 'editor-cmd' run-time configuration option was found

WTF!?

What has happend here is that no editor has been specified as the default editor with which to enter commit messages. As the error message suggest, we could use the -m "message goes here" method, or, and this is much nicer, we can specify an editor which will can be used each time a commit is made.

export SVN_EDITOR=/usr/bin/vim

Add the above command to your ~/.bash_profile and you will no longer need to export the PATH or use the -m flag for messages.

Now when you run the commit command you will see something like this

Sending schema.sql
Transmitting file data .
Committed revision 2.
`

Creating A Branch

Essentially, to create a branch, we are creating a new copy of the existing trunk, so we are going to use svn copy, to create a copy into the branches directory.

cd acl svn copy trunk branches/my_acl_branch svn status A + branches/my_acl_branch svn commit -m "Creating a private branch of /calc/trunk." Adding branches/my-calc-branch Committed revision 23.