如何识别 SVN diff 中的项目是文件还是文件夹?
我正在编写一个提交后挂钩,其中使用 svn diff -summarize 来获取更改内容的摘要。添加或修改文件/文件夹时,我可以简单地在工作副本中获取它们的文件类型,该副本始终自动更新。但是当文件或文件夹被删除时,我无法确定 svn diff -summarize 中的项目是文件还是文件夹。
我想到的一个解决方法是保留另一个工作副本,自动更新,但始终将一个修订保留在另一个工作副本后面。这样,如果文件/文件夹被删除,我可以在旧的工作副本中获取它。然而,我认为这是一种非常低效的方法,迫使我同时保留两个不同的工作副本,我想知道 SVN 中是否有任何东西可以帮助完成这项任务。
I'm writing a post-commit hook, in which I use svn diff -summarize to get a summary of what was changed. When files/folders are added or modified, I can simply get their file type in a working copy that is kept updated at all times automatically. But when a file or folder is deleted, I have no way of finding out whether an item in the svn diff -summarize was a file or a folder.
One workaround I've thought for this is to keep another working copy, updated automatically, but always kept one revision behind the other working copy. That way, if a file/folder was deleted, I could get it in the older working copy. However, I think that this is an extraordinarily inefficient way of doing this, forcing me to keep two different working copies at the same time, and I was wondering whether there is anything in SVN that'd help with this task.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在编写任何类型的 svn 挂钩时,如果您需要有关存储库或事务的信息,您需要使用 shell 脚本中的 svnlook 程序,或使用您选择的语言的 svn API 。使用 svnlook 速度更快,因为它绕过了所有 svn RA(远程访问)代码,包括授权检查层 - 因为脚本是在服务器的上下文中运行,这不是一个问题。此外,您不会因为更新不用于文件类型信息以外的任何其他用途的工作副本而产生额外开销。
svnlook
包含许多用于调查修订(对于 post-* 挂钩)和事务(对于 pre-* 事务)的选项。这里特别有趣的是changed
选项。显示构成修订或事务的更改,如下所示:
svnlook 的输出在引用目录时始终以 / 结尾。这是用于检测文件或目录的简单鉴别器。
然而,在处理删除时,事情确实变得有点模糊。如果删除某个目录,该目录下的所有内容也会被删除,并且此信息不会显示在
changed
中。请参阅此处:根据您所查看内容的描述,我猜测您正在寻找某种形式的提交后报告。如果是这样,您可能需要查看 subversion 邮件程序后*钩子。即使您不想通过电子邮件发送结果,该代码也演示了如何使用 svn API 遍历事务或修订以进行更改。
When writing svn hooks of any type, if you need information about the repository or about a transaction, you want to use the
svnlook
program from a shell script, or use the svn API in the language of your choice. Usingsvnlook
is faster, as it bypasses all the svn RA (remote access) code, including the layer of authorization checks - because the scripts are being run in the context of the server, this isn't a problem. Additionally, you don't have the additional overhead of updating a working copy that you aren't using for, well, anything other than file type information.svnlook
contains numerous options for investigating both revisions (for post-* hooks) and transactions (for pre-* transactions). Of particular interest here is thechanged
option.The changes that made up the revision or transaction are displayed, like so:
Output from svnlook, when referencing a directory, always ends in /. This is an easy discriminator for detection of file or directory.
However, things do get a little murkier when dealing with deletes. If a directory is deleted, everything under the directory is also removed and this information isn't displayed in
changed
. See here:Based on the description of what you are looking at, I'm going to guess that you are looking for some form of post-commit reporting. If so, you may want to check out the subversion mailer post-* hook. Even if you don't want to email results, the code demonstrates how to walk through a transaction or revision for changes using the svn API.