Blog

SVN: Moving from traditional FTP to advance file editing


My new job introduced me to an ever intimidating system or whatever it is called, Subversion (SVN).

What is SVN

According to wikipedia: "Apache Subversion (often abbreviated SVN, after the command name svn) is a software versioning and a revision control system distributed under a free license. Developers use Subversion to maintain current and historical versions of files such as source code, web pages, and documentation. Its goal is to be a mostly-compatible successor to the widely used Concurrent Versions System" In simple terms, it records changes on each upload. And has built in support for logs, for branching major versions, for allowing multiple programmers to edit one file and many more.

Advantages of SVN

  1. Faster upload/download - On each upload(commit) and download(update), only the changes are being transferred, which makes it faster than FTP where whole file contents are being transferred, which causes some problems, specially when you have a bad connection.
  2. Less reason to backup - Each upload(commit) is recorded so it's easy to open, edit and re-uploaded(revert, edit and committed) ; When a file is accidentally deleted, it's easier to retrieve it. This also gives less reason to comment old codes, which (old codes) usually lasts commented for years.
  3. Team mates can edit the same file at the same time
    1. example:
      1. teammate A edits the file,
      2. then teammate B edits the same file.
      3. Teammate A is done with the changes, and uploads(commits) it.
      4. On the other hand, teammate B is unaware and tried to upload(commit) his copy
      5. teammate B receives an error, that his copy is outdated
      6. teammate B download(updates) the file from the server
      7. If teammate B's changes and teammate A's changes are on different lines of the code, they will be combined(merge)
      8. Teammate B can now upload(commit) the file
  4. The team can work on separate copy of the project (branches) - The team can work on a copy based on the stable version(or any branch). When these separate copies becomes stable, they can be merged with the stable version(called trunk), using the "svn merge" action of svn or the svn client.
  5. Supports separation of stable versions(tags) - of course this can be done on your own way, but svn clients warns you when overwriting or committing a file on the stable version directory or in svn term, the "tags" directory.

Directory Structure

There is three main directories in the generally accepted structure on SVN. The "trunk", is the stable version; The "branches" contains the non stable versions of trunk, the "tags" contains the past stable versions of "trunk".
Naming convention under branches and tags depends on your preference.
The directory structure will appear like this:
  • /add.ph/
    • /branches/
      • /1.1.new-design.123/
        • /includes/
        • /assets/
          • /css/
            • common.css
            • common-new.css
        • index.php
      • /1.2.new-page.126/
        • /includes/
        • /assets/
          • /css/
            • common.css
        • about.php
        • index.php
    • /tags/
      • /1.0.basic-version.123/
        • /includes/
        • /assets/
        • index.php
    • /trunk/
      • /includes/
      • /assets/
      • index.php
  • /wow300.net/
    • /branches/
      • /1.1.new-feature.123/
        • index.php
        • macro.php
    • /tags/
      • /0.9.first-version.109/
        • macro.php
        • index.php
    • /trunk/
      • macro.php
      • index.php
Where add.ph and wow300.net are the project folders
You can also do it this way, where all projects share the same svn directories
  • /branches/
    • /add.ph/
      • /1.1.123-new-design/
      • /1.2.126-new-page/
  • /tags/
    • /add.ph/
      • /1.0.123-basic-version/
  • /trunk/
    • /add.ph/
    • /wow300.net/

How to use SVN on your web server

Your server must first have svn installed and running(through svnserve) and your computer must have a svn client like RapidSVN or TortoiseSVN (choose only one). If your server already has svn installed and running then skip to the svn client section  

How to setup SVN

  1. install "subversion" package (eg. apt-get install subversion)
  2. create a new repository using svnadmin ( eg. svnadmin create /usr/local/repositories/add.ph/ )
  3. configure your repository
    1. edit the following files (read the comments inside them, they are helpful)
      1. conf/svnserve.conf - make sure anonymous access is not allowed
        • anon-access = none
      2. conf/authz - write your desired username, and make sure you have "rw" access on "[/]"
        • [/] albert = rw
      3. conf/passwd - write your username and desired password
        • [users] albert = mySecretPassword
  4. Run your svn server using svnserve command ( eg. svnserve -d -r /usr/local/repositories )
  5. Autostart #4 on bootby adding a bash file it to "init.d" example:
    1. `vi /etc/init.d/svnserve`
    2. paste: "svnserve -d -r /usr/local/repositories"
    3. then save
  6. On your computer, install a svn client, like TortoiseSVN or RapidSVN
  7. Checkoutyour repository (eg. svn://add.ph/add.ph/)
    • TortoiseSVN: right click on a blank space on windows, SVN Checkout
    • RapidSVN: Repository -> Checkout

Server's Working Copy

Now you can edit, commit and update your "working copy", that is, the copy you have on your computer, however your server will still have no physical copy of the files, instead it has the data of the files. On the server, you can choose have a working copy, that updates itself when you commit something new. My Method:
  1. checkout a working copy of the repository on the  repository directory ( eg. `svn checkout svn://add.ph/add.ph/ /usr/local/repositories/add.ph/main_checkout_dir/ )
  2. Create hooks/post-commit file  (note:  copy post-commit.tmpl , the comments will help)
    • eg. `vi /usr/local/repositories/add.ph/hooks/post-commit`
    • REPOS="$1" REV="$2" cd "$REPOS" /usr/local/bin/svn update --non-interactive --username "albert" --password "mySecretPassword" main_checkout_dir/
This way, every time you commit, the /main_checkout_dir/ will get updated (I'm still looking for other neater ways to do this).

Publishing

Then after that you can publish it to your website by creating a symlink in your public_html or www folder: examples: for the stable copy: `ln -s /usr/local/repositories/add.ph/main_checkout_dir/trunk/ /var/www/add.ph/` for a branch you are editing `ln -s /usr/local/repositories/add.ph/main_checkout_dir/branches/1.1.123-new-design/ /var/www/add.ph/1.1.123/ `

Comments (0)


Add a Comment





Allowed tags: <b><i><br>Add a new comment:


A Few Accomplishments

Integer eu ante ornare amet commetus vestibulum blandit integer in curae ac faucibus integer non. Adipiscing cubilia elementum integer. Integer eu ante ornare amet commetus.

Possibly broke spacetime

Integer eu ante ornare amet commetus vestibulum blandit integer in curae ac faucibus integer adipiscing ornare amet.

Terraformed a small moon

Integer eu ante ornare amet commetus vestibulum blandit integer in curae ac faucibus integer adipiscing ornare amet.

Snapped dark matter in the wild

Integer eu ante ornare amet commetus vestibulum blandit integer in curae ac faucibus integer adipiscing ornare amet.

Contact Me

Integer eu ante ornare amet commetus vestibulum blandit integer in curae ac faucibus integer non. Adipiscing cubilia elementum integer. Integer eu ante ornare amet commetus.