一、背景:

使用svn进行项目代码管理过程中,我们希望,在日志里能够得到当前固件对应源代码的svn版本号,这样就可以快速的定位到是哪个svn的版本出现问题。我们可以使用TortoiseSVN的SubWCRev.exe这个应用软件来实现这个需求。

 

二、SubWCRev介绍

SubWCRev是TortoiseSVN软件中自带的一个小的应用程序。可以在安装文件夹中找到:

SubWCRev可以读取工作目录下所有文件的Subversion状态,默认情况下不包括外部文件。它记录的状态包含以下几种:

  • 记录最高提交修订号,以及该修订的提交时间戳;
  • 记录是否有本地修改,或者混合更新修订。

1、SubWCRev命令的格式

SubWCRev WorkingCopyPath [SrcVersionFile DstVersionFile] [-nmdfe]

WorkingCopyPath :要检查的工作副本路径(说白了就是从svn服务器上check out到本地的文件夹)。只能对工作副本使用SubWCRev,而不是直接对服务器上版本库,这个路径可以是绝对路径,也可以是工作目录的相对路径。

SrcVersionFile:模版文件。(看后续分析)

DstVersionFile:输出文件,就是模板文件替换之后的版本。

-nmdfe:可选的命令,总结如表:

切换描述
-nIf this switch is given, SubWCRev will exit with ERRORLEVEL 7 if the working copy contains local modifications. This may be used to prevent building with uncommitted changes present.
-NIf this switch is given, SubWCRev will exit with ERRORLEVEL 11 if the working copy contains unversioned items that are not ignored.
-mIf this switch is given, SubWCRev will exit with ERRORLEVEL 8 if the working copy contains mixed revisions. This may be used to prevent building with a partially updated working copy.
-dIf this switch is given, SubWCRev will exit with ERRORLEVEL 9 if the destination file already exists.
-f如果给出这个开关,SubWCRev 就会包含文件夹的最后修改版本。默认行为是取得版本号时只考虑文件。
-eIf this switch is given, SubWCRev will examine directories which are included with svn:externals, but only if they are from the same repository. The default behaviour is to ignore externals.
-EIf this switch is given, same as -e, but it ignores the externals with explicit revisions, when the revision range inside of them is only the given explicit revision in the properties. So it doesn't lead to mixed revisions.
-x如果给出这个开关,SubWCRev 就会以十六进制输出修订版本号。
-X如果给出这个开关,SubWCRev 就会以十六进制输出修订版本号,并且加上 '0X' 前缀。
-FIf this switch is given, SubWCRev will ignore any .subwcrevignore files and include all files.
-qIf this switch is given, SubWCRev will perform the keyword substitution without showing working copy status on stdout.

2、使用示例:最简单的命令

直接在当前目录下运行SubWCRev(别忘了提前把SubWCRev.exe的路径加到path环境变量中),可以在控制台输出相关信息:The revision number, update revision range and modification status are displayed on stdout.

SubWCRev .

如果没有错误,SubWCRev返回0,并将相关信息打印到控制台。 但是如果发生错误,错误信息将写入stderr并显示在控制台中。 返回的错误代码是:

错误代码描述
1语法错误。 一个或多个命令行参数无效。
2未找到命令行上指定的文件或文件夹。
3输入文件无法打开,或目标文件无法创建。
4无法分配内存。 发生的可能原因如源文件太大等。
5源文件无法正确扫描。
6SVN错误:当SubWCRev尝试从工作副本中查找信息时,Subversion返回了一个错误。
7工作副本包含本地修改。需要-n切换。
8工作副本包含混合的版本。需要-m切换。
9输出文件已存在,需要-d切换。
10指定的路径不是工作副本或部分或其中之一。
11此工作副本中有未版本控制的文件或文件夹。这需要 -N 切换。

 

3、使用示例:使用模板文件

在工作副本根目录下新建一个模板文件,命令为template.h,内容如下:

#define Revision       "$WCREV$"
#define Date           "$WCDATE$"
#define CustDate       "$WCDATE=%a, %d %B %Y$"
#define DateUTC        "$WCDATEUTC$"
#define CustDateUTC    "$WCDATEUTC=%a, %d %B %Y$"
#define TimeNow        "$WCNOW$"
#define TimeNowUTC     "$WCNOWUTC$"
#define RevRange       "$WCRANGE$"

输入命令:SubWCRev ./ template.h version.h

 会自动生成一个version.h文件,文件内容如下:

#define Revision       "74020"
#define Date           "2021/01/22 18:56:44"
#define CustDate       "Fri, 22 January 2021"
#define DateUTC        "2021/01/22 10:56:44"
#define CustDateUTC    "Fri, 22 January 2021"
#define TimeNow        "2021/01/27 16:51:08"
#define TimeNowUTC     "2021/01/27 08:51:08"
#define RevRange       "74020"

可以看出,version.h文件是根据模板生成的,只不过把一些关键字进行了替换,关键字的种类及其使用方法可以参考官网的说明。

关键字描述
$WCREV$用工作副本中最高的提交版本来替换
$WCREV&$用工作副本中的最高提交修订版本号替换,ANDed with the value after the & char. 例如:$WCREV&0xFFFF$
$WCREV-$, $WCREV+$Replaced with the highest commit revision in the working copy, with the value after the + or - char added or subtracted. For example: $WCREV-1000$
$WCDATE$, $WCDATEUTC$Replaced with the commit date/time of the highest commit revision. By default, international format is used: yyyy-mm-dd hh:mm:ss. Alternatively, you can specify a custom format which will be used with strftime(), for example: $WCDATE=%a %b %d %I:%M:%S %p$. For a list of available formatting characters, look at the 在线引用.
$WCNOW$, $WCNOWUTC$Replaced with the current system date/time. This can be used to indicate the build time. Time formatting can be used as described for $WCDATE$.
$WCRANGE$Replaced with the update revision range in the working copy. If the working copy is in a consistent state, this will be a single revision. If the working copy contains mixed revisions, either due to being out of date, or due to a deliberate update-to-revision, then the range will be shown in the form 100:200.
$WCMIXED$当有混合版本时用 TText 替换 $WCMIXED?TText:FText$,否则用 FText 替换。
$WCMODS$若本地存在修改,就用 TText 替换 $WCMODS?TText:FText$,否则用 FText 替换。
$WCUNVER$$WCUNVER?TText:FText$ is replaced with TText if there are unversioned items in the working copy, or FText if not.
$WCEXTALLFIXED$$WCEXTALLFIXED?TText:FText$ is replaced with TText if all externals are fixed to an explicit revision, or FText if not.
$WCISTAGGED$$WCISTAGGED?TText:FText$ is replaced with TText if the repository URL contains the tags classification pattern, or FText if not.
$WCURL$用传递给SubWCRev的工作目录的版本库地址替换。
$WCINSVN$$WCINSVN?TText:FText$ is replaced with TText if the entry is versioned, or FText if not.
$WCNEEDSLOCK$若入口有svn:needs-lock属性设定,就用 TText 替换 $WCNEEDSLOCK?TText:FText$,否则用FText 替换。
$WCISLOCKED$若入口被锁定,就用 TText 替换 $WCISLOCKED?TText:FText$,否则用 FText 替换。
$WCLOCKDATE$, $WCLOCKDATEUTC$Replaced with the lock date. Time formatting can be used as described for $WCDATE$.
$WCLOCKOWNER$用锁定所有者的名字来替换
$WCLOCKCOMMENT$用锁定的备注来替换
$WCUNVER$$WCUNVER?TText:FText$ is replaced with TText if there are unversioned files or folders in the working copy, or FText if not.

ref:

https://tortoisesvn.net/docs/nightly/TortoiseSVN_zh_CN/tsvn-subwcrev.html

Logo

瓜分20万奖金 获得内推名额 丰厚实物奖励 易参与易上手

更多推荐