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

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.

主站蜘蛛池模板: 宝坻区| 伊宁市| 长寿区| 卓尼县| 龙岩市| 长春市| 新晃| 罗城| 芜湖县| 江华| 宜章县| 彰化市| 秭归县| 达日县| 德兴市| 文水县| 中牟县| 镇原县| 清丰县| 甘孜县| 修水县| 平远县| 马尔康县| 永安市| 凤山县| 托里县| 余江县| 新泰市| 偃师市| 清丰县| 义乌市| 鄂托克旗| 阿鲁科尔沁旗| 天等县| 聂荣县| 安塞县| 白玉县| 潮州市| 新郑市| 怀远县| 正蓝旗|