Difference between revisions of "Category:SVN"

From Noah.org
Jump to: navigation, search
(svnserve init.d script)
(svnserve init.d script)
Line 37: Line 37:
This works on RedHat and Ubuntu.
This works on RedHat and Ubuntu.
<include svncat src="file:///home/svn/src/shell/svnserve" highlight="sh" />

Revision as of 15:58, 19 September 2007

Subversion has flaws and limitations, but it's a lot better than CVS and it's cheap. The low price tag doesn't reflect low quality or lack of support. There are not a lot of hidden maintenance costs that will raise the total cost of ownership. Most of the deficiencies are clear up front. Subversion does do distributed version control very well and merging is frustrating (out of the box, a few simple third-party tools make it tolerable). It is also difficult to automatically generate reports that graph changes.

Subversion remote access

After playing with WEBDAV and SVN Server (http:// and svn://) I settled on using svn+ssh://. It is secure and it requires little or no setup or service to run on the host (besides sshd). I don't know why anyone would use anything else. With a few SSH keys it's as easy to use locally as it is to use remotely. It's dead simple and reliable. The only disadvantage of svn+ssh is that you cannot use fine-grained, per-directory authorization to restrict access to files for certain users. You might encounter this problem if you want to allow a consultant to access only one part of a repository.

authz authorization with svnserve

Start svnserve with the root pointing to the directory ABOVE your repository files. For example, if I had a project repository under "/home/svn/project_a" I would use the following to test svnserve:

# svnserve -d --foreground --root /home/svn

If I wanted to allow employees access to everything, but restrict a consultant named "sid" to a branch but allow read access to the trunk I would create an authz file something like this:

employees = bob, sue, jim, ted

@employees = rw

sid = r

sid = rw

password caching

The svn client will cache passwords under "~/.subversion/auth/svn.simple/". The big downside of this is that if you enter the wrong password when you first login you will forever be shut out until you delete a file under "~/.subversion/auth/svn.simple/". You will need to delete the file associated with the svn server that you are trying to login to. Cat each file to find the right one... Yes, this does seem like a bug. If svn can't login with the password you gave it then why should it cache that password and use it again later without asking. Unfortunately, the "--no-auth-cache" option only prevents svn from caching the password in the first place. It doesn't prevent it from using a cached password that already exists.

svnserve init.d script

This works on RedHat and Ubuntu.

<include svncat src="file:///home/svn/src/shell/svnserve" highlight="sh" />

# This starts and stops svnserv. Put this in /etc/init.d/svnserve.
# This works on both Ubuntu and RedHat systems.
# On Ubuntu, run "update-rc.d svnserve defaults" to install this on startup.
# On RedHat, run "chkconfig --add svnserve" to install this on startup.
# $Id$
# chkconfig: 2345 90 10
# description: svnserve daemon start script

# Use the following variables to customize user, group, and svnserve path.
SVNSERVE=`which svnserve`

if [ -f /etc/rc.d/init.d/functions ]; then
    # RedHat style
    . /etc/rc.d/init.d/functions
    START="daemon $SVNSERVE -d --root $SVN_ROOT_PATH"
    STOP="killproc $SVNSERVE"
    # Ubuntu LSB style
    . /lib/lsb/init-functions
    START="start-stop-daemon --start --exec $SVNSERVE -- -d --root $SVN_ROOT_PATH"
    STOP="start-stop-daemon --stop --exec $SVNSERVE"

if [ ! -x $SVNSERVE ]; then
    echo "Could not find ${SVNSERVE}. PATH is ${PATH}"
    exit 0
case "$1" in
        echo "Starting svnserve..."
        umask 002
        if [ $? ]; then
            echo "Started svnserve"
            exit 0
            exit $?

        echo "Stopping svnserve..."
        exit $?

        "$0" stop && "$0" start

    echo "Usage: /etc/init.d/svnserve {start|stop|restart|force-reload}"
        exit 1

echo "Unhandled case while trying to start svnserve."
echo "see $0"
exit 3

Subversion setup for team development

If you want to share a repository among several developers you will need to configure a few trivial things.

Create a user "svn" and create the repository under "svn". This assumes the "svn" user will have a home directory of /home/svn.

Add each developer to the svn group (usermod or edit /etc/group):

 usermod -a -G svn username

Make the svn repository group writable. In this example the repository is called 'src':

 chmod -R g+w /home/svn/src

Now each developer in the svn group can checkout a project using an svn+ssh URL to the repository:

 svn co svn+ssh://username@svn.example.com/home/svn/project


Subversion merging is painful. It's confusing; it's not easy to visualize what is happening; and you have to manually keep track of the revision numbers between the sections you want to merge. But no one said that merging was supposed to be easy. Subversion is free, so less complaining and more work!

Before you do anything make sure your branch working copy is up to date and committed (`svn up` then `svn ci`).

Merging always happens to your working copy. You might merge between remote URL A and remote URL B, but the results of that merge is stored in your local working copy. This tends to confuse people at first. This is why you want to make sure you working copy is up to date and committed; otherwise, you will get a three way merge which can be even more confusing.

   A      B
    \    /
     \  /
 working copy

Merging Enhancements with svnmerge.py

The standard Subversion distribution comes with a handy script called svnmerge.py that keeps track each time you merge in either direction. This allows you to incrementally merge changes without having to manually keep track of revision numbers. Don't merge without it.

By default it expects to be run in a branch working copy and to merge from the trunk down to the branch.

svnmerge.py init

But it can run in a trunk working copy and merge from any branch up to the trunk.

svnmerge.py init BRANCH_URL

What revision was a branch created from?

Merging requires that you know at what revision a branch was branched from. When you did the "svn copy" the new branched copy got a logged with a revision number. This is tricky for a beginner because intuitively you expect Subversion to merge from one branch to another without requiring the user to keep track of branch revision numbers.

The easiest way to get the starting revision of a branch is this command:

svn log --verbose --stop-on-copy svn+ssh://svn.example.com/home/svn/module/branches/project

How to merge to or from the trunk

Use the trick above to get the revision you branched from. Assume this gives you a revision number 17.

merge FROM branch TO trunk

Do this if you want to merge changes from the branch to the trunk.

In your trunk working copy run this command:

svn merge -r 17:HEAD svn+ssh://svn.example.com/home/svn/module/branches/project

merge FROM trunk TO branch

Do this if you want to merge changes from the trunk to the branch.

In your branch working copy run this command:

svn merge -r 17:HEAD svn+ssh://svn.example.com/home/svn/module/trunk/project

The tricky thing to remember is that now your branch gets the latest Revision number. The next time you want to do an update from the trunk you need to use this new revision number! Yikes! Save yourself some trouble and use svnmerge.py.

Merge back to previous version

Sometimes you need to roll-back to a previous version after you have committed changes. For this you use merge. In the example below PREV and HEAD are aliases that svn interprets, so you don't even have to know the exact revision numbers you are merging from and to:

svn merge -rPREV:HEAD my_file.txt

Mass resolve

This does a resolve all. This will very likely result in a bunch of garbled files. All of the conflicted files will be mangled. But I found this useful from time to time when I know what I'm doing or if I am just going to "svn rm" a whole directory anway.

svn status | grep ^C | awk '{print $2}' | xargs svn resolved

Check log messages in a given date range

Revision specifications can take dates. The dates a put between {curly braces}. For example, the following shows the logs for a six month range:

 svn log -r {2006-12-25}:{2007-6-25} topip.py

What is svn going to do to my files when I update?

It's annoying that "svn status" does not do this. The status command only looks at your local revisions. Even if you add '-u' and do "svn status -u" you will still not tell you if there will be any conflicts or automatic merging. It will only show "M" or "*" (newer on server), which is misleading because you might think that you can Update or Commit without conflicts. Here svn does not follow the principle of least surprise.

The solution is to use the merge command. The "svn up" command is just a special form of merging. You can use merge to do an update. The key is that "svn merge" supports the "--dry-run" option, so you can see what files would get thrashed before they actually get thrashed.

 svn merge --dry-run -r BASE:HEAD .

This would be a lot simpler if "svn up" supported "--dry-run" or if "svn status" did what you think it should do.

Note that "svn merge" isn't exactly like "svn up", but for the purposes of detecting merges and conflicts before they happen it's close enough.

CR vs. CR/LF -- set text file line endings

This will recursively set all text files (will ignore binary files) so that svn will change text file line endings on checkout.

 svn propset -R svn:eol-style native *

Set to always CR (UNIX):

 svn propset -R svn:eol-style CR *

Set to always CRLF (Windows):

 svn propset -R svn:eol-style CRLF *


This sets keyword expansion for the Id keyword.

 svn propset svn:keywords Id *

Put $Id$ in your code and it will be expanded to something like:

 $Id: calc.c 148 2002-07-28 21:30:43Z sally $

Execute permissions

This sets execute permission on "script.sh":

 svn propset svn:executable script.sh

Note that you cannot set read and write permission or ownership.

Error: Cannot replace a directory from within

If you see this it probably means that your project directory was moved. If you have checked out the directory above then usually going back to the root and running "svn up" will fix this (it will rename your project directory). But probably you did not checkout every directory above this project, so you can't go up and run 'svn up'. Don't panic.

 svn switch

You almost always need the relocate switch:

 svn switch --relocate

Create and checkout a directory in place of an existing directory

You want to add an existing directory to svn but you don't want to do "svn import ..."; delete the import directory; then checkout the directory back in-place where you had it to begin with. For example, say you want to add your /etc directory to svn. For this example you should be in the directory you want to add. That is, your current working directory should be the directory you want to add. Here we add /etc to an SVN repository.

cd /etc
svn mkdir file://home/svn/root/trunk/etc -m"Created /etc"
svn checkout file://home/svn/root/trunk/etc/. .
svn add *
svn add .*
svn ci

Status codes

Subversion prints letter codes next to the filenames to indicate their status. You will see status when you do an update, merge, or status:

svn status:

   This means that you have no changes to your working copy, but 
   the svn server has new changes that will update your working copy.
   This means that you have changes to your working copy and 
   the svn server has new changes that will be merged automatically 
   into your working copy. BEWARE! SVN is writing code for you.
   I never trust this.
   This means that you have changes to your working copy and
   the svn server has new changes that conflict with your changes.
   These changes cannot be merged automatically.
   Your working copy will now contain conflict markers (<<<<<<<), so
   it will be broken until you edit and fix the conflicts.
   You will also have to tell Subversion that the conflict is resolved
   ("svn resolved myfile.txt").

Pages in category "SVN"

The following 7 pages are in this category, out of 7 total.