Merge remote-tracking branch 'LCTT/master'

This commit is contained in:
Xingyu Wang 2020-11-30 08:47:27 +08:00
commit 8fdcc4a02d
5 changed files with 381 additions and 374 deletions

View File

@ -1,28 +1,31 @@
[#]: collector: (lujun9972)
[#]: translator: (MjSeven)
[#]: reviewer: ( )
[#]: publisher: ( )
[#]: url: ( )
[#]: reviewer: (wxy)
[#]: publisher: (wxy)
[#]: url: (https://linux.cn/article-12871-1.html)
[#]: subject: (An introduction to mutation testing in Python)
[#]: via: (https://opensource.com/article/20/7/mutmut-python)
[#]: author: (Moshe Zadka https://opensource.com/users/moshez)
Python 突变测试
Python 突变测试介
======
通过突变测试来修复未知的 bug。
![Searching for code][1]
> 通过突变测试来修复未知的 bug。
![](https://img.linux.net.cn/data/attachment/album/202011/29/230106ie9xc89dj3jx1yj9.jpg)
你一定对所有内容都进行了测试,也许你甚至在项目仓库中有一个徽章,标明有 100% 的测试覆盖率,但是这些测试真的帮到你了吗?你怎么知道的?
开发人员很清楚单元测试的 _成本_,必须编写测试。有时它们无法按照预期工作:存在假告警或者抖动测试。在不更改任何代码的情况下有时成功,有时失败。通过单元测试发现的小问题很有价值,但是通常它们悄无声息的出现在开发人员的机器上,并且在提交到版本控制之前就已得到修复。但真正令人担忧的问题大多是不可见的。最糟糕的是_丢失的告警_ 是完全不可见的:你看不到没能捕获的错误,直到出现在用户手上--有时甚至连用户都看不到。
开发人员很清楚单元测试的*成本*。测试必须要编写。有时它们无法按照预期工作:存在假告警或者抖动测试。在不更改任何代码的情况下有时成功,有时失败。通过单元测试发现的小问题很有价值,但是通常它们悄无声息的出现在开发人员的机器上,并且在提交到版本控制之前就已得到修复。但真正令人担忧的问题大多是看不见的。最糟糕的是,*丢失的告警*是完全不可见的:你看不到没能捕获的错误,直到出现在用户手上 —— 有时甚至连用户都看不到。
有一种测试可以使不可见的错误变为可见:[突变测试][2]。
有一种测试可以使不可见的错误变为可见:<ruby>[突变测试][2]<rt>mutation testing</rt></ruby>
变异测试通过算法修改源代码,并检查每次测试是否都有“变异体”存活。任何在单元测试中幸存下来的变异体都是问题:这意味着对代码的修改(可能会引入错误)没有被标准测试套件捕获。
[Python][3] 中用于突变测试的一个框架是 `mutmut`
假设你需要编写代码来计算钟表中时针和分针之间的角度,直到最接近的度数,代码可能会这样写:
```
def hours_hand(hour, minutes):
    base = (hour % 12 ) * (360 // 12)
@ -37,6 +40,7 @@ def between(hour, minutes):
```
首先,写一个简单的单元测试:
```
import angle
@ -45,6 +49,7 @@ def test_twelve():
```
足够了吗?代码没有 `if` 语句,所以如果你查看覆盖率:
```
$ coverage run `which pytest`
============================= test session starts ==============================
@ -58,71 +63,74 @@ tests/test_angle.py .                                      
```
完美!测试通过,覆盖率为 100%,你真的是一个测试专家。但是,当你使用突变测试时,覆盖率会变成多少?
```
$ mutmut run --paths-to-mutate angle.py
&lt;snip&gt;
<snip>
Legend for output:
🎉 Killed mutants.   The goal is for everything to end up in this bucket.
⏰ Timeout.          Test suite took 10 times as long as the baseline so were killed.
🤔 Suspicious.       Tests took a long time, but not long enough to be fatal.
🙁 Survived.         This means your tests needs to be expanded.
🔇 Skipped.          Skipped.
&lt;snip&gt;
⠋ 21/21  🎉 5  ⏰ 0  🤔 0  🙁 16  🔇 0
🎉 Killed mutants. The goal is for everything to end up in this bucket.
⏰ Timeout. Test suite took 10 times as long as the baseline so were killed.
🤔 Suspicious. Tests took a long time, but not long enough to be fatal.
🙁 Survived. This means your tests needs to be expanded.
🔇 Skipped. Skipped.
<snip>
⠋ 21/21 🎉 5 ⏰ 0 🤔 0 🙁 16 🔇 0
```
天啊,在 21 个突变体中,有 16 个存活。只有 5 个通过了突变测试,但是,这意味着什么呢?
天啊,在 21 个突变体中,有 16 个存活。只有 5 个通过了突变测试,但是,这意味着什么呢?
对于每个突变测试,`mutmut` 会修改部分源代码,以模拟潜在的错误,修改的一个例子是将 `>` 比较更改为 `>=`,查看会发生什么。如果没有针对这个边界条件的单元测试,那么这个突变将会“存活”:这是一个没有任何测试用例能够检测到的潜在错误。
是时候编写更好的单元测试了。很容易检查使用 `results` 所做的更改:
```
$ mutmut results
&lt;snip&gt;
<snip>
Survived 🙁 (16)
\---- angle.py (16) ----
---- angle.py (16) ----
4-7, 9-14, 16-21
$ mutmut apply 4
$ git diff
diff --git a/angle.py b/angle.py
index b5dca41..3939353 100644
\--- a/angle.py
--- a/angle.py
+++ b/angle.py
@@ -1,6 +1,6 @@
 def hours_hand(hour, minutes):
     hour = hour % 12
\-    base = hour * (360 // 12)
\+    base = hour / (360 // 12)
     correction = int((minutes / 60) * (360 // 12))
     return base + correction
def hours_hand(hour, minutes):
hour = hour % 12
- base = hour * (360 // 12)
+ base = hour / (360 // 12)
correction = int((minutes / 60) * (360 // 12))
return base + correction
```
这是 `mutmut` 执行突变的一个典型例子,它会分析源代码并将运算符更改为不同的运算符:减法变加法。在本例中由乘法变为除法。一般来说,单元测试应该在操作符更换时捕获错误。否则,它们将无法有效地测试行为。按照这种逻辑,`mutmut` 会遍历源代码仔细检查你的测试。
你可以使用 `mutmut apply` 来应用失败的突变体。事实证明你几乎没有检查过 `hour` 参数是否被正确使用。修复它:
```
$ git diff
diff --git a/tests/test_angle.py b/tests/test_angle.py
index f51d43a..1a2e4df 100644
\--- a/tests/test_angle.py
--- a/tests/test_angle.py
+++ b/tests/test_angle.py
@@ -2,3 +2,6 @@ import angle
 
 def test_twelve():
     assert angle.between(12, 00) == 0
def test_twelve():
assert angle.between(12, 00) == 0
+
+def test_three():
\+    assert angle.between(3, 00) == 90
+ assert angle.between(3, 00) == 90
```
以前,你只测试了 12 点钟,现在增加一个 3 点钟的测试就足够了吗?
```
$ mutmut run --paths-to-mutate angle.py
&lt;snip&gt;
⠋ 21/21  🎉 7  ⏰ 0  🤔 0  🙁 14  🔇 0
<snip>
⠋ 21/21 🎉 7 ⏰ 0 🤔 0 🙁 14 🔇 0
```
这项新测试成功杀死了两个突变体,比以前更好,当然还有很长的路要走。我不会一一解决剩下的 14 个测试用例,因为我认为模式已经很明确了。(你能将它们降低到零吗?)
@ -136,7 +144,7 @@ via: https://opensource.com/article/20/7/mutmut-python
作者:[Moshe Zadka][a]
选题:[lujun9972][b]
译者:[MjSeven](https://github.com/MjSeven)
校对:[校对者ID](https://github.com/校对者ID)
校对:[wxy](https://github.com/wxy)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出

View File

@ -1,93 +0,0 @@
[#]: collector: (lujun9972)
[#]: translator: (geekpi)
[#]: reviewer: ( )
[#]: publisher: ( )
[#]: url: ( )
[#]: subject: (How to rebase to Fedora 33 on Silverblue)
[#]: via: (https://fedoramagazine.org/how-to-rebase-to-fedora-33-on-silverblue/)
[#]: author: (Michal Konečný https://fedoramagazine.org/author/zlopez/)
How to rebase to Fedora 33 on Silverblue
======
![][1]
Silverblue is [an operating system for your desktop built on Fedora][2]. Its excellent for daily use, development, and container-based workflows. It offers [numerous advantages][3] such as being able to roll back in case of any problems. If you want to update to Fedora 33 on your Silverblue system, this article tells you how. It not only shows you what to do, but also how to revert things if something unforeseen happens.
Prior to actually doing the rebase to Fedora 33, you should apply any pending updates. Enter the following in the terminal:
```
$ rpm-ostree update
```
or install updates through GNOME Software and reboot.
### Rebasing using GNOME Software
The GNOME Software shows you that there is new version of Fedora available on the Updates screen.
![Fedora 33 is available][4]
First thing you need to do is to download the new image, so click on the _Download_ button. This will take some time and after its done you will see that the update is ready to install.
![Fedora 33 is ready for installation][5]
Click on the _Install_ button. This step will take only a few moments and then you will be prompted to restart your computer.
![Restart is needed to rebase to Fedora 33 Silverblue][6]
Click on _Restart_ button and you are done. After restart you will end up in new and shiny release of Fedora 33. Easy, isnt it?
### Rebasing using terminal
If you prefer to do everything in a terminal, than this next guide is for you.
Rebasing to Fedora 33 using terminal is easy. First, check if the 33 branch is available:
```
$ ostree remote refs fedora
```
You should see the following in the output:
```
fedora:fedora/33/x86_64/silverblue
```
Next, rebase your system to the Fedora 33 branch.
```
$ rpm-ostree rebase fedora:fedora/33/x86_64/silverblue
```
Finally, the last thing to do is restart your computer and boot to Fedora 33.
### How to roll back
If anything bad happens—for instance, if you cant boot to Fedora 33 at all—its easy to go back. Pick the previous entry in the GRUB menu at boot, and your system will start in its previous state before switching to Fedora 33. To make this change permanent, use the following command:
```
$ rpm-ostree rollback
```
Thats it. Now you know how to rebase Silverblue to Fedora 33 and roll back. So why not do it today?
--------------------------------------------------------------------------------
via: https://fedoramagazine.org/how-to-rebase-to-fedora-33-on-silverblue/
作者:[Michal Konečný][a]
选题:[lujun9972][b]
译者:[译者ID](https://github.com/译者ID)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://fedoramagazine.org/author/zlopez/
[b]: https://github.com/lujun9972
[1]: https://fedoramagazine.org/wp-content/uploads/2020/02/fedora-silverblue-logo.png
[2]: https://docs.fedoraproject.org/en-US/fedora-silverblue/
[3]: https://fedoramagazine.org/give-fedora-silverblue-a-test-drive/
[4]: https://fedoramagazine.org/wp-content/uploads/2020/10/Screenshot-from-2020-10-29-12-53-37-1024x725.png
[5]: https://fedoramagazine.org/wp-content/uploads/2020/10/Screenshot-from-2020-10-29-13-00-15-1024x722.png
[6]: https://fedoramagazine.org/wp-content/uploads/2020/10/Screenshot-from-2020-10-29-13-01-32-1024x727.png

View File

@ -1,247 +0,0 @@
[#]: collector: (lujun9972)
[#]: translator: (lxbwolf)
[#]: reviewer: ( )
[#]: publisher: ( )
[#]: url: ( )
[#]: subject: (Get started with Fossil, an alternative to Git)
[#]: via: (https://opensource.com/article/20/11/fossil)
[#]: author: (Klaatu https://opensource.com/users/klaatu)
Get started with Fossil, an alternative to Git
======
Fossil is an all-in-one version control system, bug tracker, wiki,
forum, and documentation solution.
![Dinosaurs on land at sunset][1]
As any programmer knows, there are many reasons it's vital to keep track of code changes. Sometimes you just want a history of how your project started and evolved, as a matter of curiosity or education. Other times, you want to enable other coders to contribute to your project, and you need a reliable way to merge disparate parts. And more critically, sometimes an adjustment you make to fix one problem breaks something else that was working.
The [Fossil][2] source code management system is an all-in-one version control system, bug tracker, wiki, forum, and documentation solution from the creator of the famous [SQLite][3] database.
### Install Fossil
Fossil is a single, self-contained C program, so you can probably just [download Fossil][4] from its website and place it anywhere in your system [PATH][5]. For example, assuming `/usr/local/bin` is in your path, as it usually is by default:
```
$ wget <https://fossil-scm.org/home/uv/fossil-linux-x64-X.Y.tar.gz>
$ sudo tar xvf fossil-linux-x64-X.Y.tar.gz \
\--directory /usr/local/bin
```
You might also find Fossil in your software repository through your package manager, or you can compile it from source code.
### Create a Fossil repository
If you have a coding project you want to track with Fossil, the first step is to create a Fossil repository:
```
$ fossil init myproject.fossil
project-id: 010836ac6112fefb0b015702152d447c8c1d8604
server-id:  54d837e9dc938ba1caa56d31b99c35a4c9627f44
admin-user: klaatu (initial password is "14b605")
```
Creating a Fossil repo returns three items: a unique project ID, a unique server ID, and an admin ID and password. The project and server IDs are version numbers. The admin credentials establish your ownership of this repository and can be used if you decide to run Fossil as a server for other users to access.
### Work in a Fossil repository
To start working in a Fossil repo, you must create a working location for its data. You might think of this process as creating a virtual environment in Python or unzipping a ZIP file that you intend to zip back up again later.
Create a working directory and change into it:
```
$ mkdir myprojectdir
$ cd myprojectdir
```
Open your Fossil repository into the directory you created:
```
$ fossil open ../myproject
project-name: &lt;unnamed&gt;
repository:   /home/klaatu/myprojectdir/../myproject
local-root:   /home/klaatu/myprojectdir/
config-db:    /home/klaatu/.fossil
project-code: 010836ac6112fefb0b015702152d447c8c1d8604
checkout:     9e6cd96dd675544c58a246520ad58cdd460d1559 2020-11-09 04:09:35 UTC
tags:         trunk
comment:      initial empty check-in (user: klaatu)
check-ins:    1
```
You might notice that Fossil created a hidden file called `.fossil` in your home directory to track your global Fossil preferences. This is not specific to your project; it's just an artifact of the first time you use Fossil.
#### Add files
To add files to your repository, use the `add` and `commit` subcommands. For example, create a simple README file and add it to the repository:
```
$ echo "My first Fossil project" &gt; README
$ fossil add README
ADDED  README
$ fossil commit -m 'My first commit'
New_Version: 2472a43acd11c93d08314e852dedfc6a476403695e44f47061607e4e90ad01aa
```
#### Use branches
By default, a Fossile repository starts with a main branch called `trunk`. You can branch off the trunk when you want to work on code without affecting your primary codebase. Creating a new branch requires the `branch` subcommand, along with a new branch name and the branch you want to use as the basis for your new one. In this example, the only branch is `trunk`, so try creating a new branch called `dev`:
```
$ fossil branch --help
Usage: fossil branch new BRANCH-NAME BASIS ?OPTIONS?
$ fossil branch new dev trunk
New branch: cb90e9c6f23a9c98e0c3656d7e18d320fa52e666700b12b5ebbc4674a0703695
```
You've created a new branch, but your current branch is still `trunk`:
```
$ fossil branch current
trunk
```
To make your new `dev` branch active, use the `checkout` command:
```
$ fossil checkout dev
dev
```
#### Merge changes
Suppose you add an exciting new file to your `dev` branch, and having tested it, you're satisfied that it's ready to take its place in `trunk`. This is called a _merge_.
First, change your branch back to your target branch (in this example, that's `trunk`): 
```
$ fossil checkout trunk
trunk
$ ls
README
```
Your new file (or any changes you made to an existing file) doesn't exist there yet, but that's what performing the merge will take care of:
```
$ fossil merge dev
 "fossil undo" is available to undo changes to the working checkout.
$ ls
myfile.lua  README
```
### View the Fossil timeline
To see the history of your repository, use the `timeline` option. This shows a detailed list of all activity in your repository, including a hash representing the change, the commit message you provided when committing code, and who made the change:
```
$ fossil timeline
=== 2020-11-09 ===
06:24:16 [5ef06e668b] added exciting new file (user: klaatu tags: dev)
06:11:19 [cb90e9c6f2] Create new branch named "dev" (user: klaatu tags: dev)
06:08:09 [a2bb73e4a3] *CURRENT* some additions were made (user: klaatu tags: trunk)
06:00:47 [2472a43acd] This is my first commit. (user: klaatu tags: trunk)
04:09:35 [9e6cd96dd6] initial empty check-in (user: klaatu tags: trunk)
+++ no more data (5) +++
```
![Fossil UI][6]
(Klaatu, [CC BY-SA 4.0][7])
### Make your Fossil repository public
Because Fossil features a built-in web interface, Fossil doesn't need a hosting service like GitLab or Gitea do. Fossil is its own hosting service, as long as you have a server to put it on. Before making your Fossil project public, though, you must configure some attributes through the web user interface (UI).
Launch a local instance with the `ui` subcommand:
```
$ pwd
/home/klaatu/myprojectdir/
$ fossil ui
```
Specifically, look at **Users** and **Settings** for security, and **Configuration** for project properties (including a proper title). The web interface isn't just a convenience function. It's intended for actual use and is indeed used as the host for the Fossil project. There are several surprisingly advanced options, from user management (or self-management, if you please) to single-sign-on (SSO) with other Fossil repositories on the same server.
Once you're satisfied with your changes, close the web interface and press **Ctrl+C** to stop the UI engine from running. Commit your web changes just as you would any other update:
```
$ fossil commit -m 'web ui updates'
New_Version: 11fe7f2855a3246c303df00ec725d0fca526fa0b83fa67c95db92283e8273c60
```
Now you're ready to set up your Fossil server.
1. Copy your Fossil repository (in this example, `myproject.fossil`) to your server. You only need the single file.
2. Install Fossil on your server, if it's not already installed. The process for installing Fossil to your server is the same as it was for your local computer.
3. In your `cgi-bin` directory (or the equivalent of that directory, depending upon which HTTP daemon you're running), create a file called `repo_myproject.cgi`:
```
#!/usr/local/bin/fossil
repository: /home/klaatu/public_html/myproject.fossil
```
Make the script executable:
```
`$ chmod +x repo_myproject.cgi`
```
That's all there is to it. Your project is now live on the internet.
You can visit the web UI by navigating to your CGI script, such as `https://example.com/cgi-bin/repo_myproject.cgi`.
Or you can interact with your repository from a terminal through the same URL:
```
`$ fossil clone https://klaatu@example.com/cgi-bin/repo_myproject.cgi`
```
Working with a local clone requires you to use the `push` subcommand to send local changes back to your remote repository and the `pull` subcommand to get remotely made changes into your local copy:
```
`$ fossil push https://klaatu@example.com/cgi-bin/repo_myproject.cgi`
```
### Use Fossil for independent hosting
Fossil places a lot of power into your hands (and the hands of your collaborators) and makes you independent of hosting services. This article only hints at the basics. There's so much more to Fossil that can help you and your teams in your code projects. Give Fossil a try. It won't just change the way you think about version control; it'll help you stop thinking about version control altogether.
--------------------------------------------------------------------------------
via: https://opensource.com/article/20/11/fossil
作者:[Klaatu][a]
选题:[lujun9972][b]
译者:[译者ID](https://github.com/译者ID)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://opensource.com/users/klaatu
[b]: https://github.com/lujun9972
[1]: https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/osdc_dinosaur_sunset.png?itok=lbzpbW5p (Dinosaurs on land at sunset)
[2]: https://fossil-scm.org/home/doc/trunk/www/index.wiki
[3]: https://www.sqlite.org/index.html
[4]: https://fossil-scm.org/home/uv/download.html
[5]: https://opensource.com/article/17/6/set-path-linux
[6]: https://opensource.com/sites/default/files/uploads/fossil-ui.jpg (Fossil UI)
[7]: https://creativecommons.org/licenses/by-sa/4.0/

View File

@ -0,0 +1,93 @@
[#]: collector: (lujun9972)
[#]: translator: (geekpi)
[#]: reviewer: ( )
[#]: publisher: ( )
[#]: url: ( )
[#]: subject: (How to rebase to Fedora 33 on Silverblue)
[#]: via: (https://fedoramagazine.org/how-to-rebase-to-fedora-33-on-silverblue/)
[#]: author: (Michal Konečný https://fedoramagazine.org/author/zlopez/)
如何在 Silverblue 上变基到 Fedora 33
======
![][1]
Silverblue 是[一个建立在 Fedora 上的桌面操作系统][2]。它非常适合日常使用、开发和基于容器的工作流程。它提供了[众多的优势][3],例如在出现任何问题时能够回滚。如果你想在你的 Silverblue 系统上更新到 Fedora 33这篇文章会告诉你如何做。它不仅告诉你该怎么做还告诉你如果发生了不可预见的事情时该如何回退。
在实际做变基到 Fedora 33 之前,你应该应用任何挂起的更新。在终端中输入以下内容:
```
$ rpm-ostree update
```
或通过 GNOME 软件中心安装更新并重启。
### 使用 GNOME 软件中心变基
GNOME 软件中心会在更新界面显示有新版本的 Fedora 可用。
![Fedora 33 is available][4]
首先你需要做的是下载新镜像,点击 _Download_ 按钮。这将需要一些时间,完成后你会看到更新已经准备好安装了。
![Fedora 33 is ready for installation][5]
点击 _Install_ 按钮。这一步只需要几分钟,然后会提示你重启电脑。
![Restart is needed to rebase to Fedora 33 Silverblue][6]
点击 _Restart_ 按钮就可以了。重启后,你将进入新的 Fedora 33 版本。很简单,不是吗?
### 使用终端变基
如果你喜欢在终端上做所有的事情,那么接下来的指南就适合你。
使用终端变基到 Fedora 33 很简单。首先,检查 33 版本分支是否可用:
```
$ ostree remote refs fedora
```
你应该在输出中看到以下内容:
```
fedora:fedora/33/x86_64/silverblue
```
接下来,将你的系统变基到 Fedora 33 分支。
```
$ rpm-ostree rebase fedora:fedora/33/x86_64/silverblue
```
最后要做的是重启你的电脑并启动到 Fedora 33。
### 如何回滚
如果有什么不好的事情发生。例如,如果你无法启动到 Fedora 33那很容易回滚回去。在启动时选择 GRUB 菜单中的前一个条目,你的系统就会以切换到 Fedora 33 之前的状态启动。要使这一改变永久化,请使用以下命令:
```
$ rpm-ostree rollback
```
就是这样了。现在你知道如何将 Silverblue 变基为 Fedora 33 并回滚了。那为什么不在今天就做呢?
--------------------------------------------------------------------------------
via: https://fedoramagazine.org/how-to-rebase-to-fedora-33-on-silverblue/
作者:[Michal Konečný][a]
选题:[lujun9972][b]
译者:[geekpi](https://github.com/geekpi)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://fedoramagazine.org/author/zlopez/
[b]: https://github.com/lujun9972
[1]: https://fedoramagazine.org/wp-content/uploads/2020/02/fedora-silverblue-logo.png
[2]: https://docs.fedoraproject.org/en-US/fedora-silverblue/
[3]: https://fedoramagazine.org/give-fedora-silverblue-a-test-drive/
[4]: https://fedoramagazine.org/wp-content/uploads/2020/10/Screenshot-from-2020-10-29-12-53-37-1024x725.png
[5]: https://fedoramagazine.org/wp-content/uploads/2020/10/Screenshot-from-2020-10-29-13-00-15-1024x722.png
[6]: https://fedoramagazine.org/wp-content/uploads/2020/10/Screenshot-from-2020-10-29-13-01-32-1024x727.png

View File

@ -0,0 +1,246 @@
[#]: collector: "lujun9972"
[#]: translator: "lxbwolf"
[#]: reviewer: " "
[#]: publisher: " "
[#]: url: " "
[#]: subject: "Get started with Fossil, an alternative to Git"
[#]: via: "https://opensource.com/article/20/11/fossil"
[#]: author: "Klaatu https://opensource.com/users/klaatu"
开始学习 FossilGit 的一个替代品
======
Fossil 是一个集版本控制系统、bug 追踪、维基、论坛以及文档解决方案于一体的系统。
![Dinosaurs on land at sunset][1]
每个开发者都知道,追踪代码的修改是至关重要的。有时候你会处于好奇或者教育的目的需要展示你的项目开始和进化的历史。有时候你想让其他的开发者参与到你的项目中,因此你需要一种值得信赖的能合并不同代码分支的方法。更极端一点,有时候你为了解决一个问题而修改的代码导致已有的功能不能正常使用。
[Fossil][2] 源码管理系统是由著名的 [SQLite][3] 数据库的作者开发的一个集版本控制系统、bug 追踪、维基、论坛以及文档解决方案于一体的系统。
### 安装 Fossil
Fossil 是一个独立的 C 程序,因此你可以从它的网站上[下载][4]后放在环境变量 [PATH][5] 中的任意位置。例如,假定 `/usr/local/bin` 已经在你的环境变量中(默认情况下是在的)
```
$ wget <https://fossil-scm.org/home/uv/fossil-linux-x64-X.Y.tar.gz>
$ sudo tar xvf fossil-linux-x64-X.Y.tar.gz \
\--directory /usr/local/bin
```
你也可以通过包管理器从软件仓库中找到 Fossil或者直接从源码编译。
### 创建一个 Fossil 仓库
如果你已经有一个代码项目,想用 Fossil 来追踪,那么第一步就是创建一个 Fossil 仓库:
```
$ fossil init myproject.fossil
project-id: 010836ac6112fefb0b015702152d447c8c1d8604
server-id:  54d837e9dc938ba1caa56d31b99c35a4c9627f44
admin-user: klaatu (initial password is "14b605")
```
创建 Fossil 仓库的过程中会返回三行信息:一个唯一的项目 ID、一个唯一的服务 ID 以及管理员 ID 和密码。项目 ID 和服务 ID 是版本数字。管理员资质表明你对这个仓库的所有权,当你把这个 Fossil 仓库作为服务器让其他用户来访问时可以使用管理员权限。
### Fossil 仓库工作流
在你使用 Fossil 仓库之前,你需要先为它的数据创建一个工作路径。你可以把这个过程类比为使用 Python 时创建一个虚拟环境或者解压一个只会用来备份的 ZIP 文件。
创建一个工作目录并进入:
```
$ mkdir myprojectdir
$ cd myprojectdir
```
把你的 Fossil 打开到刚刚创建的目录:
```
$ fossil open ../myproject
project-name: &lt;unnamed&gt;
repository:   /home/klaatu/myprojectdir/../myproject
local-root:   /home/klaatu/myprojectdir/
config-db:    /home/klaatu/.fossil
project-code: 010836ac6112fefb0b015702152d447c8c1d8604
checkout:     9e6cd96dd675544c58a246520ad58cdd460d1559 2020-11-09 04:09:35 UTC
tags:         trunk
comment:      initial empty check-in (user: klaatu)
check-ins:    1
```
你可能注意到了Fossil 在你的家目录下创建了一个名为 `.fossil` 的隐藏文件,用来追踪你的全局 Fossil 配置。这个配置不是只适用于你的一个项目的;这个文件只会在你第一次使用 Fossil 时生成。
#### 添加文件
使用 `add``commit` 子命令来向你的仓库添加文件。例如,创建一个简单的 README 文件,把它添加到仓库:
```
$ echo "My first Fossil project" &gt; README
$ fossil add README
ADDED  README
$ fossil commit -m 'My first commit'
New_Version: 2472a43acd11c93d08314e852dedfc6a476403695e44f47061607e4e90ad01aa
```
#### 使用分支
Fossil 仓库开始时默认使用的主分支名为 `trunk`。当你想修改代码而又不影响主干代码时,你可以从 trunk 分支切走。创建新分支需要使用 `branch` 子命令,这个命令需要两个参数:一个新分支的名字,一个新分支的基分支名字。在本例中,只有一个分支 `trunk`,因此尝试创建一个名为 `dev` 的新分支:
```
$ fossil branch --help
Usage: fossil branch new BRANCH-NAME BASIS ?OPTIONS?
$ fossil branch new dev trunk
New branch: cb90e9c6f23a9c98e0c3656d7e18d320fa52e666700b12b5ebbc4674a0703695
```
你已经创建了一个新分支,但是你当前所在的分支仍然是 `trunk`
```
$ fossil branch current
trunk
```
使用 `checkout` 命令切换到你的新分支 `dev`
```
$ fossil checkout dev
dev
```
#### 合并修改
假设你在 `dev` 分支中添加了一个新文件,完成了测试,现在想把它合并到 `trunk`。这个过程叫做*合并*。
首先,切回目标分支(本例中目标分支为 `trunk`
```
$ fossil checkout trunk
trunk
$ ls
README
```
这个分支中没有你的新文件(或者你对其他文件的修改),而那些内容是合并的过程需要的信息:
```
$ fossil merge dev
 "fossil undo" is available to undo changes to the working checkout.
$ ls
myfile.lua  README
```
### 查看 Fossil 时间线
使用 `timeline` 选项来查看仓库的历史。这个命令列出了你的仓库的所有活动的详细信息,包括用来表示每次修改的哈希值、每次提交时填写的信息以及提交者:
```
$ fossil timeline
=== 2020-11-09 ===
06:24:16 [5ef06e668b] added exciting new file (user: klaatu tags: dev)
06:11:19 [cb90e9c6f2] Create new branch named "dev" (user: klaatu tags: dev)
06:08:09 [a2bb73e4a3] *CURRENT* some additions were made (user: klaatu tags: trunk)
06:00:47 [2472a43acd] This is my first commit. (user: klaatu tags: trunk)
04:09:35 [9e6cd96dd6] initial empty check-in (user: klaatu tags: trunk)
+++ no more data (5) +++
```
![Fossil UI][6]
(Klaatu, [CC BY-SA 4.0][7])
### 公开你的 Fossil 仓库
因为 Fossil 有个内置的 web 界面,所以 Fossil 不像 GitLab 和 Gitea 那样需要主机服务。Fossil 就是它自己的主机服务,只要你把它放在一台机器上就行了。在你公开你的 Fossil 仓库之前,你还需要通过 web 用户界面UI来配置一些信息
使用 `ui` 子命令启动一个本地的实例:
```
$ pwd
/home/klaatu/myprojectdir/
$ fossil ui
```
`用户`和`设置`是安全相关的,`配置`是项目属性相关的包括一个合适的标题。web 界面不仅仅是一个方便的功能。 它是能在生产环境中使用并作为 Fossil 项目的宿主来使用的。它还有一些其他的高级选项,比如用户管理(或者叫自我管理)、在同一个服务器上与其他的 Fossil 仓库进行单点登录SSO
当配置完成后,关掉 web 界面并按下 **Ctrl+C** 来停止 UI 引擎。像提交代码一样提交你的 web 修改。
```
$ fossil commit -m 'web ui updates'
New_Version: 11fe7f2855a3246c303df00ec725d0fca526fa0b83fa67c95db92283e8273c60
```
现在你可以配置你的 Fossil 服务器了。
1. 把你的 Fossil 仓库(本例中是 `myproject.fossil`)复制到服务器,你只需要这一个文件。
2. 如果你的服务器没有安装 Fossil就在你的服务器上安装 Fossil。在服务器上安装的过程跟在本地一样。
3. 在你的 `cgi-bin` 目录下(或它对应的目录,这取决于你的 HTTP 守护进程)创建一个名为 `repo_myproject.cgi` 的文件:
```
#!/usr/local/bin/fossil
repository: /home/klaatu/public_html/myproject.fossil
```
添加可执行权限:
```
`$ chmod +x repo_myproject.cgi`
```
你需要做的都已经做完了。现在可以通过互联网访问你的项目了。
你可以通过 CGI 脚本来访问 web UI例如 `https://example.com/cgi-bin/repo_myproject.cgi`
你也可以通过命令行来进行交互:
```
`$ fossil clone https://klaatu@example.com/cgi-bin/repo_myproject.cgi`
```
在本地的克隆仓库中工作时,你需要使用 `push` 子命令把本地的修改推送到远程的仓库,使用 `pull` 子命令把远程的修改拉取到本地仓库:
```
`$ fossil push https://klaatu@example.com/cgi-bin/repo_myproject.cgi`
```
### 使用Fossil 作为独立的主机
Fossil 大大提高了你的效率(也提高了你的合作者的效率),让你不再依赖主机副刷。本文只是简单的介绍了基本概念。你的代码项目还会用到很多有用的 Fossil 功能。尝试一下 Fossil。它不仅会改变你对版本控制的理解它会让你不再考虑其他的版本控制系统。
--------------------------------------------------------------------------------
via: https://opensource.com/article/20/11/fossil
作者:[Klaatu][a]
选题:[lujun9972][b]
译者:[lxbwolf](https://github.com/lxbwolf)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://opensource.com/users/klaatu
[b]: https://github.com/lujun9972
[1]: https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/osdc_dinosaur_sunset.png?itok=lbzpbW5p "Dinosaurs on land at sunset"
[2]: https://fossil-scm.org/home/doc/trunk/www/index.wiki
[3]: https://www.sqlite.org/index.html
[4]: https://fossil-scm.org/home/uv/download.html
[5]: https://opensource.com/article/17/6/set-path-linux
[6]: https://opensource.com/sites/default/files/uploads/fossil-ui.jpg "Fossil UI"
[7]: https://creativecommons.org/licenses/by-sa/4.0/