检验历史

Your Subversion repository is like a time machine. It keeps a record of every change ever committed and allows you to explore this history by examining previous versions of files and directories as well as the metadata that accompanies them. With a single Subversion command, you can check out the repository (or restore an existing working copy) exactly as it was at any date or revision number in the past. However, sometimes you just want to peer into the past instead of going into it.

有许多命令可以为你提供版本库历史:

svn log

Shows you broad information: log messages with date and author information attached to revisions and which paths changed in each revision.

svn diff

显示特定修改的行级详细信息。

svn cat

Retrieves a file as it existed in a particular revision number and displays it on your screen.

svn list

显示一个目录在某一版本存在的文件。

Generating a List of Historical Changes

To find information about the history of a file or directory, use the svn log command. svn log will provide you with a record of who made changes to a file or directory, at what revision it changed, the time and date of that revision, and—if it was provided—the log message that accompanied the commit.

$ svn log
------------------------------------------------------------------------
r3 | sally | Mon, 15 Jul 2002 18:03:46 -0500 | 1 line

Added include lines and corrected # of cheese slices.
------------------------------------------------------------------------
r2 | harry | Mon, 15 Jul 2002 17:47:57 -0500 | 1 line

Added main() methods.
------------------------------------------------------------------------
r1 | sally | Mon, 15 Jul 2002 17:40:08 -0500 | 1 line

Initial import
------------------------------------------------------------------------

Note that the log messages are printed in reverse chronological order by default. If you wish to see a different range of revisions in a particular order or just a single revision, pass the --revision (-r) option:

$ svn log -r 5:19    # shows logs 5 through 19 in chronological order

$ svn log -r 19:5    # shows logs 5 through 19 in reverse order

$ svn log -r 8       # shows log for revision 8

你也可以检查单个文件或目录的日志历史,举个例子:

$ svn log foo.c
…
$ svn log http://foo.com/svn/trunk/code/foo.c
…

这样只会显示这个工作文件(或者URL)做过修订的版本的日志信息。

If you want even more information about a file or directory, svn log also takes a --verbose (-v) option. Because Subversion allows you to move and copy files and directories, it is important to be able to track path changes in the filesystem. So, in verbose mode, svn log will include a list of changed paths in a revision in its output:

$ svn log -r 8 -v
------------------------------------------------------------------------
r8 | sally | 2002-07-14 08:15:29 -0500 | 1 line
Changed paths:
   M /trunk/code/foo.c
   M /trunk/code/bar.h
   A /trunk/code/doc/README

Frozzled the sub-space winch.

------------------------------------------------------------------------

svn log也有一个--quiet (-q)选项,会禁止日志信息的主要部分,当与--verbose结合使用,仅会显示修改的文件名。

Examining the Details of Historical Changes

我们已经看过svn diff—使用标准区别文件格式显示区别,它在提交前用来显示本地工作拷贝与版本库的区别。

事实上,svn diff种不同的用法:

  • 比较本地修改

  • 比较工作拷贝与版本库

  • 比较版本库与版本库

检查本地修改

像我们看到的,不使用任何参数调用时,svn diff将会比较你的工作文件与缓存在.svn的“原始”拷贝:

$ svn diff
Index: rules.txt
===================================================================
--- rules.txt	(revision 3)
+++ rules.txt	(working copy)
@@ -1,4 +1,5 @@
 Be kind to others
 Freedom = Responsibility
 Everything in moderation
-Chew with your mouth open
+Chew with your mouth closed
+Listen when others are speaking
$

Comparing working copy to repository

如果传递一个--revision(-r)参数,你的工作拷贝会与指定的版本比较。

$ svn diff -r 3 rules.txt
Index: rules.txt
===================================================================
--- rules.txt	(revision 3)
+++ rules.txt	(working copy)
@@ -1,4 +1,5 @@
 Be kind to others
 Freedom = Responsibility
 Everything in moderation
-Chew with your mouth open
+Chew with your mouth closed
+Listen when others are speaking
$

比较版本库与版本库

If two revision numbers, separated by a colon, are passed via --revision (-r), then the two revisions are directly compared:

$ svn diff -r 2:3 rules.txt
Index: rules.txt
===================================================================
--- rules.txt	(revision 2)
+++ rules.txt	(revision 3)
@@ -1,4 +1,4 @@
 Be kind to others
-Freedom = Chocolate Ice Cream
+Freedom = Responsibility
 Everything in moderation
 Chew with your mouth open
$

A more convenient way of comparing a revision to the previous revision is to use the --change (-c) option:

$ svn diff -c 3 rules.txt
Index: rules.txt
===================================================================
--- rules.txt	(revision 2)
+++ rules.txt	(revision 3)
@@ -1,4 +1,4 @@
 Be kind to others
-Freedom = Chocolate Ice Cream
+Freedom = Responsibility
 Everything in moderation
 Chew with your mouth open
$

最后,即使你在本机没有工作拷贝,还是可以比较版本库的修订版本,只需要在命令行中输入合适的URL:

$ svn diff -c 5 http://svn.example.com/repos/example/trunk/text/rules.txt
…
$

Browsing the Repository

通过svn catsvn list,你可以在未修改工作修订版本的情况下查看文件和目录的内容,实际上,你甚至也不需要有一个工作拷贝。

svn cat

如果你只是希望检查一个过去的版本而不希望察看它们的区别,使用svn cat

$ svn cat -r 2 rules.txt
Be kind to others
Freedom = Chocolate Ice Cream
Everything in moderation
Chew with your mouth open
$

你可以重定向输出到一个文件:

$ svn cat -r 2 rules.txt > rules.txt.v2
$

svn list

svn list可以在不下载文件到本地目录的情况下来察看目录中的文件:

$ svn list http://svn.collab.net/repos/svn
README
branches/
clients/
tags/
trunk/

If you want a more detailed listing, pass the --verbose (-v) flag to get output like this:

$ svn list -v http://svn.collab.net/repos/svn
  20620 harry            1084 Jul 13  2006 README
  23339 harry                 Feb 04 01:40 branches/
  21282 sally                 Aug 27 09:41 developer-resources/
  23198 harry                 Jan 23 17:17 tags/
  23351 sally                 Feb 05 13:26 trunk/

这些列告诉你文件和目录最后修改的修订版本、做出修改的用户、如果是文件还会有文件的大小,最后是修改日期和项目的名字。

警告

The svn list with no arguments defaults to the repository URL of the current working directory, not the local working copy directory. After all, if you want a listing of your local directory, you could use just plain ls (or any reasonable non-Unixy equivalent).

Fetching Older Repository Snapshots

In addition to all of the previous commands, you can use svn update and svn checkout with the --revision option to take an entire working copy “back in time”: [7]

$ svn checkout -r 1729 # Checks out a new working copy at r1729
…
$ svn update -r 1729 # Updates an existing working copy to r1729
…

提示

Many Subversion newcomers attempt to use the preceding svn update example to “undo” committed changes, but this won't work as you can't commit changes that you obtain from backdating a working copy if the changed files have newer revisions. See “找回删除的项目”一节 for a description of how to “undo” a commit.

最后,如果你构建了一个版本,并且希望从Subversion打包文件,但是你不希望有讨厌的.svn目录,这时你可以导出版本库的一部分文件而没有.svn目录。就像svn updatesvn checkout,你也可以传递--revision选项给svn export

$ svn export http://svn.example.com/svn/repos1 # Exports latest revision
…
$ svn export http://svn.example.com/svn/repos1 -r 1729
# Exports revision r1729
…


[7] 看到了吧?我们说过Subversion是一个时间机器。