python 如何以编程方式获取 SVN 修订号?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1499811/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to programmatically get SVN revision number?
提问by mpen
Like this question, but without the need to actually query the SVN server. This is a web-based project, so I figure I'll just use the repository as the public view (unless someone can advise me why this is a bad idea). I assume this info is in a file in the .svn
folder somewhere, but where, and how do I parse it? I want to do something like what SO does, having the revision in the bottom-right corner.
喜欢这个问题,但不需要实际查询SVN服务器。这是一个基于 Web 的项目,所以我想我只会使用存储库作为公共视图(除非有人可以告诉我为什么这是一个坏主意)。我假设此信息位于文件.svn
夹中某处的文件中,但是在哪里以及如何解析它?我想做一些类似于 SO 所做的事情,在右下角进行修订。
Code in Python or PHP appreciated if you have it on hand, or just a point in the right direction.
如果您手头有 Python 或 PHP 代码,或者只是在正确方向上的一点,那么您将不胜感激。
回答by Wim Coenen
Subversion includes the svnversiontool for exactly this purpose. A working copy may actually have local modifications, or may consist of a mix of revisions. svnversion
knows how to handle that; see the examples in the linked documentation.
Subversion 包含svnversion工具正是为了这个目的。工作副本实际上可能有本地修改,或者可能包含修订的混合。svnversion
知道如何处理;请参阅链接文档中的示例。
You can invoke svnversion
from python like this:
您可以svnversion
像这样从 python调用:
import subprocess
def svnversion():
p = subprocess.Popen("svnversion", stdout=subprocess.PIPE, stderr=subprocess.PIPE)
(stdout, stderr) = p.communicate()
return stdout
回答by inkedmn
I would probably write out the version number to a text file somewhere within the website using the post-commit hook and then just read that file when loading your page.
我可能会使用 post-commit 钩子将版本号写到网站内某处的文本文件中,然后在加载页面时读取该文件。
回答by Pascal MARTIN
You can get the current revision number of a checkout using, on the command line, "svn info
".
您可以在命令行中使用“ svn info
”来获取结帐的当前修订号。
For instance :
例如 :
$ svn info
Chemin : .
URL : http://.../trunk
Racine du dép?t : http://...
UUID du dép?t : 128b9c1a-...-612a326c9977
Révision : 185
Type de n?ud : réperttheitroade
Tache programmée : normale
Auteur de la dernière modification : ...
Révision de la dernière modification : 185
Date de la dernière modification: 2009-09-28 20:12:29 +0200 (lun. 28 sept. 2009)
Note it's localized ; if you're on Linux, you could try using :
注意它是本地化的;如果你在 Linux 上,你可以尝试使用:
$ LANG=en svn info
svn: warning: cannot set LC_CTYPE locale
svn: warning: environment variable LANG is en
svn: warning: please check that your locale name is correct
Path: .
URL: http://.../trunk
Repository Root: http://...
Repository UUID: 128b9c1a-...-612a326c9977
Revision: 185
Node Kind: directory
Schedule: normal
Last Changed Author: mzeis
Last Changed Rev: 185
Last Changed Date: 2009-09-28 20:12:29 +0200 (Mon, 28 Sep 2009)
If using this from PHP, though, getting it as XML might be more helpful (easier to parse, and not locale-aware):
但是,如果从 PHP 使用它,将它作为 XML 获取可能会更有帮助(更容易解析,而不是区域设置):
$ svn info --xml
<?xml version="1.0"?>
<info>
<entry
kind="dir"
path="."
revision="185">
<url>http://.../trunk</url>
<repository>
<root>http://...</root>
<uuid>128b9c1a-...-612a326c9977</uuid>
</repository>
<wc-info>
<schedule>normal</schedule>
<depth>infinity</depth>
</wc-info>
<commit
revision="185">
<author>...</author>
<date>2009-09-28T18:12:29.130307Z</date>
</commit>
</entry>
</info>
Just use simplexml_load_string
on that, and fetch the revision
attribute of the entry
tag.
只需使用simplexml_load_string
它,并获取标签的revision
属性entry
。
Note that I would not do that on each page view : not quite as fast as one would hope for your application.
请注意,我不会在每个页面视图上都这样做:不像您希望的应用程序那么快。
Instead, I would get that revision number when creating the archive I will, later, send to the production server -- and store it in some kind of configuration file.
相反,我会在创建存档时获得该修订号,稍后我将发送到生产服务器 - 并将其存储在某种配置文件中。
This way, you don't need to use the svn
command on your production server, and you don't need to do your checkout on that server either.
这样,您无需svn
在生产服务器上使用该命令,也无需在该服务器上进行结帐。
回答by Lance Rushing
This works for svn version 1.6.2. But be warned, different SVN versions use different formats of the .svn folder.
这适用于 svn 1.6.2 版。但请注意,不同的 SVN 版本使用不同格式的 .svn 文件夹。
<?php
$contents = file_get_contents(dirname(__FILE__) . '/.svn/entries');
preg_match("/dir\n(.+)/", $contents, $matches);
$svnRevision = $matches[1];
print "This Directory's SVN Revsion = $svnRevision\n";
回答by Ewan Todd
Following Lance, here's a command line one-liner.
在 Lance 之后,这是一个命令行单行程序。
svn log -q --limit 1 http://svnbox/path/to/repo/trunk \
| php -r 'fgets(STDIN); preg_match("/(\d+)/", fgets(STDIN), $m); echo $m[0];'
回答by Annika Backstrom
I like the solution used by MediaWiki, reading the contents of .svn/entities
. Check the first answer on this question: Getting the subversion repository number into code
我喜欢 MediaWiki 使用的解决方案,阅读.svn/entities
. 检查这个问题的第一个答案:Getting the subversion repository number into code
回答by Rich Seller
If you're looking for a Python implementation, check out pysvn, you can invoke the info commandto get details including the current revision:
如果您正在寻找 Python 实现,请查看pysvn,您可以调用info 命令以获取包括当前修订在内的详细信息:
entry = info( path )
回答by Jason Antman
I know this is pretty old, but I thought I'd offer up how I do this in PHP for my web apps:
我知道这已经很老了,但我想我会提供如何在 PHP 中为我的网络应用程序执行此操作:
Put this in a PHP file in your app, and set the SVN Keywords property for "Date Revision Author HeadURL Id" (keyword substitution in the file).
将其放入应用程序的 PHP 文件中,并为“Date Revision Author HeadURL Id”(文件中的关键字替换)设置 SVN 关键字属性。
$SVN_rev = "$LastChangedRevision$";
$SVN_headURL = "$HeadURL$";
function stripSVNstuff($s)
{
$s = substr($s, strpos($s, ":")+1);
$s = str_replace("$", "", $s);
return trim($s);
}
So after svn does keyword replacement on the source file, the $SVN_rev line will look like:
因此,在 svn 对源文件进行关键字替换后,$SVN_rev 行将如下所示:
$SVN_rev = "$LastChangedRevision: 6 $";
so the output of stripSVNstuff($SVN_rev) would be "6" (without quotes).
所以 stripSVNstuff($SVN_rev) 的输出将是“6”(不带引号)。
回答by PilotBob
You could just shell out to the command line and run svn info. Just keep in mind this could be a mixed revision. You could also run an svn up and the last line of STDOUT will give you the revision number.
您可以直接使用命令行并运行 svn info。请记住,这可能是一个混合修订。你也可以运行一个 svn up 并且 STDOUT 的最后一行会给你修订号。
Or, are you wanting to use the svn API? You might also want to look at the TortoiseSVN code from SubWCRev which is used to replace a token in a file with the current revisions of the WC.
或者,您想使用 svn API?您可能还想查看 SubWCRev 中的 TortoiseSVN 代码,该代码用于将文件中的标记替换为 WC 的当前修订版。