3

I've read too many articles talking about subversion keywords and many other details like anchors and anchor signs, but none of them are talking c++.

I am using QT creator and I would like c++ to read the svn-version and pass it through my code as a value or string (or whatever data-type).

So, how can I do that?

jsumrall
  • 111
  • 7
McLan
  • 2,552
  • 9
  • 51
  • 85
  • do you mean char *my_rev = $Revision$; – user1810087 Apr 04 '13 at 15:52
  • You can add the svn prop to the files. And then have the id, author... somewhere at the top of each file. svn will update them properly. Then open the file with C++ and read that. –  Apr 04 '13 at 15:53
  • Do you need to work with svn, or you just want to show you svn version in you program's help? – Amartel Apr 04 '13 at 15:54
  • @itwasntpete: thanks for your reply. I create a file with the svn prop as below: `QFile file("Data/svnversion"); file.open(QIODevice::ReadWrite); file.write("$LastChangedDate$\n$svn propset svn:keywords\"Date Author\" svnversion.txt\n$Rev$"); file.close();` but it is not working. it creates the files with these props as strings and doesn't substitute them with any information!! am I missing something ? – McLan Apr 05 '13 at 10:36
  • for better understanding like @Amartel asked: Do you need to work with svn, or you just want to show you svn version in you program's help? **the substitution is done on commit your file into svn**. why are you writing `svn propset svn:keywords\"Date Author\"` into the file? this is the terminal command to set up the properties to your file(s). if you give that file (snippet above) the svn props (e.g. with **svn propset svn:keywords "Date Author Id Revision" [file.??]** in terminal on *x os) and commit it to your svn, the keywords will be replaced. – user1810087 Apr 05 '13 at 11:10
  • @itwasntpete: I just want to see the svn version in my program's help. I wrote that line to set my property and I thought I can do the setting during my execution of the code. but even without anything that comes after the $ is written as string – McLan Apr 05 '13 at 11:38
  • how did you set the properties of the file? what svn client do you use? how do you commit/ checkout your repository? – user1810087 Apr 05 '13 at 14:57

3 Answers3

2

The text from the SVNBook is probably relevant here:

New users are often confused by how the $Rev$ keyword works. Since the repository has a single, globally increasing revision number, many people assume that it is this number that is reflected by the $Rev$ keyword's value. But $Rev$ expands to show the last revision in which the file changed, not the last revision to which it was updated. Understanding this clears the confusion, but frustration often remains—without the support of a Subversion keyword to do so, how can you automatically get the global revision number into your files?

To do this, you need external processing. Subversion ships with a tool called svnversion, which was designed for just this purpose. It crawls your working copy and generates as output the revision(s) it finds. You can use this program, plus some additional tooling, to embed that revision information into your files. For more information on svnversion, see the section called “svnversion”.

In my project we ended up not using svnversion, primarily because our source tree is huge and this command seemed to take an inordinate amount of time. But we did end up with something similar to get the revision number into our software. The configure script executes the following command

svn info . > svn_revision

This assumes the command is executed from a directory where that was checked out from the svn repository. To parse this data, I have a Makefile that greps various strings into constants

SVN_REV = $(shell cat svn_revision | grep "Last Changed Rev" | cut -f4 -d\ )                                              

This is fed through a template file I call version.cc.template, there's a Makefile rule to turn it into a .cc file

version.cc: version.cc.template /svn_revision
        sed -e "s/REVISION/$(SVN_REV)/" version.cc.template > $@                                     

so I end up with something like this

version.h

namespace project {

extern const char* Revision;
extern const unsigned int SvnRevision;

}

version.cc

namespace project {

const char* Revision = "1234";
const unsigned int SvnRevision = 1234;

}

The version.cc file may not be needed in your case, the constants can be defined in the header as well.

Community
  • 1
  • 1
Sam Miller
  • 23,808
  • 4
  • 67
  • 87
2

How to get the output of system() command qt c++?

Like the accepted answer says, you can run the command through the system terminal using QProcess, wait for it to finish and then get the text of the command.

Community
  • 1
  • 1
phyatt
  • 18,472
  • 5
  • 61
  • 80
  • thanks for your reply. Actually I am now able to write the `svn --version` commnad to a file. however, i dont need this command and I need `svn info` command which write the string "exported" only into the .txt file. the problem now is that I can't state the working copy where the `svn info` command can be excuted – McLan Apr 05 '13 at 13:46
  • http://qt-project.org/doc/qt-4.8/qprocess.html#setWorkingDirectory Use this function to set the directory where you want to run the QProcess commands. Then your `svn info` should work. – phyatt Apr 05 '13 at 16:31
0

Thanks all for your help. Actually I found another way of doing it. please refer to this question:

how to pass a Command-line to a QMake #DEFINE

Community
  • 1
  • 1
McLan
  • 2,552
  • 9
  • 51
  • 85