官术网_书友最值得收藏!

File modifications

When you should modify a text file, I'm going to use the unified context diff format since this is a very efficient and compact way to represent a text modification. This format can be obtained by using the diff command with the -u option argument, or by using the git diff command within a git repository.

As a simple example, let's consider the following text in file1.old :

This is first line
This is the second line
This is the third line
...
...
This is the last line

Suppose we have to modify the third line, as highlighted in the following snippet:

This is first line
This is the second line
This is the new third line modified by me
...
...
This is the last line

You can easily understand that reporting the whole file  each time for a simple modification is unnecessary and space-consuming; however, by using the unified context diff format, the preceding modification can be written as follows:

$ diff -u file1.old file1.new
--- file1.old 2019-05-18 14:49:04.354377460 +0100
+++ file1.new 2019-05-18 14:51:57.450373836 +0100
@@ -1,6 +1,6 @@
This is first line
This is the second line
-This is the third line
+This is the new third line modified by me
...
...
This is the last line

Now, the modification is very clear and written in a compact form! It starts with a two-line header, where the original file is preceded by --- and the new file is preceded by +++. Then, it follows one or more change hunks that contain the line differences in the file. The preceding example has just one hunk where the unchanged lines are preceded by a space character, while the lines to be added are preceded by a + character and the lines to be removed are preceded by a - character.

Nonetheless, for space reasons, most patches reproduced in this book have reduced indentation in order to fit the width of printed pages; however, they are still perfectly readable. For the full patch, you should refer to the provided files on GitHub or the Packt site.

主站蜘蛛池模板: 长兴县| 临潭县| 东港市| 基隆市| 太原市| 丹江口市| 永丰县| 北川| 饶河县| 南昌市| 潍坊市| 来凤县| 全州县| 江津市| 南通市| 榆林市| 阿勒泰市| 卫辉市| 淮阳县| 建德市| 阿城市| 巴林左旗| 开阳县| 孟州市| 珲春市| 密山市| 大同市| 措勤县| 泰来县| 青川县| 磐石市| 碌曲县| 土默特左旗| 葫芦岛市| 古蔺县| 凤阳县| 宣汉县| 察雅县| 阿克陶县| 三明市| 达孜县|