Merge remote-tracking branch 'LCTT/master'

This commit is contained in:
Xingyu.Wang 2019-02-19 20:54:51 +08:00
commit dbf4bedc25
5 changed files with 213 additions and 218 deletions

View File

@ -1,93 +0,0 @@
[#]: collector: (lujun9972)
[#]: translator: (lujun9972)
[#]: reviewer: ( )
[#]: publisher: ( )
[#]: url: ( )
[#]: subject: (How to use Magit to manage Git projects)
[#]: via: (https://opensource.com/article/19/1/how-use-magit)
[#]: author: (Sachin Patil https://opensource.com/users/psachin)
How to use Magit to manage Git projects
======
Emacs' Magit extension makes it easy to get started with Git version control.
![](https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/rh_003588_01_rd3os.combacktoschoolseriesk12_rh_021x_0.png?itok=fvorN0e-)
[Git][1] is an excellent [version control][2] tool for managing projects, but it can be hard for novices to learn. It's difficult to work from the Git command line unless you're familiar with the flags and options and the appropriate situations to use them. This can be discouraging and cause people to be stuck with very limited usage.
Fortunately, most of today's integrated development environments (IDEs) include Git extensions that make using it a lot easier. One such Git extension available in Emacs is called [Magit][3].
The Magit project has been around for 10 years and defines itself as "a Git porcelain inside Emacs." In other words, it's an interface where every action can be managed by pressing a key. This article walks you through the Magit interface and explains how to use it to manage a Git project.
If you haven't already, [install Emacs][4], then [install Magit][5] before you continue with this tutorial.
### Magit's interface
Start by visiting a project directory in Emacs' [Dired mode][6]. For example, all my Emacs configurations are stored in the **~/.emacs.d/** directory, which is managed by Git.
![](https://opensource.com/sites/default/files/uploads/visiting_a_git_project.png)
If you were working from the command line, you would enter **git status** to find a project's current status. Magit has a similar function: **magit-status**. You can call this function using **M-x magit-status** (short for the keystroke **Alt+x magit-status** ). Your result will look something like this:
![](https://opensource.com/sites/default/files/uploads/magit_status.png)
Magit shows much more information than you would get from the **git status** command. It shows a list of untracked files, files that aren't staged, and staged files. It also shows the stash list and the most recent commits—all in a single window.
If you want to know what has changed, use the Tab key. For example, if I move my cursor over the unstaged file **custom_functions.org** and press the Tab key, Magit will display the changes:
![](https://opensource.com/sites/default/files/uploads/show_unstaged_content.png)
This is similar to using the command **git diff custom_functions.org**. Staging a file is even easier. Simply move the cursor over a file and press the **s** key. The file will be quickly moved to the staged file list:
![](https://opensource.com/sites/default/files/uploads/staging_a_file.png)
To unstage a file, use the **u** key. It is quicker and more fun to use **s** and **u** instead of entering **git add -u <file>** and **git reset HEAD <file>** on the command line.
### Commit changes
In the same Magit window, pressing the **c** key will display a commit window that provides flags like **\--all** to stage all files or **\--signoff** to add a signoff line to a commit message.
![](https://opensource.com/sites/default/files/uploads/magit_commit_popup.png)
Move your cursor to the line where you want to enable a signoff flag and press Enter. This will highlight the **\--signoff** text, which indicates that the flag is enabled.
![](https://opensource.com/sites/default/files/uploads/magit_signoff_commit.png)
Pressing **c** again will display the window to write the commit message.
![](https://opensource.com/sites/default/files/uploads/magit_commit_message.png)
Finally, use **C-c C-c **(short form of the keys Ctrl+cc) to commit the changes.
![](https://opensource.com/sites/default/files/uploads/magit_commit_message_2.png)
### Push changes
Once the changes are committed, the commit line will appear in the **Recent commits** section.
![](https://opensource.com/sites/default/files/uploads/magit_commit_log.png)
Place the cursor on that commit and press **p** to push the changes.
I've uploaded a [demonstration][7] on YouTube if you want to get a feel for using Magit. I have just scratched the surface in this article. It has many cool features to help you with Git branches, rebasing, and more. You can find [documentation, support, and more][8] linked from Magit's homepage.
--------------------------------------------------------------------------------
via: https://opensource.com/article/19/1/how-use-magit
作者:[Sachin Patil][a]
选题:[lujun9972][b]
译者:[lujun9972](https://github.com/lujun9972)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://opensource.com/users/psachin
[b]: https://github.com/lujun9972
[1]: https://git-scm.com
[2]: https://git-scm.com/book/en/v2/Getting-Started-About-Version-Control
[3]: https://magit.vc
[4]: https://www.gnu.org/software/emacs/download.html
[5]: https://magit.vc/manual/magit/Installing-from-Melpa.html#Installing-from-Melpa
[6]: https://www.gnu.org/software/emacs/manual/html_node/emacs/Dired-Enter.html#Dired-Enter
[7]: https://youtu.be/Vvw75Pqp7Mc
[8]: https://magit.vc/

View File

@ -1,5 +1,5 @@
[#]: collector: (lujun9972)
[#]: translator: ( )
[#]: translator: (sndnvaps)
[#]: reviewer: ( )
[#]: publisher: ( )
[#]: url: ( )

View File

@ -1,124 +0,0 @@
[#]: collector: (lujun9972)
[#]: translator: (wxy)
[#]: reviewer: ( )
[#]: publisher: ( )
[#]: url: ( )
[#]: subject: (Drinking coffee with AWK)
[#]: via: (https://opensource.com/article/19/2/drinking-coffee-awk)
[#]: author: (Moshe Zadka https://opensource.com/users/moshez)
Drinking coffee with AWK
======
Keep track of what your office mates owe for the coffee they drink with a simple AWK program.
![](https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/coffee_cafe_brew_laptop_desktop.jpg?itok=G-n1o1-o)
The following is based on a true story, although some names and details have been changed.
> A long time ago, in a place far away, there was an office. The office did not, for various reasons, buy instant coffee. Some workers in that office got together and decided to institute the "Coffee Corner."
>
> A member of the Coffee Corner would buy some instant coffee, and the other members would pay them back. It came to pass that some people drank more coffee than others, so the level of a "half-member" was added: a half-member was allowed a limited number of coffees per week and would pay half of what a member paid.
Managing this was a huge pain. I had just read The Unix Programming Environment and wanted to practice my [AWK][1] programming. So I volunteered to create a system.
Step 1: I kept a database of members and their debt to the Coffee Corner. I did it in an AWK-friendly format, where fields are separated by colons:
```
member:john:1:22
member:jane:0.5:33
member:pratyush:0.5:17
member:jing:1:27
```
The first field above identifies what kind of row this is (member). The second field is the member's name (i.e., their email username without the @). The next field is their membership level (full=1 or half=0.5). The last field is their debt to the Coffee Corner. A positive number means they owe money, a negative number means the Coffee Corner owes them.
Step 2: I kept a log of inputs to and outputs from the Coffee Corner:
```
payment:jane:33
payment:pratyush:17
bought:john:60
payback:john:50
```
Jane paid $33, Pratyush paid $17, John bought $60 worth of coffee, and the Coffee Corner paid John $50.
Step 3: I was ready to write some code. The code would process the members and payments and spit out an updated members file with the new debts.
```
#!/usr/bin/env --split-string=awk -F: -f
```
**#!** ) line required some work! I used the **env** command to allow passing multiple arguments from the shebang: specifically, the **-F** command-line argument to AWK tells it what the field separator is.
The shebang () line required some work! I used thecommand to allow passing multiple arguments from the shebang: specifically, thecommand-line argument to AWK tells it what the field separator is.
An AWK program is a sequence of rules. (It can also contain function definitions, but I don't need any for the Coffee Corner.)
The first rule reads the members file. When I run the command, I always give it the members file first, and the payments file second. It uses AWK associative arrays to record membership levels in the **members** array and current debt in the **debt** array.
```
$1 == "member" {
   members[$2]=$3
   debt[$2]=$4
   total_members += $3
}
```
The second rule reduces the debt when a **payment** is recorded.
```
$1 == "payment" {
   debt[$2] -= $3
}
```
**Payback** is the opposite: it increases the debt. This elegantly supports the case of accidentally giving someone too much money.
```
$1 == "payback" {
   debt[$2] += $3
}
```
The most complicated part happens when someone buys ( **"bought"** ) instant coffee for the Coffee Club's use. It is treated as a payment and the person's debt is reduced by the appropriate amount. Next, it calculates the per-member fee. It iterates over all members and increases their debt, according to their level of membership.
```
$1 == "bought" {
   debt[$2] -= $3
   per_member = $3/total_members
   for (x in members) {
       debt[x] += per_member * members[x]
   }
}
```
The **END** pattern is special: it happens exactly once, when AWK has no more lines to process. At this point, it spits out the new members file with updated debt levels.
```
END {
   for (x in members) {
       printf "%s:%s:%s\n", x, members[x], debt[x]
   }
}
```
Along with a script that iterates over the members and sends a reminder email to people to pay their dues (for positive debts), this system managed the Coffee Corner for quite a while.
--------------------------------------------------------------------------------
via: https://opensource.com/article/19/2/drinking-coffee-awk
作者:[Moshe Zadka][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/moshez
[b]: https://github.com/lujun9972
[1]: https://en.wikipedia.org/wiki/AWK

View File

@ -0,0 +1,93 @@
[#]collector(lujun9972)
[#]translator(lujun9972)
[#]reviewer( )
[#]publisher( )
[#]url( )
[#]subject(How to use Magit to manage Git projects)
[#]via(https://opensource.com/article/19/1/how-use-magit)
[#]author(Sachin Patil https://opensource.com/users/psachin)
如何使用 Magit 管理 Git 项目
======
Emacs 的 Magit 扩展插件使得使用 Git 进行版本控制变得简单起来。
![](https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/rh_003588_01_rd3os.combacktoschoolseriesk12_rh_021x_0.pngitok=fvorN0e-)
[Git][1] 是一个很棒的用于项目管理的 [版本控制 ][2] 工具就是新人学习起来太难。Git 的命令行工具很难用,你不仅需要熟悉它的标志和选项还需要知道什么环境下使用他们。这使人望而生畏,因此不少人只在有限的几个场景中才使用它。
还在,现今大多数的集成开发环境 (IDE) 都包含了 Git 扩展大大地简化了使用使用的难度。Emacs 中就有这么一款 Git 扩展名叫 [Magit][3]。
Magit 项目成立有差不多 10 年了,它将自己定义为 "一件 Emacs 内的 Git 瓷器。" 也就是说,它是一个操作界面,每个操作都能一键完成。本文会带你领略一下 Magit 的操作界面并告诉你如何使用它来管理 Git 项目。
若你还未 [安装 Emacs][4],则开始本教程之前请先 [安装 Magit][5]。
### Magit 的界面
首先用 Emacs 的 [Dired mode][6] 访问一个项目目录。比如我所有的 Emacs 配置存储在 **~/.emacs.d/** 目录中,就是用 Git 来进行管理的。
![](https://opensource.com/sites/default/files/uploads/visiting_a_git_project.png)
若你在命令行下工作,则你需要输入 **git status** 来查看项目的当前状态。Magit 也有类似的函数:**magit-status**。你可以通过 **M-x magit-status** (快捷方式是 **Alt+x magit-status** )来调用该函数。结果看起来像下面这样:
![](https://opensource.com/sites/default/files/uploads/magit_status.png)
Magit 显示的信息比 **git status** 命令的要多得多。它分别列出了未追踪文件列表、未暂存文件列表以及已暂存文件列表。它还列出了储藏 (stash) 列表以及最近几次的提交—所有这些信息都在一个窗口中展示。
如果你想查看修改了哪些内容,按下 Tab 键。比如,我移动光标到未暂存文件 **custom_functions.org** 上然后按下 Tab 键Magit 会显示修改了哪些内容:
![](https://opensource.com/sites/default/files/uploads/show_unstaged_content.png)
这跟运行命令 **git diff custom_functions.org** 类似。储藏文件更简单。只需要移动光标到文件上然后按下 **s** 键。该文件就会迅速移动到被储藏文件列表中:
![](https://opensource.com/sites/default/files/uploads/staging_a_file.png)
要反储藏 (unstage) 某个文件,使用 **u** 键。按下 **s****u** 键要比在命令行输入 **git add -u <file>****git reset HEAD <file>** 快的多也更有趣的多。
### 提交更改
在同一个 Magit 窗口中,按下 **c** 键会显示一个提交窗口,其中提供了许多标志,比如 **--all** 用来暂存所有文件或者 **--signoff** 来往提交信息中添加签名行。
![](https://opensource.com/sites/default/files/uploads/magit_commit_popup.png)
将光标移动到想要启动的行,比如 signoff 标签行,然后按下回车。**--signoff** 文本会变成高亮,这说明该标志已经被启用。
![](https://opensource.com/sites/default/files/uploads/magit_signoff_commit.png)
再次按下 **c** 键会显示一个窗口供你输入提交信息。
![](https://opensource.com/sites/default/files/uploads/magit_commit_message.png)
最后,使用 **C-c C-c **(按键 Ctrl+cc 的缩写形式) 来提交更改。
![](https://opensource.com/sites/default/files/uploads/magit_commit_message_2.png)
### 推送更改
更改提交后,提交行将会显示在 **Recent commits** 段中显示。
![](https://opensource.com/sites/default/files/uploads/magit_commit_log.png)
将光标放到该提交处然后按下 **p** 来推送该变更。
若你想感受一下使用 Magit 的感觉,我已经在 YouTube 上上传了一段 [演示 ][7]。本文只涉及到 Magit 的一点皮毛。它有许多超酷的功能可以帮你使用 Git 分支,变基等功能。你可以在 Magit 的主页上找到 [文档,支持,以及更多 ][8] 链接。
--------------------------------------------------------------------------------
via: https://opensource.com/article/19/1/how-use-magit
作者:[Sachin Patil][a]
选题:[lujun9972][b]
译者:[lujun9972](https://github.com/lujun9972)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://opensource.com/users/psachin
[b]: https://github.com/lujun9972
[1]: https://git-scm.com
[2]: https://git-scm.com/book/en/v2/Getting-Started-About-Version-Control
[3]: https://magit.vc
[4]: https://www.gnu.org/software/emacs/download.html
[5]: https://magit.vc/manual/magit/Installing-from-Melpa.html#Installing-from-Melpa
[6]: https://www.gnu.org/software/emacs/manual/html_node/emacs/Dired-Enter.html#Dired-Enter
[7]: https://youtu.be/Vvw75Pqp7Mc
[8]: https://magit.vc/

View File

@ -0,0 +1,119 @@
[#]: collector: (lujun9972)
[#]: translator: (wxy)
[#]: reviewer: ( )
[#]: publisher: ( )
[#]: url: ( )
[#]: subject: (Drinking coffee with AWK)
[#]: via: (https://opensource.com/article/19/2/drinking-coffee-awk)
[#]: author: (Moshe Zadka https://opensource.com/users/moshez)
用 AWK 喝咖啡
======
> 用一个简单的 AWK 程序跟踪你的同事所喝咖啡的欠款。
![](https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/coffee_cafe_brew_laptop_desktop.jpg?itok=G-n1o1-o)
以下基于一个真实的故事,虽然一些名字和细节有所改变。
> 很久以前,在一个遥远的地方,有一间~~庙~~(划掉)办公室。由于各种原因,这个办公室没有购买速溶咖啡。所以那个办公室的一些人聚在一起决定建立“咖啡角”。
>
> 咖啡角的一名成员会购买一些速溶咖啡,而其他成员会付给他钱。有人喝咖啡比其他人多,所以增加了“半成员”的级别:半成员每周允许喝的咖啡限量,并可以支付其它成员支付的一半。
管理这事非常操心。而我刚读过《Unix 编程环境》这本书,想练习一下我的 [AWK][1] 编程技能,所以我自告奋勇创建了一个系统。
第 1 步:我用一个数据库来记录成员及其应支付给咖啡角的欠款。我是以 AWK 便于处理的格式记录的,其中字段用冒号分隔:
```
member:john:1:22
member:jane:0.5:33
member:pratyush:0.5:17
member:jing:1:27
```
上面的第一个字段标识了这是哪一种行(`member`)。第二个字段是成员的名字(即他们的电子邮件用户名,但没有 @ )。下一个字段是其成员级别(成员 = 1或半会员 = 0.5)。最后一个字段是他们欠咖啡角的钱。正数表示他们欠咖啡角钱,负数表示咖啡角欠他们。
第 2 步:我记录了咖啡角的收入和支出:
```
payment:jane:33
payment:pratyush:17
bought:john:60
payback:john:50
```
Jane 付款 $33Pratyush 付款 $17John 买了价值 $60 的咖啡,而咖啡角还款给 John $50。
第 3 步:我准备写一些代码,用来处理成员和付款,并生成记录了新欠账的更新的成员文件。
```
#!/usr/bin/env --split-string=awk -F: -f
```
释伴行(`!`)需要做一些调整,我使用 `env` 命令来允许从释伴行传递多个参数具体来说AWK 的 `-F` 命令行参数会告诉它字段分隔符是什么。
AWK 程序就是一个规则序列(也可以包含函数定义,但是对于这个咖啡角应用来说不需要)
第一条规则读取该成员文件。当我运行该命令时,我总是首先给它的是成员文件,然后是付款文件。它使用 AWK 关联数组来在 `members` 数组中记录成员级别,以及在 `debt` 数组中记录当前欠账。
```
$1 == "member" {
   members[$2]=$3
   debt[$2]=$4
   total_members += $3
}
```
第二条规则在记录付款时减少欠账。
```
$1 == "payment" {
   debt[$2] -= $3
}
```
还款则相反:它增加欠账。这可以优雅地支持意外地给了某人太多钱的情况。
```
$1 == "payback" {
   debt[$2] += $3
}
```
最复杂的部分出现在有人购买速溶咖啡供咖啡角使用时。它被视为付款,并且该人的债务减少了适当的金额。接下来,它计算每个会员的费用。它根据成员的级别对所有成员进行迭代并增加欠款
```
$1 == "bought" {
   debt[$2] -= $3
   per_member = $3/total_members
   for (x in members) {
       debt[x] += per_member * members[x]
   }
}
```
`END` 模式很特殊:当 AWK 没有更多的数据要处理时,它会一次性执行。此时,它会使用更新的欠款数生成新的成员文件。
```
END {
   for (x in members) {
       printf "%s:%s:%s\n", x, members[x], debt[x]
   }
}
```
除了一个遍历成员文件,并向人们发送提醒电子邮件以支付他们的会费(积极清账)的脚本外,这个系统管理咖啡角相当一段时间。
--------------------------------------------------------------------------------
via: https://opensource.com/article/19/2/drinking-coffee-awk
作者:[Moshe Zadka][a]
选题:[lujun9972][b]
译者:[wxy](https://github.com/wxy)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://opensource.com/users/moshez
[b]: https://github.com/lujun9972
[1]: https://en.wikipedia.org/wiki/AWK