Save Time Use Subversion

If you are not using a version control system for your code, please, please read this post and implement it. It will only take about thirty minutes of your time and will save you countless hours of time in the future.

If you are going to code anything at all ever, use a version control system. My version control system of choice is SVN. A version control system will keep the various “builds” of your system organized. It saves files intelligently.

First of all go install SVN. I am not going to explain how to install things, its trivial and you already know how what to do.

Ok, now you have svn installed. There are basically three steps to getting svn up and ready. I will explain the single trunk method because my goal is to get as many people who are not using anything at all to use svn TODAY. In the future you can learn branching and merging (don’t cross that bridge until you reach it).

Step 0

The first time you use svn you need to create a repository directory to save all your svn project files to. To do so Use:

svnadmin create repository direcory

 
mkdir /home/username/svn/

Step 1

Lets assume you have a bunch of code for a given project in the directory /home/username/projects/killerapp. We need to create a svn project directory to save the killerapp in. To do this we use svnadmin.

svnadmin create repository

 
svnadmin create /home/username/svn/killerapp

Step 2

Next we want to import our current killerapp code into killerapp’s SVN project directory. The repository has to be specified as a full URL, so if you are on just the one system you can use file://. Note that instead of using the svnadmin command as in step 1, we are using just the svn command.

This will bring up your editor so that you can leave a note. Most systems have vim as the default editor. If you don’t know vim you should learn the basic commands. However you can pass a comment using the -m "my note here" to avoid having to learn vim now.

svn import killerapp to the repository

 
cd /home/username/projects/
svn import killerapp file:///home/username/svn/killerapp \
  -m "my note goes here"

Step 3

Finally, we have to checkout our code so that svn can do its magic and keep track of versions.

svn checkout killerapp so you can work on it

 
cd /home/username/projects/
mv killerapp killerapp.bak #save it just in case
svn checkout file:///home/username/svn/killerapp

Yay! You have an svn project for killerapp. Now you need to do stuff with it, the most basic thing you can do is commit this will save changes to the repository.

svn commit killerapp

cd /home/username/projects/killerapp
# make some changes to some files in killer app
svn commit -m "i was playing around with foobar"

Other commands of interest are as follows

svn important commands

cd /home/username/projects/killerapp
# make some changes to some files in killer app
svn info # show info about the project
svn status # Show a List of Modified/Added/? files
svn add  # add file/directory to the project (status showed '?')
svn diff # show differences since last commit
svn help # get help, you need it kid

IMPORTANT: Don’t use cp, mv, rm, mkdir directly in the svn directory, instead use:

svn commit killerapp

cd /home/username/projects/killerapp
svn cp file.php file2.php
svn rm file3.css
svn mv file4.txt file4.tex
svn mkdir newdir

Congratulations! you have taken a huge step in the right direction in saving your self time and headache.

A script for step one through four

Here is a script that does steps one through four and step zero when need be.

newsvn.sh

#!/bin/bash
 
# @file newsvn.sh
# @Author: Victory
# @Site: dfhu.ORG
# @Date: 090716
# @Version: 1.0
# @License: BSD
# @Requires: svn and svnadmin installed and in $PATH
# @Description: Creates a new svn project
# @Usage: newsvn.sh projectdir
 
# The base directory for svn
SVN_REPOSITORY=$HOME/svntest
 
# a bitch and bail
function usage(){
    echo "-------------------------------"
    echo ""
    echo "ERROR:"
    echo "$1"
    echo "Usage: $0 projectdir"
    echo ""
    echo "-------------------------------"
    exit;
}
 
 
# makes sure there are at least one argument ...
if [ $# -ne 1 ]; then
    # ... if not, exit
    usage ""
fi
 
# get rid of trailing / and make more pretty var name
project=$(echo $1 | sed 's;/$;;');
 
# make sure that one argument is directory ...
if [ ! -d "$project" ]; then
    # ... if not, exit
    usage "'$project' is not a directory"
fi
 
# If you don't have a repository directory ...
if [ ! -e $SVN_REPOSITORY ]; then
    # ... then you should, so we create one or bail on failure
    mkdir $SVN_REPOSITORY || exit
fi
 
# lets create a directory for the repository
svnadmin create "$SVN_REPOSITORY/$project" || exit
 
# now we can import our crud to the repository
svn import "$project" "file://$SVN_REPOSITORY/$project" \
    -m "initial import dfhu.org script" || exit
 
# save the old project, just in case, delete by hand
mv "$project" "$project".$RANDOM.bak || exit
 
# finally get the svn working directory
svn checkout "file://$SVN_REPOSITORY/$project" "$project" || exit
 
# Change into the project dir and show the info
cd "$project" || exit
svn info || exit
 
echo ""
echo "I think everything worked out!"
echo "Thanks For Using Victory's awsome visit http://dfhu.ORG"
echo ""
echo ""

Please consider leaving a comment to tell me if you have started to use subversion as a result of this post or why you did not.

Edit – Adding checkout file modification to SVN Files

To add modification times to subversion files you use propset. I have the following alias alias propset="svn propset -R svn:keywords 'Date Revision Id' ".

So i will for example do zsh% propset *php *inc.

I use that on all the files i want to have revison numbers on. For PHP source files the template i use at the top of each file is

php-crud

<?php
/*
 
@author: Victory
@site: http://dfhu.org
@copyright: dfhu.org
@report_bugs: bugs(at)dfhu.org
@feature_request: features(at)dfhu.org
@file:
@license: BSD
 
@description:
 
  This file is great.
 
$Date::                      $:
$Rev::                       $:
 
?>
*/

The $Date:: $: and $Rev:: $: bits get update automagically next time i run svn commit.

Share
This entry was posted in beginner-programming and tagged , , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.
1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

If you are going to post code please use:
<pre lang="php" escaped="true"> YOUR_CODE_HERE </pre>

Change the lang to mysql, python, lisp, whatever. This will escape your code.