合并代码库
作者: | Steve Losh |
---|
日期: | 2009-11-17 |
---|
你意识到这的时候你会怎么做:“噢,文档可能应该和代码放到同一个库里?”
Thomas 的上一个贴士有关 分解代码库 让我想到要写一篇与之相反的东西:合并两个不同的库为一个库。
假设这有一个你工作的项目,代码放在 project
中还有文档放在 docs
中。你可能会觉得将它们放到同一个库中会很棒,这样当有人克隆你的项目代码时它们也得到了文档。
你 可能 会仅仅是创建一个新的库然后把所有的数据拷贝进去而已,但是这样并不会将你那些出色的变更历史保留下来。那就让我们来看看怎样合并这些分隔的代码库吧。
创建用来合并的新库
首先我们将会创建一个新库用来存放所有的东西:
$ ls
total 24
drwxr-xr-x 5 sjl 170B Nov 17 19:56 docs
drwxr-xr-x 4 sjl 136B Nov 17 19:58 project
$ mv project project-code
$ hg init project
$ ls
total 24
drwxr-xr-x 5 sjl 170B Nov 17 19:56 docs
drwxr-xr-x 3 sjl 102B Nov 17 20:04 project
drwxr-xr-x 4 sjl 136B Nov 17 19:58 project-code
我们暂时先把 project
重命名为 project-code
以免与新库冲突。
各库准备
我们可不想只是把所有的东西直接扔到新库的根目录下就完了,所以让我们一点点的来做吧。首先我们会把 project
库中的所有东西移到一个 src/
目录下:
$ cd project-code
$ mkdir src
$ mv * src
mv: rename src to src/src: Invalid argument
$ ls
total 0
drwxr-xr-x 3 sjl 102B Nov 17 20:08 src
$ hg addremove --similarity 100
removing myproject.py
adding src/myproject.py
recording removal of myproject.py as rename to src/myproject.py (100% similar)
$ hg commit -m 'Move the code into the src/ directory.'
现在我们对 docs
做同样的事情:
$ cd ../docs
$ mkdir docs
$ mv * docs
mv: rename docs to docs/docs: Invalid argument
$ ls
total 0
drwxr-xr-x 4 sjl 136B Nov 17 20:11 docs
$ hg addremove --similarity 100
removing LICENSE
removing README
adding docs/LICENSE
adding docs/README
recording removal of LICENSE as rename to docs/LICENSE (100% similar)
recording removal of README as rename to docs/README (100% similar)
$ hg commit -m 'Move the documentation into the docs/ directory.'
Pull 全部库
现在我们把结构调整为了我们想要的样子了,我们需要把两个库都 pull 到 project
去:
$ cd ..
$ ls
total 24
drwxr-xr-x 4 sjl 136B Nov 17 20:12 docs
drwxr-xr-x 3 sjl 102B Nov 17 20:04 project
drwxr-xr-x 4 sjl 136B Nov 17 20:08 project-code
$ cd project
$ ls
$ hg pull --update ../project-code
pulling from ../project-code
requesting all changes
adding changesets
adding manifests
adding file changes
added 4 changesets with 4 changes to 2 files
1 files updated, 0 files merged, 0 files removed, 0 files unresolved
$ ls
total 0
drwxr-xr-x 3 sjl 102B Nov 17 20:15 src
$ hg pull --force --update ../docs
pulling from ../docs
searching for changes
warning: repository is unrelated
adding changesets
adding manifests
adding file changes
added 3 changesets with 4 changes to 4 files (+1 heads)
not updating, since new heads added
(run 'hg heads' to see heads, 'hg merge' to merge)
$ ls
total 0
drwxr-xr-x 3 sjl 102B Nov 17 20:15 src
注意这里我们在 hg pull
后用了 --force
选项,这也就是对 Mercurial 说:“好了好了,我知道我在做什么,我真的是想合并这些库。”
合并代码库
看一看上一个 ls
命令的输出。看到没这依旧只有 src/
目录?我们需要合并两个库到一块以得到一个真正 “合并好的” 代码库。
为了让其更清晰一些,让我们来看看新库的图志:
$ hg glog
o 6 Move the documentation into the docs/ directory. (7 minutes ago by Steve Losh) tip
|
o 5 Add a LICENSE file. (22 minutes ago by Steve Losh)
|
o 4 Add a README file. (22 minutes ago by Steve Losh)
@ 3 Move the code into the src/ directory. (10 minutes ago by Steve Losh)
|
o 2 Fix a bug. (21 minutes ago by Steve Losh)
|
o 1 Implement some basic functionality. (21 minutes ago by Steve Losh)
|
o 0 Start the project. (21 minutes ago by Steve Losh)
看见没我们居然有两个分隔开的图志?变更集0到3连在一起,而变更集4到6连在一起。现在我们需要合并这两个图志到一块,这不是什么问题因为我们事先已将文件目录结构组织好了,所以这里不会有任何冲突:
$ hg update 6
2 files updated, 0 files merged, 1 files removed, 0 files unresolved
$ hg merge 3
1 files updated, 0 files merged, 0 files removed, 0 files unresolved
(branch merge, don't forget to commit)
$ hg commit -m 'Combine the source and docs repositories.'
现在看一看图志:
$ hg glog
@ 7 Combine the source and docs repositories. (37 seconds ago by Steve Losh) tip
|\
| o 6 Move the documentation into the docs/ directory. (11 minutes ago by Steve Losh)
| |
| o 5 Add a LICENSE file. (26 minutes ago by Steve Losh)
| |
| o 4 Add a README file. (27 minutes ago by Steve Losh)
|
o 3 Move the code into the src/ directory. (15 minutes ago by Steve Losh)
|
o 2 Fix a bug. (25 minutes ago by Steve Losh)
|
o 1 Implement some basic functionality. (25 minutes ago by Steve Losh)
|
o 0 Start the project. (26 minutes ago by Steve Losh)
$ ls
total 0
drwxr-xr-x 4 sjl 136B Nov 17 20:22 docs
drwxr-xr-x 3 sjl 102B Nov 17 20:22 src
我们的两个不同的库现在已经完美的合并为了一个,而且变更集也完整无缺!现在我们可以把老的库删掉了还有可以把新的这个库 push 到公共服务器上提供给大家使用了。
这真是非常的漂亮因为我们并没有改变到变更集的哈希值,这即是说我们能够轻易的合并来自那些依然在使用老的分隔的代码库的用户的变更集。