Include SVN revision number in source code

My requirement is simple. At the beginning of each file there should be a block comment like this:

 * This file was last modified by {username} at {date} and has revision number {revisionnumber}

I want to populate the {username}, {date} and {revisionnumber} with the appropriate content from SVN.

  • “aapt” IOException error=2, No such file or directory" why can't I build my gradle on jenkins?
  • Eclipse with EGit, git-blame info is hard to see
  • Eclipse SVN Synchronze workspace error - Updating Synchronize view for SVN Workspace - java/nio/file/Paths
  • obtain current svn revision in webapp
  • How can I generate an API version from source?
  • Couldn't find any revision to build. Verify the repository and branch configuration for this job. Finished: FAILURE
  • How can I achieve this with NetBeans and Subversion? I have searched a lot but I can’t find exactly what I need.

  • How to get the current revision number into a file after export?
  • SVN: How do I maintain my local config.blah file?
  • How do you make Git work with IntelliJ?
  • Compare files between two branches in TortoiseSVN
  • SVN and revision numbers
  • Better Merge Tool for Subversion
  • 5 Solutions collect form web for “Include SVN revision number in source code”

    I looked at this question and got some useful information. It is not exactly duplicate because I am working with NetBeans but the idea is the same. This is my header:

     * $LastChangedDate$
     * $LastChangedRevision$

    Then I go to Team > Subversion > Svn properties and add svn:keywords as property name and LastChangedDate LastChangedRevision as property value.

    And when I commit from NetBeans it looks like this:

     * $LastChangedDate: 2012-02-13 17:38:57 +0200 (Пн, 13 II 2012) $
     * $LastChangedRevision: 27 $

    Thanks all for the support! I will accept my answer because other answers do not include the NetBeans information. Nevertheless I give +1 to the other answers.

    As this data only exists after the file was committed it should be set by SVN itself, not a client program. (And client-side processing tends to get disabled or not configured at all.) This means there is no simple template/substitute like you want, because then after the first replacement the template variables would be lost.

    You can find information abut SVN’s keyword substitution here. Then things like $Rev$ can be replaced by $Rev: 12 $.

    You can do this with The SubWCRev Program.

    SubWCRev is Windows console program which can be used to read the
    status of a Subversion working copy and optionally perform keyword
    substitution in a template file. This is often used as part of the
    build process as a means of incorporating working copy information
    into the object you are building. Typically it might be used to
    include the revision number in an “About” box.

    This is typically done during the build process.

    If you use Linux, you can find a Linux binary here. If you wish, you could also write your own using the output of svn log.

    I followed Petar Minchev’s suggestions, only I put the $LastChangedRevision$ tag not in a comment block but embedded it in a string. Now it is available to programmatically display the revision number in a Help -> About dialog.

    String build = "$LastChangedRevision$";

    I can later display the revision value in the about dialog using a String that has all of the fluff trimmed off.

    String version = build.replace("$LastChangedRevision:", "").replace("$", "").trim();

    I recommend a slightly different approach.

    Put the following header at the top of your source files.

     * This file was last modified by {username} at {date} and has revision number {revisionnumber}

    Then add a shell script like this

    post update, checkout script

    USERNAME=# // use svnversion to get username
    DATE=# // use svnversion to get revisio nnumber
    sed -e "s#{username}#${USERNAME}#" -e "s#{date}#${DATE}#" ${SOURCE_CONTROL_FILE} > ${SOURCE_FILE}

    pre commit script

    cat standard_header.txt > ${SOURCE_CONTROL_FILE}
    tail --lines $((${LENGTH}-4)) ${SOURCE_FILE} >> ${SOURCE_CONTROL_FILE}
    Git Baby is a git and github fan, let's start git clone.