Wiki Tools

  • Find Page
  • Recent Changes
  • Page History
  • Attachments

Differences between revisions 10 and 11
Revision 10 as of 2006-06-03 14:10:58
Size: 19116
Editor: Leal
Revision 11 as of 2006-06-03 14:20:01
Size: 19129
Editor: Leal
Deletions are marked like this. Additions are marked like this.
Line 1: Line 1:
## page was renamed from zh/IntroductionToBzr
## page was renamed from zh/介绍
#language zh
Line 5: Line 3:
#language zh
#pragma section-numbers off
~- (!) 参考自: {en} ["IntroductionToBzr"]-~


branch - 分支

centralized model - 集中式模型

checkout - 检出

commit - 提交

identifier - 标识符

merge - 合并

revision - 修订,修订版,版本

revision control system - 版本控制系统

subcommand - 子命令

version control system - 版本控制系统
Line 43: Line 16:

~- (!) 参考自: {en} ["IntroductionToBzr"]-~ 术语对照表::

    branch - 分支
    centralized model - 集中式模型
    checkout - 检出
    commit - 提交
    identifier - 标识符
    merge - 合并
    revision - 修订,修订版,版本
    revision control system - 版本控制系统
    subcommand - 子命令
    version control system - 版本控制系统

Bazaar-NG 指南

~- (!) 参考自: {en} ["IntroductionToBzr"]-~ 术语对照表:

branch                  - 分支
centralized model       - 集中式模型
checkout                - 检出
commit                  - 提交
identifier              - 标识符
merge                   - 合并
revision                - 修订,修订版,版本
revision control system - 版本控制系统
subcommand              - 子命令
version control system  - 版本控制系统

Current for bzr-0.8, 2006-04 (from repository: doc/tutorial.txt)


如果您业已熟悉分散式版本控制(decentralized revision control),那么完全可以 放心大胆的直接跳到“了解 Bazaar-NG“一节。另外,如果您只是熟悉版本控制而非分散式 版本控制,那么请从”DRCS 有何不同“一节开始。否则,最好弄点咖啡或绿茶,调整状态, 准备迎头赶上吧。


System Message: WARNING/2 (<string>, line 42)

Title underline too short.


您总会时不时需要处理一些文本数据--比如程序源代码,web 站点或是 Unix 系统管理员 必须面对的 /etc 下的配置文件。自然而然的,您很可能犯一些让自己懊恼的错误。也许您 一不小心删除了自己邮件服务器的配置文件,或者把某个小项目(pet project)的源代码 搞得一团糟。无论是何种情形,您已经删除了重要信息,却又没法要回来,徒叹“时运不济”。 凡此种种,如果您曾遭遇过,那么或许该考虑试试 Bazaar-NG。

版本控制系统(下文简称为 RCS)比如 Bazaar-NG 可以让您跟踪一个目录的变化(修改), 方法是将它转化成一个比目录稍稍复杂些的东西,我们称之为 分支 。分支不仅保存有 这个目录当前状态,还保存了过去各个不同时间点的状态。因此,当您不小心做了些违背 自己意愿的操作时,就可以把该目录恢复到过去某个时间点的状态。

只要“提交一个 修订 ”,版本控制系统就能让用户保存对一个分支所做的修改。所创建 的修订实际上是自上次整棵树被保存以来所有修改的摘要。

这些修订还有其它用途。例如,我们可以给修订添加注释以便记录最近做修改的用意,方法 是提供一可选的日志信息。实际使用中,日志信息类似于“修正 web 模板,令 table 闭合” 或是“增加 sftp 支持。修正 #595”。

我们记下这些日志以便日后如果出现 stfp 相关的问题,就可能找出问题是何时发生的。

DRCS 有何不同

多数版本控制系统(RCS)都存储在服务器上。如果想对存储在 RCS 里的代码进行修改, 就需要连接到服务器上并“检出”这些代码。随后就会取得一个目录,用户可以在这个目录 里进行修改然后提交。RCS 客户端便会连接到 RCS 服务器上,并保存用户所做的修改。 上述方法称为集中式模型。

The centralized model can have some drawbacks. A centralized RCS requires that one is able to connect to the server whenever one wants to do version control work. This can be a bit of a problem if your server on some other machine on the internet and you are not. Or, worse yet, you ''are'' on the internet but the server is missing!

Decentralized Revision Control Systems (which I'll call DRCS after this point) deal with this problem by keeping branches on the same machine as the client. In Bazaar-NG's case, the branch is kept in the same place as the code that is being version controlled. This allows the user to save his changes (commit) whenever he wants -- even if he is offline. The user only needs internet access when he wants to access the changes in someone else's branch that are somewhere else.

A common requirement that many people have is the need to keep track of the changes for a directory such as file and subdirectory changes. Performing this tracking by hand is a awkward process that over time becomes unwieldy. That is, until one considers version control tools such as Bazaar-NG. These tools automate the process of storing data by creating a revision of the directory tree whenever the user asks.

Revision control software such as Bazaar-NG can do much more than just storage and performing undo. For example, with Bazaar-NG developer can take the modifications in one branch of software and apply them to another, related, branch -- even if those changes exist in a branch owned by somebody else. This allows developers to cooperate without giving write access to repository.

Bazaar-NG remembers the ''ancestry'' of a revision: the previous revisions that it is based upon. A single revision may have more than one direct descendant, each with different changes, representing a divergence in the evolution of the tree. By branching, Bazaar-NG allows multiple people to cooperate on the evolution of a project, without all needing to work in strict lock-step. Branching can be useful even for a single developer.

了解 Bazaar-NG

Bazaar-NG 只安装了一个新命令 bzr ,其余皆为它的子命令。您可以使用`bzr help` 命令获得一些帮助。将来会有更丰富的帮助信息。

版本(version)控制系统的作用之一是跟踪谁做了怎样的修改。在分散式系统中,这就 要求每个作者有一个全球惟一的标识符。大多数人已经有了一种:email 地址。bzr 已经 相当智能,通过查看您的用户名和主机名,它能自动生成一个 email 地址。如果您不喜欢 Bazaar-NG 猜想的地址,可以通过如下三种方法进行设置:

1. 在 ~/.bazaar/bazaar.conf 中添加如下两行,设置 email 地址。请注意 [DEFAULT] 是区分大小写的:

email= Your Name <>

1. Override the previous setting on a branch by branch basis by creating a branch section in ~/.bazaar/branches.conf by adding the following lines:

email=Your Name <>

1. Overriding the two previous options by setting the global environment variable $BZREMAIL or $EMAIL ($BZREMAIL will take precedence) to your full email address.


History is by default stored in the .bzr directory of the branch. There will be a facility to store it in a separate repository, which may be remote. We create a new branch by running bzr init in an existing directory:

% mkdir tutorial
% cd tutorial
% ls -a
./  ../
% pwd
% bzr init
% ls -aF
./  ../  .bzr/

As for CVS, there are three classes of file: unknown, ignored, and versioned. The add command makes a file versioned: that is, changes to it will be recorded by the system:

% echo 'hello world' > hello.txt
% bzr status
% bzr unknowns
% bzr add hello.txt
added hello.txt
% bzr unknowns

If you add the wrong file, simply use bzr remove to make it unversioned again. This does not delete the working copy.

Branch locations

All history is stored in a branch, which is just an on-disk directory containing control files. By default there is no separate repository or database as used in svn or svk. You can choose to create a repository if you want to (see the bzr init-repo command). You may wish to do this if you have very large branches, or many branches of a moderate sized project.

You'll usually refer to branches on your computer's filesystem just by giving the name of the directory containing the branch. bzr also supports accessing branches over http, for example:

% bzr log

By installing bzr plugins you can also access branches over the sftp or rsync protocols.

Reviewing changes

Once you have completed some work, you will want to commit it to the version history. It is good to commit fairly often: whenever you get a new feature working, fix a bug, or improve some code or documentation. It's also a good practice to make sure that the code compiles and passes its test suite before committing, to make sure that every revision is a known-good state. You can also review your changes, to make sure you're committing what you intend to, and as a chance to rethink your work before you permanently record it.

Two bzr commands are particularly useful here: status and diff.

bzr status

The status command tells you what changes have been made to the working directory since the last revision:

% bzr status

By default bzr status hides "boring" files that are either unchanged or ignored. To see them too, use the --all option. The status command can optionally be given the name of some files or directories to check.

bzr diff

The diff command shows the full text of changes to all files as a standard unified diff. This can be piped through many programs such as ''patch'', ''diffstat'', ''filterdiff'' and ''colordiff'':

% bzr diff
*** added file 'hello.txt'
--- /dev/null
+++ hello.txt
@@ -1,0 +1,1 @@
+hello world

With the ''-r'' option, the tree is compared to an earlier revision, or the differences between two versions are shown:

% bzr diff -r 1000..          # everything since r1000
% bzr diff -r 1000..1100      # changes from 1000 to 1100

The --diff-options option causes bzr to run the external diff program, passing options. For example:

% bzr diff --diff-options --side-by-side foo

Some projects prefer patches to show a prefix at the start of the path for old and new files. The --prefix option can be used to provide such a prefix. As a shortcut, bzr diff -p1 produces a form that works with the command patch -p1.


When the working tree state is satisfactory, it can be committed to the branch, creating a new revision holding a snapshot of that state.

bzr commit

The commit command takes a message describing the changes in the revision. It also records your userid, the current time and timezone, and the inventory and contents of the tree. The commit message is specified by the ''-m'' or ''--message'' option. You can enter a multi-line commit message; in most shells you can enter this just by leaving the quotes open at the end of the line.

% bzr commit -m "added my first file"

You can also use the -F option to take the message from a file. Some people like to make notes for a commit message while they work, then review the diff to make sure they did what they said they did. (This file can also be useful when you pick up your work after a break.)

Message from an editor

If you use neither the -m nor the -F option then bzr will open an editor for you to enter a message. The editor to run is controlled by your $EDITOR environment variable or add editor to ~/.bazaar/bazaar.conf; $BZR_EDITOR will override the above mentioned editor options. If you quit the editor without making any changes, the commit will be cancelled.


If you give file or directory names on the commit command line then only the changes to those files will be committed. For example:

% bzr commit -m "documentation fix"

By default bzr always commits all changes to the tree, even if run from a subdirectory. To commit from only the current directory down, use:

% bzr commit .


System Message: WARNING/2 (<string>, line 303)

Title underline too short.


如果您已经进行了一些更改,随后又不想保留这些更改,可以使用 revert 命令回复 到上一个 head 版本。不过在此之前最好先用 bzr diff 查看一下将要移除哪些内容。 revert(回复)命令缺省会回复整棵树(the whole tree);如果指定了文件名或目录名, 那么只会回复指定文件或目录。 revert 还会清除等待合并的修订版本(revision)。


许多源码树都包含一些不需要进行版本控制的文件,比如编辑器备份文件,目标文件或字节码 (bytecode)文件,还有编译产生的程序等等。您只要不添加它们即可,不过这样一来这些 文件总会以未知文件形式突然出现(always crop up as unknown files)。您还可以指定 bzr 忽略这些文件,只要将它们添加到源码树顶层目录内名为 ''.bzrignore'' 的文件即可。

This file contains a list of file wildcards (or "globs"), one per line. Typical contents are like this:


If a glob contains a slash, it is matched against the whole path from the top of the tree; otherwise it is matched against only the filename. So the previous example ignores files with extension .o in all subdirectories, but this example ignores only config.h at the top level and HTML files in doc/:


To get a list of which files are ignored and what pattern they matched, use ''bzr ignored'':

% bzr ignored
config.h                 ./config.h            *~

It is OK to have either an ignore pattern match a versioned file, or to add an ignored file. Ignore patterns have no effect on versioned files; they only determine whether unversioned files are reported as unknown or ignored.

The ''.bzrignore'' file should normally be versioned, so that new copies of the branch see the same patterns:

% bzr add .bzrignore
% bzr commit -m "Add ignore patterns"


bzr log

The bzr log command shows a list of previous revisions. The bzr log --forward command does the same in chronological order to get most recent revisions printed at last.

As with bzr diff, bzr log supports the -r argument:

% bzr log -r 1000..          # Revision 1000 and everything after it
% bzr log -r ..1000          # Everything up to and including r1000
% bzr log -r 1000..1100      # changes from 1000 to 1100
% bzr log -r 1000            # The changes in only revision 1000


System Message: WARNING/2 (<string>, line 373)

Title underline too short.


bzr info 命令能够显示一些工作树和分支历史相关的摘要信息。


System Message: WARNING/2 (<string>, line 379)

Title underline too short.


bzr versions files and directories in a way that can keep track of renames and intelligently merge them:

% mkdir src
% echo 'int main() {}' > src/simple.c
% bzr add src
% bzr status
A       src/
?       src/simple.c
% bzr add src/simple.c
% bzr status
A       src/
A       src/simple.c


You can delete files or directories by just deleting them from the working directory. This is a bit different to CVS, which requires that you also do cvs remove.

bzr remove makes the file un-versioned, but does not delete the working copy. This is useful when you add the wrong file, or decide that a file should actually not be versioned.
% rm -r src
% bzr remove -v hello.txt
?       hello.txt
% bzr status
?       hello.txt
D       src/
D       src/simple.c

如果您不小心移除错了文件,可以使用 bzr revert 命令恢复之。


Often rather than starting your own project, you will want to submit a change to an existing project. You can get a copy of an existing branch by copying its directory, expanding a tarball, or by a remote copy using something like rsync. You can also use bzr to fetch a copy. Because this new copy is potentially a new branch, the command is called branch:

% bzr branch
% cd

This copies down the complete history of this branch, so we can do all operations on it locally: log, annotate, making and merging branches. There will be an option to get only part of the history if you wish.


You can stay up-to-date with the parent branch by "pulling" in their changes:

% bzr pull

After this change, the local directory will be a mirror of the source. This includes the ''revision-history'' - which is a list of the commits done in this branch, rather than merged from other branches.

This command only works if your local (destination) branch is either an older copy of the parent branch with no new commits of its own, or if the most recent commit in your local branch has been merged into the parent branch.


If two branches have diverged (both have unique changes) then bzr merge is the appropriate command to use. Merge will automatically calculate the changes that exist in the branch you're merging from that are not in your branch and attempt to apply them in your branch.

% bzr merge URL

If there is a conflict during a merge, 3 files with the same basename are created. The filename of the common base is appended with .BASE, the filename of the file containing your changes is appended .THIS and the filename with the changes from the other tree is appended .OTHER. Using a program such as kdiff3, you can now comfortably merge them into one file. To commit you have to rename it to the original basename and delete the other two files. As long as there exist files with .BASE, .THIS or .OTHER the commit command will complain.

[TODO: explain conflict markers within files]


You don't need a special server to publish a bzr branch, just a normal web server. Just mirror the files to your server, including the .bzr directory. One can push a branch (or the changes for a branch) by one of the following three methods:

  • Rsync: rsync -avrz LOCALBRANCH

    (or any other tool for publishing a directory to a web site.)

  • bzr push s

    (The directory that must already exist)

  • The push plugin that comes with BzrTools