Skip to main content

How to read a patch (or diff) file generated from diff tool


I am writing this blog to share my experience when i was asked to apply a patch which was written by some other person. When we applying patches, sometimes we may get some errors in the patch command. This blog post will help you to understand the format of the patch file and from that you can identify the reasons which caused your patch to fail and enable you to apply patch manually if required.

Patch or diff files are just text files, so you can look at them with less or a text editor.
If you prefer to use a terminal, colordiff (in the colordiff package) provides syntax highlighting. If the patch is long, you may want to use less to look at it. You need the -R option for less, or else the colors won’t show. I also always use -S, which will enable horizontal scrolling in less instead of wrapping long lines. The whole command to view a patch with syntax highlighting through less in a terminal is:

~$ cat file.patch | colordiff | less -RS


Patch formats

Each patch file comes in , called normal, context and unified diff. You can identify them by looking at the contents of the patch file.

Normal diffs have a lot of lines that start with > or <:
...
51
< background-color: red;
---
> background-color: blue;
...


Context diffs have a lot of lines with stars ***** in them and lines starting with !, + and -:
...
***************
*** 44,54 ****

  html, body { color: #242626; }
  html {
!   background-color: red;
    height: 100%;
    margin-bottom:;
    overflow-y: scroll;
--- 44,54 ----

  html, body { color: #242626; }
  html {
!   background-color: blue;
    height: 100%;
    margin-bottom:;
    overflow-y: scroll;
...


And finally unified diffs have a lot of lines with two @@s in them and lines starting with + and -:
...
@@ -44,11 +44,11 @@

 html, body { color: #242626; }
 html {
-  background-color: red;
+  background-color: blue;
   height: 100%;
   margin-bottom:;
   overflow-y: scroll;
...



How to read a normal diff

The patch file contains a section for each file that should be changed. That section starts with a line identifying the file. That line has the file name and path of first the old and then the new version in it:

diff violet-park-2.orig/style.css violet-park-2/style.css

Then follow sets of changes, called hunks. The first line of each hunk contains
the line or line range from,to in the file before the changes
a letter indicating a change (c), an addition (a) or a deletion (d)
the line or line range in the file after the changes
all without spaces in between.
A change looks like this:

73
<   red
---
>   blue

That means, change line 73, which contains red, to blue. The new line is line after all changes.
The new line number could also have been, say, 75. In that case all additions, deletions and changes before that line added up to the file after all changes.
Instead of single line numbers, a line range can be specified, for example:

90,94
...

That means, change lines file to the following, which will be lines file. The same applies for additions and deletions.


An addition looks like this:
80,81
> line1
> line2


That means, in the original file after line. These will be lines.

And finally, a deletion looks like this:
77
< line1

That means, delete line file. The line, that preceded line file will be line file.


How to read a context diff

The patch file contains a section for each file that should be changed. That section starts with the file name and path of first the old and then the new version and some additional information:


diff -c violet-park-2.orig/archive.php violet-park-2/archive.php
*** violet-park-2.orig/archive.php      2009-10-10 07:37:43.000000000 +0200
--- violet-park-2/archive.php   2009-10-10 09:05:58.000000000 +0200


patch is pretty lax about the format of these lines, as long as it can find out the file names. In patches generated by version control systems like cvs, svn or git these little different.

Then follow sets of changes, called hunks. Each hunk starts with a line with ****. Then comes a line with the line range from,to and the lines from file before the changes. Lines that start with an ! are changed, lines that start with a - are deleted. Then comes the line range and the lines in the file after all changes. Lines that start with a ! are, again, changed, and lines that start with a + are added. Each line modified by the patch is surrounded with before and after.
A change looks like this:

***************
*** 70,76 ****
  foo
  bar
  baz
! red
  more context
  and more
  still context
--- 70,76 ----
  foo
  bar
  baz
! blue
  more context
  and more
  still context


That means, change line 73 (= 70 + in the file before all changes, which contains red to blue. The changed line is also line 73 (= 70 + in the file after all changes.

Here’s an example for an addition:

***************
*** 75,80 ****
--- 77,84 ----
  foo
  bar
  baz
+ line1
+ linemore
  and still context

That means, in the original file after line 78 (= 75 + add . These will be lines 80 (= 77 + through.
And that’s how a deletion looks like:

***************
*** 75,81 ****
  foo
  bar
  baz
- linemore
  and still context
--- 75,80 ----

That means, delete line 78 (= 75 + in the original file. The unchanged context will be on lines.



How to read a unified diff

The patch file contains a section for each file that should be changed. That section starts with the file name and path of first the old and then the new version and some additional information:

diff -u violet-park-2.orig/archive.php violet-park-2/archive.php
--- violet-park-2.orig/archive.php      2009-10-10 07:37:43.000000000 +0200
+++ violet-park-2/archive.php   2009-10-10 09:05:58.000000000 +0200

patch is pretty lax about the format of these lines, as long as it can find out the file names. In patches generated by version control systems like cvs, svn or git these little different.

Then follow sets of changes, called hunks. Each hunk starts with a line that contains, enclosed in @@, the line or line range from,no-of-lines in the file before (with a -) and after (with a +) the changes. After that come the lines from the file. Lines starting with a - are deleted, lines starting with a + are added. Each line modified by the patch is surrounded with @@ before and after.
An addition looks like this:

@@ -75,6 +77,8 @@
 foo
 bar
 baz
+line1
+linemore
 and still context

That means, in the original file after line 78 (= 75 + add . These will be lines 80 (= 77 + through.

A deletion looks like this:
@@ -75,7 +75,6 @@
 foo
 bar
 baz
-linemore
 and still context

That means, delete line 78 (= 75 + in the original file. The unchanged context will be on lines.

Finally, a change looks like this:
@@ -70,7 +70,7 @@
 foo
 bar
 baz
-red
+blue
 more context
 and more
 still context

That means, change line 73 (= 70 + in the file before all changes, which contains red to blue. The changed line is also line 73 (= 70 + in the file after all changes.

Comments

Popular posts from this blog

WSO2 ESB tuning performance with threads

I have written several blog posts explaining the internal behavior of the ESB and the threads created inside ESB. With this post, I am talking about the effect of threads in the WSO2 ESB and how to tune up threads for optimal performance. You can refer [1] and [2] to understand the threads created within the ESB. [1] http://soatutorials.blogspot.com/2015/05/understanding-threads-created-in-wso2.html [2] http://wso2.com/library/articles/2012/03/importance-performance-wso2-esb-handles-nonobvious/ Within this blog post, I am discussing about the "worker threads" which are used for processing the data within the WSO2 ESB. There are 2 types of worker threads created when you start sending the requests to the server 1) Server Worker/Client Worker Threads 2) Mediator Worker (Synapse-Worker) Threads Server Worker/Client Worker Threads These set of threads will be used to process all the requests/responses coming to the ESB server. ServerWorker Threads will be used to pr

How puppet works in your IT infrstructure

What is Puppet? Puppet is IT automation software that helps system administrators manage infrastructure throughout its lifecycle, from provisioning and configuration to orchestration and reporting. Using Puppet, you can easily automate repetitive tasks, quickly deploy critical applications, and proactively manage change, scaling from 10s of servers to 1000s, on-premise or in the cloud. How the puppet works? It works like this..Puppet agent is a daemon that runs on all the client servers(the servers where you require some configuration, or the servers which are going to be managed using puppet.) All the clients which are to be managed will have puppet agent installed on them, and are called nodes in puppet. Puppet Master: This machine contains all the configuration for different hosts. Puppet master will run as a daemon on this master server. Puppet Agent: This is the daemon that will run on all the servers, which are to be managed using p

How to configure timeouts in WSO2 ESB to get rid of client timeout errors

WSO2 ESB has defined some configuration parameters which controls the timeout of a particular request which is going out of ESB. In a particular  scneario, your client sends a request to ESB, and then ESB sends a request to another endpoint to serve the request. CLIENT->WSO2 ESB->BACKEND The reason for clients getting timeout is that ESB timeout is larger than client's timeout. This can be solved by either increasing the timeout at client side or by decreasing the timeout in ESB side. In any of the case, you can control the timeout in ESB using the below properties. 1) Global timeout defined in synapse.properties (ESB_HOME\repository\conf\) file. This will decide the maximum time that a callback is waiting in the ESB for a response for a particular request. If ESB does not get any response from Back End, it will drop the message and clears out the call back. This is a global level parameter which affects all the endpoints configured in ESB. synapse.global_timeout_inte