Automatically adding keywords to files in TortoiseSVN under Windows

    In Subversion, there is a feature for automatically substituting embedded keywords. This feature allows you to add to the file, for example, information such as the last user who edited the file, revision, and modification date.
    This functionality is currently very limited, but can nevertheless be very useful. One of the main limitations is the need to add keyword processing for each new file. The same applies to renamed and moved files (SVN treats them as new).

    Substitute Keywords [1]


    According to the manual, there are the following auto-substitutions:
    Date
    Last modification date. $ Date $ is replaced with something like
    $ Date: 2006-07-22 21:42:37 -0700 (Sat, 22 Jul 2006) $
    Revision
    latest revision with modification
    Author
    author of recent changes
    Headurl
    path in the repository to the latest version of the file
    Id
    a combination of the previous information is obtained in a format like:
    $ Id: calc.c 148 2006-07-28 21: 30: 43Z sally $
    where calc.c is the file name, 158 is the revision, after the date and time, and then the author

    In one of the current projects in which I participate, it was decided to use these auto-substitutions in the headers of SQL scripts. In particular, this will make it possible to version files and view installed versions on a production server.
    Initially adding keywords to files was very simple. In TortoiseSVN 1.7, this process has become even simpler than in the previous ones [2] . After that, a problem arose - developers did not always add versions to new files. The same thing happened after changing the structure of the project, when most of the files were organized by functionality.

    Limitations


    The company's security policies limited developers' access to the server side of the repository, so there was no possibility to use server hooks.
    Some developers have a limited set of software installed, which they do not intend to expand without substantial arguments. For example, some do not want to migrate from TSVN 1.6.

    Decision


    As the most painless solution, it was decided to use an automatic script that will add these words to new and modified files.
    Since not everyone has PowerShell installed, we will use BAT. And for obtaining information about the repository and adding keywords - Subversion Command-line Client (for example, it is installed with TSVN).
    The result is the following script:
    @echo off
    title Update tags
    
    rem %CD% - current directory
    :startrem for %%i in (*.tmp) do del %%idel *.tmp > nul
    svn status "%CD%\Sql" > svn_chg.tmp
    set CNT=0
    :repeat
    set line=""
    for /f "eol= tokens=2 delims= " %%iin (svn_chg.tmp) doset line=%%iif "%line%"=="" goto ender
    if "%line%"=="""" goto ender
    SET /A CNT=%CNT%+1echo%CNT%: %line%
    svn propset svn:keywords "Author Date Revision" "%line%">nultype svn_chg.tmp| find /v "%line%"> svn_chg1.tmp
    copy /Y svn_chg1.tmp svn_chg.tmp >nulgoto repeat
    :ender
    del *.tmp > nulecho Press <Enter> to exit...
    pause

    Functionality Used:


    %CD%current directory
    for %%i in (*.tmp) do del %%idelete * .tmp files in a loop
    svn status "%CD%\Sql" > svn_chg.tmpreading the list of modified / new files in the Sql directory to the svn_chg.tmp file
    for /f "eol= tokens=2 delims= " %%i in (svn_chg.tmp) do set line=%%igetting a substring with the path to the file (take the last line)

    Thus, developers have the opportunity to gradually add keyword support to their own files.

    Alternative solutions


    This script can be used in the client hook on Pre-Commit. Also, for a hook, you can modify this script so that there is no “svn status" [3] .
    As an extension of keywords, there is SubWCRev [4] . A solution using this utility was not provided initially, because the use of scripts was not supposed, they wanted everything out of the box. Perhaps there will be a migration later.
    There is still the opportunity to configure your scripts on a build server (we have TeamCity), which is also able to add changes to the repository.
    1. svnbook.red-bean.com/en/1.5/svn.advanced.props.special.keywords.html
    2. tortoisesvn.net/docs/release/TortoiseSVN_en/tsvn-dug-propertypage.html#id597790
    3. tortoisesvn.net/docs/release/TortoiseSVN_en/tsvn-dug-settings.html#tsvn-dug-settings-hooks
    4. tortoisesvn.net/docs/release/TortoiseSVN_en/tsvn-subwcrev.html

    Also popular now: