Thursday, February 24, 2011

Patch/diff Exercises

TOS Exercises:  7.2.2, 7.8, 7.9

7.2.2 - Testing the 'diff' command
When using the '-u' flag for the diff command, the output is changed to be more robust.  The entire contents of the file are shown as well as the lines that were removed and added.  Without the '-u' command, only the modified lines are displayed.

$ diff -u hello.c.punct hello.c
--- hello.c.punct       2011-02-24 11:45:00.000000000 -0500
+++ hello.c     2011-02-24 11:44:19.000000000 -0500
@@ -5,6 +5,6 @@
 #include <stdio.h>
 int main() {
-   printf("Hello, World!\n");
+   printf("Hello, World.\n");
    return 0;
 }

$ diff hello.c.punct hello.c
8c8
<    printf("Hello, World!\n");
---
>    printf("Hello, World.\n");


7.8 - Creating a patch file
First, I created a file called 'foo' with the contents 'bar' using vi editor.

To make the patch file, I entered the command:
$ diff -u /dev/null foo > foo-1.0.patch

Next I deleted the file 'foo' in order to test the patch.
$ rm foo

To apply the patch, I typed the following command:
$ patch < foo-1.0.patch

Then I checked the contents of the file:
$ cat foo
bar

Success!  A patch file was created, and applied successfully.


7.9 - Patch echo
I followed the 7.9 Exercise steps (since they are explicitly listed out, it would be redundant for me to explain the process).

Everything worked as it should.  I now know how to create patches!  Cool!



No comments:

Post a Comment