Locking in Subversion

The main model of operation for Subversion is Copy-Modify-Merge. But there are a few cases when it's hard or impossible to merge conflicting changes from several users, for example:

  • "resource.h" file in C++ project,
  • binary files like images or sounds,
  • *resx files in C# project.

For such cases Subversion offers limited implementation of Lock-Modify-Unlock model via Locking.

To use locking model you should set svn:needs-lock property on files you want to be edited by one user at a time. This property cannot be set for folders. After applying svn:needs-lock to a file the file gets read-only attribute set. Before editing the file should be explicitly locked (this fact is reflected in the repository). After committing the lock is released by default.

Support for Locking in VisualSVN

To set svn:needs-lock property you can click VisualSVN | Properties in context menu for a file or set of files in Solution Explorer, click New, select "svn:needs-lock" from drop-down and click OK.

When you start editing file with svn:needs-lock property VisualSVN automatically asks you to get lock for that file. In case you don't have access to the repository (working on an airplane for example) but want to edit a file anyway you should clear read-only attribute of the file in Windows Explorer. Please note that you will need to get lock before committing changes to repository.

If you have locked files in your working copy then VisualSVN will show small lock icon in Visual Studio status bar. By clicking on this icon you can see list of locked files and release locks for some of them if necessary.

Advanced Topics

To apply svn:needs-lock property to all files of some type in the working copy (e.g. "*.resx") you can open "Show Changes" window, select "Show unmodified files" option at the bottom, right-click on the list view header and select "Extension" column to be displayed, sort by this column, select all files with specific extension, then click "Properties..." in context menu and set property for all selected files simultaneously.

To have svn:needs-lock property applied automatically to all newly added files you can use the auto-props configuration section. Please note that this option works with Subversion client and should be configured on each client. You can also consider configuring Subversion hook for the repository on server to check that svn:needs-lock property has been applied correctly.

Last Modified: